Update kill, last, ln, mesg, mkdir, mv, nice, od, pagesize, pr, printenv.

This commit is contained in:
Serge
2022-07-10 18:33:12 -07:00
parent 26e2d7dd5e
commit 1253330adc
11 changed files with 643 additions and 768 deletions

View File

@@ -1,24 +1,24 @@
/*
* kill - send signal to process
*/
#include <ctype.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <ctype.h>
#include <string.h>
#include <errno.h>
char *signm[] = { 0,
"HUP", "INT", "QUIT", "ILL", "TRAP", "IOT", "EMT", "FPE", /* 1-8 */
"KILL", "BUS", "SEGV", "SYS", "PIPE", "ALRM", "TERM", "URG", /* 9-16 */
"STOP", "TSTP", "CONT", "CHLD", "TTIN", "TTOU", "IO", "XCPU", /* 17-24 */
"XFSZ", "VTALRM", "PROF", "WINCH", 0, "USR1", "USR2", 0, /* 25-31 */
char *signm[] = {
0, "HUP", "INT", "QUIT", "ILL", "TRAP", "IOT", "EMT", "FPE", /* 1-8 */
"KILL", "BUS", "SEGV", "SYS", "PIPE", "ALRM", "TERM", "URG", /* 9-16 */
"STOP", "TSTP", "CONT", "CHLD", "TTIN", "TTOU", "IO", "XCPU", /* 17-24 */
"XFSZ", "VTALRM", "PROF", "WINCH", 0, "USR1", "USR2", 0, /* 25-31 */
};
main(argc, argv)
char **argv;
int main(int argc, char **argv)
{
register signo, pid, res;
int signo, pid, res;
int errlev;
extern errno;
errlev = 0;
if (argc <= 1) {
@@ -38,22 +38,20 @@ char **argv;
printf("\n");
exit(0);
} else if (isdigit(argv[1][1])) {
signo = atoi(argv[1]+1);
signo = atoi(argv[1] + 1);
if (signo < 0 || signo > NSIG) {
printf("kill: %s: number out of range\n",
argv[1]);
printf("kill: %s: number out of range\n", argv[1]);
exit(1);
}
} else {
char *name = argv[1]+1;
char *name = argv[1] + 1;
for (signo = 0; signo <= NSIG; signo++)
if (signm[signo] && !strcmp(signm[signo], name))
goto foundsig;
printf("kill: %s: unknown signal; kill -l lists signals\n", name);
exit(1);
foundsig:
;
}
foundsig:
argc--;
argv++;
} else
@@ -63,12 +61,12 @@ foundsig:
if (!(isdigit(**argv) || **argv == '-'))
goto usage;
res = kill(pid = atoi(*argv), signo);
if (res<0) {
if (res < 0) {
printf("%u: %s\n", pid, strerror(errno));
errlev = 1;
}
argc--;
argv++;
}
return(errlev);
return (errlev);
}

View File

