diff options
author | Eric Jackson <ericj@cvs.openbsd.org> | 2000-07-20 17:47:18 +0000 |
---|---|---|
committer | Eric Jackson <ericj@cvs.openbsd.org> | 2000-07-20 17:47:18 +0000 |
commit | 3119035ada5d482229e31efed27f732f469dcc7f (patch) | |
tree | 1d17f567027869a7eaa20de126a58e087b1d2204 /usr.bin/lndir | |
parent | 1274b0a88bc0164ab1016a6df6bca4e3641e1ad8 (diff) |
Code cleanup, user errx and warn instead of the hacks already there.
Diffstat (limited to 'usr.bin/lndir')
-rw-r--r-- | usr.bin/lndir/lndir.c | 278 |
1 files changed, 129 insertions, 149 deletions
diff --git a/usr.bin/lndir/lndir.c b/usr.bin/lndir/lndir.c index 361fdab3f12..007ac9afe1c 100644 --- a/usr.bin/lndir/lndir.c +++ b/usr.bin/lndir/lndir.c @@ -1,7 +1,10 @@ -/* $OpenBSD: lndir.c,v 1.4 1996/08/19 14:20:18 niklas Exp $ */ +/* $OpenBSD: lndir.c,v 1.5 2000/07/20 17:47:17 ericj Exp $ */ /* $XConsortium: lndir.c /main/15 1995/08/30 10:56:18 gildea $ */ -/* Create shadow link tree (after X11R4 script of the same name) - Mark Reinhold (mbr@lcs.mit.edu)/3 January 1990 */ + +/* + * Create shadow link tree (after X11R4 script of the same name) + * Mark Reinhold (mbr@lcs.mit.edu)/3 January 1990 + */ /* Copyright (c) 1990, X Consortium @@ -49,10 +52,14 @@ in this Software without prior written authorization from the X Consortium. #include <sys/types.h> #include <sys/stat.h> #include <sys/param.h> +#include <err.h> #include <errno.h> #include <dirent.h> #include <stdarg.h> #include <string.h> +#include <unistd.h> + +extern char *__progname; int silent = 0; /* -silent */ int ignore_links = 0; /* -ignorelinks */ @@ -60,74 +67,91 @@ int ignore_links = 0; /* -ignorelinks */ char *rcurdir; char *curdir; +int equivalent __P((char *, char *)); +void addexcept __P((char *)); +int dodir __P((char *, struct stat *, struct stat *, int)); +void usage __P((void)); + struct except { char *name; - struct except *next; }; + struct except *exceptions = (struct except *)NULL; -void -quit (int code, char * fmt, ...) +int +main (argc, argv) + int argc; + char **argv; { - va_list args; - va_start(args, fmt); - vfprintf (stderr, fmt, args); - va_end(args); - putc ('\n', stderr); - exit (code); -} + int ch; + char *fn, *tn; + struct stat fs, ts; + + 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 = "."; -void -quiterr (code, s) - char *s; -{ - perror (s); - exit (code); -} + /* to directory */ + if (stat(tn, &ts) < 0) + errx(1, "%s", tn); + if (!(S_ISDIR(ts.st_mode))) + errx(2, "%s: Not a directory", tn); + if (chdir(tn) < 0) + errx(1, "%s", tn); -void -msg (char * fmt, ...) -{ - va_list args; - if (curdir) { - fprintf (stderr, "%s:\n", curdir); - curdir = 0; - } - va_start(args, fmt); - vfprintf (stderr, fmt, args); - va_end(args); - putc ('\n', stderr); -} + /* from directory */ + if (stat(fn, &fs) < 0) + errx(1, "%s", fn); + if (!(S_ISDIR(fs.st_mode))) + errx(2, "%s: Not a directory", fn); -void -mperror (s) - char *s; -{ - if (curdir) { - fprintf (stderr, "%s:\n", curdir); - curdir = 0; - } - perror (s); + exit(dodir (fn, &fs, &ts, 0)); } - -int equivalent(lname, rname) +int +equivalent(lname, rname) char *lname; char *rname; { char *s; if (!strcmp(lname, rname)) - return 1; + return(1); for (s = lname; *s && (s = strchr(s, '/')); s++) { while (s[1] == '/') strcpy(s+1, s+2); } - return !strcmp(lname, rname); + return(!strcmp(lname, rname)); } - +void addexcept(name) char *name; { @@ -135,24 +159,26 @@ addexcept(name) new = (struct except *)malloc(sizeof(struct except)); if (new == (struct except *)NULL) - quiterr(1, "addexcept"); + errx(1, "addexcept"); new->name = strdup(name); if (new->name == (char *)NULL) - quiterr(1, "addexcept"); + errx(1, "addexcept"); new->next = exceptions; exceptions = new; } -/* Recursively create symbolic links from the current directory to the "from" - directory. Assumes that files described by fs and ts are directories. */ - -dodir (fn, fs, ts, rel) -char *fn; /* name of "from" directory, either absolute or +/* + * 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) + 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 */ + struct stat *fs, *ts; /* stats for the "from" directory and cwd */ + int rel; /* if true, prepend "../" to fn before using */ { DIR *df; struct dirent *dp; @@ -167,25 +193,25 @@ int rel; /* if true, prepend "../" to fn before using */ struct except *cur; if ((fs->st_dev == ts->st_dev) && (fs->st_ino == ts->st_ino)) { - msg ("%s: From and to directories are identical!", fn); - return 1; + warn("%s: From and to directories are identical!", fn); + return(1); } if (rel) - strcpy (buf, "../"); + strcpy(buf, "../"); else buf[0] = '\0'; - strcat (buf, fn); + strcat(buf, fn); - if (!(df = opendir (buf))) { - msg ("%s: Cannot opendir", buf); - return 1; + if (!(df = opendir(buf))) { + warn("%s: Cannot opendir", buf); + return(1); } - p = buf + strlen (buf); + p = buf + strlen(buf); *p++ = '/'; n_dirs = fs->st_nlink; - while (dp = readdir (df)) { + while ((dp = readdir(df))) { if (dp->d_name[strlen(dp->d_name) - 1] == '~') continue; for (cur = exceptions; cur != (struct except *)NULL; @@ -193,68 +219,71 @@ int rel; /* if true, prepend "../" to fn before using */ if (!strcmp (dp->d_name, cur->name)) goto next; /* can't continue */ } - strcpy (p, dp->d_name); + strcpy(p, dp->d_name); if (n_dirs > 0) { - if (stat (buf, &sb) < 0) { - mperror (buf); + if (stat(buf, &sb) < 0) { + warn("%s", buf); continue; } - if(S_ISDIR(sb.st_mode)) { + 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")) + if (!strcmp(dp->d_name, "RCS")) continue; - if (!strcmp (dp->d_name, "SCCS")) + if (!strcmp(dp->d_name, "SCCS")) continue; - if (!strcmp (dp->d_name, "CVS")) + if (!strcmp(dp->d_name, "CVS")) continue; - if (!strcmp (dp->d_name, "CVS.adm")) + if (!strcmp(dp->d_name, "CVS.adm")) continue; ocurdir = rcurdir; rcurdir = buf; - curdir = silent ? buf : (char *)0; + 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) { - mperror (dp->d_name); + 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) { - msg ("%s: is a link instead of a directory", dp->d_name); + 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; } - if (chdir (dp->d_name) < 0) { - mperror (dp->d_name); + if (chdir(dp->d_name) < 0) { + warn("%s", dp->d_name); curdir = rcurdir = ocurdir; continue; } - dodir (buf, &sb, &sc, (buf[0] != '/')); + dodir(buf, &sb, &sc, (buf[0] != '/')); if (chdir ("..") < 0) - quiterr (1, ".."); + errx (1, ".."); curdir = rcurdir = ocurdir; continue; } } /* non-directory */ - symlen = readlink (dp->d_name, symbuf, sizeof(symbuf) - 1); + 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. */ + /* + * 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); @@ -264,72 +293,23 @@ int rel; /* if true, prepend "../" to fn before using */ if (symlen >= 0) { /* Link exists in new tree. Print message if it doesn't match. */ - if (!equivalent (basesymlen>=0 ? basesym : buf, symbuf)) - msg ("%s: %s", dp->d_name, symbuf); + 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) - mperror (dp->d_name); + if (symlink(basesymlen>=0 ? basesym : buf, dp->d_name) < 0) + warn("%s", dp->d_name); } next: } - closedir (df); - return 0; + closedir(df); + return(0); } - -main (ac, av) -int ac; -char **av; +void +usage() { - char *prog_name = av[0]; - char *fn, *tn; - struct stat fs, ts; - - while (++av, --ac) { - if ((strcmp(*av, "-silent") == 0) || (strcmp(*av, "-s") == 0)) - silent = 1; - else if ((strcmp(*av, "-ignorelinks") == 0) || (strcmp(*av, "-i") == 0)) - ignore_links = 1; - else if (strcmp(*av, "-e") == 0) { - ++av, --ac; - - if (ac < 2) - quit (1, "usage: %s [-e except] [-s] [-i] fromdir [todir]", - prog_name); - addexcept(*av); - } - else if (strcmp(*av, "--") == 0) { - ++av, --ac; - break; - } - else - break; - } - - if (ac < 1 || ac > 2) - quit (1, "usage: %s [-e except] [-s] [-i] fromdir [todir]", - prog_name); - - fn = av[0]; - if (ac == 2) - tn = av[1]; - else - tn = "."; - - /* to directory */ - if (stat (tn, &ts) < 0) - quiterr (1, tn); - if (!(S_ISDIR(ts.st_mode))) - quit (2, "%s: Not a directory", tn); - if (chdir (tn) < 0) - quiterr (1, tn); - - /* from directory */ - if (stat (fn, &fs) < 0) - quiterr (1, fn); - if (!(S_ISDIR(fs.st_mode))) - quit (2, "%s: Not a directory", fn); - - exit (dodir (fn, &fs, &ts, 0)); + (void)fprintf(stderr, "usage: %s [-e except] [-si] fromdir todir\n", + __progname); + exit(1); } |