diff --git a/src/cmd/kill.c b/src/cmd/kill.c index 1a0335b..f7619ab 100644 --- a/src/cmd/kill.c +++ b/src/cmd/kill.c @@ -1,24 +1,24 @@ /* * kill - send signal to process */ +#include +#include #include #include -#include -#include +#include +#include -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); } diff --git a/src/cmd/last.c b/src/cmd/last.c index 79c517d..b2dac8d 100644 --- a/src/cmd/last.c +++ b/src/cmd/last.c @@ -5,38 +5,41 @@ * All rights reserved. The Berkeley software License Agreement * specifies the terms and conditions for redistribution. */ -#include +#include +#include #include #include #include -#include +#include #include +#include #include -#include +#include +#include -#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); diff --git a/src/cmd/ln.c b/src/cmd/ln.c index d6eb781..1c98fec 100644 --- a/src/cmd/ln.c +++ b/src/cmd/ln.c @@ -1,24 +1,25 @@ /* * ln */ +#include #include #include -#include +#include +#include +#include #include -#include +#include -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; diff --git a/src/cmd/mesg.c b/src/cmd/mesg.c index 9d60792..7e7dca4 100644 --- a/src/cmd/mesg.c +++ b/src/cmd/mesg.c @@ -8,50 +8,54 @@ */ #include #include -#include +#include #include +#include 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"); -} diff --git a/src/cmd/mkdir.c b/src/cmd/mkdir.c index 3ed535e..28c2467 100644 --- a/src/cmd/mkdir.c +++ b/src/cmd/mkdir.c @@ -30,25 +30,24 @@ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. */ -#include -#include #include #include #include #include +#include +#include +#include -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); diff --git a/src/cmd/mv.c b/src/cmd/mv.c index 2ec6473..3ec5341 100644 --- a/src/cmd/mv.c +++ b/src/cmd/mv.c @@ -7,37 +7,42 @@ /* * mv file1 file2 */ +#include +#include +#include +#include +#include +#include +#include +#include +#include #include #include #include -#include -#include -#include -#include -#include -#include -#include - -#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]; diff --git a/src/cmd/nice.c b/src/cmd/nice.c index ad84277..2df0cee 100644 --- a/src/cmd/nice.c +++ b/src/cmd/nice.c @@ -5,13 +5,11 @@ */ #include #include - +#include #include #include -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); } diff --git a/src/cmd/od.c b/src/cmd/od.c index 1556b1c..ac5409f 100644 --- a/src/cmd/od.c +++ b/src/cmd/od.c @@ -4,26 +4,28 @@ * usage: od [-abBcdDefFhHiIlLopPs[n]vw[n]xX] [file] [[+]offset[.][b] [label]] * * where the option flags have the following meaning: - * character object radix signed? - * a byte (10) (n.a.) ASCII named byte stream - * b byte 8 no byte octal - * c byte (8) (no) character with octal non-graphic bytes - * d short 10 no - * D long 10 no - * e,F double (10) double precision floating pt. - * f float (10) single precision floating pt. - * h,x short 16 no - * H,X long 16 no - * i short 10 yes - * I,l,L long 10 yes - * o,B short 8 no (default conversion) - * O long 8 no - * s[n] string (8) ASCII graphic strings * - * p indicate EVEN parity on 'a' conversion - * P indicate ODD parity on 'a' conversion - * v show all data - don't skip like lines. - * w[n] bytes per display line + * character object radix signed? + * ----------------------------------------------------------- + * a byte (10) (n.a.) ASCII named byte stream + * b byte 8 no byte octal + * c byte (8) (no) character with octal non-graphic bytes + * d short 10 no + * D long 10 no + * e,F double (10) double precision floating pt. + * f float (10) single precision floating pt. + * h,x short 16 no + * H,X long 16 no + * i short 10 yes + * I,l,L long 10 yes + * o,B short 8 no (default conversion) + * O long 8 no + * s[n] string (8) ASCII graphic strings + * + * p indicate EVEN parity on 'a' conversion + * P indicate ODD parity on 'a' conversion + * v show all data - don't skip like lines. + * w[n] bytes per display line * * More than one format character may be given. * If {file} is not specified, standard input is read. @@ -34,127 +36,134 @@ #include #include #include +#include +#include -#define DBUF_SIZE BUFSIZ -#define BIG_DBUF 32 -#define NO 0 -#define YES 1 -#define EVEN -1 -#define ODD 1 -#define UNSIGNED 0 -#define SIGNED 1 -#define PADDR 1 -#define MIN_SLEN 3 - -int a_put(); -int b_put(); -int c_put(); -int s_put(); -int us_put(); -int l_put(); -int f_put(); -int d_put(); -int st_put(); +#define DBUF_SIZE BUFSIZ +#define BIG_DBUF 32 +#define NO 0 +#define YES 1 +#define EVEN -1 +#define ODD 1 +#define UNSIGNED 0 +#define SIGNED 1 +#define PADDR 1 +#define MIN_SLEN 3 struct dfmt { - int df_field; /* external field required for object */ - int df_size; /* size (bytes) of object */ - int df_radix; /* conversion radix */ - int df_signed; /* signed? flag */ - int df_paddr; /* "put address on each line?" flag */ - int (*df_put)(); /* function to output object */ - char *df_fmt; /* output string format */ -} *conv_vec[32]; /* vector of conversions to be done */ + int df_field; /* external field required for object */ + int df_size; /* size (bytes) of object */ + int df_radix; /* conversion radix */ + int df_signed; /* signed? flag */ + int df_paddr; /* "put address on each line?" flag */ + int (*df_put)(); /* function to output object */ + char *df_fmt; /* output string format */ +} *conv_vec[32]; /* vector of conversions to be done */ -struct dfmt ascii = { 3, sizeof (char), 10, 0, PADDR, a_put, 0}; -struct dfmt byte = { 3, sizeof (char), 8, UNSIGNED, PADDR, b_put, 0}; -struct dfmt cchar = { 3, sizeof (char), 8, UNSIGNED, PADDR, c_put, 0}; -struct dfmt u_s_oct = { 6, sizeof (short), 8, UNSIGNED, PADDR, us_put, 0}; -struct dfmt u_s_dec = { 5, sizeof (short), 10, UNSIGNED, PADDR, us_put, 0}; -struct dfmt u_s_hex = { 4, sizeof (short), 16, UNSIGNED, PADDR, us_put, 0}; -struct dfmt u_l_oct = {11, sizeof (long), 8, UNSIGNED, PADDR, l_put, 0}; -struct dfmt u_l_dec = {10, sizeof (long), 10, UNSIGNED, PADDR, l_put, 0}; -struct dfmt u_l_hex = { 8, sizeof (long), 16, UNSIGNED, PADDR, l_put, 0}; -struct dfmt s_s_dec = { 6, sizeof (short), 10, SIGNED, PADDR, s_put, 0}; -struct dfmt s_l_dec = {11, sizeof (long), 10, SIGNED, PADDR, l_put, 0}; -struct dfmt flt = {14, sizeof (float), 10, SIGNED, PADDR, f_put, 0}; -struct dfmt dble = {21, sizeof (double), 10, SIGNED, PADDR, d_put, 0}; -struct dfmt string = { 0, 0, 8, 0, NO, st_put, 0}; +static int a_put(char *cc, struct dfmt *d); +static int b_put(char *bb, struct dfmt *d); +static int c_put(char *cc, struct dfmt *d); +static int us_put(unsigned short *n, struct dfmt *d); +static int l_put(long *n, struct dfmt *d); +static int s_put(short *n, struct dfmt *d); +static int f_put(float *f, struct dfmt *d); +static int d_put(double *f, struct dfmt *d); +static int st_put(char *cc, struct dfmt *d); +struct dfmt ascii = { 3, sizeof(char), 10, 0, PADDR, a_put, 0 }; +struct dfmt byte = { 3, sizeof(char), 8, UNSIGNED, PADDR, b_put, 0 }; +struct dfmt cchar = { 3, sizeof(char), 8, UNSIGNED, PADDR, c_put, 0 }; +struct dfmt u_s_oct = { 6, sizeof(short), 8, UNSIGNED, PADDR, us_put, 0 }; +struct dfmt u_s_dec = { 5, sizeof(short), 10, UNSIGNED, PADDR, us_put, 0 }; +struct dfmt u_s_hex = { 4, sizeof(short), 16, UNSIGNED, PADDR, us_put, 0 }; +struct dfmt u_l_oct = { 11, sizeof(long), 8, UNSIGNED, PADDR, l_put, 0 }; +struct dfmt u_l_dec = { 10, sizeof(long), 10, UNSIGNED, PADDR, l_put, 0 }; +struct dfmt u_l_hex = { 8, sizeof(long), 16, UNSIGNED, PADDR, l_put, 0 }; +struct dfmt s_s_dec = { 6, sizeof(short), 10, SIGNED, PADDR, s_put, 0 }; +struct dfmt s_l_dec = { 11, sizeof(long), 10, SIGNED, PADDR, l_put, 0 }; +struct dfmt flt = { 14, sizeof(float), 10, SIGNED, PADDR, f_put, 0 }; +struct dfmt dble = { 21, sizeof(double), 10, SIGNED, PADDR, d_put, 0 }; +struct dfmt string = { 0, 0, 8, 0, NO, st_put, 0 }; -char usage[] ="usage: od [-abcdfhilopswvx] [file] [[+]offset[.][b] [label]]"; -char dbuf[DBUF_SIZE]; -char lastdbuf[DBUF_SIZE]; -int addr_base = 8; /* default address base is OCTAL */ -long addr = 0L; /* current file offset */ -long label = -1L; /* current label; -1 is "off" */ -int dbuf_size = 16; /* file bytes / display line */ -int _parity = NO; /* show parity on ascii bytes */ -char fmt[] = " %s"; /* 12 blanks */ -char *icvt(); -char *scvt(); -char *underline(); -long get_addr(); - +char usage[] = "usage: od [-abcdfhilopswvx] [file] [[+]offset[.][b] [label]]"; +char dbuf[DBUF_SIZE]; +char lastdbuf[DBUF_SIZE]; +int addr_base = 8; /* default address base is OCTAL */ +long addr = 0L; /* current file offset */ +long label = -1L; /* current label; -1 is "off" */ +int dbuf_size = 16; /* file bytes / display line */ +int _parity = NO; /* show parity on ascii bytes */ +char fmt[] = " %s"; /* 12 blanks */ /* * special form of _ctype */ +#define A 01 +#define G 02 +#define D 04 +#define P 010 +#define X 020 +#define isdigit(c) (_ctype[c] & D) +#define isascii(c) (_ctype[c] & A) +#define isgraphic(c) (_ctype[c] & G) +#define isprint(c) (_ctype[c] & P) +#define ishex(c) (_ctype[c] & (X | D)) -#define A 01 -#define G 02 -#define D 04 -#define P 010 -#define X 020 -#define isdigit(c) (_ctype[c] & D) -#define isascii(c) (_ctype[c] & A) -#define isgraphic(c) (_ctype[c] & G) -#define isprint(c) (_ctype[c] & P) -#define ishex(c) (_ctype[c] & (X|D)) - -char _ctype[256] = { -/* 000 */ 0, 0, 0, 0, 0, 0, 0, 0, -/* 010 */ A, A, A, 0, A, A, 0, 0, -/* 020 */ 0, 0, 0, 0, 0, 0, 0, 0, -/* 030 */ 0, 0, 0, 0, 0, 0, 0, 0, -/* 040 */ P|A, P|G|A, P|G|A, P|G|A, P|G|A, P|G|A, P|G|A, P|G|A, -/* 050 */ P|G|A, P|G|A, P|G|A, P|G|A, P|G|A, P|G|A, P|G|A, P|G|A, -/* 060 */ P|G|D|A,P|G|D|A,P|G|D|A,P|G|D|A,P|G|D|A,P|G|D|A,P|G|D|A,P|G|D|A, -/* 070 */ P|G|D|A,P|G|D|A, P|G|A, P|G|A, P|G|A, P|G|A, P|G|A, P|G|A, -/* 100 */ P|G|A, P|G|A, P|G|A, P|G|A, P|G|A, P|G|A, P|G|A, P|G|A, -/* 110 */ P|G|A, P|G|A, P|G|A, P|G|A, P|G|A, P|G|A, P|G|A, P|G|A, -/* 120 */ P|G|A, P|G|A, P|G|A, P|G|A, P|G|A, P|G|A, P|G|A, P|G|A, -/* 130 */ P|G|A, P|G|A, P|G|A, P|G|A, P|G|A, P|G|A, P|G|A, P|G|A, -/* 140 */ P|G|A,X|P|G|A,X|P|G|A,X|P|G|A,X|P|G|A,X|P|G|A,X|P|G|A, P|G|A, -/* 150 */ P|G|A, P|G|A, P|G|A, P|G|A, P|G|A, P|G|A, P|G|A, P|G|A, -/* 160 */ P|G|A, P|G|A, P|G|A, P|G|A, P|G|A, P|G|A, P|G|A, P|G|A, -/* 170 */ P|G|A, P|G|A, P|G|A, P|G|A, P|G|A, P|G|A, P|G|A, 0, -/* 200 */ 0, 0, 0, 0, 0, 0, 0, 0, -/* 210 */ 0, 0, 0, 0, 0, 0, 0, 0, -/* 220 */ 0, 0, 0, 0, 0, 0, 0, 0, -/* 230 */ 0, 0, 0, 0, 0, 0, 0, 0, -/* 240 */ 0, 0, 0, 0, 0, 0, 0, 0, -/* 250 */ 0, 0, 0, 0, 0, 0, 0, 0, -/* 260 */ 0, 0, 0, 0, 0, 0, 0, 0, -/* 270 */ 0, 0, 0, 0, 0, 0, 0, 0, -/* 300 */ 0, 0, 0, 0, 0, 0, 0, 0, -/* 310 */ 0, 0, 0, 0, 0, 0, 0, 0, -/* 320 */ 0, 0, 0, 0, 0, 0, 0, 0, -/* 330 */ 0, 0, 0, 0, 0, 0, 0, 0, -/* 340 */ 0, 0, 0, 0, 0, 0, 0, 0, -/* 350 */ 0, 0, 0, 0, 0, 0, 0, 0, -/* 360 */ 0, 0, 0, 0, 0, 0, 0, 0, -/* 370 */ 0, 0, 0, 0, 0, 0, 0, 0, +char _ctype[256] = { + // clang-format off + /* 000 */ 0, 0, 0, 0, 0, 0, 0, 0, + /* 010 */ A, A, A, 0, A, A, 0, 0, + /* 020 */ 0, 0, 0, 0, 0, 0, 0, 0, + /* 030 */ 0, 0, 0, 0, 0, 0, 0, 0, + /* 040 */ P|A, P|G|A, P|G|A, P|G|A, P|G|A, P|G|A, P|G|A, P|G|A, + /* 050 */ P|G|A, P|G|A, P|G|A, P|G|A, P|G|A, P|G|A, P|G|A, P|G|A, + /* 060 */ P|G|D|A,P|G|D|A,P|G|D|A,P|G|D|A,P|G|D|A,P|G|D|A,P|G|D|A,P|G|D|A, + /* 070 */ P|G|D|A,P|G|D|A, P|G|A, P|G|A, P|G|A, P|G|A, P|G|A, P|G|A, + /* 100 */ P|G|A, P|G|A, P|G|A, P|G|A, P|G|A, P|G|A, P|G|A, P|G|A, + /* 110 */ P|G|A, P|G|A, P|G|A, P|G|A, P|G|A, P|G|A, P|G|A, P|G|A, + /* 120 */ P|G|A, P|G|A, P|G|A, P|G|A, P|G|A, P|G|A, P|G|A, P|G|A, + /* 130 */ P|G|A, P|G|A, P|G|A, P|G|A, P|G|A, P|G|A, P|G|A, P|G|A, + /* 140 */ P|G|A,X|P|G|A,X|P|G|A,X|P|G|A,X|P|G|A,X|P|G|A,X|P|G|A, P|G|A, + /* 150 */ P|G|A, P|G|A, P|G|A, P|G|A, P|G|A, P|G|A, P|G|A, P|G|A, + /* 160 */ P|G|A, P|G|A, P|G|A, P|G|A, P|G|A, P|G|A, P|G|A, P|G|A, + /* 170 */ P|G|A, P|G|A, P|G|A, P|G|A, P|G|A, P|G|A, P|G|A, 0, + /* 200 */ 0, 0, 0, 0, 0, 0, 0, 0, + /* 210 */ 0, 0, 0, 0, 0, 0, 0, 0, + /* 220 */ 0, 0, 0, 0, 0, 0, 0, 0, + /* 230 */ 0, 0, 0, 0, 0, 0, 0, 0, + /* 240 */ 0, 0, 0, 0, 0, 0, 0, 0, + /* 250 */ 0, 0, 0, 0, 0, 0, 0, 0, + /* 260 */ 0, 0, 0, 0, 0, 0, 0, 0, + /* 270 */ 0, 0, 0, 0, 0, 0, 0, 0, + /* 300 */ 0, 0, 0, 0, 0, 0, 0, 0, + /* 310 */ 0, 0, 0, 0, 0, 0, 0, 0, + /* 320 */ 0, 0, 0, 0, 0, 0, 0, 0, + /* 330 */ 0, 0, 0, 0, 0, 0, 0, 0, + /* 340 */ 0, 0, 0, 0, 0, 0, 0, 0, + /* 350 */ 0, 0, 0, 0, 0, 0, 0, 0, + /* 360 */ 0, 0, 0, 0, 0, 0, 0, 0, + /* 370 */ 0, 0, 0, 0, 0, 0, 0, 0, + // clang-format on }; +static long get_addr(char *s); +static void offset(long a); +static void line(int n); +static void put_addr(long a, long l, char c); +static char *icvt(long value, int radix, int signd, int ndigits); +static int parity(int word); +static char *underline(char *s); +static char *scvt(int c, struct dfmt *d); +static void pr_sbuf(struct dfmt *d, int end); +static void put_sbuf(int c, struct dfmt *d); +static int canseek(FILE *f); +static void dumbseek(FILE *s, long offset); -main(argc, argv) -int argc; -char **argv; +int main(int argc, char **argv) { - register char *p; - register char *l; - register n, same; + char *p; + char *l; + int n, same; struct dfmt *d; struct dfmt **cv = conv_vec; int showall = NO; @@ -164,15 +173,11 @@ char **argv; argv++; argc--; - if(argc > 0) - { + if (argc > 0) { p = *argv; - if(*p == '-') - { - while(*++p != '\0') - { - switch(*p) - { + if (*p == '-') { + while (*++p != '\0') { + switch (*p) { case 'a': d = &ascii; break; @@ -258,7 +263,7 @@ char **argv; /* * if nothing spec'd, setup default conversion. */ - if(cv == conv_vec) + if (cv == conv_vec) *(cv++) = &u_s_oct; *cv = (struct dfmt *)0; @@ -266,8 +271,7 @@ char **argv; /* * calculate display parameters */ - for (cv = conv_vec; d = *cv; cv++) - { + for (cv = conv_vec; (d = *cv); cv++) { nelm = (dbuf_size + d->df_size - 1) / d->df_size; llen = nelm * (d->df_field + 1); if (llen > max_llen) @@ -277,9 +281,8 @@ char **argv; /* * setup df_fmt to point to uniform output fields. */ - for (cv = conv_vec; d = *cv; cv++) - { - if (d->df_field) /* only if external field is known */ + for (cv = conv_vec; (d = *cv); cv++) { + if (d->df_field) /* only if external field is known */ { nelm = (dbuf_size + d->df_size - 1) / d->df_size; field = max_llen / nelm; @@ -290,10 +293,8 @@ char **argv; /* * input file specified ? */ - if(argc > 0 && **argv != '+') - { - if (freopen(*argv, "r", stdin) == NULL) - { + if (argc > 0 && **argv != '+') { + if (freopen(*argv, "r", stdin) == NULL) { perror(*argv); exit(1); } @@ -304,8 +305,7 @@ char **argv; /* * check for possible offset [label] */ - if (argc > 0) - { + if (argc > 0) { addr = get_addr(*argv); offset(addr); argv++; @@ -319,24 +319,18 @@ char **argv; * main dump loop */ same = -1; - while ((n = fread(dbuf, 1, dbuf_size, stdin)) > 0) - { - if (same>=0 && bcmp(dbuf, lastdbuf, dbuf_size) == 0 && !showall) - { - if (same==0) - { + while ((n = fread(dbuf, 1, dbuf_size, stdin)) > 0) { + if (same >= 0 && bcmp(dbuf, lastdbuf, dbuf_size) == 0 && !showall) { + if (same == 0) { printf("*\n"); same = 1; } - } - else - { + } else { line(n); same = 0; p = dbuf; l = lastdbuf; - for (nelm = 0; nelm < dbuf_size; nelm++) - { + for (nelm = 0; nelm < dbuf_size; nelm++) { *l++ = *p; *p++ = '\0'; } @@ -350,22 +344,16 @@ char **argv; * Some conversions require "flushing". */ n = 0; - for (cv = conv_vec; *cv; cv++) - { - if ((*cv)->df_paddr) - { + for (cv = conv_vec; *cv; cv++) { + if ((*cv)->df_paddr) { if (n++ == 0) put_addr(addr, label, '\n'); - } - else + } else (*((*cv)->df_put))(0, *cv); } } -put_addr(a, l, c) -long a; -long l; -char c; +void put_addr(long a, long l, char c) { fputs(icvt(a, addr_base, UNSIGNED, 7), stdout); if (l >= 0) @@ -373,25 +361,19 @@ char c; putchar(c); } -line(n) -int n; +void line(int n) { - register i, first; - register struct dfmt *c; - register struct dfmt **cv = conv_vec; + int i, first; + struct dfmt *c; + struct dfmt **cv = conv_vec; first = YES; - while (c = *cv++) - { - if (c->df_paddr) - { - if (first) - { + while ((c = *cv++)) { + if (c->df_paddr) { + if (first) { put_addr(addr, label, ' '); first = NO; - } - else - { + } else { putchar('\t'); if (label >= 0) fputs("\t ", stdout); @@ -399,99 +381,73 @@ int n; } i = 0; while (i < n) - i += (*(c->df_put))(dbuf+i, c); + i += (*(c->df_put))(dbuf + i, c); if (c->df_paddr) putchar('\n'); } } -s_put(n, d) -short *n; -struct dfmt *d; +int s_put(short *n, struct dfmt *d) { printf(d->df_fmt, icvt((long)*n, d->df_radix, d->df_signed, d->df_field)); - return(d->df_size); + return (d->df_size); } -us_put(n, d) -unsigned short *n; -struct dfmt *d; +int us_put(unsigned short *n, struct dfmt *d) { printf(d->df_fmt, icvt((long)*n, d->df_radix, d->df_signed, d->df_field)); - return(d->df_size); + return (d->df_size); } -l_put(n, d) -long *n; -struct dfmt *d; +int l_put(long *n, struct dfmt *d) { printf(d->df_fmt, icvt(*n, d->df_radix, d->df_signed, d->df_field)); - return(d->df_size); + return (d->df_size); } -d_put(f, d) -double *f; -struct dfmt *d; +int d_put(double *f, struct dfmt *d) { char fbuf[24]; - struct l { long n[2]; }; + struct l { + long n[2]; + }; -#if vax - if ((((struct l *)f)->n[0] & 0xff00) == 0x8000) /* Vax illegal f.p. */ - sprintf(fbuf, " %08x %08x", - ((struct l *)f)->n[0], ((struct l *)f)->n[1]); - else -#endif - - sprintf(fbuf, "%21.14e", *f); + sprintf(fbuf, "%21.14e", *f); printf(d->df_fmt, fbuf); - return(d->df_size); + return (d->df_size); } -f_put(f, d) -float *f; -struct dfmt *d; +int f_put(float *f, struct dfmt *d) { char fbuf[16]; -#if vax - if ((*(long *)f & 0xff00) == 0x8000) /* Vax illegal f.p. form */ - sprintf(fbuf, " %08x", *(long *)f); - else -#endif - sprintf(fbuf, "%14.7e", *f); + sprintf(fbuf, "%14.7e", *f); printf(d->df_fmt, fbuf); - return(d->df_size); + return (d->df_size); } - -char asc_name[34][4] = { -/* 000 */ "nul", "soh", "stx", "etx", "eot", "enq", "ack", "bel", -/* 010 */ " bs", " ht", " nl", " vt", " ff", " cr", " so", " si", -/* 020 */ "dle", "dc1", "dc2", "dc3", "dc4", "nak", "syn", "etb", -/* 030 */ "can", " em", "sub", "esc", " fs", " gs", " rs", " us", -/* 040 */ " sp", "del" +char asc_name[34][4] = { + /* 000 */ "nul", "soh", "stx", "etx", "eot", "enq", "ack", "bel", + /* 010 */ " bs", " ht", " nl", " vt", " ff", " cr", " so", " si", + /* 020 */ "dle", "dc1", "dc2", "dc3", "dc4", "nak", "syn", "etb", + /* 030 */ "can", " em", "sub", "esc", " fs", " gs", " rs", " us", + /* 040 */ " sp", "del" }; -a_put(cc, d) -char *cc; -struct dfmt *d; +int a_put(char *cc, struct dfmt *d) { int c = *cc; - register char *s = " "; - register pbit = parity((int)c & 0377); + char *s = " "; + int pbit = parity((int)c & 0377); c &= 0177; - if (isgraphic(c)) - { + if (isgraphic(c)) { s[2] = c; if (pbit == _parity) printf(d->df_fmt, underline(s)); else printf(d->df_fmt, s); - } - else - { + } else { if (c == 0177) c = ' ' + 1; if (pbit == _parity) @@ -499,99 +455,85 @@ struct dfmt *d; else printf(d->df_fmt, asc_name[c]); } - return(1); + return (1); } -parity(word) -int word; +int parity(int word) { - register int p = 0; - register int w = word; + int p = 0; + int w = word; if (w) - do - { + do { p ^= 1; - } while(w &= (~(-w))); - return (p? ODD:EVEN); + } while (w &= (~(-w))); + return (p ? ODD : EVEN); } -char * -underline(s) -char *s; +char *underline(char *s) { static char ulbuf[16]; - register char *u = ulbuf; + char *u = ulbuf; - while (*s) - { - if (*s != ' ') - { + while (*s) { + if (*s != ' ') { *u++ = '_'; *u++ = '\b'; } *u++ = *s++; } *u = '\0'; - return(ulbuf); + return (ulbuf); } -b_put(b, d) -char *b; -struct dfmt *d; +int b_put(char *b, struct dfmt *d) { printf(d->df_fmt, icvt((long)*b & 0377, d->df_radix, d->df_signed, d->df_field)); - return(1); + return (1); } -c_put(cc, d) -char *cc; -struct dfmt *d; +int c_put(char *cc, struct dfmt *d) { - register char *s; - register int n; - register int c = *cc & 0377; + char *s; + int n; + int c = *cc & 0377; s = scvt(c, d); for (n = d->df_field - strlen(s); n > 0; n--) putchar(' '); printf(d->df_fmt, s); - return(1); + return (1); } -char *scvt(c, d) -int c; -struct dfmt *d; +char *scvt(int c, struct dfmt *d) { static char s[2]; - switch(c) - { - case '\0': - return("\\0"); + switch (c) { + case '\0': + return ("\\0"); - case '\b': - return("\\b"); + case '\b': + return ("\\b"); - case '\f': - return("\\f"); + case '\f': + return ("\\f"); - case '\n': - return("\\n"); + case '\n': + return ("\\n"); - case '\r': - return("\\r"); + case '\r': + return ("\\r"); - case '\t': - return("\\t"); + case '\t': + return ("\\t"); - default: - if (isprint(c)) - { - s[0] = c; - return(s); - } - return(icvt((long)c, d->df_radix, d->df_signed, d->df_field)); + default: + if (isprint(c)) { + s[0] = c; + return (s); + } + return (icvt((long)c, d->df_radix, d->df_signed, d->df_field)); } } @@ -600,79 +542,65 @@ struct dfmt *d; * A string contains bytes > 037 && < 177, and ends with a null. * The minimum length is given in the dfmt structure. */ - -#define CNULL '\0' +#define CNULL '\0' #define S_EMPTY 0 -#define S_FILL 1 -#define S_CONT 2 -#define SBUFSIZE 1024 +#define S_FILL 1 +#define S_CONT 2 +#define SBUFSIZE 1024 static char str_buf[SBUFSIZE]; -static int str_mode = S_EMPTY; +static int str_mode = S_EMPTY; static char *str_ptr; static long str_addr; static long str_label; -st_put(cc, d) -char *cc; -struct dfmt *d; +int st_put(char *cc, struct dfmt *d) { - register int c; + int c; - if (cc == 0) - { + if (cc == 0) { pr_sbuf(d, YES); - return(1); + return (1); } c = (*cc & 0377); - if (str_mode & S_FILL) - { + if (str_mode & S_FILL) { if (isascii(c)) put_sbuf(c, d); - else - { + else { *str_ptr = CNULL; if (c == NULL) pr_sbuf(d, YES); str_mode = S_EMPTY; } - } - else if (isascii(c)) - { + } else if (isascii(c)) { str_mode = S_FILL; - str_addr = addr + (cc - dbuf); /* ugly */ + str_addr = addr + (cc - dbuf); /* ugly */ if ((str_label = label) >= 0) str_label += (cc - dbuf); /* '' */ str_ptr = str_buf; put_sbuf(c, d); } - return(1); + return (1); } -put_sbuf(c, d) -int c; -struct dfmt *d; +void put_sbuf(int c, struct dfmt *d) { *str_ptr++ = c; - if (str_ptr >= (str_buf + SBUFSIZE)) - { + if (str_ptr >= (str_buf + SBUFSIZE)) { pr_sbuf(d, NO); str_ptr = str_buf; str_mode |= S_CONT; } } -pr_sbuf(d, end) -struct dfmt *d; -int end; +void pr_sbuf(struct dfmt *d, int end) { - register char *p = str_buf; + char *p = str_buf; - if (str_mode == S_EMPTY - || (!(str_mode & S_CONT) && (str_ptr - str_buf) < d->df_size)) + if (str_mode == S_EMPTY || (!(str_mode & S_CONT) && (str_ptr - str_buf) < d->df_size)) return; if (!(str_mode & S_CONT)) @@ -691,36 +619,30 @@ int end; * This code has been rearranged to produce optimized runtime code. */ -#define MAXINTLENGTH 32 +#define MAXINTLENGTH 32 static char _digit[] = "0123456789abcdef"; -static char _icv_buf[MAXINTLENGTH+1]; +static char _icv_buf[MAXINTLENGTH + 1]; static long _mask = 0x7fffffff; -char * -icvt (value, radix, signd, ndigits) -long value; -int radix; -int signd; -int ndigits; +char *icvt(long value, int radix, int signd, int ndigits) { - register long val = value; - register long rad = radix; - register char *b = &_icv_buf[MAXINTLENGTH]; - register char *d = _digit; - register long tmp1; - register long tmp2; - long rem; - long kludge; + long val = value; + long rad = radix; + char *b = &_icv_buf[MAXINTLENGTH]; + char *d = _digit; + long tmp1; + long tmp2; + long rem; + long kludge; int sign; - if (val == 0) - { + if (val == 0) { *--b = '0'; sign = 0; goto done; /*return(b);*/ } - if (signd && (sign = (val < 0))) /* signed conversion */ + if (signd && (sign = (val < 0))) /* signed conversion */ { /* * It is necessary to do the first divide @@ -735,12 +657,10 @@ int ndigits; tmp1 = val / rad; *--b = d[(tmp1 * rad) - val]; val = -tmp1; - } - else /* unsigned conversion */ + } else /* unsigned conversion */ { sign = 0; - if (val < 0) - { /* ALL THIS IS TO SIMULATE UNSIGNED LONG MOD & DIV */ + if (val < 0) { /* ALL THIS IS TO SIMULATE UNSIGNED LONG MOD & DIV */ kludge = _mask - (rad - 1); val &= _mask; /* @@ -759,8 +679,7 @@ int ndigits; } } - while (val) - { + while (val) { /* * This is really what's being done ... * *--b = d[val % rad]; @@ -777,68 +696,58 @@ done: tmp1 = ndigits - (&_icv_buf[MAXINTLENGTH] - b); tmp2 = signd ? ' ' : '0'; - while (tmp1 > 0) - { + while (tmp1 > 0) { *--b = tmp2; tmp1--; } - return(b); + return (b); } -long get_addr(s) -register char *s; +long get_addr(char *s) { - register char *p; - register long a; - register int d; + char *p; + long a; + int d; - if (*s=='+') + if (*s == '+') s++; - if (*s=='x') - { + if (*s == 'x') { s++; addr_base = 16; - } - else if (*s=='0' && s[1]=='x') - { + } else if (*s == '0' && s[1] == 'x') { s += 2; addr_base = 16; - } - else if (*s == '0') + } else if (*s == '0') addr_base = 8; p = s; - while(*p) - { - if (*p++=='.') + while (*p) { + if (*p++ == '.') addr_base = 10; } - for (a=0; *s; s++) - { + for (a = 0; *s; s++) { d = *s; - if(isdigit(d)) - a = a*addr_base + d - '0'; - else if (ishex(d) && addr_base==16) - a = a*addr_base + d + 10 - 'a'; + if (isdigit(d)) + a = a * addr_base + d - '0'; + else if (ishex(d) && addr_base == 16) + a = a * addr_base + d + 10 - 'a'; else break; } if (*s == '.') s++; - if(*s=='b') + if (*s == 'b') a *= 512; - if(*s=='B') + if (*s == 'B') a *= 1024; - return(a); + return (a); } -offset(a) -long a; +void offset(long a) { - if (canseek(stdin)) - { + if (canseek(stdin)) { /* * in case we're accessing a raw disk, * we have to seek in multiples of a physical block. @@ -849,19 +758,15 @@ long a; dumbseek(stdin, a); } -dumbseek(s, offset) -FILE *s; -long offset; +void dumbseek(FILE *s, long offset) { - char buf[BUFSIZ]; + char buf[BUFSIZ]; int n; int nr; - while (offset > 0) - { + while (offset > 0) { nr = (offset > BUFSIZ) ? BUFSIZ : (int)offset; - if ((n = fread(buf, 1, nr, s)) != nr) - { + if ((n = fread(buf, 1, nr, s)) != nr) { fprintf(stderr, "EOF\n"); exit(1); } @@ -869,15 +774,13 @@ long offset; } } -#include #include +#include -canseek(f) -FILE *f; +int canseek(FILE *f) { struct stat statb; - return( (fstat(fileno(f),&statb)==0) && - (statb.st_nlink > 0) && /*!pipe*/ - (!isatty(fileno(f))) ); + return ((fstat(fileno(f), &statb) == 0) && (statb.st_nlink > 0) && /*!pipe*/ + (!isatty(fileno(f)))); } diff --git a/src/cmd/pagesize.c b/src/cmd/pagesize.c index 72f90b8..0dd4b23 100644 --- a/src/cmd/pagesize.c +++ b/src/cmd/pagesize.c @@ -4,6 +4,7 @@ * specifies the terms and conditions for redistribution. */ #include +#include int main() { diff --git a/src/cmd/pr.c b/src/cmd/pr.c index a9997ee..2a50a6e 100644 --- a/src/cmd/pr.c +++ b/src/cmd/pr.c @@ -2,110 +2,118 @@ * print file with headings * 2+head+2+page[56]+5 */ +#include #include #include -#include -#include -#include #include +#include +#include /* 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=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 (line512) + 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--; diff --git a/src/cmd/printenv.c b/src/cmd/printenv.c index d3481d1..67e36b6 100644 --- a/src/cmd/printenv.c +++ b/src/cmd/printenv.c @@ -12,8 +12,7 @@ #include #include -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) {