#include <sys/types.h>
#include <sys/stat.h>
#include <ctype.h>
#include <errno.h>
#include <limits.h>
#include <signal.h>
#include <stdio.h>
#include <stdarg.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include "pr.h"
#include "extern.h"
int pgnm;
int skipping;
int clcnt;
int colwd;
int across;
int dspace;
char inchar;
int ingap;
int formfeed;
int inform;
char *header;
char ochar;
int ogap;
int lines;
int merge;
char nmchar;
int nmwd;
int offst;
int nodiag;
char schar;
int sflag;
int nohead;
int pgwd;
volatile sig_atomic_t ferr;
int addone = 0;
int errcnt = 0;
int beheaded = 0;
char digs[] = "0123456789";
int
main(int argc, char *argv[])
{
int ret_val;
if (pledge("stdio rpath", NULL) == -1) {
perror("pledge");
exit(1);
}
if (signal(SIGINT, SIG_IGN) != SIG_IGN)
(void)signal(SIGINT, terminate);
ret_val = setup(argc, argv);
if (!ret_val) {
if (merge)
ret_val = mulfile(argc, argv);
else if (clcnt == 1)
ret_val = onecol(argc, argv);
else if (across)
ret_val = horzcol(argc, argv);
else
ret_val = vertcol(argc, argv);
} else
usage();
flsh_errs();
if (errcnt || ret_val)
exit(1);
return(0);
}
int
onecol(int argc, char *argv[])
{
int off;
int lrgln;
int linecnt;
int num;
int cnt;
int rc;
int lncnt;
int pagecnt;
int ips;
int ops;
int cps;
char *obuf = NULL;
char *lbuf;
char *nbuf;
char *hbuf = NULL;
char *ohbuf;
FILE *inf = NULL;
char *fname;
int mor;
int error = 1;
if (nmwd)
num = nmwd + 1;
else
num = 0;
off = num + offst;
if ((obuf = malloc((unsigned)(LBUF + off)*sizeof(char))) == NULL)
goto oomem;
if ((hbuf = malloc((unsigned)(HDBUF + offst)*sizeof(char))) == NULL)
goto oomem;
ohbuf = hbuf + offst;
nbuf = obuf + offst;
lbuf = nbuf + num;
if (num)
nbuf[--num] = nmchar;
if (offst) {
(void)memset(obuf, (int)' ', offst);
(void)memset(hbuf, (int)' ', offst);
}
while ((inf = nxtfile(argc, argv, &fname, ohbuf, 0)) != NULL) {
pagecnt = 0;
lncnt = 0;
for(;;) {
for(;;) {
linecnt = 0;
lrgln = 0;
ops = 0;
ips = 0;
cps = 0;
while (linecnt < lines) {
rc = inln(inf,lbuf,LBUF,&cnt,&cps,0,&mor);
if (cnt >= 0) {
if (!lrgln)
if (!linecnt && prhead(hbuf, fname, ++pagecnt))
goto out;
if (!lrgln) {
if (num)
addnum(nbuf, num, ++lncnt);
if (otln(obuf,cnt+off, &ips, &ops, mor))
goto out;
} else
if (otln(lbuf, cnt, &ips, &ops, mor))
goto out;
if (mor) {
lrgln = 1;
} else {
++linecnt;
lrgln = 0;
ops = 0;
ips = 0;
}
}
if (rc != NORMAL)
break;
}
if (prtail(lines - linecnt, lrgln))
goto out;
if (rc == END)
break;
}
if (rc == END)
break;
}
if (inf != stdin)
(void)fclose(inf);
}
if (eoptind < argc) {
goto out;
} else {
error = 0;
goto out;
}
oomem:
mfail();
out:
free(obuf);
free(hbuf);
if (inf != NULL && inf != stdin)
(void)fclose(inf);
return error;
}
int
vertcol(int argc, char *argv[])
{
char *ptbf;
char **lstdat = NULL;
int i;
int j;
int pln;
int *indy = NULL;
int cnt;
int rc;
int cvc;
int *lindy = NULL;
int lncnt;
int stp;
int pagecnt;
int col = colwd + 1;
int mxlen = pgwd + offst + 1;
int mclcnt = clcnt - 1;
struct vcol *vc = NULL;
int mvc;
int tvc;
int cw = nmwd + 1;
int fullcol;
char *buf = NULL;
char *hbuf = NULL;
char *ohbuf;
char *fname;
FILE *inf = NULL;
int ips = 0;
int cps = 0;
int ops = 0;
int mor = 0;
int error = 1;
if ((buf = calloc((unsigned)lines, mxlen)) == NULL)
goto oomem;
if ((hbuf = malloc((unsigned)HDBUF + offst)) == NULL)
goto oomem;
ohbuf = hbuf + offst;
if (offst)
(void)memset(hbuf, (int)' ', offst);
mvc = lines * clcnt;
if ((vc = calloc((unsigned)mvc, sizeof(struct vcol))) == NULL)
goto oomem;
if ((lstdat = calloc((unsigned)lines, sizeof(char *))) == NULL)
goto oomem;
if ((indy = calloc((unsigned)lines, sizeof(int))) == NULL)
goto oomem;
if ((lindy = calloc((unsigned)lines, sizeof(int))) == NULL)
goto oomem;
if (nmwd)
fullcol = col + cw;
else
fullcol = col;
for (j = 0; j < lines; ++j) {
lindy[j] = j * mxlen;
indy[j] = lindy[j] + offst;
if (offst) {
ptbf = buf + lindy[j];
(void)memset(ptbf, (int)' ', offst);
ptbf += offst;
} else
ptbf = buf + indy[j];
lstdat[j] = ptbf;
}
while ((inf = nxtfile(argc, argv, &fname, ohbuf, 0)) != NULL) {
pagecnt = 0;
lncnt = 0;
for (;;) {
for(;;) {
cvc = 0;
for (i = 0; i < clcnt; ++i) {
j = 0;
if (i == mclcnt)
stp = 1;
else
stp = 0;
for(;;) {
if (!i) {
ptbf = buf + indy[j];
lstdat[j] = ptbf;
} else
ptbf = lstdat[j];
vc[cvc].pt = ptbf;
if (nmwd) {
addnum(ptbf, nmwd, ++lncnt);
ptbf += nmwd;
*ptbf++ = nmchar;
}
rc = inln(inf,ptbf,colwd,&cnt,&cps,1,&mor);
vc[cvc++].cnt = cnt;
if (cnt >= 0) {
ptbf += cnt;
if (!stp) {
if (sflag)
*ptbf++ = schar;
else if ((pln = col-cnt) > 0) {
(void)memset(ptbf,
(int)' ',pln);
ptbf += pln;
}
}
lstdat[j] = ptbf;
if (++j >= lines)
break;
}
if (rc != NORMAL)
break;
}
if (rc != NORMAL)
break;
}
if (vc[0].cnt >= 0) {
if (prhead(hbuf, fname, ++pagecnt))
goto out;
--cvc;
if ((rc != NORMAL) && cvc && ((mvc-cvc) >= clcnt)){
pln = cvc/clcnt;
if (cvc % clcnt)
++pln;
for (i = 0; i < pln; ++i) {
ips = 0;
ops = 0;
if (offst && otln(buf,offst,&ips,&ops,1))
goto out;
tvc = i;
for (j = 0; j < clcnt; ++j) {
if (j == mclcnt) {
cnt = vc[tvc].cnt;
if (nmwd)
cnt += cw;
} else if (sflag) {
cnt = vc[tvc].cnt + 1;
if (nmwd)
cnt += cw;
} else
cnt = fullcol;
if (otln(vc[tvc].pt, cnt, &ips, &ops, 1))
goto out;
tvc += pln;
if (tvc > cvc)
break;
}
if (otln(buf, 0, &ips, &ops, 0))
goto out;
}
} else {
if (i > 0)
pln = lines;
else
pln = j;
for (i = 0; i < pln; ++i) {
ptbf = buf + lindy[i];
if ((j = lstdat[i] - ptbf) <= offst)
break;
else {
ips = 0;
ops = 0;
if (otln(ptbf, j, &ips, &ops, 0))
goto out;
}
}
}
}
if (prtail((lines - pln), 0))
goto out;
if (rc != NORMAL)
break;
}
if (rc == END)
break;
}
if (inf != stdin)
(void)fclose(inf);
}
if (eoptind < argc){
goto out;
} else {
error = 0;
goto out;
}
oomem:
mfail();
out:
free(buf);
free(hbuf);
free(vc);
free(lstdat);
free(lindy);
if (inf != NULL && inf != stdin)
(void)fclose(inf);
return error;
}
int
horzcol(int argc, char *argv[])
{
char *ptbf;
int pln;
char *lstdat;
int col = colwd + 1;
int j;
int i;
int cnt;
int rc;
int lncnt;
int pagecnt;
char *buf = NULL;
char *hbuf = NULL;
char *ohbuf;
char *fname;
FILE *inf = NULL;
int cps = 0;
int mor = 0;
int ips = 0;
int ops = 0;
int error = 1;
if ((buf = malloc((unsigned)pgwd + offst + 1)) == NULL)
goto oomem;
if ((hbuf = malloc((unsigned)HDBUF + offst)) == NULL)
goto oomem;
ohbuf = hbuf + offst;
if (offst) {
(void)memset(buf, (int)' ', offst);
(void)memset(hbuf, (int)' ', offst);
}
while ((inf = nxtfile(argc, argv, &fname, ohbuf, 0)) != NULL) {
pagecnt = 0;
lncnt = 0;
for (;;) {
for(;;) {
for (i = 0; i < lines; ++i) {
ptbf = buf + offst;
lstdat = ptbf;
j = 0;
for(;;) {
if (nmwd) {
addnum(ptbf, nmwd, ++lncnt);
ptbf += nmwd;
*ptbf++ = nmchar;
}
rc = inln(inf,ptbf,colwd,&cnt,&cps,1, &mor);
if (cnt >= 0) {
if (!i && !j && prhead(hbuf, fname, ++pagecnt))
goto out;
ptbf += cnt;
lstdat = ptbf;
if (++j >= clcnt)
break;
if (sflag)
*ptbf++ = schar;
else if ((pln = col - cnt) > 0) {
(void)memset(ptbf,(int)' ',pln);
ptbf += pln;
}
}
if (rc != NORMAL)
break;
}
if (j) {
if (otln(buf, lstdat-buf, &ips, &ops, 0))
goto out;
}
if (rc != NORMAL)
break;
}
if (prtail(lines - i, 0))
return(1);
if (rc == END)
break;
}
if (rc == END)
break;
}
if (inf != stdin)
(void)fclose(inf);
}
if (eoptind < argc){
goto out;
} else {
error = 0;
goto out;
}
oomem:
mfail();
out:
free(buf);
free(hbuf);
if (inf != NULL && inf != stdin)
(void)fclose(inf);
return error;
}
struct ferrlist {
struct ferrlist *next;
char *buf;
};
struct ferrlist *ferrhead, *ferrtail;
void
flsh_errs(void)
{
struct ferrlist *f;
if (ferr) {
for (f = ferrhead; f; f = f->next)
(void)write(STDERR_FILENO, f->buf, strlen(f->buf));
}
}
static void ferrout(char *fmt, ...) __attribute__((format (printf, 1, 2)));
static void
ferrout(char *fmt, ...)
{
sigset_t block, oblock;
struct ferrlist *f;
va_list ap;
char *p;
va_start(ap, fmt);
if (ferr == 0)
vfprintf(stderr, fmt, ap);
else {
sigemptyset(&block);
sigaddset(&block, SIGINT);
sigprocmask(SIG_BLOCK, &block, &oblock);
if (vasprintf(&p, fmt, ap) == -1 || (f = malloc(sizeof(*f))) == NULL) {
va_end(ap);
va_start(ap, fmt);
flsh_errs();
vfprintf(stderr, fmt, ap);
fputs("pr: memory allocation failed\n", stderr);
exit(1);
}
f->next = NULL;
f->buf = p;
if (ferrhead == NULL)
ferrhead = f;
if (ferrtail)
ferrtail->next = f;
ferrtail = f;
sigprocmask(SIG_SETMASK, &oblock, NULL);
}
va_end(ap);
}
int
mulfile(int argc, char *argv[])
{
char *ptbf;
int j;
int pln;
int *rc;
int cnt;
char *lstdat;
int i;
FILE **fbuf = NULL;
int actf;
int lncnt;
int col;
int pagecnt;
int fproc;
char *buf = NULL;
char *hbuf = NULL;
char *ohbuf;
char *fname;
int ips = 0;
int cps = 0;
int ops = 0;
int mor = 0;
int error = 1;
if ((fbuf = calloc((unsigned)clcnt, sizeof(FILE *))) == NULL)
goto oomem;
if ((rc = calloc((unsigned)clcnt, sizeof(int))) == NULL)
goto oomem;
if ((hbuf = malloc((unsigned)HDBUF + offst)) == NULL)
goto oomem;
ohbuf = hbuf + offst;
j = 0;
while (j < clcnt) {
if ((fbuf[j] = nxtfile(argc, argv, &fname, ohbuf, 1)) != NULL) {
rc[j] = NORMAL;
j++;
}
}
if (j)
clcnt = j;
else
goto out;
if (nmwd) {
colwd = (pgwd - clcnt - nmwd)/clcnt;
pgwd = ((colwd + 1) * clcnt) - nmwd - 2;
} else {
colwd = (pgwd + 1 - clcnt)/clcnt;
pgwd = ((colwd + 1) * clcnt) - 1;
}
if (colwd < 1) {
ferrout("pr: page width too small for %d columns\n", clcnt);
goto out;
}
col = colwd + 1;
if ((buf = malloc((unsigned)pgwd + offst + 1)) == NULL)
goto oomem;
if (offst) {
(void)memset(buf, (int)' ', offst);
(void)memset(hbuf, (int)' ', offst);
}
pagecnt = 0;
lncnt = 0;
actf = clcnt;
while (actf > 0) {
for (;;) {
for (i = 0; i < lines; ++i) {
ptbf = buf + offst;
lstdat = ptbf;
if (nmwd) {
addnum(ptbf, nmwd, ++lncnt);
ptbf += nmwd;
*ptbf++ = nmchar;
}
fproc = 0;
for (j = 0; j < clcnt; ++j) {
if (rc[j] == NORMAL ) {
rc[j] = inln(fbuf[j], ptbf, colwd, &cnt, &cps, 1, &mor);
if (cnt >= 0) {
ptbf += cnt;
lstdat = ptbf;
fproc++;
} else
cnt = 0;
if (rc[j] == END) {
if (fbuf[j] != stdin)
(void)fclose(fbuf[j]);
--actf;
}
} else
cnt = 0;
if (fproc >= actf)
break;
if (sflag) {
*ptbf++ = schar;
} else {
if (cnt >= 0)
pln = col - cnt;
else
pln = col;
if (pln > 0) {
(void)memset(ptbf, (int)' ', pln);
ptbf += pln;
}
}
}
if (fproc != 0) {
if (!i && prhead(hbuf, fname, ++pagecnt))
goto out;
if (otln(buf, lstdat-buf, &ips, &ops, 0))
goto out;
} else
break;
}
if (prtail(lines - i, 0))
return(1);
for (j = 0; j < clcnt; ++j)
if (rc[j] != END)
rc[j] = NORMAL;
if (actf <= 0)
break;
}
if (actf <= 0)
break;
}
if (eoptind < argc){
goto out;
} else {
error = 0;
goto out;
}
oomem:
mfail();
out:
if (fbuf) {
for (j = 0; j < clcnt; j++) {
if (fbuf[j] && fbuf[j] != stdin)
(void)fclose(fbuf[j]);
}
free(fbuf);
}
free(hbuf);
free(buf);
return error;
}
int
inln(FILE *inf, char *buf, int lim, int *cnt, int *cps, int trnc, int *mor)
{
int col;
int gap = ingap;
int ch = -1;
char *ptbuf;
int chk = (int)inchar;
ptbuf = buf;
if (gap) {
while ((--lim >= 0) && ((ch = getc(inf)) != EOF)) {
if (ch == chk) {
col = (ptbuf - buf) + *cps;
col = gap - (col % gap);
if ((col > lim) && (ungetc(ch, inf) == EOF)) {
*cnt = -1;
return(END);
}
while ((--col >= 0) && (--lim >= 0))
*ptbuf++ = ' ';
continue;
}
if (ch == '\n' || (inform && ch == INFF))
break;
*ptbuf++ = ch;
}
} else {
while ((--lim >= 0) && ((ch = getc(inf)) != EOF)) {
if (ch == '\n' || (inform && ch == INFF))
break;
*ptbuf++ = ch;
}
}
col = ptbuf - buf;
if (ch == EOF) {
*mor = 0;
*cps = 0;
*cnt = col ? col : -1;
return(END);
}
if (inform && ch == INFF) {
*mor = 0;
*cps = 0;
*cnt = col;
return(FORM);
}
if (ch == '\n') {
*mor = 0;
*cps = 0;
*cnt = col;
return(NORMAL);
}
if (trnc) {
while ((ch = getc(inf)) != EOF) {
if (ch == '\n')
break;
}
*cps = 0;
*mor = 0;
} else {
*cps += col;
*mor = 1;
}
*cnt = col;
return(NORMAL);
}
int
otln(char *buf, int cnt, int *svips, int *svops, int mor)
{
int ops;
int ips;
int gap = ogap;
int tbps;
char *endbuf;
if (skipping)
return (0);
if (ogap) {
endbuf = buf + cnt;
ops = *svops;
ips = *svips;
while (buf < endbuf) {
if (*buf == ' ') {
++ips;
++buf;
continue;
}
if (*buf == ochar) {
ips += gap - (ips % gap);
++buf;
continue;
}
while (ops < ips) {
if (ips - ops == 1) {
putchar(' ');
break;
}
if ((tbps = ops + gap - (ops % gap)) > ips)
break;
if (putchar(ochar) == EOF) {
pfail();
return(1);
}
ops = tbps;
}
while (ops < ips) {
if (putchar(' ') == EOF) {
pfail();
return(1);
}
++ops;
}
if (putchar(*buf++) == EOF) {
pfail();
return(1);
}
++ips;
++ops;
}
if (mor > 0) {
*svops = ops;
*svips = ips;
return(0);
}
if (mor < 0) {
while (ops < ips) {
if (ips - ops == 1) {
putchar(' ');
break;
}
if ((tbps = ops + gap - (ops % gap)) > ips)
break;
if (putchar(ochar) == EOF) {
pfail();
return(1);
}
ops = tbps;
}
while (ops < ips) {
if (putchar(' ') == EOF) {
pfail();
return(1);
}
++ops;
}
return(0);
}
} else {
if (cnt && (fwrite(buf, sizeof(char), cnt, stdout) < cnt)) {
pfail();
return(1);
}
if (mor != 0)
return(0);
}
if ((putchar('\n') == EOF) || (dspace && (putchar('\n') == EOF))) {
pfail();
return(1);
}
return(0);
}
FILE *
nxtfile(int argc, char *argv[], char **fname, char *buf, int dt)
{
FILE *inf = NULL;
struct tm *timeptr = NULL;
struct stat statbuf;
time_t curtime;
static int twice = -1;
++twice;
if (eoptind >= argc) {
if (twice)
return(NULL);
clearerr(stdin);
inf = stdin;
if (header != NULL)
*fname = header;
else
*fname = FNAME;
if (nohead)
return(inf);
curtime = time(NULL);
timeptr = localtime(&curtime);
}
for (; eoptind < argc; ++eoptind) {
if (strcmp(argv[eoptind], "-") == 0) {
clearerr(stdin);
inf = stdin;
if (header != NULL)
*fname = header;
else
*fname = FNAME;
++eoptind;
if (nohead || (dt && twice))
return(inf);
curtime = time(NULL);
timeptr = localtime(&curtime);
} else {
if ((inf = fopen(argv[eoptind], "r")) == NULL) {
++errcnt;
if (nodiag)
continue;
ferrout("pr: Cannot open %s, %s\n",
argv[eoptind], strerror(errno));
continue;
}
if (header != NULL)
*fname = header;
else if (dt)
*fname = FNAME;
else
*fname = argv[eoptind];
++eoptind;
if (nohead || (dt && twice))
return(inf);
if (dt) {
curtime = time(NULL);
timeptr = localtime(&curtime);
} else {
if (fstat(fileno(inf), &statbuf) == -1) {
++errcnt;
(void)fclose(inf);
ferrout("pr: Cannot stat %s, %s\n",
argv[eoptind], strerror(errno));
return(NULL);
}
timeptr = localtime(&(statbuf.st_mtime));
}
}
break;
}
if (inf == NULL)
return(NULL);
if (strftime(buf, HDBUF, TIMEFMT, timeptr) == 0) {
++errcnt;
if (inf != stdin)
(void)fclose(inf);
ferrout("pr: time conversion failed\n");
return(NULL);
}
return(inf);
}
void
addnum(char *buf, int wdth, int line)
{
char *pt = buf + wdth;
do {
*--pt = digs[line % 10];
line /= 10;
} while (line && (pt > buf));
while (pt > buf)
*--pt = ' ';
}
int
prhead(char *buf, char *fname, int pagcnt)
{
int ips = 0;
int ops = 0;
beheaded = 1;
if (skipping && pagcnt >= pgnm)
skipping = 0;
if (nohead || skipping)
return (0);
if ((putchar('\n') == EOF) || (putchar('\n') == EOF)) {
pfail();
return(1);
}
if (offst && otln(buf, offst, &ips, &ops, -1))
return(1);
(void)printf(HDFMT,buf+offst, fname, pagcnt);
return(0);
}
int
prtail(int cnt, int incomp)
{
if (skipping || beheaded == 0)
return (0);
beheaded = 0;
if (nohead) {
if (incomp) {
if (dspace)
if (putchar('\n') == EOF) {
pfail();
return(1);
}
if (putchar('\n') == EOF) {
pfail();
return(1);
}
}
if (formfeed)
if (putchar(OUTFF) == EOF) {
pfail();
return(1);
}
} else {
if (dspace)
cnt *= 2;
if (addone)
++cnt;
if (formfeed) {
if (incomp)
if (putchar('\n') == EOF) {
pfail();
return(1);
}
if (putchar(OUTFF) == EOF) {
pfail();
return(1);
}
} else {
if (incomp)
cnt++;
cnt += TAILLEN;
while (--cnt >= 0) {
if (putchar('\n') == EOF) {
pfail();
return(1);
}
}
}
}
return(0);
}
void
terminate(int which_sig)
{
flsh_errs();
_exit(1);
}
void
mfail(void)
{
ferrout("pr: memory allocation failed\n");
}
void
pfail(void)
{
ferrout("pr: write failure, %s\n", strerror(errno));
}
void
usage(void)
{
ferrout(
"usage: pr [+page] [-column] [-adFfmrt] [-e[char][gap]] [-h header]\n");
ferrout(
"\t[-i[char][gap]] [-l lines] [-n[char][width]] [-o offset] [-s[char]]\n");
ferrout(
"\t[-w width] [file ...]\n");
}
int
setup(int argc, char *argv[])
{
int c;
int eflag = 0;
int iflag = 0;
int wflag = 0;
int cflag = 0;
const char *errstr;
if (isatty(fileno(stdout)))
ferr = 1;
while ((c = egetopt(argc, argv, "#adfFmrte?h:i?l:n?o:s?w:")) != -1) {
switch (c) {
case '+':
pgnm = strtonum(eoptarg, 1, INT_MAX, &errstr);
if (errstr) {
ferrout("pr: +page number is %s: %s\n", errstr, eoptarg);
return(1);
}
skipping = 1;
break;
case '-':
clcnt = strtonum(eoptarg, 1, INT_MAX, &errstr);
if (errstr) {
ferrout("pr: -columns number is %s: %s\n", errstr, eoptarg);
return(1);
}
if (clcnt > 1)
cflag = 1;
break;
case 'a':
across = 1;
break;
case 'd':
dspace = 1;
break;
case 'e':
eflag = 1;
if ((eoptarg != NULL) && !isdigit((unsigned char)*eoptarg))
inchar = *eoptarg++;
else
inchar = INCHAR;
if ((eoptarg != NULL) && isdigit((unsigned char)*eoptarg)) {
ingap = strtonum(eoptarg, 0, INT_MAX, &errstr);
if (errstr) {
ferrout("pr: -e gap is %s: %s\n", errstr, eoptarg);
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;
break;
case 'f':
case 'F':
formfeed = 1;
break;
case 'h':
header = eoptarg;
break;
case 'i':
iflag = 1;
if ((eoptarg != NULL) && !isdigit((unsigned char)*eoptarg))
ochar = *eoptarg++;
else
ochar = OCHAR;
if ((eoptarg != NULL) && isdigit((unsigned char)*eoptarg)) {
ogap = strtonum(eoptarg, 0, INT_MAX, &errstr);
if (errstr) {
ferrout("pr: -i gap is %s: %s\n", errstr, eoptarg);
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':
lines = strtonum(eoptarg, 1, INT_MAX, &errstr);
if (errstr) {
ferrout("pr: number of lines is %s: %s\n", errstr, eoptarg);
return(1);
}
break;
case 'm':
merge = 1;
break;
case 'n':
if ((eoptarg != NULL) && !isdigit((unsigned char)*eoptarg))
nmchar = *eoptarg++;
else
nmchar = NMCHAR;
if ((eoptarg != NULL) && isdigit((unsigned char)*eoptarg)) {
nmwd = strtonum(eoptarg, 1, INT_MAX, &errstr);
if (errstr) {
ferrout("pr: -n width is %s: %s\n", errstr, eoptarg);
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':
offst = strtonum(eoptarg, 1, INT_MAX, &errstr);
if (errstr) {
ferrout("pr: -o offset is %s: %s\n", errstr, eoptarg);
return(1);
}
break;
case 'r':
nodiag = 1;
break;
case 's':
sflag = 1;
if (eoptarg == NULL)
schar = SCHAR;
else {
schar = *eoptarg++;
if (*eoptarg != '\0') {
ferrout("pr: invalid value for -s %s\n", eoptarg);
return(1);
}
}
break;
case 't':
nohead = 1;
break;
case 'w':
wflag = 1;
pgwd = strtonum(eoptarg, 1, INT_MAX, &errstr);
if (errstr) {
ferrout("pr: -w width is %s: %s\n", errstr, eoptarg);
return(1);
}
break;
default:
return(1);
}
}
inform++;
if (!clcnt) {
if (merge) {
if ((clcnt = argc - eoptind) <= 1) {
clcnt = CLCNT;
#ifdef stupid
merge = 0;
#endif
}
} else
clcnt = CLCNT;
}
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);
}
}
if (!wflag) {
if (sflag)
pgwd = SPGWD;
else
pgwd = PGWD;
}
if (cflag || merge) {
if (!eflag) {
inchar = INCHAR;
ingap = INGAP;
}
if (!iflag) {
ochar = OCHAR;
ogap = OGAP;
}
}
if (cflag) {
if (merge) {
ferrout("pr: -m cannot be used with multiple columns\n");
return(1);
}
if (nmwd) {
colwd = (pgwd + 1 - (clcnt * (nmwd + 2)))/clcnt;
pgwd = ((colwd + nmwd + 2) * clcnt) - 1;
} else {
colwd = (pgwd + 1 - clcnt)/clcnt;
pgwd = ((colwd + 1) * clcnt) - 1;
}
if (colwd < 1) {
ferrout("pr: page width is too small for %d columns\n",clcnt);
return(1);
}
}
if (!lines)
lines = LINES;
if (lines <= HEADLEN + TAILLEN)
nohead = 1;
else if (!nohead)
lines -= HEADLEN + TAILLEN;
if (dspace) {
if (lines == 1)
dspace = 0;
else {
if (lines & 1)
++addone;
lines /= 2;
}
}
return(0);
}