diff --git a/src/cmd/pwd.c b/src/cmd/pwd.c index 389f139..e161f81 100644 --- a/src/cmd/pwd.c +++ b/src/cmd/pwd.c @@ -10,10 +10,9 @@ #include #include #include +#include -char *getwd(); - -main() +int main() { char pathname[MAXPATHLEN + 1]; diff --git a/src/cmd/rev.c b/src/cmd/rev.c index 0d1fd26..803b186 100644 --- a/src/cmd/rev.c +++ b/src/cmd/rev.c @@ -8,23 +8,22 @@ char line[N]; FILE *input; -main(argc,argv) -char **argv; +int main(int argc, char **argv) { - register i,c; + int i, c; + input = stdin; do { - if(argc>1) { - if((input=fopen(argv[1],"r"))==NULL) { - fprintf(stderr,"rev: cannot open %s\n", - argv[1]); + if (argc > 1) { + if ((input = fopen(argv[1], "r")) == NULL) { + fprintf(stderr, "rev: cannot open %s\n", argv[1]); exit(1); } } - for(;;){ - for(i=0;i=0) - putc(line[i],stdout); - putc('\n',stdout); + while (--i >= 0) + putc(line[i], stdout); + putc('\n', stdout); } eof: fclose(input); argc--; argv++; - } while(argc>1); + } while (argc > 1); } diff --git a/src/cmd/rm.c b/src/cmd/rm.c index c6d4b60..c3d7327 100644 --- a/src/cmd/rm.c +++ b/src/cmd/rm.c @@ -4,21 +4,25 @@ #include #include #include +#include +#include #include #include -#include -#include -int fflg; /* -f force - supress error messages */ -int iflg; /* -i interrogate user on each file */ -int rflg; /* -r recurse */ +int fflg; /* -f force - supress error messages */ +int iflg; /* -i interrogate user on each file */ +int rflg; /* -r recurse */ -int errcode; /* true if errors occured */ +int errcode; /* true if errors occured */ -main(argc, argv) - char *argv[]; +static int rm(char arg[], int level); +static int dotname(char *s); +static int yes(void); +static void append(char *name); + +int main(int argc, char *argv[]) { - register char *arg; + char *arg; fflg = !isatty(0); iflg = 0; @@ -34,7 +38,7 @@ main(argc, argv) break; while (*++arg != '\0') - switch(*arg) { + switch (*arg) { case 'f': fflg++; break; @@ -60,26 +64,25 @@ main(argc, argv) } while (--argc > 0) - (void) rm(*++argv, 0); + (void)rm(*++argv, 0); exit(errcode != 0); } -char *path; /* pointer to malloc'ed buffer for path */ -char *pathp; /* current pointer to end of path */ -int pathsz; /* size of path */ +char *path; /* pointer to malloc'ed buffer for path */ +char *pathp; /* current pointer to end of path */ +int pathsz; /* size of path */ /* * Return TRUE if sucessful. Recursive with -r (rflg) */ -rm(arg, level) - char arg[]; +int rm(char arg[], int level) { - int ok; /* true if recursive rm succeeded */ - struct stat buf; /* for finding out what a file is */ - struct direct *dp; /* for reading a directory */ - DIR *dirp; /* for reading a directory */ - char prevname[MAXNAMLEN + 1]; /* previous name for -r */ + int ok; /* true if recursive rm succeeded */ + struct stat buf; /* for finding out what a file is */ + struct direct *dp; /* for reading a directory */ + DIR *dirp; /* for reading a directory */ + char prevname[MAXNAMLEN + 1]; /* previous name for -r */ char *cp; if (dotname(arg)) { @@ -91,9 +94,9 @@ rm(arg, level) fprintf(stderr, "rm: %s nonexistent\n", arg); errcode++; } - return (0); /* error */ + return (0); /* error */ } - if ((buf.st_mode&S_IFMT) == S_IFDIR) { + if ((buf.st_mode & S_IFMT) == S_IFDIR) { if (!rflg) { if (!fflg) { fprintf(stderr, "rm: %s directory\n", arg); @@ -106,14 +109,14 @@ rm(arg, level) if (!yes()) return (0); /* didn't remove everything */ } - if (access(arg, R_OK|W_OK|X_OK) != 0) { + if (access(arg, R_OK | W_OK | X_OK) != 0) { if (rmdir(arg) == 0) return (1); /* salvaged: removed empty dir */ if (!fflg) { fprintf(stderr, "rm: %s not changed\n", arg); errcode++; } - return (0); /* error */ + return (0); /* error */ } if ((dirp = opendir(arg)) == NULL) { if (!fflg) { @@ -133,7 +136,7 @@ rm(arg, level) append(dp->d_name); closedir(dirp); ok = rm(path, level + 1); - for (cp = pathp; *--cp != '/' && cp > path; ) + for (cp = pathp; *--cp != '/' && cp > path;) ; pathp = cp; *cp++ = '\0'; @@ -146,17 +149,15 @@ rm(arg, level) } /* pick up where we left off */ if (prevname[0] != '\0') { - while ((dp = readdir(dirp)) != NULL && - strcmp(prevname, dp->d_name) != 0) + while ((dp = readdir(dirp)) != NULL && strcmp(prevname, dp->d_name) != 0) ; } /* skip the one we just failed to delete */ if (!ok) { dp = readdir(dirp); if (dp != NULL && strcmp(cp, dp->d_name)) { - fprintf(stderr, - "rm: internal synchronization error: %s, %s, %s\n", - arg, cp, dp->d_name); + fprintf(stderr, "rm: internal synchronization error: %s, %s, %s\n", arg, cp, + dp->d_name); } strcpy(prevname, dp->d_name); } @@ -186,9 +187,8 @@ rm(arg, level) if (!yes()) return (0); } else if (!fflg) { - if ((buf.st_mode&S_IFMT) != S_IFLNK && access(arg, W_OK) < 0) { - printf("rm: override protection %o for %s? ", - buf.st_mode&0777, arg); + if ((buf.st_mode & S_IFMT) != S_IFLNK && access(arg, W_OK) < 0) { + printf("rm: override protection %o for %s? ", buf.st_mode & 0777, arg); if (!yes()) return (0); } @@ -206,24 +206,25 @@ rm(arg, level) /* * boolean: is it "." or ".." ? */ -dotname(s) - char *s; +int dotname(char *s) { - if (s[0] == '.') - if (s[1] == '.') + if (s[0] == '.') { + if (s[1] == '.') { if (s[2] == '\0') return (1); else return (0); - else if (s[1] == '\0') + } else if (s[1] == '\0') { return (1); + } + } return (0); } /* * Get a yes/no answer from the user. */ -yes() +int yes() { int i, b; @@ -236,10 +237,9 @@ yes() /* * Append 'name' to 'path'. */ -append(name) - char *name; +void append(char *name) { - register int n; + int n; n = strlen(name); if (path == NULL) { diff --git a/src/cmd/rmail.c b/src/cmd/rmail.c index c67214d..d3f8342 100644 --- a/src/cmd/rmail.c +++ b/src/cmd/rmail.c @@ -6,68 +6,61 @@ ** It calls sendmail giving it a -f option built from these ** lines. */ +#include #include #include #include #include #include -#include -typedef char bool; -#define TRUE 1 -#define FALSE 0 +typedef char bool; +#define TRUE 1 +#define FALSE 0 -bool Debug; +bool Debug; -main(argc, argv) - char **argv; +int main(int argc, char **argv) { - FILE *out; /* output to sendmail */ - char lbuf[1024]; /* one line of the message */ - char from[512]; /* accumulated path of sender */ - char ufrom[512]; /* user on remote system */ - char sys[512]; /* a system in path */ - char junk[1024]; /* scratchpad */ + FILE *out; /* output to sendmail */ + char lbuf[1024]; /* one line of the message */ + char from[512]; /* accumulated path of sender */ + char ufrom[512]; /* user on remote system */ + char sys[512]; /* a system in path */ + char junk[1024]; /* scratchpad */ char cmd[2000]; - register char *cp; - register char *uf = ufrom; /* ptr into ufrom */ + char *cp; + char *uf = ufrom; /* ptr into ufrom */ int i; #ifdef DEBUG - if (argc > 1 && strcmp(argv[1], "-T") == 0) - { + if (argc > 1 && strcmp(argv[1], "-T") == 0) { Debug = TRUE; argc--; argv++; } #endif - if (argc < 2) - { + if (argc < 2) { fprintf(stderr, "Usage: rmail user ...\n"); exit(EX_USAGE); } - (void) strcpy(from, ""); - (void) strcpy(ufrom, "/dev/null"); + (void)strcpy(from, ""); + (void)strcpy(ufrom, "/dev/null"); - for (;;) - { - (void) fgets(lbuf, sizeof lbuf, stdin); + for (;;) { + (void)fgets(lbuf, sizeof lbuf, stdin); if (strncmp(lbuf, "From ", 5) != 0 && strncmp(lbuf, ">From ", 6) != 0) break; - (void) sscanf(lbuf, "%s %s", junk, ufrom); + (void)sscanf(lbuf, "%s %s", junk, ufrom); cp = lbuf; - for (;;) - { - cp = index(cp+1, 'r'); - if (cp == NULL) - { - register char *p = rindex(uf, '!'); + for (;;) { + cp = index(cp + 1, 'r'); + if (cp == NULL) { + char *p = rindex(uf, '!'); - if (p != NULL) - { + if (p != NULL) { *p = '\0'; - (void) strcpy(sys, uf); + (void)strcpy(sys, uf); uf = p + 1; break; } @@ -77,29 +70,28 @@ main(argc, argv) if (Debug) printf("cp='%s'\n", cp); #endif - if (strncmp(cp, "remote from ", 12)==0) + if (strncmp(cp, "remote from ", 12) == 0) break; } if (cp != NULL) - (void) sscanf(cp, "remote from %s", sys); - (void) strcat(from, sys); - (void) strcat(from, "!"); + (void)sscanf(cp, "remote from %s", sys); + (void)strcat(from, sys); + (void)strcat(from, "!"); #ifdef DEBUG if (Debug) printf("ufrom='%s', sys='%s', from now '%s'\n", uf, sys, from); #endif } - (void) strcat(from, uf); + (void)strcat(from, uf); - (void) sprintf(cmd, "%s -ee -f%s -i", _PATH_SENDMAIL, from); - while (*++argv != NULL) - { - (void) strcat(cmd, " '"); + (void)sprintf(cmd, "%s -ee -f%s -i", _PATH_SENDMAIL, from); + while (*++argv != NULL) { + (void)strcat(cmd, " '"); if (**argv == '(') - (void) strncat(cmd, *argv + 1, strlen(*argv) - 2); + (void)strncat(cmd, *argv + 1, strlen(*argv) - 2); else - (void) strcat(cmd, *argv); - (void) strcat(cmd, "'"); + (void)strcat(cmd, *argv); + (void)strcat(cmd, "'"); } #ifdef DEBUG if (Debug) @@ -110,8 +102,7 @@ main(argc, argv) while (fgets(lbuf, sizeof lbuf, stdin)) fputs(lbuf, out); i = pclose(out); - if ((i & 0377) != 0) - { + if ((i & 0377) != 0) { fprintf(stderr, "pclose: status 0%o\n", i); exit(EX_OSERR); } diff --git a/src/cmd/rmdir.c b/src/cmd/rmdir.c index 96db2be..038c184 100644 --- a/src/cmd/rmdir.c +++ b/src/cmd/rmdir.c @@ -9,10 +9,9 @@ */ #include #include +#include -main(argc,argv) - int argc; - char **argv; +int main(int argc, char **argv) { int errors = 0; @@ -20,11 +19,12 @@ main(argc,argv) fprintf(stderr, "usage: %s directory ...\n", argv[0]); exit(1); } - while (--argc) + while (--argc) { if (rmdir(*++argv) < 0) { fprintf(stderr, "rmdir: "); - perror(*argv);; + perror(*argv); errors++; } + } exit(errors != 0); } diff --git a/src/cmd/size.c b/src/cmd/size.c index 6aee5d1..f208961 100644 --- a/src/cmd/size.c +++ b/src/cmd/size.c @@ -2,19 +2,17 @@ * size */ #ifdef CROSS -# include +#include #else -# include +#include #endif +#include #include #include -#include int header; -int -main(argc, argv) -char **argv; +int main(int argc, char **argv) { struct exec buf; long sum; @@ -22,12 +20,12 @@ char **argv; FILE *f; while ((ch = getopt(argc, argv, "h")) != EOF) { - switch(ch) { + switch (ch) { case 'h': default: fprintf(stderr, "Usage:\n"); fprintf(stderr, " size file...\n"); - return(1); + return (1); } } argc -= optind; @@ -38,14 +36,13 @@ char **argv; argc++; } - for (nfiles=argc; argc--; argv++) { - if ((f = fopen(*argv, "r"))==NULL) { + for (nfiles = argc; argc--; argv++) { + if ((f = fopen(*argv, "r")) == NULL) { printf("size: %s not found\n", *argv); err++; continue; } - if (fread((char *)&buf, sizeof(buf), 1, f) != 1 || - N_BADMAG(buf)) { + if (fread((char *)&buf, sizeof(buf), 1, f) != 1 || N_BADMAG(buf)) { printf("size: %s not an object file\n", *argv); fclose(f); err++; @@ -55,8 +52,8 @@ char **argv; printf("text\tdata\tbss\tdec\thex\n"); header = 1; } - printf("%u\t%u\t%u\t", buf.a_text,buf.a_data,buf.a_bss); - sum = (long) buf.a_text + (long) buf.a_data + (long) buf.a_bss; + printf("%u\t%u\t%u\t", buf.a_text, buf.a_data, buf.a_bss); + sum = (long)buf.a_text + (long)buf.a_data + (long)buf.a_bss; printf("%ld\t%lx", sum, sum); if (nfiles > 1) printf("\t%s", *argv); diff --git a/src/cmd/sleep.c b/src/cmd/sleep.c index d6d5497..9831e71 100644 --- a/src/cmd/sleep.c +++ b/src/cmd/sleep.c @@ -1,24 +1,24 @@ #include #include +#include -main(argc, argv) -char **argv; +int main(int argc, char **argv) { int c, n; char *s; n = 0; - if(argc < 2) { + if (argc < 2) { printf("arg count\n"); exit(0); } s = argv[1]; - while(c = *s++) { - if(c<'0' || c>'9') { + while ((c = *s++)) { + if (c < '0' || c > '9') { printf("bad character\n"); exit(0); } - n = n*10 + c - '0'; + n = n * 10 + c - '0'; } sleep(n); } diff --git a/src/cmd/sort.c b/src/cmd/sort.c index 9ec7b96..5646e7f 100644 --- a/src/cmd/sort.c +++ b/src/cmd/sort.c @@ -1,121 +1,88 @@ -#include -#include -#include #include -#include -#include #include -#include +#include +#include +#include +#include #include +#include +#include +#include -#define L 1024 -#define N 7 -#define C 20 +#define L 1024 +#define N 7 +#define C 20 #ifndef pdp11 -#define MEM (32*2048) +#define MEM (32 * 2048) #else -#define MEM (16*2048) +#define MEM (16 * 2048) #endif -#define NF 10 +#define NF 10 -#define rline(mp) (fgets((mp)->l, L, (mp)->b) == NULL) +#define rline(mp) (fgets((mp)->l, L, (mp)->b) == NULL) -FILE *is, *os; -char *dirtry[] = {_PATH_USRTMP, _PATH_TMP, NULL}; -char **dirs; -char file1[30]; -char *file = file1; -char *filep; +FILE *is, *os; +char *dirtry[] = { _PATH_USRTMP, _PATH_TMP, NULL }; +char **dirs; +char file1[30]; +char *file = file1; +char *filep; int nfiles; -unsigned nlines; -unsigned ntext; +unsigned nlines; +unsigned ntext; int *lspace; -char *tspace; -int cmp(), cmpa(); -int (*compare)() = cmpa; -char *eol(); -void term(int); -int mflg; +char *tspace; +void term(int); +int mflg; int cflg; int uflg; -char *outfil; -int unsafeout; /*kludge to assure -m -o works*/ -char tabchar; -int eargc; -char **eargv; +char *outfil; +int unsafeout; /*kludge to assure -m -o works*/ +char tabchar; +int eargc; +char **eargv; char zero[256]; -char fold[256] = { - 0200,0201,0202,0203,0204,0205,0206,0207, - 0210,0211,0212,0213,0214,0215,0216,0217, - 0220,0221,0222,0223,0224,0225,0226,0227, - 0230,0231,0232,0233,0234,0235,0236,0237, - 0240,0241,0242,0243,0244,0245,0246,0247, - 0250,0251,0252,0253,0254,0255,0256,0257, - 0260,0261,0262,0263,0264,0265,0266,0267, - 0270,0271,0272,0273,0274,0275,0276,0277, - 0300,0301,0302,0303,0304,0305,0306,0307, - 0310,0311,0312,0313,0314,0315,0316,0317, - 0320,0321,0322,0323,0324,0325,0326,0327, - 0330,0331,0332,0333,0334,0335,0336,0337, - 0340,0341,0342,0343,0344,0345,0346,0347, - 0350,0351,0352,0353,0354,0355,0356,0357, - 0360,0361,0362,0363,0364,0365,0366,0367, - 0370,0371,0372,0373,0374,0375,0376,0377, - 0000,0001,0002,0003,0004,0005,0006,0007, - 0010,0011,0012,0013,0014,0015,0016,0017, - 0020,0021,0022,0023,0024,0025,0026,0027, - 0030,0031,0032,0033,0034,0035,0036,0037, - 0040,0041,0042,0043,0044,0045,0046,0047, - 0050,0051,0052,0053,0054,0055,0056,0057, - 0060,0061,0062,0063,0064,0065,0066,0067, - 0070,0071,0072,0073,0074,0075,0076,0077, - 0100,0101,0102,0103,0104,0105,0106,0107, - 0110,0111,0112,0113,0114,0115,0116,0117, - 0120,0121,0122,0123,0124,0125,0126,0127, - 0130,0131,0132,0133,0134,0135,0136,0137, - 0140,0101,0102,0103,0104,0105,0106,0107, - 0110,0111,0112,0113,0114,0115,0116,0117, - 0120,0121,0122,0123,0124,0125,0126,0127, - 0130,0131,0132,0173,0174,0175,0176,0177 +char fold[256] = { + 0200, 0201, 0202, 0203, 0204, 0205, 0206, 0207, 0210, 0211, 0212, 0213, 0214, 0215, 0216, 0217, + 0220, 0221, 0222, 0223, 0224, 0225, 0226, 0227, 0230, 0231, 0232, 0233, 0234, 0235, 0236, 0237, + 0240, 0241, 0242, 0243, 0244, 0245, 0246, 0247, 0250, 0251, 0252, 0253, 0254, 0255, 0256, 0257, + 0260, 0261, 0262, 0263, 0264, 0265, 0266, 0267, 0270, 0271, 0272, 0273, 0274, 0275, 0276, 0277, + 0300, 0301, 0302, 0303, 0304, 0305, 0306, 0307, 0310, 0311, 0312, 0313, 0314, 0315, 0316, 0317, + 0320, 0321, 0322, 0323, 0324, 0325, 0326, 0327, 0330, 0331, 0332, 0333, 0334, 0335, 0336, 0337, + 0340, 0341, 0342, 0343, 0344, 0345, 0346, 0347, 0350, 0351, 0352, 0353, 0354, 0355, 0356, 0357, + 0360, 0361, 0362, 0363, 0364, 0365, 0366, 0367, 0370, 0371, 0372, 0373, 0374, 0375, 0376, 0377, + 0000, 0001, 0002, 0003, 0004, 0005, 0006, 0007, 0010, 0011, 0012, 0013, 0014, 0015, 0016, 0017, + 0020, 0021, 0022, 0023, 0024, 0025, 0026, 0027, 0030, 0031, 0032, 0033, 0034, 0035, 0036, 0037, + 0040, 0041, 0042, 0043, 0044, 0045, 0046, 0047, 0050, 0051, 0052, 0053, 0054, 0055, 0056, 0057, + 0060, 0061, 0062, 0063, 0064, 0065, 0066, 0067, 0070, 0071, 0072, 0073, 0074, 0075, 0076, 0077, + 0100, 0101, 0102, 0103, 0104, 0105, 0106, 0107, 0110, 0111, 0112, 0113, 0114, 0115, 0116, 0117, + 0120, 0121, 0122, 0123, 0124, 0125, 0126, 0127, 0130, 0131, 0132, 0133, 0134, 0135, 0136, 0137, + 0140, 0101, 0102, 0103, 0104, 0105, 0106, 0107, 0110, 0111, 0112, 0113, 0114, 0115, 0116, 0117, + 0120, 0121, 0122, 0123, 0124, 0125, 0126, 0127, 0130, 0131, 0132, 0173, 0174, 0175, 0176, 0177 }; char nofold[256] = { - 0200,0201,0202,0203,0204,0205,0206,0207, - 0210,0211,0212,0213,0214,0215,0216,0217, - 0220,0221,0222,0223,0224,0225,0226,0227, - 0230,0231,0232,0233,0234,0235,0236,0237, - 0240,0241,0242,0243,0244,0245,0246,0247, - 0250,0251,0252,0253,0254,0255,0256,0257, - 0260,0261,0262,0263,0264,0265,0266,0267, - 0270,0271,0272,0273,0274,0275,0276,0277, - 0300,0301,0302,0303,0304,0305,0306,0307, - 0310,0311,0312,0313,0314,0315,0316,0317, - 0320,0321,0322,0323,0324,0325,0326,0327, - 0330,0331,0332,0333,0334,0335,0336,0337, - 0340,0341,0342,0343,0344,0345,0346,0347, - 0350,0351,0352,0353,0354,0355,0356,0357, - 0360,0361,0362,0363,0364,0365,0366,0367, - 0370,0371,0372,0373,0374,0375,0376,0377, - 0000,0001,0002,0003,0004,0005,0006,0007, - 0010,0011,0012,0013,0014,0015,0016,0017, - 0020,0021,0022,0023,0024,0025,0026,0027, - 0030,0031,0032,0033,0034,0035,0036,0037, - 0040,0041,0042,0043,0044,0045,0046,0047, - 0050,0051,0052,0053,0054,0055,0056,0057, - 0060,0061,0062,0063,0064,0065,0066,0067, - 0070,0071,0072,0073,0074,0075,0076,0077, - 0100,0101,0102,0103,0104,0105,0106,0107, - 0110,0111,0112,0113,0114,0115,0116,0117, - 0120,0121,0122,0123,0124,0125,0126,0127, - 0130,0131,0132,0133,0134,0135,0136,0137, - 0140,0141,0142,0143,0144,0145,0146,0147, - 0150,0151,0152,0153,0154,0155,0156,0157, - 0160,0161,0162,0163,0164,0165,0166,0167, - 0170,0171,0172,0173,0174,0175,0176,0177 + 0200, 0201, 0202, 0203, 0204, 0205, 0206, 0207, 0210, 0211, 0212, 0213, 0214, 0215, 0216, 0217, + 0220, 0221, 0222, 0223, 0224, 0225, 0226, 0227, 0230, 0231, 0232, 0233, 0234, 0235, 0236, 0237, + 0240, 0241, 0242, 0243, 0244, 0245, 0246, 0247, 0250, 0251, 0252, 0253, 0254, 0255, 0256, 0257, + 0260, 0261, 0262, 0263, 0264, 0265, 0266, 0267, 0270, 0271, 0272, 0273, 0274, 0275, 0276, 0277, + 0300, 0301, 0302, 0303, 0304, 0305, 0306, 0307, 0310, 0311, 0312, 0313, 0314, 0315, 0316, 0317, + 0320, 0321, 0322, 0323, 0324, 0325, 0326, 0327, 0330, 0331, 0332, 0333, 0334, 0335, 0336, 0337, + 0340, 0341, 0342, 0343, 0344, 0345, 0346, 0347, 0350, 0351, 0352, 0353, 0354, 0355, 0356, 0357, + 0360, 0361, 0362, 0363, 0364, 0365, 0366, 0367, 0370, 0371, 0372, 0373, 0374, 0375, 0376, 0377, + 0000, 0001, 0002, 0003, 0004, 0005, 0006, 0007, 0010, 0011, 0012, 0013, 0014, 0015, 0016, 0017, + 0020, 0021, 0022, 0023, 0024, 0025, 0026, 0027, 0030, 0031, 0032, 0033, 0034, 0035, 0036, 0037, + 0040, 0041, 0042, 0043, 0044, 0045, 0046, 0047, 0050, 0051, 0052, 0053, 0054, 0055, 0056, 0057, + 0060, 0061, 0062, 0063, 0064, 0065, 0066, 0067, 0070, 0071, 0072, 0073, 0074, 0075, 0076, 0077, + 0100, 0101, 0102, 0103, 0104, 0105, 0106, 0107, 0110, 0111, 0112, 0113, 0114, 0115, 0116, 0117, + 0120, 0121, 0122, 0123, 0124, 0125, 0126, 0127, 0130, 0131, 0132, 0133, 0134, 0135, 0136, 0137, + 0140, 0141, 0142, 0143, 0144, 0145, 0146, 0147, 0150, 0151, 0152, 0153, 0154, 0155, 0156, 0157, + 0160, 0161, 0162, 0163, 0164, 0165, 0166, 0167, 0170, 0171, 0172, 0173, 0174, 0175, 0176, 0177 }; -char nonprint[256] = { +char nonprint[256] = { + // clang-format off 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, @@ -132,9 +99,10 @@ char nonprint[256] = { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1 + // clang-format on }; -char dict[256] = { +char dict[256] = { 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, @@ -153,7 +121,7 @@ char dict[256] = { 0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1 }; -struct field { +struct field { char *code; char *ignore; int nflg; @@ -161,26 +129,35 @@ struct field { int bflg[2]; int m[2]; int n[2]; -} fields[NF]; -struct field proto = { - nofold+128, - zero+128, - 0, - 1, - 0,0, - 0,-1, - 0,0 -}; +} fields[NF]; + +struct field proto = { nofold + 128, zero + 128, 0, 1, 0, 0, 0, -1, 0, 0 }; int nfields; -int error = 1; -char *setfil(); +int error = 1; -#define blank(c) ((c) == ' ' || (c) == '\t') +#define blank(c) ((c) == ' ' || (c) == '\t') -main(argc, argv) -char **argv; +static int cmpa(char *pa, char *pb); +static void copyproto(void); +static void field(char *s, int k); +static void diag(char *s, char *t); +static void safeoutfil(void); +static void sort(void); +static void newfile(void); +static void merge(int a, int b); +static void oldfile(void); +static char *setfil(int i); +static void cant(char *f); +static void qusort(char **a, char **l); +static void disorder(char *s, char *t); +static char *eol(char *p); +static int number(char **ppa); + +int (*compare)(char *pa, char *pb) = cmpa; + +int main(int argc, char **argv) { - register a; + int a; extern char end[1]; char *ep; char *arg; @@ -190,84 +167,92 @@ char **argv; copyproto(); eargv = argv; while (--argc > 0) { - if(**++argv == '-') for(arg = *argv;;) { - switch(*++arg) { - case '\0': - if(arg[-1] == '-') - eargv[eargc++] = "-"; - break; + if (**++argv == '-') + for (arg = *argv;;) { + switch (*++arg) { + case '\0': + if (arg[-1] == '-') + eargv[eargc++] = "-"; + break; - case 'o': - if(--argc > 0) - outfil = *++argv; - continue; + case 'o': + if (--argc > 0) + outfil = *++argv; + continue; - case 'T': - if (--argc > 0) - dirtry[0] = *++argv; - continue; + case 'T': + if (--argc > 0) + dirtry[0] = *++argv; + continue; - default: - field(++*argv,nfields>0); + default: + field(++*argv, nfields > 0); + break; + } break; } - break; - } else if (**argv == '+') { - if(++nfields>=NF) { - diag("too many keys",""); + else if (**argv == '+') { + if (++nfields >= NF) { + diag("too many keys", ""); exit(1); } copyproto(); - field(++*argv,0); + field(++*argv, 0); } else eargv[eargc++] = *argv; } q = &fields[0]; - for(a=1; a<=nfields; a++) { + for (a = 1; a <= nfields; a++) { p = &fields[a]; - if(p->code != proto.code) continue; - if(p->ignore != proto.ignore) continue; - if(p->nflg != proto.nflg) continue; - if(p->rflg != proto.rflg) continue; - if(p->bflg[0] != proto.bflg[0]) continue; - if(p->bflg[1] != proto.bflg[1]) continue; + if (p->code != proto.code) + continue; + if (p->ignore != proto.ignore) + continue; + if (p->nflg != proto.nflg) + continue; + if (p->rflg != proto.rflg) + continue; + if (p->bflg[0] != proto.bflg[0]) + continue; + if (p->bflg[1] != proto.bflg[1]) + continue; p->code = q->code; p->ignore = q->ignore; p->nflg = q->nflg; p->rflg = q->rflg; p->bflg[0] = p->bflg[1] = q->bflg[0]; } - if(eargc == 0) + if (eargc == 0) eargv[eargc++] = "-"; - if(cflg && eargc>1) { - diag("can check only 1 file",""); + if (cflg && eargc > 1) { + diag("can check only 1 file", ""); exit(1); } safeoutfil(); ep = end + MEM; lspace = (int *)sbrk(0); - while((int)brk(ep) == -1) + while ((int)brk(ep) == -1) ep -= 512; #ifndef vax brk(ep -= 512); /* for recursion */ #endif - a = ep - (char*)lspace; - nlines = (a-L); - nlines /= (5*(sizeof(char *)/sizeof(char))); - ntext = nlines * 4 * (sizeof(char *)/sizeof(char)); + a = ep - (char *)lspace; + nlines = (a - L); + nlines /= (5 * (sizeof(char *) / sizeof(char))); + ntext = nlines * 4 * (sizeof(char *) / sizeof(char)); tspace = (char *)(lspace + nlines); a = -1; - for(dirs=dirtry; *dirs; dirs++) { - sprintf(filep=file1, "%s/stm%05uaa", *dirs, getpid()); + for (dirs = dirtry; *dirs; dirs++) { + sprintf(filep = file1, "%s/stm%05uaa", *dirs, getpid()); while (*filep) filep++; filep -= 2; - if ( (a=creat(file, 0600)) >=0) + if ((a = creat(file, 0600)) >= 0) break; } - if(a < 0) { - diag("can't locate temp",""); + if (a < 0) { + diag("can't locate temp", ""); exit(1); } close(a); @@ -280,18 +265,18 @@ char **argv; if (signal(SIGTERM, SIG_IGN) != SIG_IGN) signal(SIGTERM, term); nfiles = eargc; - if(!mflg && !cflg) { + if (!mflg && !cflg) { sort(); fclose(stdin); } - for(a = mflg|cflg?0:eargc; a+N=nfiles) + for (a = mflg | cflg ? 0 : eargc; a + N < nfiles || unsafeout && a < eargc; a = i) { + i = a + N; + if (i >= nfiles) i = nfiles; newfile(); merge(a, i); } - if(a != nfiles) { + if (a != nfiles) { oldfile(); merge(a, nfiles); } @@ -299,19 +284,19 @@ char **argv; term(0); } -sort() +void sort() { - register char *cp; - register char **lp; - register lines, text, len; + char *cp; + char **lp; + int lines, text, len; int done = 0; int i = 0; char *f; char c; - if((f = setfil(i++)) == NULL) + if ((f = setfil(i++)) == NULL) is = stdin; - else if((is = fopen(f, "r")) == NULL) + else if ((is = fopen(f, "r")) == NULL) cant(f); do { @@ -319,49 +304,49 @@ sort() lp = (char **)lspace; lines = nlines; text = ntext; - while(lines > 0 && text > 0) { - if(fgets(cp, L, is) == NULL) { - if(i >= eargc) { + while (lines > 0 && text > 0) { + if (fgets(cp, L, is) == NULL) { + if (i >= eargc) { ++done; break; } fclose(is); - if((f = setfil(i++)) == NULL) + if ((f = setfil(i++)) == NULL) is = stdin; - else if((is = fopen(f, "r")) == NULL) + else if ((is = fopen(f, "r")) == NULL) cant(f); continue; } *lp++ = cp; len = strlen(cp) + 1; /* null terminate */ - if(cp[len - 2] != '\n') + if (cp[len - 2] != '\n') { if (len == L) { diag("line too long (skipped): ", cp); - while((c=getc(is)) != EOF && c != '\n') + while ((c = getc(is)) != EOF && c != '\n') /* throw it away */; --lp; continue; } else { - diag("missing newline before EOF in ", - f ? f : "standard input"); + diag("missing newline before EOF in ", f ? f : "standard input"); /* be friendly, append a newline */ ++len; cp[len - 2] = '\n'; cp[len - 1] = '\0'; } + } cp += len; --lines; text -= len; } qusort((char **)lspace, lp); - if(done == 0 || nfiles != eargc) + if (done == 0 || nfiles != eargc) newfile(); else oldfile(); clearerr(os); - while(lp > (char **)lspace) { + while (lp > (char **)lspace) { cp = *--lp; - if(*cp) + if (*cp) fputs(cp, os); if (ferror(os)) { error = 1; @@ -369,231 +354,224 @@ sort() } } fclose(os); - } while(done == 0); + } while (done == 0); } -struct merg -{ - char l[L]; - FILE *b; +struct merg { + char l[L]; + FILE *b; } *ibuf[256]; -merge(a,b) +void merge(int a, int b) { - struct merg *p; - register char *cp, *dp; - register i; + struct merg *p; + char *cp, *dp; + int i; struct merg **ip, *jp; - char *f; + char *f; int j; int k, l; int muflg; p = (struct merg *)lspace; j = 0; - for(i=a; i < b; i++) { + for (i = a; i < b; i++) { f = setfil(i); - if(f == 0) + if (f == 0) p->b = stdin; - else if((p->b = fopen(f, "r")) == NULL) + else if ((p->b = fopen(f, "r")) == NULL) cant(f); ibuf[j] = p; - if(!rline(p)) j++; + if (!rline(p)) + j++; p++; } do { i = j; - qusort((char **)ibuf, (char **)(ibuf+i)); + qusort((char **)ibuf, (char **)(ibuf + i)); l = 0; - while(i--) { + while (i--) { cp = ibuf[i]->l; - if(*cp == '\0') { + if (*cp == '\0') { l = 1; - if(rline(ibuf[i])) { + if (rline(ibuf[i])) { k = i; - while(++k < j) - ibuf[k-1] = ibuf[k]; + while (++k < j) + ibuf[k - 1] = ibuf[k]; j--; } } } - } while(l); + } while (l); clearerr(os); muflg = mflg & uflg | cflg; i = j; - while(i > 0) { - cp = ibuf[i-1]->l; - if (!cflg && (uflg == 0 || muflg || i == 1 || - (*compare)(ibuf[i-1]->l,ibuf[i-2]->l))) { + while (i > 0) { + cp = ibuf[i - 1]->l; + if (!cflg && (uflg == 0 || muflg || i == 1 || (*compare)(ibuf[i - 1]->l, ibuf[i - 2]->l))) { fputs(cp, os); if (ferror(os)) { error = 1; term(0); } } - if(muflg){ - cp = ibuf[i-1]->l; + if (muflg) { + cp = ibuf[i - 1]->l; dp = p->l; do { - } while((*dp++ = *cp++) != '\n'); + } while ((*dp++ = *cp++) != '\n'); } - for(;;) { - if(rline(ibuf[i-1])) { + for (;;) { + if (rline(ibuf[i - 1])) { i--; - if(i == 0) + if (i == 0) break; - if(i == 1) + if (i == 1) muflg = uflg; } ip = &ibuf[i]; - while(--ip>ibuf&&(*compare)(ip[0]->l,ip[-1]->l)<0){ + while (--ip > ibuf && (*compare)(ip[0]->l, ip[-1]->l) < 0) { jp = *ip; - *ip = *(ip-1); - *(ip-1) = jp; + *ip = *(ip - 1); + *(ip - 1) = jp; } - if(!muflg) + if (!muflg) break; - j = (*compare)(ibuf[i-1]->l,p->l); - if(cflg) { - if(j > 0) - disorder("disorder:",ibuf[i-1]->l); - else if(uflg && j==0) - disorder("nonunique:",ibuf[i-1]->l); - } else if(j == 0) + j = (*compare)(ibuf[i - 1]->l, p->l); + if (cflg) { + if (j > 0) + disorder("disorder:", ibuf[i - 1]->l); + else if (uflg && j == 0) + disorder("nonunique:", ibuf[i - 1]->l); + } else if (j == 0) continue; break; } } p = (struct merg *)lspace; - for(i=a; ib); p++; - if(i >= eargc) + if (i >= eargc) unlink(setfil(i)); } fclose(os); } -disorder(s,t) -char *s, *t; +void disorder(char *s, char *t) { - register char *u; - for(u=t; *u!='\n';u++) ; + char *u; + + for (u = t; *u != '\n'; u++) + ; *u = 0; - diag(s,t); + diag(s, t); term(0); } -newfile() +void newfile() { - register char *f; + char *f; f = setfil(nfiles); - if((os=fopen(f, "w")) == NULL) { - diag("can't create ",f); + if ((os = fopen(f, "w")) == NULL) { + diag("can't create ", f); term(0); } nfiles++; } -char * -setfil(i) +char *setfil(int i) { - - if(i < eargc) - if(eargv[i][0] == '-' && eargv[i][1] == '\0') - return(0); + if (i < eargc) { + if (eargv[i][0] == '-' && eargv[i][1] == '\0') + return (0); else - return(eargv[i]); + return (eargv[i]); + } i -= eargc; - filep[0] = i/26 + 'a'; - filep[1] = i%26 + 'a'; - return(file); + filep[0] = i / 26 + 'a'; + filep[1] = i % 26 + 'a'; + return (file); } -oldfile() +void oldfile() { - - if(outfil) { - if((os=fopen(outfil, "w")) == NULL) { - diag("can't create ",outfil); + if (outfil) { + if ((os = fopen(outfil, "w")) == NULL) { + diag("can't create ", outfil); term(0); } } else os = stdout; } -safeoutfil() +void safeoutfil() { - register int i; - struct stat obuf,ibuf; + int i; + struct stat obuf, ibuf; - if(!mflg||outfil==0) + if (!mflg || outfil == 0) return; - if(stat(outfil,&obuf)==-1) + if (stat(outfil, &obuf) == -1) return; - for(i=eargc-N;i0; k<=nfields; k++) { + for (k = nfields > 0; k <= nfields; k++) { fp = &fields[k]; pa = i; pb = j; - if(k) { + if (k) { la = skip(pa, fp, 1); pa = skip(pa, fp, 0); lb = skip(pb, fp, 1); @@ -602,166 +580,166 @@ char *i, *j; la = eol(pa); lb = eol(pb); } - if(fp->nflg) { - if(tabchar) { - if(panflg) { + if (tabchar) { + if (pa < la && *pa == tabchar) pa++; - if(pbrflg; - if(*pa == '-') { + if (*pa == '-') { pa++; sa = -sa; } - if(*pb == '-') { + if (*pb == '-') { pb++; sb = -sb; } - for(ipa = pa; ipa pa && ipb > pb) - if(b = *--ipb - *--ipa) + if (sa == sb) + while (ipa > pa && ipb > pb) + if ((b = *--ipb - *--ipa)) a = b; - while(ipa > pa) - if(*--ipa != '0') - return(-sa); - while(ipb > pb) - if(*--ipb != '0') - return(sb); - if(a) return(a*sa); - if(*(pa=jpa) == '.') + while (ipa > pa) + if (*--ipa != '0') + return (-sa); + while (ipb > pb) + if (*--ipb != '0') + return (sb); + if (a) + return (a * sa); + if (*(pa = jpa) == '.') pa++; - if(*(pb=jpb) == '.') + if (*(pb = jpb) == '.') pb++; - if(sa==sb) - while(pacode; ignore = fp->ignore; -loop: - while(ignore[*pa]) + loop: + while (ignore[*pa]) pa++; - while(ignore[*pb]) + while (ignore[*pb]) pb++; - if(pa>=la || *pa=='\n') - if(pbrflg); - else continue; - if(pb>=lb || *pb=='\n') - return(-fp->rflg); - if((sa = code[*pb++]-code[*pa++]) == 0) + if (pa >= la || *pa == '\n') { + if (pb < lb && *pb != '\n') + return (fp->rflg); + else + continue; + } + if (pb >= lb || *pb == '\n') + return (-fp->rflg); + if ((sa = code[*pb++] - code[*pa++]) == 0) goto loop; - return(sa*fp->rflg); + return (sa * fp->rflg); } - if(uflg) - return(0); - return(cmpa(i, j)); + if (uflg) + return (0); + return (cmpa(i, j)); } -cmpa(pa, pb) -register char *pa, *pb; +int cmpa(char *pa, char *pb) { - while(*pa == *pb) { - if(*pa++ == '\n') - return(0); + while (*pa == *pb) { + if (*pa++ == '\n') + return (0); pb++; } - return( - *pa == '\n' ? fields[0].rflg: - *pb == '\n' ?-fields[0].rflg: - *pb > *pa ? fields[0].rflg: - -fields[0].rflg - ); + return (*pa == '\n' ? fields[0].rflg + : *pb == '\n' ? -fields[0].rflg + : *pb > *pa ? fields[0].rflg + : -fields[0].rflg); } -char * -skip(pp, fp, j) -struct field *fp; -char *pp; +char *skip(char *pp, struct field *fp, int j) { - register i; - register char *p; + int i; + char *p; p = pp; - if( (i=fp->m[j]) < 0) - return(eol(p)); - while(i-- > 0) { - if(tabchar != 0) { - while(*p != tabchar) - if(*p != '\n') + if ((i = fp->m[j]) < 0) + return (eol(p)); + while (i-- > 0) { + if (tabchar != 0) { + while (*p != tabchar) + if (*p != '\n') p++; - else goto ret; - if(i>0||j==0) + else + goto ret; + if (i > 0 || j == 0) p++; } else { - while(blank(*p)) + while (blank(*p)) p++; - while(!blank(*p)) - if(*p != '\n') + while (!blank(*p)) + if (*p != '\n') p++; - else goto ret; + else + goto ret; } } - if(tabchar==0||fp->bflg[j]) - while(blank(*p)) + if (tabchar == 0 || fp->bflg[j]) + while (blank(*p)) p++; i = fp->n[j]; - while(i-- > 0) { - if(*p != '\n') + while (i-- > 0) { + if (*p != '\n') p++; - else goto ret; + else + goto ret; } ret: - return(p); + return (p); } -char * -eol(p) -register char *p; +char *eol(char *p) { - while(*p != '\n') p++; - return(p); + while (*p != '\n') + p++; + return (p); } -copyproto() +void copyproto() { - register i; - register int *p, *q; + int i; + int *p, *q; p = (int *)&proto; q = (int *)&fields[nfields]; - for(i=0; iignore = dict+128; + p->ignore = dict + 128; break; case 'f': - p->code = fold+128; + p->code = fold + 128; break; case 'i': - p->ignore = nonprint+128; + p->ignore = nonprint + 128; break; case 'c': @@ -793,7 +771,8 @@ char *s; break; case 't': tabchar = *++s; - if(tabchar == 0) s--; + if (tabchar == 0) + s--; continue; case 'r': @@ -804,38 +783,44 @@ char *s; break; case '.': - if(p->m[k] == -1) /* -m.n with m missing */ + if (p->m[k] == -1) /* -m.n with m missing */ p->m[k] = 0; - d = &fields[0].n[0]-&fields[0].m[0]; + d = &fields[0].n[0] - &fields[0].m[0]; default: - p->m[k+d] = number(&s); + p->m[k + d] = number(&s); } compare = cmp; } } -number(ppa) -char **ppa; +int number(char **ppa) { int n; - register char *pa; + char *pa; + pa = *ppa; n = 0; - while(isdigit(*pa)) { - n = n*10 + *pa - '0'; + while (isdigit(*pa)) { + n = n * 10 + *pa - '0'; *ppa = pa++; } - return(n); + return (n); } -#define qsexc(p,q) t= *p;*p= *q;*q=t -#define qstexc(p,q,r) t= *p;*p= *r;*r= *q;*q=t +#define qsexc(p, q) \ + t = *p; \ + *p = *q; \ + *q = t +#define qstexc(p, q, r) \ + t = *p; \ + *p = *r; \ + *r = *q; \ + *q = t -qusort(a,l) -char **a, **l; +void qusort(char **a, char **l) { - register char **i, **j; + char **i, **j; char **k; char **lp, **hp; int c; @@ -843,38 +828,36 @@ char **a, **l; unsigned n; start: - if((n=l-a) <= 1) + if ((n = l - a) <= 1) return; - n /= 2; - hp = lp = a+n; + hp = lp = a + n; i = a; - j = l-1; + j = l - 1; - - for(;;) { - if(i < lp) { - if((c = (*compare)(*i, *lp)) == 0) { + for (;;) { + if (i < lp) { + if ((c = (*compare)(*i, *lp)) == 0) { --lp; qsexc(i, lp); continue; } - if(c < 0) { + if (c < 0) { ++i; continue; } } -loop: - if(j > hp) { - if((c = (*compare)(*hp, *j)) == 0) { + loop: + if (j > hp) { + if ((c = (*compare)(*hp, *j)) == 0) { ++hp; qsexc(hp, j); goto loop; } - if(c > 0) { - if(i == lp) { + if (c > 0) { + if (i == lp) { ++hp; qstexc(i, hp, j); i = ++lp; @@ -889,21 +872,20 @@ loop: goto loop; } - - if(i == lp) { - if(uflg) - for(k=lp+1; k<=hp;) **k++ = '\0'; - if(lp-a >= l-hp) { - qusort(hp+1, l); + if (i == lp) { + if (uflg) + for (k = lp + 1; k <= hp;) + **k++ = '\0'; + if (lp - a >= l - hp) { + qusort(hp + 1, l); l = lp; } else { qusort(a, lp); - a = hp+1; + a = hp + 1; } goto start; } - --lp; qstexc(j, lp, i); j = --hp; diff --git a/src/cmd/split.c b/src/cmd/split.c index b7fbe7f..ee8ff2b 100644 --- a/src/cmd/split.c +++ b/src/cmd/split.c @@ -3,22 +3,20 @@ unsigned count = 1000; int fnumber; -char fname[100]; -char *ifil; -char *ofil; -FILE *is; -FILE *os; +char fname[100]; +char *ifil; +char *ofil; +FILE *is; +FILE *os; -main(argc, argv) -char *argv[]; +int main(int argc, char *argv[]) { - register i, c, f; + int i, c, f; int iflg = 0; - for(i=1; i #include -main(argc,argv) -char **argv; +int main(int argc, char **argv) { - register unsigned sum; - register i, c; - register FILE *f; - register long nbytes; + unsigned sum; + int i, c; + FILE *f; + long nbytes; int errflg = 0; i = 1; do { - if(i < argc) { + if (i < argc) { if ((f = fopen(argv[i], "r")) == NULL) { fprintf(stderr, "sum: Can't open %s\n", argv[i]); errflg += 10; @@ -27,8 +26,8 @@ char **argv; nbytes = 0; while ((c = getc(f)) != EOF) { nbytes++; - if (sum&01) - sum = (sum>>1) + 0x8000; + if (sum & 01) + sum = (sum >> 1) + 0x8000; else sum >>= 1; sum += c; @@ -36,13 +35,13 @@ char **argv; } if (ferror(f)) { errflg++; - fprintf(stderr, "sum: read error on %s\n", argc>1?argv[i]:"-"); + fprintf(stderr, "sum: read error on %s\n", argc > 1 ? argv[i] : "-"); } - printf("%05u%6ld", sum, (nbytes+BUFSIZ-1)/BUFSIZ); - if(argc > 2) + printf("%05u%6ld", sum, (nbytes + BUFSIZ - 1) / BUFSIZ); + if (argc > 2) printf(" %s", argv[i]); printf("\n"); fclose(f); - } while(++i < argc); + } while (++i < argc); exit(errflg); } diff --git a/src/cmd/sync.c b/src/cmd/sync.c index 6803c99..203cfd4 100644 --- a/src/cmd/sync.c +++ b/src/cmd/sync.c @@ -1,3 +1,5 @@ +#include + int main() { sync();