@@ -5,38 +5,41 @@
* All rights reserved. The Berkeley software License Agreement
* specifies the terms and conditions for redistribution.
*/
#include <sys/types.h>
#include <paths.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <signal.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <utmp.h>
#include <paths.h>
#include <pwd.h>
#include <fcntl.h>
#define NMAX sizeof(buf[0].ut_name)
#define LMAX sizeof(buf[0].ut_line)
#define HMAX sizeof(buf[0].ut_host)
#define SECDAY ((long)24*60*60)
#define NMAX sizeof(buf[0].ut_name)
#define LMAX sizeof(buf[0].ut_line)
#define HMAX sizeof(buf[0].ut_host)
#define SECDAY ((long)24 * 60 * 60)
#define lineq(a,b) (!strncmp(a,b,LMAX))
#define nameq(a,b) (!strncmp(a,b,NMAX))
#define hosteq(a,b) (!strncmp(a,b,HMAX))
#define lineq(a, b) (!strncmp(a, b, LMAX))
#define nameq(a, b) (!strncmp(a, b, NMAX))
#define hosteq(a, b) (!strncmp(a, b, HMAX))
#define MAXTTYS 256
char **argv;
char **argv;
int argc;
int nameargs;
struct utmp buf[128];
char ttnames[MAXTTYS][LMAX+1];
long logouts[MAXTTYS];
struct utmp buf[128];
char ttnames[MAXTTYS][LMAX + 1];
long logouts[MAXTTYS];
char *ctime(), *strspl();
static char *strspl(char *left, char *right);
static int want(struct utmp *bp);
void onintr(signo)
int signo;
void onintr(int signo)
{
char *ct;
@@ -49,52 +52,48 @@ void onintr(signo)
exit(1);
}
void
usage(progname)
char *progname;
void usage(char *progname)
{
printf("Usage: %s [ -f filename ] [-number] [name...] [tty...]\n",
progname);
printf("Usage: %s [ -f filename ] [-number] [name...] [tty...]\n", progname);
exit(1);
}
main(ac, av)
char **av;
int main(int ac, char **av)
{
register int i, k;
int i, k;
int wtmp;
off_t bl;
char *ct;
char wtmpfile[256];
char progname[256];
register struct utmp *bp;
struct utmp *bp;
long otime;
struct stat stb;
int print;
int sinput = 0;
char * crmsg = (char *)0;
char *crmsg = (char *)0;
long crtime;
long outrec = 0;
long maxrec = 0x7fffffffL;
time(&buf[0].ut_time);
strcpy(wtmpfile, _PATH_WTMP);
strcpy(progname,av[0]);
strcpy(progname, av[0]);
ac--, av++;
nameargs = argc = ac;
argv = av;
for (i = 0; i < argc; i++) {
if (argv[i][0] == '-' ) {
if ( argv[i][1] >= '0' && argv[i][1] <= '9') {
maxrec = atoi(argv[i]+1);
if (argv[i][0] == '-') {
if (argv[i][1] >= '0' && argv[i][1] <= '9') {
maxrec = atoi(argv[i] + 1);
nameargs--;
continue;
} else {
if (argv[i][1] == 'f') {
i++;
if ( i < argc) {
strcpy(wtmpfile,argv[i]);
nameargs = nameargs -2;
if (i < argc) {
strcpy(wtmpfile, argv[i]);
nameargs = nameargs - 2;
continue;
} else {
usage(progname);
@@ -104,7 +103,7 @@ main(ac, av)
}
}
}
if (strlen(argv[i])>2)
if (strlen(argv[i]) > 2)
continue;
if (!strcmp(argv[i], "~"))
continue;
@@ -122,28 +121,24 @@ main(ac, av)
exit(1);
}
fstat(wtmp, &stb);
bl = (stb.st_size + sizeof (buf)-1) / sizeof (buf);
bl = (stb.st_size + sizeof(buf) - 1) / sizeof(buf);
if (signal(SIGINT, SIG_IGN) != SIG_IGN) {
signal(SIGINT, onintr);
signal(SIGQUIT, onintr);
}
for (bl--; bl >= 0; bl--) {
lseek(wtmp, bl * sizeof (buf), 0);
bp = &buf[read(wtmp, buf, sizeof (buf)) / sizeof(buf[0]) - 1];
for ( ; bp >= buf; bp--) {
lseek(wtmp, bl * sizeof(buf), 0);
bp = &buf[read(wtmp, buf, sizeof(buf)) / sizeof(buf[0]) - 1];
for (; bp >= buf; bp--) {
print = want(bp);
if (print) {
ct = ctime(&bp->ut_time);
printf("%-*.*s %-*.*s %-*.*s %10.10s %5.5s ",
NMAX, NMAX, bp->ut_name,
LMAX, LMAX, bp->ut_line,
HMAX, HMAX, bp->ut_host,
ct, 11+ct);
printf("%-*.*s %-*.*s %-*.*s %10.10s %5.5s ", NMAX, NMAX, bp->ut_name, LMAX, LMAX,
bp->ut_line, HMAX, HMAX, bp->ut_host, ct, 11 + ct);
}
for (i = 0; i < MAXTTYS; i++) {
if (ttnames[i][0] == 0) {
strncpy(ttnames[i], bp->ut_line,
sizeof(bp->ut_line));
strncpy(ttnames[i], bp->ut_line, sizeof(bp->ut_line));
otime = logouts[i];
logouts[i] = bp->ut_time;
break;
@@ -165,16 +160,12 @@ main(ac, av)
otime = -otime;
printf("- %s", crmsg);
} else
printf("- %5.5s",
ctime(&otime)+11);
printf("- %5.5s", ctime(&otime) + 11);
delta = otime - bp->ut_time;
if (delta < SECDAY)
printf(" (%5.5s)\n",
asctime(gmtime(&delta))+11);
printf(" (%5.5s)\n", asctime(gmtime(&delta)) + 11);
else
printf(" (%ld+%5.5s)\n",
delta / SECDAY,
asctime(gmtime(&delta))+11);
printf(" (%ld+%5.5s)\n", delta / SECDAY, asctime(gmtime(&delta)) + 11);
}
fflush(stdout);
if (++outrec >= maxrec)
@@ -195,14 +186,13 @@ main(ac, av)
exit(0);
}
want(bp)
struct utmp *bp;
int want(struct utmp *bp)
{
register char **av;
register int ac;
char **av;
int ac;
if (bp->ut_line[0] == '~' && bp->ut_name[0] == '\0')
strcpy(bp->ut_name, "reboot"); /* bandaid */
strcpy(bp->ut_name, "reboot"); /* bandaid */
if (strncmp(bp->ut_line, "ftp", 3) == 0)
bp->ut_line[3] = '\0';
if (strncmp(bp->ut_line, "uucp", 4) == 0)
@@ -221,11 +211,9 @@ want(bp)
return (0);
}
char *
strspl(left, right)
char *left, *right;
char *strspl(char *left, char *right)
{
char *res = (char *)malloc(strlen(left)+strlen(right)+1);
char *res = (char *)malloc(strlen(left) + strlen(right) + 1);
strcpy(res, left);
strcat(res, right);

View File

@@ -1,24 +1,25 @@
/*
* ln
*/
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <string.h>
#include <strings.h>
#include <unistd.h>
#include <sys/stat.h>
#include <errno.h>
#include <sys/types.h>
struct stat stb;
int fflag; /* force flag set? */
struct stat stb;
int fflag; /* force flag set? */
int sflag;
char name[BUFSIZ];
char *rindex();
extern int errno;
char name[BUFSIZ];
main(argc, argv)
int argc;
register char **argv;
static int linkit(char *from, char *to);
int main(int argc, char **argv)
{
register int i, r;
int i, r;
argc--, argv++;
again:
@@ -39,35 +40,31 @@ again:
argc++;
}
if (sflag == 0 && argc > 2) {
if (stat(argv[argc-1], &stb) < 0)
if (stat(argv[argc - 1], &stb) < 0)
goto usage;
if ((stb.st_mode&S_IFMT) != S_IFDIR)
if ((stb.st_mode & S_IFMT) != S_IFDIR)
goto usage;
}
r = 0;
for(i = 0; i < argc-1; i++)
r |= linkit(argv[i], argv[argc-1]);
for (i = 0; i < argc - 1; i++)
r |= linkit(argv[i], argv[argc - 1]);
exit(r);
usage:
fprintf(stderr, "Usage: ln [ -s ] f1\nor: ln [ -s ] f1 f2\nln [ -s ] f1 ... fn d2\n");
exit(1);
}
int link(), symlink();
linkit(from, to)
char *from, *to;
int linkit(char *from, char *to)
{
char *tail;
int (*linkf)() = sflag ? symlink : link;
int (*linkf)(const char *target, const char *linkpath) = sflag ? symlink : link;
/* is target a directory? */
if (sflag == 0 && fflag == 0 && stat(from, &stb) >= 0
&& (stb.st_mode&S_IFMT) == S_IFDIR) {
if (sflag == 0 && fflag == 0 && stat(from, &stb) >= 0 && (stb.st_mode & S_IFMT) == S_IFDIR) {
printf("%s is a directory\n", from);
return (1);
}
if (stat(to, &stb) >= 0 && (stb.st_mode&S_IFMT) == S_IFDIR) {
if (stat(to, &stb) >= 0 && (stb.st_mode & S_IFMT) == S_IFDIR) {
tail = rindex(from, '/');
if (tail == 0)
tail = from;

View File

@@ -8,50 +8,54 @@
*/
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
struct stat sbuf;
char *tty;
char *ttyname();
main(argc, argv)
char *argv[];
void error(char *s)
{
int r=0;
fprintf(stderr, "mesg: %s\n", s);
exit(-1);
}
void newmode(m)
{
if (chmod(tty, m) < 0)
error("cannot change mode");
}
int main(int argc, char *argv[])
{
int r = 0;
tty = ttyname(2);
if (tty == 0)
exit(13);
if(stat(tty, &sbuf) < 0) error("cannot stat");
if(argc < 2) {
if(sbuf.st_mode & 020)
fprintf(stderr,"is y\n");
else { r=1;
fprintf(stderr,"is n\n");
if (stat(tty, &sbuf) < 0)
error("cannot stat");
if (argc < 2) {
if (sbuf.st_mode & 020)
fprintf(stderr, "is y\n");
else {
r = 1;
fprintf(stderr, "is n\n");
}
} else switch(*argv[1]) {
} else
switch (*argv[1]) {
case 'y':
newmode(sbuf.st_mode|020); break;
newmode(sbuf.st_mode | 020);
break;
case 'n':
newmode(sbuf.st_mode&~020); r=1; break;
newmode(sbuf.st_mode & ~020);
r = 1;
break;
default:
error("usage: mesg [y] [n]");
}
exit(r);
}
error(s)
char *s;
{
fprintf(stderr,"mesg: %s\n",s);
exit(-1);
}
newmode(m)
{
if(chmod(tty,m)<0)
error("cannot change mode");
}

View File

@@ -30,25 +30,24 @@
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include <sys/types.h>
#include <sys/stat.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
extern int errno;
static void usage(void);
static int build(char *path);
main(argc, argv)
int argc;
char **argv;
int main(int argc, char **argv)
{
extern int optind;
int ch, exitval, pflag;
pflag = 0;
while ((ch = getopt(argc, argv, "p")) != EOF)
switch(ch) {
switch (ch) {
case 'p':
pflag = 1;
break;
@@ -64,29 +63,26 @@ main(argc, argv)
if (pflag)
exitval |= build(*argv);
else if (mkdir(*argv, 0777) < 0) {
(void)fprintf(stderr, "mkdir: %s: %s\n",
*argv, strerror(errno));
(void)fprintf(stderr, "mkdir: %s: %s\n", *argv, strerror(errno));
exitval = 1;
}
exit(exitval);
}
build(path)
char *path;
int build(char *path)
{
register char *p;
struct stat sb;
int create, ch;
for (create = 0, p = path;; ++p)
if (!*p || *p == '/') {
if (!*p || *p == '/') {
ch = *p;
*p = '\0';
if (stat(path, &sb)) {
if (errno != ENOENT || mkdir(path, 0777) < 0) {
(void)fprintf(stderr, "mkdir: %s: %s\n",
path, strerror(errno));
return(1);
(void)fprintf(stderr, "mkdir: %s: %s\n", path, strerror(errno));
return (1);
}
create = 1;
}
@@ -94,14 +90,13 @@ build(path)
break;
}
if (!create) {
(void)fprintf(stderr, "mkdir: %s: %s\n", path,
strerror(EEXIST));
return(1);
(void)fprintf(stderr, "mkdir: %s: %s\n", path, strerror(EEXIST));
return (1);
}
return(0);
return (0);
}
usage()
void usage()
{
(void)fprintf(stderr, "usage: mkdir [-p] dirname ...\n");
exit(1);

View File

@@ -7,37 +7,42 @@
/*
* mv file1 file2
*/
#include <errno.h>
#include <signal.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/dir.h>
#include <sys/param.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include <sys/dir.h>
#include <errno.h>
#include <signal.h>
#define DELIM '/'
#define DELIM '/'
#define MODEBITS 07777
#define ISDIR(st) (((st).st_mode&S_IFMT) == S_IFDIR)
#define ISLNK(st) (((st).st_mode&S_IFMT) == S_IFLNK)
#define ISREG(st) (((st).st_mode&S_IFMT) == S_IFREG)
#define ISDEV(st) \
(((st).st_mode&S_IFMT) == S_IFCHR || ((st).st_mode&S_IFMT) == S_IFBLK)
#define ISDIR(st) (((st).st_mode & S_IFMT) == S_IFDIR)
#define ISLNK(st) (((st).st_mode & S_IFMT) == S_IFLNK)
#define ISREG(st) (((st).st_mode & S_IFMT) == S_IFREG)
#define ISDEV(st) (((st).st_mode & S_IFMT) == S_IFCHR || ((st).st_mode & S_IFMT) == S_IFBLK)
char *dname();
struct stat s1, s2;
int iflag = 0; /* interactive mode */
int fflag = 0; /* force overwriting */
struct stat s1, s2;
int iflag = 0; /* interactive mode */
int fflag = 0; /* force overwriting */
main(argc, argv)
register char *argv[];
static int movewithshortname(char *src, char *dest);
static int move(char *source, char *target);
static char *dname(char *name);
static void error(char *fmt, ...);
static void Perror(char *s);
static void Perror2(char *s1, char *s2);
int main(int argc, char *argv[])
{
register i, r;
register char *arg;
int i, r;
char *arg;
char *dest;
if (argc < 2)
@@ -50,28 +55,28 @@ main(argc, argv)
* all files following a null option
* are considered file names
*/
if (*(arg+1) == '\0')
if (*(arg + 1) == '\0')
break;
while (*++arg != '\0') switch (*arg) {
while (*++arg != '\0')
switch (*arg) {
case 'i':
iflag++;
break;
case 'i':
iflag++;
break;
case 'f':
fflag++;
break;
case 'f':
fflag++;
break;
default:
goto usage;
}
default:
goto usage;
}
}
if (argc < 3)
goto usage;
dest = argv[argc-1];
dest = argv[argc - 1];
if (stat(dest, &s2) >= 0 && ISDIR(s2)) {
r = 0;
for (i = 1; i < argc-1; i++)
for (i = 1; i < argc - 1; i++)
r |= movewithshortname(argv[i], dest);
exit(r);
}
@@ -82,43 +87,39 @@ main(argc, argv)
/*NOTREACHED*/
usage:
fprintf(stderr,
"usage: mv [-if] f1 f2 or mv [-if] f1 ... fn d1 (`fn' is a file or directory)\n");
"usage: mv [-if] f1 f2 or mv [-if] f1 ... fn d1 (`fn' is a file or directory)\n");
return (1);
}
movewithshortname(src, dest)
char *src, *dest;
int movewithshortname(char *src, char *dest)
{
register char *shortname;
char *shortname;
char target[MAXPATHLEN + 1];
shortname = dname(src);
if (strlen(dest) + strlen(shortname) > MAXPATHLEN - 1) {
error("%s/%s: pathname too long", dest,
shortname);
error("%s/%s: pathname too long", dest, shortname);
return (1);
}
sprintf(target, "%s/%s", dest, shortname);
return (move(src, target));
}
int
query (char *prompt, ...)
int query(char *prompt, ...)
{
va_list args;
register int i, c;
int i, c;
va_start (args, prompt);
va_start(args, prompt);
vfprintf(stderr, prompt, args);
va_end (args);
va_end(args);
i = c = getchar();
while (c != '\n' && c != EOF)
c = getchar();
return (i == 'y');
}
move(source, target)
char *source, *target;
int move(char *source, char *target)
{
int targetexists;
@@ -138,20 +139,17 @@ move(source, target)
error("%s and %s are identical", source, target);
return (1);
}
if (iflag && !fflag && isatty(fileno(stdin)) &&
query("remove %s? ", target) == 0)
if (iflag && !fflag && isatty(fileno(stdin)) && query("remove %s? ", target) == 0)
return (1);
if (access(target, 2) < 0 && !fflag && isatty(fileno(stdin))) {
if (query("override protection %o for %s? ",
s2.st_mode & MODEBITS, target) == 0)
if (query("override protection %o for %s? ", s2.st_mode & MODEBITS, target) == 0)
return (1);
}
}
if (rename(source, target) >= 0)
return (0);
if (errno != EXDEV) {
Perror2(errno == ENOENT && targetexists == 0 ? target : source,
"rename");
Perror2(errno == ENOENT && targetexists == 0 ? target : source, "rename");
return (1);
}
if (ISDIR(s1)) {
@@ -168,10 +166,10 @@ move(source, target)
* between file systems.
*/
if (ISLNK(s1)) {
register m;
int m;
char symln[MAXPATHLEN + 1];
m = readlink(source, symln, sizeof (symln) - 1);
m = readlink(source, symln, sizeof(symln) - 1);
if (m < 0) {
Perror(source);
return (1);
@@ -183,7 +181,7 @@ move(source, target)
Perror(target);
return (1);
}
(void) umask(m);
(void)umask(m);
goto cleanup;
}
if (ISDEV(s1)) {
@@ -198,11 +196,11 @@ move(source, target)
tv[0].tv_usec = 0;
tv[1].tv_sec = s1.st_mtime;
tv[1].tv_usec = 0;
(void) utimes(target, tv);
(void)utimes(target, tv);
goto cleanup;
}
if (ISREG(s1)) {
register int fi, fo, n;
int fi, fo, n;
struct timeval tv[2];
char buf[MAXBSIZE];
@@ -243,7 +241,7 @@ move(source, target)
tv[0].tv_usec = 0;
tv[1].tv_sec = s1.st_mtime;
tv[1].tv_usec = 0;
(void) utimes(target, tv);
(void)utimes(target, tv);
goto cleanup;
}
error("%s: unknown file type %o", source, s1.st_mode);
@@ -257,11 +255,9 @@ cleanup:
return (0);
}
char *
dname(name)
register char *name;
char *dname(char *name)
{
register char *p;
char *p;
p = name;
while (*p)
@@ -270,18 +266,17 @@ dname(name)
return name;
}
/*VARARGS*/
error(fmt, a1, a2)
char *fmt;
void error(char *fmt, ...)
{
va_list args;
va_start(args, fmt);
fprintf(stderr, "mv: ");
fprintf(stderr, fmt, a1, a2);
vfprintf(stderr, fmt, args);
fprintf(stderr, "\n");
va_end(args);
}
Perror(s)
char *s;
void Perror(char *s)
{
char buf[MAXPATHLEN + 10];
@@ -289,8 +284,7 @@ Perror(s)
perror(buf);
}
Perror2(s1, s2)
char *s1, *s2;
void Perror2(char *s1, char *s2)
{
char buf[MAXPATHLEN + 20];

View File

@@ -5,13 +5,11 @@
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/time.h>
#include <sys/resource.h>
main(argc, argv)
int argc;
char *argv[];
int main(int argc, char *argv[])
{
int nicarg = 10;
@@ -23,8 +21,7 @@ main(argc, argv)
fputs("usage: nice [ -n ] command\n", stderr);
exit(1);
}
if (setpriority(PRIO_PROCESS, 0,
getpriority(PRIO_PROCESS, 0) + nicarg) < 0) {
if (setpriority(PRIO_PROCESS, 0, getpriority(PRIO_PROCESS, 0) + nicarg) < 0) {
perror("setpriority");
exit(1);
}

File diff suppressed because it is too large Load Diff

View File

@@ -4,6 +4,7 @@
* specifies the terms and conditions for redistribution.
*/
#include <stdio.h>
#include <unistd.h>
int main()
{

View File

@@ -2,110 +2,118 @@
* print file with headings
* 2+head+2+page[56]+5
*/
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
/* Making putcp a macro sped things up by 14%. */
#define putcp(c) if (page >= fpage) putchar(c)
#define putcp(c) \
if (page >= fpage) \
putchar(c)
int ncol = 1;
char *header;
int ncol = 1;
char *header;
int col;
int icol;
FILE *file;
char *bufp;
#define BUFS 9000 /* at least 66 * 132 */
char buffer[BUFS]; /* for multi-column output */
char obuf[BUFSIZ];
#define FF 014
FILE *file;
char *bufp;
#define BUFS 9000 /* at least 66 * 132 */
char buffer[BUFS]; /* for multi-column output */
char obuf[BUFSIZ];
#define FF 014
int line;
char *colp[72];
char *colp[72];
int nofile;
char isclosed[10];
FILE *ifile[10];
char **lastarg;
char isclosed[10];
FILE *ifile[10];
char **lastarg;
int peekc;
int fpage;
int page;
int colw;
int nspace;
int width = 72;
int length = 66;
int width = 72;
int length = 66;
int plength = 61;
int margin = 10;
int margin = 10;
int ntflg;
int fflg;
int mflg;
int tabc;
char *tty;
char *tty;
int mode;
char *ttyname();
char *ctime();
void
onintr (sig)
int sig;
static void fixtty(void);
static int numeric(char *str);
static void print(char *fp, char **argp);
static void done(void);
static void mopen(char **ap);
static void nexbuf(void);
static int tpgetc(int ai);
static void put(int ac);
static void putpage(void);
static int pgetc(int i);
void onintr(int sig)
{
if (tty)
chmod(tty, mode);
_exit(1);
}
main(argc, argv)
char **argv;
int main(int argc, char **argv)
{
int nfdone;
setbuf(stdout, obuf);
if (signal(SIGINT, SIG_IGN) != SIG_IGN)
signal(SIGINT, onintr);
lastarg = &argv[argc-1];
lastarg = &argv[argc - 1];
fixtty();
for (nfdone=0; argc>1; argc--) {
for (nfdone = 0; argc > 1; argc--) {
argv++;
if (**argv == '-') {
switch (*++*argv) {
case 'h': /* define page header */
if (argc>=2) {
case 'h': /* define page header */
if (argc >= 2) {
header = *++argv;
argc--;
}
continue;
case 't': /* don't print page headers */
case 't': /* don't print page headers */
ntflg++;
continue;
case 'f': /* use form feeds */
case 'f': /* use form feeds */
fflg++;
plength = 60;
continue;
case 'l': /* length of page */
case 'l': /* length of page */
length = atoi(++*argv);
continue;
case 'w': /* width of page */
case 'w': /* width of page */
width = atoi(++*argv);
continue;
case 's': /* col separator */
case 's': /* col separator */
if (*++*argv)
tabc = **argv;
else
tabc = '\t';
continue;
case 'm': /* all files at once */
case 'm': /* all files at once */
mflg++;
continue;
default:
if (numeric(*argv)) { /* # of cols */
if (numeric(*argv)) { /* # of cols */
if ((ncol = atoi(*argv)) == 0) {
fprintf(stderr, "can't print 0 cols, using 1 instead.\n");
ncol = 1;
@@ -125,32 +133,30 @@ char **argv;
break;
}
}
if (nfdone==0)
if (nfdone == 0)
print((char *)0, (char **)0);
done();
}
done()
void done()
{
if (tty)
chmod(tty, mode);
exit(0);
}
/* numeric -- returns 1 if str is numeric, elsewise 0 */
numeric(str)
char *str;
int numeric(char *str)
{
for (; *str ; str++) {
for (; *str; str++) {
if (*str > '9' || *str < '0') {
return(0);
return (0);
}
}
return(1);
return (1);
}
fixtty()
void fixtty()
{
struct stat sbuf;
@@ -158,19 +164,17 @@ fixtty()
if (tty == 0)
return;
stat(tty, &sbuf);
mode = sbuf.st_mode&0777;
mode = sbuf.st_mode & 0777;
chmod(tty, 0600);
}
/* print -- print file */
print(fp, argp)
char *fp;
char **argp;
void print(char *fp, char **argp)
{
struct stat sbuf;
register sncol;
register char *sheader;
register char *cbuf;
int sncol;
char *sheader;
char *cbuf;
char linebuf[150], *cp;
if (ntflg)
@@ -181,7 +185,7 @@ char **argp;
length = 66;
if (width <= 0)
width = 72;
if (ncol>72 || ncol>width) {
if (ncol > 72 || ncol > width) {
fprintf(stderr, "pr: No room for columns.\n");
done();
}
@@ -189,19 +193,19 @@ char **argp;
mopen(argp);
ncol = nofile;
}
colw = width/(ncol==0? 1 : ncol);
colw = width / (ncol == 0 ? 1 : ncol);
sncol = ncol;
sheader = header;
plength = length-5;
plength = length - 5;
if (ntflg)
plength = length;
if (--ncol<0)
if (--ncol < 0)
ncol = 0;
if (mflg)
fp = 0;
if (fp) {
if((file=fopen(fp, "r"))==NULL) {
if (tty==NULL)
if ((file = fopen(fp, "r")) == NULL) {
if (tty == NULL)
perror(fp);
ncol = sncol;
header = sheader;
@@ -213,40 +217,39 @@ char **argp;
time(&sbuf.st_mtime);
}
if (header == 0)
header = fp?fp:"";
header = fp ? fp : "";
cbuf = ctime(&sbuf.st_mtime);
cbuf[16] = '\0';
cbuf[24] = '\0';
page = 1;
icol = 0;
colp[ncol] = bufp = buffer;
if (mflg==0)
if (mflg == 0)
nexbuf();
while (mflg&&nofile || (!mflg)&&tpgetc(ncol)>0) {
if (mflg==0) {
while (mflg && nofile || (!mflg) && tpgetc(ncol) > 0) {
if (mflg == 0) {
colp[ncol]--;
if (colp[ncol] < buffer)
colp[ncol] = &buffer[BUFS];
}
line = 0;
if (ntflg==0) {
if (ntflg == 0) {
if (fflg) {
/* Assume a ff takes two blank lines at the
top of the page. */
line = 2;
sprintf(linebuf, "%s %s %s Page %d\n\n\n",
cbuf+4, cbuf+20, header, page);
sprintf(linebuf, "%s %s %s Page %d\n\n\n", cbuf + 4, cbuf + 20, header, page);
} else
sprintf(linebuf, "\n\n%s %s %s Page %d\n\n\n",
cbuf+4, cbuf+20, header, page);
for(cp=linebuf;*cp;) put(*cp++);
sprintf(linebuf, "\n\n%s %s %s Page %d\n\n\n", cbuf + 4, cbuf + 20, header, page);
for (cp = linebuf; *cp;)
put(*cp++);
}
putpage();
if (ntflg==0) {
if (ntflg == 0) {
if (fflg)
put('\f');
else
while(line<length)
while (line < length)
put('\n');
}
page++;
@@ -256,85 +259,85 @@ char **argp;
header = sheader;
}
mopen(ap)
char **ap;
void mopen(char **ap)
{
register char **p, *p1;
char **p, *p1;
p = ap;
while((p1 = *p) && p++ <= lastarg) {
if((ifile[nofile]=fopen(p1, "r")) == NULL){
while ((p1 = *p) && p++ <= lastarg) {
if ((ifile[nofile] = fopen(p1, "r")) == NULL) {
isclosed[nofile] = 1;
nofile--;
}
else
} else
isclosed[nofile] = 0;
if(++nofile>=10) {
if (++nofile >= 10) {
fprintf(stderr, "pr: Too many args\n");
done();
}
}
}
putpage()
void putpage()
{
register int lastcol, i, c;
int lastcol, i, c;
int j;
if (ncol==0) {
while (line<plength) {
while((c = tpgetc(0)) && c!='\n' && c!=FF)
if (ncol == 0) {
while (line < plength) {
while ((c = tpgetc(0)) && c != '\n' && c != FF)
putcp(c);
if (c==0) break;
if (c == 0)
break;
putcp('\n');
line++;
if (c==FF)
if (c == FF)
break;
}
return;
}
colp[0] = colp[ncol];
if (mflg==0) for (i=1; i<=ncol; i++) {
colp[i] = colp[i-1];
for (j = margin; j<length; j++)
while((c=tpgetc(i))!='\n')
if (c==0)
break;
}
while (line<plength) {
if (mflg == 0)
for (i = 1; i <= ncol; i++) {
colp[i] = colp[i - 1];
for (j = margin; j < length; j++)
while ((c = tpgetc(i)) != '\n')
if (c == 0)
break;
}
while (line < plength) {
lastcol = colw;
for (i=0; i<ncol; i++) {
while ((c=pgetc(i)) && c!='\n')
if (col<lastcol || tabc!=0)
for (i = 0; i < ncol; i++) {
while ((c = pgetc(i)) && c != '\n')
if (col < lastcol || tabc != 0)
put(c);
if (c==0)
if (c == 0)
continue;
if (tabc)
put(tabc);
else while (col<lastcol)
put(' ');
else
while (col < lastcol)
put(' ');
lastcol += colw;
}
while ((c = pgetc(ncol)) && c!='\n')
while ((c = pgetc(ncol)) && c != '\n')
put(c);
put('\n');
}
}
nexbuf()
void nexbuf()
{
register int n;
register char *rbufp;
int n;
char *rbufp;
rbufp = bufp;
n = &buffer[BUFS] - rbufp;
if (n>512)
if (n > 512)
n = 512;
if((n=fread(rbufp,1,n,file)) <= 0){
if ((n = fread(rbufp, 1, n, file)) <= 0) {
fclose(file);
*rbufp = 0376;
}
else {
} else {
rbufp += n;
if (rbufp >= &buffer[BUFS])
rbufp = buffer;
@@ -343,24 +346,24 @@ nexbuf()
bufp = rbufp;
}
tpgetc(ai)
int tpgetc(int ai)
{
register char **p;
register int c, i;
char **p;
int c, i;
i = ai;
if (mflg) {
if((c=getc(ifile[i])) == EOF) {
if (isclosed[i]==0) {
if ((c = getc(ifile[i])) == EOF) {
if (isclosed[i] == 0) {
isclosed[i] = 1;
if (--nofile <= 0)
return(0);
return (0);
}
return('\n');
return ('\n');
}
if (c==FF && ncol>0)
if (c == FF && ncol > 0)
c = '\n';
return(c);
return (c);
}
loop:
c = **(p = &colp[i]) & 0377;
@@ -369,18 +372,18 @@ loop:
c = **p & 0377;
}
if (c == 0376)
return(0);
return (0);
(*p)++;
if (*p >= &buffer[BUFS])
*p = buffer;
if (c==0)
if (c == 0)
goto loop;
return(c);
return (c);
}
pgetc(i)
int pgetc(int i)
{
register int c;
int c;
if (peekc) {
c = peekc;
@@ -388,14 +391,13 @@ pgetc(i)
} else
c = tpgetc(i);
if (tabc)
return(c);
return (c);
switch (c) {
case '\t':
icol++;
if ((icol&07) != 0)
if ((icol & 07) != 0)
peekc = '\t';
return(' ');
return (' ');
case '\n':
icol = 0;
@@ -408,21 +410,21 @@ pgetc(i)
}
if (c >= ' ')
icol++;
return(c);
return (c);
}
put(ac)
void put(int ac)
{
register int ns, c;
int ns, c;
c = ac;
if (tabc) {
putcp(c);
if (c=='\n')
if (c == '\n')
line++;
return;
}
switch (c) {
case ' ':
nspace++;
col++;
@@ -436,15 +438,14 @@ put(ac)
case 010:
case 033:
if (--col<0)
if (--col < 0)
col = 0;
if (--nspace<0)
if (--nspace < 0)
nspace = 0;
}
while(nspace) {
if (nspace>2 && col > (ns=((col-nspace)|07))) {
nspace = col-ns-1;
while (nspace) {
if (nspace > 2 && col > (ns = ((col - nspace) | 07))) {
nspace = col - ns - 1;
putcp('\t');
} else {
nspace--;

View File

@@ -12,8 +12,7 @@
#include <stdlib.h>
#include <unistd.h>
int prefix(cp, dp)
char *cp, *dp;
int prefix(char *cp, char *dp)
{
while (*cp && *dp && *cp == *dp)
cp++, dp++;
@@ -22,20 +21,18 @@ int prefix(cp, dp)
return 0;
}
int main(argc, argv)
int argc;
char *argv[];
int main(int argc, char *argv[])
{
register char **ep;
char **ep;
int found = 0;
if (! environ)
if (!environ)
return 1;
argc--, argv++;
for (ep = environ; *ep; ep++) {
if (argc == 0 || prefix(argv[0], *ep)) {
register char *cp = *ep;
char *cp = *ep;
found++;
if (argc) {