#include <sys/types.h>
#include <sys/time.h>
#include <sys/stat.h>
#include <ctype.h>
#include <errno.h>
#include <langinfo.h>
#include <locale.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "pr.h"
#include "extern.h"
static int pgnm;
static int clcnt;
static int colwd;
static int across;
static int dspace;
static char inchar;
static int ingap;
static int pausefst;
static int pauseall;
static int formfeed;
static char *header;
static char ochar;
static int ogap;
static int lines;
static int merge;
static char nmchar;
static int nmwd;
static int offst;
static int nodiag;
static char schar;
static int sflag;
static int nohead;
static int pgwd;
static char *timefrmt;
static FILE *err;
static int addone;
static int errcnt;
static char digs[] = "0123456789";
static char fnamedefault[] = FNAME;
int
main(int argc, char *argv[])
{
int ret_val;
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);
free(timefrmt);
} else
usage();
flsh_errs();
if (errcnt || ret_val)
exit(1);
return(0);
}
static void
ttypause(int pagecnt)
{
int pch;
FILE *ttyfp;
if ((pauseall || (pausefst && pagecnt == 1)) &&
isatty(STDOUT_FILENO)) {
if ((ttyfp = fopen("/dev/tty", "r")) != NULL) {
(void)putc('\a', stderr);
while ((pch = getc(ttyfp)) != '\n' && pch != EOF)
;
(void)fclose(ttyfp);
}
}
}
int
onecol(int argc, char *argv[])
{
int cnt = -1;
int off;
int lrgln;
int linecnt;
int num;
int lncnt;
int pagecnt;
int ips;
int ops;
int cps;
char *obuf;
char *lbuf;
char *nbuf;
char *hbuf;
char *ohbuf;
FILE *inf;
const char *fname;
int mor;
if (nmwd)
num = nmwd + 1;
else
num = 0;
off = num + offst;
if ((obuf = malloc((unsigned)(LBUF + off)*sizeof(char))) == NULL) {
mfail();
return(1);
}
if ((hbuf = malloc((unsigned)(HDBUF + offst)*sizeof(char))) == NULL) {
free(obuf);
mfail();
return(1);
}
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) {
if (pgnm) {
if (inskip(inf, pgnm, lines))
continue;
pagecnt = pgnm;
} else
pagecnt = 1;
lncnt = 0;
for(;;) {
linecnt = 0;
lrgln = 0;
ops = 0;
ips = 0;
cps = 0;
ttypause(pagecnt);
while (linecnt < lines) {
if ((cnt = inln(inf,lbuf,LBUF,&cps,0,&mor)) < 0)
break;
if (!linecnt && !nohead &&
prhead(hbuf, fname, pagecnt))
goto err;
if (!lrgln) {
if (num)
addnum(nbuf, num, ++lncnt);
if (otln(obuf,cnt+off, &ips, &ops, mor))
goto err;
} else if (otln(lbuf, cnt, &ips, &ops, mor))
goto err;
if (mor) {
lrgln = 1;
continue;
}
++linecnt;
lrgln = 0;
ops = 0;
ips = 0;
}
if (linecnt && prtail(lines-linecnt-lrgln, lrgln))
goto err;
if (cnt < 0)
break;
++pagecnt;
}
if (inf != stdin)
(void)fclose(inf);
}
if (eoptind < argc)
goto err;
free(hbuf);
free(obuf);
return(0);
err:
free(hbuf);
free(obuf);
return(1);
}
int
vertcol(int argc, char *argv[])
{
char *ptbf;
char **lstdat = NULL;
int i;
int j;
int cnt = -1;
int pln;
int *indy = NULL;
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;
const char *fname;
FILE *inf;
int ips = 0;
int cps = 0;
int ops = 0;
int mor = 0;
int retval = 1;
if ((buf = malloc((unsigned)lines*mxlen*sizeof(char))) == NULL) {
mfail();
return(1);
}
if ((hbuf = malloc((unsigned)(HDBUF + offst)*sizeof(char))) == NULL) {
mfail();
goto out;
}
ohbuf = hbuf + offst;
if (offst)
(void)memset(hbuf, (int)' ', offst);
mvc = lines * clcnt;
if ((vc =
(struct vcol *)malloc((unsigned)mvc*sizeof(struct vcol))) == NULL) {
mfail();
goto out;
}
if ((lstdat = (char **)malloc((unsigned)lines*sizeof(char *))) == NULL){
mfail();
goto out;
}
if ((indy = (int *)malloc((unsigned)lines*sizeof(int))) == NULL) {
mfail();
goto out;
}
if ((lindy = (int *)malloc((unsigned)lines*sizeof(int))) == NULL) {
mfail();
goto out;
}
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) {
if (pgnm) {
if (inskip(inf, pgnm, lines))
continue;
pagecnt = pgnm;
} else
pagecnt = 1;
lncnt = 0;
for(;;) {
ttypause(pagecnt);
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;
}
cnt = inln(inf,ptbf,colwd,&cps,1,&mor);
vc[cvc++].cnt = cnt;
if (cnt < 0)
break;
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 (cnt < 0)
break;
}
--cvc;
if ((cnt < 0) && cvc && ((mvc-cvc) >= clcnt)){
pln = cvc/clcnt;
if (cvc % clcnt)
++pln;
if (!nohead && prhead(hbuf, fname, pagecnt))
goto out;
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;
}
if (prtail((lines - pln), 0))
goto out;
break;
}
if (i > 0)
pln = lines;
else
pln = j;
if (pln && !nohead && prhead(hbuf, fname, pagecnt))
goto out;
for (i = 0; i < pln; ++i) {
ptbf = buf + lindy[i];
if ((j = lstdat[i] - ptbf) <= offst)
break;
if (otln(ptbf, j, &ips, &ops, 0))
goto out;
}
if (pln && prtail((lines - pln), 0))
goto out;
if (cnt < 0)
break;
++pagecnt;
}
if (inf != stdin)
(void)fclose(inf);
}
if (eoptind < argc)
goto out;
retval = 0;
out:
free(lindy);
free(indy);
free(lstdat);
free(vc);
free(hbuf);
free(buf);
return(retval);
}
int
horzcol(int argc, char *argv[])
{
char *ptbf;
int pln;
int cnt = -1;
char *lstdat;
int col = colwd + 1;
int j;
int i;
int lncnt;
int pagecnt;
char *buf;
char *hbuf;
char *ohbuf;
const char *fname;
FILE *inf;
int ips = 0;
int cps = 0;
int ops = 0;
int mor = 0;
if ((buf = malloc((unsigned)(pgwd+offst+1)*sizeof(char))) == NULL) {
mfail();
return(1);
}
if ((hbuf = malloc((unsigned)(HDBUF + offst)*sizeof(char))) == NULL) {
free(buf);
mfail();
return(1);
}
ohbuf = hbuf + offst;
if (offst) {
(void)memset(buf, (int)' ', offst);
(void)memset(hbuf, (int)' ', offst);
}
while ((inf = nxtfile(argc, argv, &fname, ohbuf, 0)) != NULL) {
if (pgnm) {
if (inskip(inf, pgnm, lines))
continue;
pagecnt = pgnm;
} else
pagecnt = 1;
lncnt = 0;
for(;;) {
ttypause(pagecnt);
for (i = 0; i < lines; ++i) {
ptbf = buf + offst;
lstdat = ptbf;
j = 0;
for(;;) {
if (nmwd) {
addnum(ptbf, nmwd, ++lncnt);
ptbf += nmwd;
*ptbf++ = nmchar;
}
if ((cnt = inln(inf,ptbf,colwd,&cps,1,
&mor)) < 0)
break;
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 ((j = lstdat - buf) <= offst)
break;
if (!i && !nohead &&
prhead(hbuf, fname, pagecnt))
goto err;
if (otln(buf, j, &ips, &ops, 0))
goto err;
}
if (i && prtail(lines-i, 0))
goto err;
if (cnt < 0)
break;
++pagecnt;
}
if (inf != stdin)
(void)fclose(inf);
}
if (eoptind < argc)
goto err;
free(hbuf);
free(buf);
return(0);
err:
free(hbuf);
free(buf);
return(1);
}
int
mulfile(int argc, char *argv[])
{
char *ptbf;
int j;
int pln;
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;
const char *fname;
int ips = 0;
int cps = 0;
int ops = 0;
int mor = 0;
int retval = 1;
if ((fbuf = (FILE **)malloc((unsigned)clcnt*sizeof(FILE *))) == NULL) {
mfail();
goto out;
}
if ((hbuf = malloc((unsigned)(HDBUF + offst)*sizeof(char))) == NULL) {
mfail();
goto out;
}
ohbuf = hbuf + offst;
j = 0;
while (j < clcnt) {
if ((fbuf[j] = nxtfile(argc, argv, &fname, ohbuf, 1)) == NULL)
break;
if (pgnm && (inskip(fbuf[j], pgnm, lines)))
fbuf[j] = NULL;
++j;
}
if (!j)
goto out;
clcnt = j;
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) {
(void)fprintf(err,
"pr: page width too small for %d columns\n", clcnt);
goto out;
}
actf = clcnt;
col = colwd + 1;
if ((buf = malloc((unsigned)(pgwd+offst+1)*sizeof(char))) == NULL) {
mfail();
goto out;
}
if (offst) {
(void)memset(buf, (int)' ', offst);
(void)memset(hbuf, (int)' ', offst);
}
if (pgnm)
pagecnt = pgnm;
else
pagecnt = 1;
lncnt = 0;
while (actf > 0) {
ttypause(pagecnt);
for (i = 0; i < lines; ++i) {
ptbf = buf + offst;
lstdat = ptbf;
if (nmwd) {
addnum(ptbf, nmwd, ++lncnt);
ptbf += nmwd;
*ptbf++ = nmchar;
}
j = 0;
fproc = 0;
for (j = 0; j < clcnt; ++j) {
if (fbuf[j] == NULL) {
cnt = 0;
} else if ((cnt = inln(fbuf[j], ptbf, colwd,
&cps, 1, &mor)) < 0) {
if (fbuf[j] != stdin)
(void)fclose(fbuf[j]);
fbuf[j] = NULL;
--actf;
cnt = 0;
} else {
ptbf += cnt;
lstdat = ptbf;
fproc++;
}
if (fproc >= actf)
break;
if (sflag) {
*ptbf++ = schar;
} else if ((pln = col - cnt) > 0) {
(void)memset(ptbf, (int)' ', pln);
ptbf += pln;
}
}
if ((j = lstdat - buf) <= offst)
break;
if (!i && !nohead && prhead(hbuf, fname, pagecnt))
goto out;
if (otln(buf, j, &ips, &ops, 0))
goto out;
if (actf <= 0) {
++i;
break;
}
}
if (i && prtail(lines-i, 0))
goto out;
++pagecnt;
}
if (eoptind < argc)
goto out;
retval = 0;
out:
free(buf);
free(hbuf);
free(fbuf);
return(retval);
}
int
inln(FILE *inf, char *buf, int lim, int *cps, int trnc, int *mor)
{
int col;
int gap = ingap;
int ch = EOF;
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))
return(1);
while ((--col >= 0) && (--lim >= 0))
*ptbuf++ = ' ';
continue;
}
if (ch == '\n')
break;
*ptbuf++ = ch;
}
} else {
while ((--lim >= 0) && ((ch = getc(inf)) != EOF)) {
if (ch == '\n')
break;
*ptbuf++ = ch;
}
}
col = ptbuf - buf;
if (ch == EOF) {
*mor = 0;
*cps = 0;
if (!col)
return(-1);
return(col);
}
if (ch == '\n') {
*mor = 0;
*cps = 0;
return(col);
}
if (trnc) {
while ((ch = getc(inf)) != EOF) {
if (ch == '\n')
break;
}
*cps = 0;
*mor = 0;
} else {
*cps += col;
*mor = 1;
}
return(col);
}
int
otln(char *buf, int cnt, int *svips, int *svops, int mor)
{
int ops;
int ips;
int gap = ogap;
int tbps;
char *endbuf;
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 (ips - ops > 1) {
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 (ips - ops > 1) {
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) <= 0)) {
pfail();
return(1);
}
if (mor != 0)
return(0);
}
if ((putchar('\n') == EOF) || (dspace && (putchar('\n') == EOF))) {
pfail();
return(1);
}
return(0);
}
int
inskip(FILE *inf, int pgcnt, int lncnt)
{
int c;
int cnt;
while(--pgcnt > 0) {
cnt = lncnt;
while ((c = getc(inf)) != EOF) {
if ((c == '\n') && (--cnt == 0))
break;
}
if (c == EOF) {
if (inf != stdin)
(void)fclose(inf);
return(1);
}
}
return(0);
}
FILE *
nxtfile(int argc, char **argv, const char **fname, char *buf, int dt)
{
FILE *inf = NULL;
time_t tv_sec;
struct tm *timeptr = NULL;
struct stat statbuf;
static int twice = -1;
++twice;
if (eoptind >= argc) {
if (twice)
return(NULL);
clearerr(stdin);
inf = stdin;
if (header != NULL)
*fname = header;
else
*fname = fnamedefault;
if (nohead)
return(inf);
if ((tv_sec = time(NULL)) == -1) {
++errcnt;
(void)fprintf(err, "pr: cannot get time of day, %s\n",
strerror(errno));
eoptind = argc - 1;
return(NULL);
}
timeptr = localtime(&tv_sec);
}
for (; eoptind < argc; ++eoptind) {
if (strcmp(argv[eoptind], "-") == 0) {
clearerr(stdin);
inf = stdin;
if (header != NULL)
*fname = header;
else
*fname = fnamedefault;
++eoptind;
if (nohead || (dt && twice))
return(inf);
if ((tv_sec = time(NULL)) == -1) {
++errcnt;
(void)fprintf(err,
"pr: cannot get time of day, %s\n",
strerror(errno));
return(NULL);
}
timeptr = localtime(&tv_sec);
} else {
if ((inf = fopen(argv[eoptind], "r")) == NULL) {
++errcnt;
if (nodiag)
continue;
(void)fprintf(err, "pr: cannot open %s, %s\n",
argv[eoptind], strerror(errno));
continue;
}
if (header != NULL)
*fname = header;
else if (dt)
*fname = fnamedefault;
else
*fname = argv[eoptind];
++eoptind;
if (nohead || (dt && twice))
return(inf);
if (dt) {
if ((tv_sec = time(NULL)) == -1) {
++errcnt;
(void)fprintf(err,
"pr: cannot get time of day, %s\n",
strerror(errno));
fclose(inf);
return(NULL);
}
timeptr = localtime(&tv_sec);
} else {
if (fstat(fileno(inf), &statbuf) < 0) {
++errcnt;
(void)fclose(inf);
(void)fprintf(err,
"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, timefrmt, timeptr) <= 0) {
++errcnt;
if (inf != stdin)
(void)fclose(inf);
(void)fputs("pr: time conversion failed\n", err);
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, const char *fname, int pagcnt)
{
int ips = 0;
int ops = 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 (nohead) {
if (incomp &&
((dspace && (putchar('\n') == EOF)) ||
(putchar('\n') == EOF))) {
pfail();
return(1);
}
if (formfeed) {
if (putchar('\f') == EOF) {
pfail();
return(1);
}
}
return(0);
}
if (dspace)
cnt *= 2;
if (addone)
++cnt;
if (formfeed) {
if ((incomp && (putchar('\n') == EOF)) ||
(putchar('\f') == EOF)) {
pfail();
return(1);
}
return(0);
}
cnt += TAILLEN;
while (--cnt >= 0) {
if (putchar('\n') == EOF) {
pfail();
return(1);
}
}
return(0);
}
void
terminate(int which_sig __unused)
{
flsh_errs();
exit(1);
}
void
flsh_errs(void)
{
char buf[BUFSIZ];
(void)fflush(stdout);
(void)fflush(err);
if (err == stderr)
return;
rewind(err);
while (fgets(buf, BUFSIZ, err) != NULL)
(void)fputs(buf, stderr);
}
void
mfail(void)
{
(void)fputs("pr: memory allocation failed\n", err);
}
void
pfail(void)
{
(void)fprintf(err, "pr: write failure, %s\n", strerror(errno));
}
void
usage(void)
{
(void)fputs(
"usage: pr [+page] [-col] [-adFfmprt] [-e[ch][gap]] [-h header]\n",
err);
(void)fputs(
" [-i[ch][gap]] [-l line] [-n[ch][width]] [-o offset]\n",err);
(void)fputs(
" [-L locale] [-s[ch]] [-w width] [-] [file ...]\n", err);
}
int
setup(int argc, char *argv[])
{
int c;
int d_first;
int eflag = 0;
int iflag = 0;
int wflag = 0;
int cflag = 0;
char *Lflag = NULL;
if (isatty(fileno(stdout))) {
if ((err = tmpfile()) == NULL) {
err = stderr;
(void)fputs("Cannot defer diagnostic messages\n",stderr);
return(1);
}
} else
err = stderr;
while ((c = egetopt(argc, argv, "#adFfmrte?h:i?L:l:n?o:ps?w:")) != -1) {
switch (c) {
case '+':
if ((pgnm = atoi(eoptarg)) < 1) {
(void)fputs("pr: +page number must be 1 or more\n",
err);
return(1);
}
break;
case '-':
if ((clcnt = atoi(eoptarg)) < 1) {
(void)fputs("pr: -columns must be 1 or more\n",err);
return(1);
}
if (clcnt > 1)
++cflag;
break;
case 'a':
++across;
break;
case 'd':
++dspace;
break;
case 'e':
++eflag;
if ((eoptarg != NULL) && !isdigit((unsigned char)*eoptarg))
inchar = *eoptarg++;
else
inchar = INCHAR;
if ((eoptarg != NULL) && isdigit((unsigned char)*eoptarg)) {
if ((ingap = atoi(eoptarg)) < 0) {
(void)fputs(
"pr: -e gap must be 0 or more\n", err);
return(1);
}
if (ingap == 0)
ingap = INGAP;
} else if ((eoptarg != NULL) && (*eoptarg != '\0')) {
(void)fprintf(err,
"pr: invalid value for -e %s\n", eoptarg);
return(1);
} else
ingap = INGAP;
break;
case 'f':
++pausefst;
case 'F':
++formfeed;
break;
case 'h':
header = eoptarg;
break;
case 'i':
++iflag;
if ((eoptarg != NULL) && !isdigit((unsigned char)*eoptarg))
ochar = *eoptarg++;
else
ochar = OCHAR;
if ((eoptarg != NULL) && isdigit((unsigned char)*eoptarg)) {
if ((ogap = atoi(eoptarg)) < 0) {
(void)fputs(
"pr: -i gap must be 0 or more\n", err);
return(1);
}
if (ogap == 0)
ogap = OGAP;
} else if ((eoptarg != NULL) && (*eoptarg != '\0')) {
(void)fprintf(err,
"pr: invalid value for -i %s\n", eoptarg);
return(1);
} else
ogap = OGAP;
break;
case 'L':
Lflag = eoptarg;
break;
case 'l':
if (!isdigit((unsigned char)*eoptarg) || ((lines=atoi(eoptarg)) < 1)) {
(void)fputs(
"pr: number of lines must be 1 or more\n",err);
return(1);
}
break;
case 'm':
++merge;
break;
case 'n':
if ((eoptarg != NULL) && !isdigit((unsigned char)*eoptarg))
nmchar = *eoptarg++;
else
nmchar = NMCHAR;
if ((eoptarg != NULL) && isdigit((unsigned char)*eoptarg)) {
if ((nmwd = atoi(eoptarg)) < 1) {
(void)fputs(
"pr: -n width must be 1 or more\n",err);
return(1);
}
} else if ((eoptarg != NULL) && (*eoptarg != '\0')) {
(void)fprintf(err,
"pr: invalid value for -n %s\n", eoptarg);
return(1);
} else
nmwd = NMWD;
break;
case 'o':
if (!isdigit((unsigned char)*eoptarg) || ((offst = atoi(eoptarg))< 1)){
(void)fputs("pr: -o offset must be 1 or more\n",
err);
return(1);
}
break;
case 'p':
++pauseall;
break;
case 'r':
++nodiag;
break;
case 's':
++sflag;
if (eoptarg == NULL)
schar = SCHAR;
else {
schar = *eoptarg++;
if (*eoptarg != '\0') {
(void)fprintf(err,
"pr: invalid value for -s %s\n",
eoptarg);
return(1);
}
}
break;
case 't':
++nohead;
break;
case 'w':
++wflag;
if ((eoptarg == NULL ) ||
!isdigit((unsigned char)*eoptarg) ||
((pgwd = atoi(eoptarg)) < 1)){
(void)fputs(
"pr: -w width must be 1 or more \n",err);
return(1);
}
break;
case '?':
default:
return(1);
}
}
if (!clcnt) {
if (merge) {
if ((clcnt = argc - eoptind) <= 1) {
clcnt = CLCNT;
merge = 0;
}
} else
clcnt = CLCNT;
}
if (across) {
if (clcnt == 1) {
(void)fputs("pr: -a flag requires multiple columns\n",
err);
return(1);
}
if (merge) {
(void)fputs("pr: -m cannot be used with -a\n", err);
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) {
(void)fputs(
"pr: -m cannot be used with multiple columns\n", err);
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) {
(void)fprintf(err,
"pr: page width is too small for %d columns\n",clcnt);
return(1);
}
}
if (!lines)
lines = LINES;
if (lines <= HEADLEN + TAILLEN)
++nohead;
else if (!nohead)
lines -= HEADLEN + TAILLEN;
if (dspace) {
if (lines == 1)
dspace = 0;
else {
if (lines & 1)
++addone;
lines /= 2;
}
}
(void) setlocale(LC_TIME, (Lflag != NULL) ? Lflag : "");
d_first = (*nl_langinfo(D_MD_ORDER) == 'd');
timefrmt = strdup(d_first ? TIMEFMTD : TIMEFMTM);
return(0);
}