=================================================================== RCS file: /cvsrepo/anoncvs/cvs/src/usr.bin/pr/pr.c,v retrieving revision 1.11 retrieving revision 1.12 diff -c -r1.11 -r1.12 *** src/usr.bin/pr/pr.c 2001/11/02 16:25:02 1.11 --- src/usr.bin/pr/pr.c 2001/11/19 03:37:33 1.12 *************** *** 1,4 **** ! /* $OpenBSD: pr.c,v 1.11 2001/11/02 16:25:02 deraadt Exp $ */ /*- * Copyright (c) 1991 Keith Muller. --- 1,4 ---- ! /* $OpenBSD: pr.c,v 1.12 2001/11/19 03:37:33 deraadt Exp $ */ /*- * Copyright (c) 1991 Keith Muller. *************** *** 45,51 **** #ifndef lint /* from: static char sccsid[] = "@(#)pr.c 8.1 (Berkeley) 6/6/93"; */ ! static char *rcsid = "$OpenBSD: pr.c,v 1.11 2001/11/02 16:25:02 deraadt Exp $"; #endif /* not lint */ #include --- 45,51 ---- #ifndef lint /* from: static char sccsid[] = "@(#)pr.c 8.1 (Berkeley) 6/6/93"; */ ! static char *rcsid = "$OpenBSD: pr.c,v 1.12 2001/11/19 03:37:33 deraadt Exp $"; #endif /* not lint */ #include *************** *** 56,61 **** --- 56,62 ---- #include #include #include + #include #include #include #include *************** *** 82,88 **** * bugs were noted and fixed in the processs. Some implementations have this * as the as -f, some as -F so we accept either. * ! * The impelmentation of form feeds on top of the existing I/O structure is * a bit ideosyncratic. Basically they are treated as temporary end-of-file * conditions and an additional level of "loop on form feed" is added to each * of the output modes to continue after such a transient end-of-file's. This --- 83,89 ---- * bugs were noted and fixed in the processs. Some implementations have this * as the as -f, some as -F so we accept either. * ! * The implementation of form feeds on top of the existing I/O structure is * a bit ideosyncratic. Basically they are treated as temporary end-of-file * conditions and an additional level of "loop on form feed" is added to each * of the output modes to continue after such a transient end-of-file's. This *************** *** 142,148 **** /* * misc globals */ ! FILE *ferr; /* error message file pointer */ int addone = 0; /* page length is odd with double space */ int errcnt = 0; /* error count on file processing */ int beheaded = 0; /* header / trailer link */ --- 143,149 ---- /* * misc globals */ ! int ferr; /* error message delayed */ int addone = 0; /* page length is odd with double space */ int errcnt = 0; /* error count on file processing */ int beheaded = 0; /* header / trailer link */ *************** *** 823,829 **** --- 824,880 ---- return(0); } + struct ferrlist { + struct ferrlist *next; + char *buf; + }; + struct ferrlist *ferrhead, *ferrtail; + /* + * flsh_errs(): output saved up diagnostic messages after all normal + * processing has completed + */ + void + flsh_errs() + { + struct ferrlist *f; + + if (ferr) { + for (f = ferrhead; f; f = f->next) + (void)write(STDERR_FILENO, f->buf, strlen(f->buf)); + } + } + + void + ferrout(char *fmt, ...) + { + sigset_t block, oblock; + struct ferrlist *f; + va_list ap; + char *p; + + va_start(ap, fmt); + if (ferr == NULL) + vfprintf(stderr, fmt, ap); + else { + sigemptyset(&block); + sigaddset(&block, SIGINT); + sigprocmask(SIG_BLOCK, &block, &oblock); + + vasprintf(&p, fmt, ap); + f = (struct ferrlist *)malloc(sizeof(*f)); + f->next = NULL; + f->buf = p; + if (ferrhead == NULL) + ferrhead = f; + if (ferrtail) + ferrtail->next = f; + ferrtail = f; + sigprocmask(SIG_SETMASK, &oblock, NULL); + } + } + + /* * mulfile: print files with more than one column of output and * more than one file concurrently */ *************** *** 918,925 **** pgwd = ((colwd + 1) * clcnt) - 1; } if (colwd < 1) { ! (void)fprintf(ferr, ! "pr: page width too small for %d columns\n", clcnt); return(1); } col = colwd + 1; --- 969,975 ---- pgwd = ((colwd + 1) * clcnt) - 1; } if (colwd < 1) { ! ferrout("pr: page width too small for %d columns\n", clcnt); return(1); } col = colwd + 1; *************** *** 1414,1420 **** return(inf); if (gettimeofday(&tv, &tz) < 0) { ++errcnt; ! (void)fprintf(ferr, "pr: cannot get time of day, %s\n", strerror(errno)); eoptind = argc - 1; return(NULL); --- 1464,1470 ---- return(inf); if (gettimeofday(&tv, &tz) < 0) { ++errcnt; ! ferrout("pr: cannot get time of day, %s\n", strerror(errno)); eoptind = argc - 1; return(NULL); *************** *** 1438,1445 **** return(inf); if (gettimeofday(&tv, &tz) < 0) { ++errcnt; ! (void)fprintf(ferr, ! "pr: cannot get time of day, %s\n", strerror(errno)); return(NULL); } --- 1488,1494 ---- return(inf); if (gettimeofday(&tv, &tz) < 0) { ++errcnt; ! ferrout("pr: cannot get time of day, %s\n", strerror(errno)); return(NULL); } *************** *** 1453,1459 **** ++errcnt; if (nodiag) continue; ! (void)fprintf(ferr, "pr: Cannot open %s, %s\n", argv[eoptind], strerror(errno)); continue; } --- 1502,1508 ---- ++errcnt; if (nodiag) continue; ! ferrout("pr: Cannot open %s, %s\n", argv[eoptind], strerror(errno)); continue; } *************** *** 1470,1477 **** if (dt) { if (gettimeofday(&tv, &tz) < 0) { ++errcnt; ! (void)fprintf(ferr, ! "pr: cannot get time of day, %s\n", strerror(errno)); return(NULL); } --- 1519,1525 ---- if (dt) { if (gettimeofday(&tv, &tz) < 0) { ++errcnt; ! ferrout("pr: cannot get time of day, %s\n", strerror(errno)); return(NULL); } *************** *** 1481,1488 **** if (fstat(fileno(inf), &statbuf) < 0) { ++errcnt; (void)fclose(inf); ! (void)fprintf(ferr, ! "pr: Cannot stat %s, %s\n", argv[eoptind], strerror(errno)); return(NULL); } --- 1529,1535 ---- if (fstat(fileno(inf), &statbuf) < 0) { ++errcnt; (void)fclose(inf); ! ferrout("pr: Cannot stat %s, %s\n", argv[eoptind], strerror(errno)); return(NULL); } *************** *** 1501,1507 **** ++errcnt; if (inf != stdin) (void)fclose(inf); ! (void)fputs("pr: time conversion failed\n", ferr); return(NULL); } return(inf); --- 1548,1554 ---- ++errcnt; if (inf != stdin) (void)fclose(inf); ! ferrout("pr: time conversion failed\n"); return(NULL); } return(inf); *************** *** 1701,1749 **** terminate(which_sig) int which_sig; { ! flsh_errs(); /* XXX signal race */ _exit(1); } - - /* - * flsh_errs(): output saved up diagnostic messages after all normal - * processing has completed - */ void - flsh_errs() - { - char buf[BUFSIZ]; - - (void)fflush(stdout); - (void)fflush(ferr); - if (ferr == stderr) - return; - rewind(ferr); - while (fgets(buf, BUFSIZ, ferr) != NULL) - (void)fputs(buf, stderr); - } - - void mfail() { ! (void)fputs("pr: memory allocation failed\n", ferr); } void pfail() { ! (void)fprintf(ferr, "pr: write failure, %s\n", strerror(errno)); } void usage() { ! (void)fputs( ! "usage: pr [+page] [-col] [-adfFmrt] [-e[ch][gap]] [-h header]\n", ferr); ! (void)fputs( ! " [-i[ch][gap]] [-l line] [-n[ch][width]] [-o offset]\n", ferr); ! (void)fputs( " [-s[ch]] [-w width] [-] [file ...]\n", ferr); } --- 1748,1777 ---- terminate(which_sig) int which_sig; { ! flsh_errs(); _exit(1); } void mfail() { ! ferrout("pr: memory allocation failed\n"); } void pfail() { ! ferrout("pr: write failure, %s\n", strerror(errno)); } void usage() { ! ferrout( ! "usage: pr [+page] [-col] [-adfFmrt] [-e[ch][gap]] [-h header]\n"); ! ferrout( ! " [-i[ch][gap]] [-l line] [-n[ch][width]] [-o offset]\n"); ! ferrout( " [-s[ch]] [-w width] [-] [file ...]\n", ferr); } *************** *** 1762,1790 **** int wflag = 0; int cflag = 0; ! if (isatty(fileno(stdout))) { ! /* ! * defer diagnostics until processing is done ! */ ! if ((ferr = tmpfile()) == NULL) { ! (void)fputs("Cannot defer diagnostic messages\n",stderr); ! return(1); ! } ! } else ! ferr = stderr; while ((c = egetopt(argc, argv, "#adfFmrte?h:i?l:n?o:s?w:")) != -1) { switch (c) { case '+': if ((pgnm = atoi(eoptarg)) < 1) { ! (void)fputs("pr: +page number must be 1 or more\n", ! ferr); return(1); } ++skipping; break; case '-': if ((clcnt = atoi(eoptarg)) < 1) { ! (void)fputs("pr: -columns must be 1 or more\n",ferr); return(1); } if (clcnt > 1) --- 1790,1810 ---- int wflag = 0; int cflag = 0; ! if (isatty(fileno(stdout))) ! ferr = 1; ! while ((c = egetopt(argc, argv, "#adfFmrte?h:i?l:n?o:s?w:")) != -1) { switch (c) { case '+': if ((pgnm = atoi(eoptarg)) < 1) { ! ferrout("pr: +page number must be 1 or more\n"); return(1); } ++skipping; break; case '-': if ((clcnt = atoi(eoptarg)) < 1) { ! ferrout("pr: -columns must be 1 or more\n"); return(1); } if (clcnt > 1) *************** *** 1804,1818 **** inchar = INCHAR; if ((eoptarg != NULL) && isdigit(*eoptarg)) { if ((ingap = atoi(eoptarg)) < 0) { ! (void)fputs( ! "pr: -e gap must be 0 or more\n", ferr); return(1); } if (ingap == 0) ingap = INGAP; } else if ((eoptarg != NULL) && (*eoptarg != '\0')) { ! (void)fprintf(ferr, ! "pr: invalid value for -e %s\n", eoptarg); return(1); } else ingap = INGAP; --- 1824,1836 ---- inchar = INCHAR; if ((eoptarg != NULL) && isdigit(*eoptarg)) { if ((ingap = atoi(eoptarg)) < 0) { ! ferrout("pr: -e gap must be 0 or more\n"); return(1); } if (ingap == 0) ingap = INGAP; } else if ((eoptarg != NULL) && (*eoptarg != '\0')) { ! ferrout("pr: invalid value for -e %s\n", eoptarg); return(1); } else ingap = INGAP; *************** *** 1832,1854 **** ochar = OCHAR; if ((eoptarg != NULL) && isdigit(*eoptarg)) { if ((ogap = atoi(eoptarg)) < 0) { ! (void)fputs( ! "pr: -i gap must be 0 or more\n", ferr); return(1); } if (ogap == 0) ogap = OGAP; } else if ((eoptarg != NULL) && (*eoptarg != '\0')) { ! (void)fprintf(ferr, ! "pr: invalid value for -i %s\n", eoptarg); return(1); } else ogap = OGAP; break; case 'l': if (!isdigit(*eoptarg) || ((lines=atoi(eoptarg)) < 1)) { ! (void)fputs( ! "pr: Number of lines must be 1 or more\n",ferr); return(1); } break; --- 1850,1869 ---- ochar = OCHAR; if ((eoptarg != NULL) && isdigit(*eoptarg)) { if ((ogap = atoi(eoptarg)) < 0) { ! ferrout("pr: -i gap must be 0 or more\n"); return(1); } if (ogap == 0) ogap = OGAP; } else if ((eoptarg != NULL) && (*eoptarg != '\0')) { ! ferrout("pr: invalid value for -i %s\n", eoptarg); return(1); } else ogap = OGAP; break; case 'l': if (!isdigit(*eoptarg) || ((lines=atoi(eoptarg)) < 1)) { ! ferrout("pr: Number of lines must be 1 or more\n"); return(1); } break; *************** *** 1862,1882 **** nmchar = NMCHAR; if ((eoptarg != NULL) && isdigit(*eoptarg)) { if ((nmwd = atoi(eoptarg)) < 1) { ! (void)fputs( ! "pr: -n width must be 1 or more\n",ferr); return(1); } } else if ((eoptarg != NULL) && (*eoptarg != '\0')) { ! (void)fprintf(ferr, ! "pr: invalid value for -n %s\n", eoptarg); return(1); } else nmwd = NMWD; break; case 'o': if (!isdigit(*eoptarg) || ((offst = atoi(eoptarg))< 1)){ ! (void)fputs("pr: -o offset must be 1 or more\n", ! ferr); return(1); } break; --- 1877,1894 ---- nmchar = NMCHAR; if ((eoptarg != NULL) && isdigit(*eoptarg)) { if ((nmwd = atoi(eoptarg)) < 1) { ! ferrout("pr: -n width must be 1 or more\n"); return(1); } } else if ((eoptarg != NULL) && (*eoptarg != '\0')) { ! ferrout("pr: invalid value for -n %s\n", eoptarg); return(1); } else nmwd = NMWD; break; case 'o': if (!isdigit(*eoptarg) || ((offst = atoi(eoptarg))< 1)){ ! ferrout("pr: -o offset must be 1 or more\n"); return(1); } break; *************** *** 1890,1897 **** else { schar = *eoptarg++; if (*eoptarg != '\0') { ! (void)fprintf(ferr, ! "pr: invalid value for -s %s\n", eoptarg); return(1); } } --- 1902,1908 ---- else { schar = *eoptarg++; if (*eoptarg != '\0') { ! ferrout("pr: invalid value for -s %s\n", eoptarg); return(1); } } *************** *** 1902,1909 **** case 'w': ++wflag; if (!isdigit(*eoptarg) || ((pgwd = atoi(eoptarg)) < 1)){ ! (void)fputs( ! "pr: -w width must be 1 or more \n",ferr); return(1); } break; --- 1913,1919 ---- case 'w': ++wflag; if (!isdigit(*eoptarg) || ((pgwd = atoi(eoptarg)) < 1)){ ! ferrout("pr: -w width must be 1 or more \n"); return(1); } break; *************** *** 1931,1942 **** } if (across) { if (clcnt == 1) { ! (void)fputs("pr: -a flag requires multiple columns\n", ! ferr); return(1); } if (merge) { ! (void)fputs("pr: -m cannot be used with -a\n", ferr); return(1); } } --- 1941,1951 ---- } if (across) { if (clcnt == 1) { ! ferrout("pr: -a flag requires multiple columns\n"); return(1); } if (merge) { ! ferrout("pr: -m cannot be used with -a\n"); return(1); } } *************** *** 1958,1965 **** } if (cflag) { if (merge) { ! (void)fputs( ! "pr: -m cannot be used with multiple columns\n", ferr); return(1); } if (nmwd) { --- 1967,1973 ---- } if (cflag) { if (merge) { ! ferrout("pr: -m cannot be used with multiple columns\n"); return(1); } if (nmwd) { *************** *** 1970,1977 **** pgwd = ((colwd + 1) * clcnt) - 1; } if (colwd < 1) { ! (void)fprintf(ferr, ! "pr: page width is too small for %d columns\n",clcnt); return(1); } } --- 1978,1984 ---- pgwd = ((colwd + 1) * clcnt) - 1; } if (colwd < 1) { ! ferrout("pr: page width is too small for %d columns\n",clcnt); return(1); } }