diff options
author | Theo de Raadt <deraadt@cvs.openbsd.org> | 2002-06-17 07:06:40 +0000 |
---|---|---|
committer | Theo de Raadt <deraadt@cvs.openbsd.org> | 2002-06-17 07:06:40 +0000 |
commit | ef41ea1998d7c91c32cf5a8dea8671253a9d8de7 (patch) | |
tree | bcd70d3088edbdc994849899ecbcaa53ac45af06 /usr.bin | |
parent | f728d495ce54d8e64392f0c511d610724218a83a (diff) |
KNF
Diffstat (limited to 'usr.bin')
-rw-r--r-- | usr.bin/fold/fold.c | 31 | ||||
-rw-r--r-- | usr.bin/lndir/lndir.c | 389 | ||||
-rw-r--r-- | usr.bin/nice/nice.c | 26 |
3 files changed, 222 insertions, 224 deletions
diff --git a/usr.bin/fold/fold.c b/usr.bin/fold/fold.c index f00be30ec68..93ff652a7f5 100644 --- a/usr.bin/fold/fold.c +++ b/usr.bin/fold/fold.c @@ -1,4 +1,4 @@ -/* $OpenBSD: fold.c,v 1.5 2001/11/19 19:02:14 mpech Exp $ */ +/* $OpenBSD: fold.c,v 1.6 2002/06/17 07:06:12 deraadt Exp $ */ /* $NetBSD: fold.c,v 1.6 1995/09/01 01:42:44 jtc Exp $ */ /*- @@ -47,7 +47,7 @@ static char copyright[] = #if 0 static char sccsid[] = "@(#)fold.c 8.1 (Berkeley) 6/6/93"; #endif -static char rcsid[] = "$OpenBSD: fold.c,v 1.5 2001/11/19 19:02:14 mpech Exp $"; +static char rcsid[] = "$OpenBSD: fold.c,v 1.6 2002/06/17 07:06:12 deraadt Exp $"; #endif /* not lint */ #include <stdio.h> @@ -110,7 +110,7 @@ main(argc, argv) fold(width); else for (; *argv; ++argv) if (!freopen(*argv, "r", stdin)) { - err (1, "%s", *argv); + err(1, "%s", *argv); /* NOTREACHED */ } else fold(width); @@ -141,47 +141,48 @@ fold(width) while ((ch = getchar()) != EOF) { if (ch == '\n') { if (indx != 0) - fwrite (buf, 1, indx, stdout); + fwrite(buf, 1, indx, stdout); putchar('\n'); col = indx = 0; continue; } - col = new_column_position (col, ch); + col = new_column_position(col, ch); if (col > width) { int i, last_space; if (split_words) { for (i = 0, last_space = -1; i < indx; i++) - if(buf[i] == ' ') last_space = i; + if(buf[i] == ' ') + last_space = i; } if (split_words && last_space != -1) { last_space++; - fwrite (buf, 1, last_space, stdout); - memmove (buf, buf+last_space, indx-last_space); + fwrite(buf, 1, last_space, stdout); + memmove(buf, buf+last_space, indx-last_space); indx -= last_space; col = 0; for (i = 0; i < indx; i++) { - col = new_column_position (col, buf[i]); + col = new_column_position(col, buf[i]); } } else { - fwrite (buf, 1, indx, stdout); + fwrite(buf, 1, indx, stdout); col = indx = 0; } putchar('\n'); /* calculate the column position for the next line. */ - col = new_column_position (col, ch); + col = new_column_position(col, ch); } if (indx + 1 > buf_max) { /* Allocate buffer in LINE_MAX increments */ buf_max += 2048; - if((buf = realloc (buf, buf_max)) == NULL) { - err (1, NULL); + if((buf = realloc(buf, buf_max)) == NULL) { + err(1, NULL); /* NOTREACHED */ } } @@ -189,14 +190,14 @@ fold(width) } if (indx != 0) - fwrite (buf, 1, indx, stdout); + fwrite(buf, 1, indx, stdout); } /* * calculate the column position */ static int -new_column_position (col, ch) +new_column_position(col, ch) int col; int ch; { diff --git a/usr.bin/lndir/lndir.c b/usr.bin/lndir/lndir.c index 84a9db47216..10218330093 100644 --- a/usr.bin/lndir/lndir.c +++ b/usr.bin/lndir/lndir.c @@ -1,4 +1,4 @@ -/* $OpenBSD: lndir.c,v 1.9 2002/05/29 18:33:39 deraadt Exp $ */ +/* $OpenBSD: lndir.c,v 1.10 2002/06/17 07:06:39 deraadt Exp $ */ /* $XConsortium: lndir.c /main/15 1995/08/30 10:56:18 gildea $ */ /* @@ -42,9 +42,9 @@ in this Software without prior written authorization from the X Consortium. If your master sources are located in /usr/local/src/X and you would like your link tree to be in /usr/local/src/new-X, do the following: - % mkdir /usr/local/src/new-X + % mkdir /usr/local/src/new-X % cd /usr/local/src/new-X - % lndir ../X + % lndir ../X */ #include <stdio.h> @@ -80,91 +80,85 @@ struct except { struct except *exceptions = (struct except *)NULL; int -main (argc, argv) - int argc; - char **argv; +main(int argc, char *argv[]) { - char *fn, *tn; struct stat fs, ts; + char *fn, *tn; + + while (++argv, --argc) { + if ((strcmp(*argv, "-silent") == 0) || + (strcmp(*argv, "-s") == 0)) + silent = 1; + else if ((strcmp(*argv, "-ignorelinks") == 0) || + (strcmp(*argv, "-i") == 0)) + ignore_links = 1; + else if (strcmp(*argv, "-e") == 0) { + ++argv, --argc; + + if (argc < 2) + usage(); + addexcept(*argv); + } else if (strcmp(*argv, "--") == 0) { + ++argv, --argc; + break; + } else + break; + } - while (++argv, --argc) { - if ((strcmp(*argv, "-silent") == 0) - || (strcmp(*argv, "-s") == 0)) - silent = 1; - else if ((strcmp(*argv, "-ignorelinks") == 0) - || (strcmp(*argv, "-i") == 0)) - ignore_links = 1; - else if (strcmp(*argv, "-e") == 0) { - ++argv, --argc; - - if (argc < 2) - usage(); - addexcept(*argv); - } else if (strcmp(*argv, "--") == 0) { - ++argv, --argc; - break; - } else - break; - } - - - if (argc < 1 || argc > 2) - usage(); - - fn = argv[0]; - if (argc == 2) - tn = argv[1]; - else - tn = "."; - - /* to directory */ - if (stat(tn, &ts) < 0) - err(1, "%s", tn); - if (!(S_ISDIR(ts.st_mode))) - errx(2, "%s: %s", tn, strerror(ENOTDIR)); - if (chdir(tn) < 0) - err(1, "%s", tn); - - /* from directory */ - if (stat(fn, &fs) < 0) - err(1, "%s", fn); - if (!(S_ISDIR(fs.st_mode))) - err(2, "%s: %s", fn, strerror(ENOTDIR)); - - exit(dodir (fn, &fs, &ts, 0)); + if (argc < 1 || argc > 2) + usage(); + + fn = argv[0]; + if (argc == 2) + tn = argv[1]; + else + tn = "."; + + /* to directory */ + if (stat(tn, &ts) < 0) + err(1, "%s", tn); + if (!(S_ISDIR(ts.st_mode))) + errx(2, "%s: %s", tn, strerror(ENOTDIR)); + if (chdir(tn) < 0) + err(1, "%s", tn); + + /* from directory */ + if (stat(fn, &fs) < 0) + err(1, "%s", fn); + if (!(S_ISDIR(fs.st_mode))) + err(2, "%s: %s", fn, strerror(ENOTDIR)); + + exit(dodir(fn, &fs, &ts, 0)); } int -equivalent(lname, rname) - char *lname; - char *rname; +equivalent(char *lname, char *rname) { - char *s; - - if (!strcmp(lname, rname)) - return(1); - for (s = lname; *s && (s = strchr(s, '/')); s++) { - while (s[1] == '/') - strcpy(s+1, s+2); - } - return(!strcmp(lname, rname)); + char *s; + + if (!strcmp(lname, rname)) + return(1); + for (s = lname; *s && (s = strchr(s, '/')); s++) { + while (s[1] == '/') + strcpy(s+1, s+2); + } + return(!strcmp(lname, rname)); } void -addexcept(name) - char *name; +addexcept(char *name) { - struct except *new; - - new = (struct except *)malloc(sizeof(struct except)); - if (new == (struct except *)NULL) - err(1, "addexcept"); - new->name = strdup(name); - if (new->name == (char *)NULL) - err(1, "addexcept"); - - new->next = exceptions; - exceptions = new; + struct except *new; + + new = (struct except *)malloc(sizeof(struct except)); + if (new == (struct except *)NULL) + err(1, "addexcept"); + new->name = strdup(name); + if (new->name == (char *)NULL) + err(1, "addexcept"); + + new->next = exceptions; + exceptions = new; } @@ -172,143 +166,146 @@ addexcept(name) * Recursively create symbolic links from the current directory to the "from" * directory. Assumes that files described by fs and ts are directories. */ -int -dodir(fn, fs, ts, rel) +#if 0 char *fn; /* name of "from" directory, either absolute or relative to cwd */ struct stat *fs, *ts; /* stats for the "from" directory and cwd */ int rel; /* if true, prepend "../" to fn before using */ +#endif +int +dodir(char *fn, struct stat *fs, struct stat *ts, int rel) { - DIR *df; - struct dirent *dp; - char buf[MAXPATHLEN + 1], *p; - char symbuf[MAXPATHLEN + 1]; - char basesym[MAXPATHLEN + 1]; - struct stat sb, sc; - int n_dirs; - int symlen; - int basesymlen = -1; - char *ocurdir; - struct except *cur; - - if ((fs->st_dev == ts->st_dev) && (fs->st_ino == ts->st_ino)) { - warn("%s: From and to directories are identical!", fn); - return(1); - } - - if (rel) - strcpy(buf, "../"); - else - buf[0] = '\0'; - strlcat(buf, fn, sizeof buf); + char buf[MAXPATHLEN + 1], symbuf[MAXPATHLEN + 1], basesym[MAXPATHLEN + 1]; + int n_dirs, symlen, basesymlen = -1; + struct stat sb, sc; + struct except *cur; + struct dirent *dp; + char *ocurdir, *p; + DIR *df; + + if (fs->st_dev == ts->st_dev && fs->st_ino == ts->st_ino) { + warn("%s: From and to directories are identical!", fn); + return(1); + } + + if (rel) + strlcpy(buf, "../", sizeof buf); + else + buf[0] = '\0'; + strlcat(buf, fn, sizeof buf); - if (!(df = opendir(buf))) { - warn("%s: Cannot opendir", buf); - return(1); - } - - p = buf + strlen(buf); - *p++ = '/'; - n_dirs = fs->st_nlink; - while ((dp = readdir(df))) { - if (dp->d_name[strlen(dp->d_name) - 1] == '~') - continue; - for (cur = exceptions; cur != (struct except *)NULL; - cur = cur->next) { - if (!strcmp (dp->d_name, cur->name)) - goto next; /* can't continue */ + if (!(df = opendir(buf))) { + warn("%s: Cannot opendir", buf); + return(1); } - strcpy(p, dp->d_name); - - if (n_dirs > 0) { - if (stat(buf, &sb) < 0) { - warn("%s", buf); - continue; - } - - if (S_ISDIR(sb.st_mode)) { - /* directory */ - n_dirs--; - if (dp->d_name[0] == '.' && - (dp->d_name[1] == '\0' || (dp->d_name[1] == '.' && - dp->d_name[2] == '\0'))) - continue; - if (!strcmp(dp->d_name, "RCS")) - continue; - if (!strcmp(dp->d_name, "SCCS")) - continue; - if (!strcmp(dp->d_name, "CVS")) - continue; - if (!strcmp(dp->d_name, "CVS.adm")) - continue; - ocurdir = rcurdir; - rcurdir = buf; - curdir = silent ? buf : NULL; - if (!silent) - printf("%s:\n", buf); - if ((stat(dp->d_name, &sc) < 0) && (errno == ENOENT)) { - if (mkdir(dp->d_name, 0777) < 0 || - stat(dp->d_name, &sc) < 0) { - warn("%s", dp->d_name); - curdir = rcurdir = ocurdir; + + p = buf + strlen(buf); + *p++ = '/'; + n_dirs = fs->st_nlink; + while ((dp = readdir(df))) { + if (dp->d_name[strlen(dp->d_name) - 1] == '~') continue; - } + for (cur = exceptions; cur != (struct except *)NULL; + cur = cur->next) { + if (!strcmp (dp->d_name, cur->name)) + goto next; /* can't continue */ } - if (readlink(dp->d_name, symbuf, sizeof(symbuf) - 1) >= 0) { - (void)fprintf(stderr, "%s: is a link instead of a directory\n", - dp->d_name); - curdir = rcurdir = ocurdir; - continue; + strcpy(p, dp->d_name); + + if (n_dirs > 0) { + if (stat(buf, &sb) < 0) { + warn("%s", buf); + continue; + } + + if (S_ISDIR(sb.st_mode)) { + /* directory */ + n_dirs--; + if (dp->d_name[0] == '.' && + (dp->d_name[1] == '\0' || (dp->d_name[1] == '.' && + dp->d_name[2] == '\0'))) + continue; + if (!strcmp(dp->d_name, "RCS")) + continue; + if (!strcmp(dp->d_name, "SCCS")) + continue; + if (!strcmp(dp->d_name, "CVS")) + continue; + if (!strcmp(dp->d_name, "CVS.adm")) + continue; + ocurdir = rcurdir; + rcurdir = buf; + curdir = silent ? buf : NULL; + if (!silent) + printf("%s:\n", buf); + if (stat(dp->d_name, &sc) < 0 && errno == ENOENT) { + if (mkdir(dp->d_name, 0777) < 0 || + stat(dp->d_name, &sc) < 0) { + warn("%s", dp->d_name); + curdir = rcurdir = ocurdir; + continue; + } + } + if (readlink(dp->d_name, symbuf, + sizeof(symbuf) - 1) >= 0) { + fprintf(stderr, + "%s: is a link instead of a directory\n", + dp->d_name); + curdir = rcurdir = ocurdir; + continue; + } + if (chdir(dp->d_name) < 0) { + warn("%s", dp->d_name); + curdir = rcurdir = ocurdir; + continue; + } + dodir(buf, &sb, &sc, (buf[0] != '/')); + if (chdir("..") < 0) + err(1, ".."); + curdir = rcurdir = ocurdir; + continue; + } } - if (chdir(dp->d_name) < 0) { - warn("%s", dp->d_name); - curdir = rcurdir = ocurdir; - continue; - } - dodir(buf, &sb, &sc, (buf[0] != '/')); - if (chdir ("..") < 0) - err (1, ".."); - curdir = rcurdir = ocurdir; - continue; - } - } - /* non-directory */ - symlen = readlink(dp->d_name, symbuf, sizeof(symbuf) - 1); - if (symlen >= 0) - symbuf[symlen] = '\0'; - - /* - * The option to ignore links exists mostly because - * checking for them slows us down by 10-20%. - * But it is off by default because this really is a useful check. - */ - if (!ignore_links) { - /* see if the file in the base tree was a symlink */ - basesymlen = readlink(buf, basesym, sizeof(basesym) - 1); - if (basesymlen >= 0) - basesym[basesymlen] = '\0'; - } + /* non-directory */ + symlen = readlink(dp->d_name, symbuf, sizeof(symbuf) - 1); + if (symlen >= 0) + symbuf[symlen] = '\0'; + + /* + * The option to ignore links exists mostly because + * checking for them slows us down by 10-20%. + * But it is off by default because this really is a useful check. + */ + if (!ignore_links) { + /* see if the file in the base tree was a symlink */ + basesymlen = readlink(buf, basesym, sizeof(basesym) - 1); + if (basesymlen >= 0) + basesym[basesymlen] = '\0'; + } - if (symlen >= 0) { - /* Link exists in new tree. Print message if it doesn't match. */ - if (!equivalent(basesymlen>=0 ? basesym : buf, symbuf)) - (void)fprintf(stderr,"%s: %s\n", dp->d_name, symbuf); - } else { - if (symlink(basesymlen>=0 ? basesym : buf, dp->d_name) < 0) - warn("%s", dp->d_name); - } + if (symlen >= 0) { + /* + * Link exists in new tree. Print message if + * it doesn't match. + */ + if (!equivalent(basesymlen>=0 ? basesym : buf, symbuf)) + fprintf(stderr,"%s: %s\n", dp->d_name, symbuf); + } else { + if (symlink(basesymlen>=0 ? basesym : buf, dp->d_name) < 0) + warn("%s", dp->d_name); + } next: - } + } - closedir(df); - return(0); + closedir(df); + return(0); } void -usage() +usage(void) { (void)fprintf(stderr, "usage: %s [-e except] [-si] fromdir todir\n", - __progname); + __progname); exit(1); } diff --git a/usr.bin/nice/nice.c b/usr.bin/nice/nice.c index ad498b1f9aa..fd3eee45c83 100644 --- a/usr.bin/nice/nice.c +++ b/usr.bin/nice/nice.c @@ -1,4 +1,4 @@ -/* $OpenBSD: nice.c,v 1.6 2002/02/16 21:27:50 millert Exp $ */ +/* $OpenBSD: nice.c,v 1.7 2002/06/17 07:06:25 deraadt Exp $ */ /* $NetBSD: nice.c,v 1.9 1995/08/31 23:30:58 jtc Exp $ */ /* @@ -44,7 +44,7 @@ char copyright[] = #if 0 static char sccsid[] = "@(#)nice.c 5.4 (Berkeley) 6/1/90"; #endif -static char rcsid[] = "$OpenBSD: nice.c,v 1.6 2002/02/16 21:27:50 millert Exp $"; +static char rcsid[] = "$OpenBSD: nice.c,v 1.7 2002/06/17 07:06:25 deraadt Exp $"; #endif /* not lint */ #include <sys/time.h> @@ -72,16 +72,17 @@ main(argc, argv) setlocale(LC_ALL, ""); - /* handle obsolete -number syntax */ - if (argc > 1 && argv[1][0] == '-' && isdigit(argv[1][1])) { - niceness = atoi (argv[1] + 1); - argc--; argv++; - } + /* handle obsolete -number syntax */ + if (argc > 1 && argv[1][0] == '-' && isdigit(argv[1][1])) { + niceness = atoi(argv[1] + 1); + argc--; + argv++; + } while ((c = getopt (argc, argv, "n:")) != -1) { switch (c) { case 'n': - niceness = atoi (optarg); + niceness = atoi(optarg); break; case '?': @@ -98,15 +99,14 @@ main(argc, argv) errno = 0; niceness += getpriority(PRIO_PROCESS, 0); if (errno) { - err (1, "getpriority"); + err(1, "getpriority"); /* NOTREACHED */ } - if (setpriority(PRIO_PROCESS, 0, niceness)) { - warn ("setpriority"); - } + if (setpriority(PRIO_PROCESS, 0, niceness)) + warn("setpriority"); execvp(argv[0], &argv[0]); - err ((errno == ENOENT) ? 127 : 126, "%s", argv[0]); + err((errno == ENOENT) ? 127 : 126, "%s", argv[0]); /* NOTREACHED */ } |