lint, KNF.
authorJan-Simon Pendry <pendry@ucbvax.Berkeley.EDU>
Fri, 18 Feb 1994 23:07:38 +0000 (15:07 -0800)
committerJan-Simon Pendry <pendry@ucbvax.Berkeley.EDU>
Fri, 18 Feb 1994 23:07:38 +0000 (15:07 -0800)
fixed so that nfs unmounts actually get round to calling the remote mountd.

SCCS-vsn: sbin/umount/umount.c 8.2

usr/src/sbin/umount/umount.c

index bbb7b3d..340282c 100644 (file)
@@ -12,7 +12,7 @@ static char copyright[] =
 #endif /* not lint */
 
 #ifndef lint
 #endif /* not lint */
 
 #ifndef lint
-static char sccsid[] = "@(#)umount.c   8.1 (Berkeley) %G%";
+static char sccsid[] = "@(#)umount.c   8.2 (Berkeley) %G%";
 #endif /* not lint */
 
 #include <sys/param.h>
 #endif /* not lint */
 
 #include <sys/param.h>
@@ -30,40 +30,46 @@ static char sccsid[] = "@(#)umount.c        8.1 (Berkeley) %G%";
 #include <nfs/rpcv2.h>
 #endif
 
 #include <nfs/rpcv2.h>
 #endif
 
+#include <err.h>
 #include <fstab.h>
 #include <stdio.h>
 #include <fstab.h>
 #include <stdio.h>
+#include <stdlib.h>
 #include <string.h>
 #include <string.h>
+#include <unistd.h>
 
 #ifdef NFS
 
 #ifdef NFS
-int xdr_dir();
-char *nfshost;
+char   *nfshost;
+int    namematch __P((struct hostent *, char *));
+int    xdr_dir __P((XDR *, char *));
 #endif
 
 #endif
 
-int    vflag, all, errs, fake;
-int    fflag = 0;
-char   *getmntname();
-
-#define        MNTON   1
-#define        MNTFROM 2
-#define        MNTTYPE 3
-
-int *typelist, *maketypelist();
-
-char *namelist[] = INITMOUNTNAMES;
+typedef enum { MNTON, MNTFROM } mntwhat;
 
 
+int    all, errs, fake, vflag;
+int    fflag = 0;
+char   *namelist[] = INITMOUNTNAMES;
+int    *typelist;
+
+int    badtype __P((int, int *));
+int    fsnametotype __P((char *));
+char   *getmntname __P((char *, mntwhat, int *));
+int    *maketypelist __P((char *));
+void   umountall __P((int *));
+int    umountfs __P((char *, int *));
+void   usage __P((void));
+
+int
 main(argc, argv)
        int argc;
        char **argv;
 {
 main(argc, argv)
        int argc;
        char **argv;
 {
-       extern char *optarg;
-       extern int optind;
        int ch;
 
        sync();
        while ((ch = getopt(argc, argv, "afFh:t:v")) != EOF)
        int ch;
 
        sync();
        while ((ch = getopt(argc, argv, "afFh:t:v")) != EOF)
-               switch((char)ch) {
-               case 'v':
-                       vflag++;
+               switch (ch) {
+               case 'a':
+                       all++;
                        break;
                case 'f':
                        fflag = MNT_FORCE;
                        break;
                case 'f':
                        fflag = MNT_FORCE;
@@ -71,12 +77,6 @@ main(argc, argv)
                case 'F':
                        fake++;
                        break;
                case 'F':
                        fake++;
                        break;
-               case 'a':
-                       all++;
-                       break;
-               case 't':
-                       typelist = maketypelist(optarg);
-                       break;
 #ifdef NFS
                case 'h':
                        /* -h flag implies -a, and "-t nfs" if no -t flag */
 #ifdef NFS
                case 'h':
                        /* -h flag implies -a, and "-t nfs" if no -t flag */
@@ -86,7 +86,12 @@ main(argc, argv)
                                typelist = maketypelist("nfs");
                        break;
 #endif /* NFS */
                                typelist = maketypelist("nfs");
                        break;
 #endif /* NFS */
-               case '?':
+               case 't':
+                       typelist = maketypelist(optarg);
+                       break;
+               case 'v':
+                       vflag++;
+                       break;
                default:
                        usage();
                        /* NOTREACHED */
                default:
                        usage();
                        /* NOTREACHED */
@@ -96,42 +101,46 @@ main(argc, argv)
 
        if (argc == 0 && !all)
                usage();
 
        if (argc == 0 && !all)
                usage();
+
+       if (setfsent() == 0)
+               err(1, "%s", FSTAB);
+
        if (all) {
                if (argc > 0)
                        usage();
        if (all) {
                if (argc > 0)
                        usage();
-               if (setfsent() == 0)
-                       perror(FSTAB), exit(1);
                umountall(typelist);
                exit(0);
                umountall(typelist);
                exit(0);
-       } else
-               setfsent();
+       }
+
        while (argc > 0) {
                if (umountfs(*argv++, 0) == 0)
                        errs++;
                argc--;
        }
        while (argc > 0) {
                if (umountfs(*argv++, 0) == 0)
                        errs++;
                argc--;
        }
-       exit(errs);
+       exit(errs ? 1 : 0);
 }
 
 }
 
+void
 usage()
 {
        fprintf(stderr,
 usage()
 {
        fprintf(stderr,
-               "%s\n%s\n",
-               "Usage: umount [-fv] special | node",
+               "usage:\n%s\n%s\n",
+               "  umount [-fv] special | node",
 #ifndef        NFS
 #ifndef        NFS
-               "    or umount -a[fv] [-t fstypelist]"
+               "  umount -a[fv] [-t fstypelist]"
 #else
 #else
-               "    or umount -a[fv] [-h host] [-t fstypelist]"
+               "  umount -a[fv] [-h host] [-t fstypelist]"
 #endif
              );
        exit(1);
 }
 
 #endif
              );
        exit(1);
 }
 
+void
 umountall(typelist)
 umountall(typelist)
-       char **typelist;
+       int *typelist;
 {
 {
-       register struct fstab *fs;
        char *cp;
        char *cp;
+       struct fstab *fs;
 
        while (fs = getfsent()) {
                if (badtype(fsnametotype(fs->fs_vfstype), typelist))
 
        while (fs = getfsent()) {
                if (badtype(fsnametotype(fs->fs_vfstype), typelist))
@@ -142,11 +151,9 @@ umountall(typelist)
                    strcmp(fs->fs_type, FSTAB_RO) &&
                    strcmp(fs->fs_type, FSTAB_RQ))
                        continue;
                    strcmp(fs->fs_type, FSTAB_RO) &&
                    strcmp(fs->fs_type, FSTAB_RQ))
                        continue;
-               cp = (char *)malloc((unsigned)strlen(fs->fs_file) + 1);
-               if (cp == NULL) {
-                       fprintf(stderr, "Out of memory.\n");
-                       exit(2);
-               }
+               cp = (char *)malloc((size_t)strlen(fs->fs_file) + 1);
+               if (cp == NULL)
+                       err(2, "malloc");
                strcpy(cp, fs->fs_file);
                umountall(typelist);
                break;
                strcpy(cp, fs->fs_file);
                umountall(typelist);
                break;
@@ -157,56 +164,63 @@ umountall(typelist)
        }
 }
 
        }
 }
 
+int
 umountfs(name, typelist)
        char *name;
        int *typelist;
 {
 umountfs(name, typelist)
        char *name;
        int *typelist;
 {
-       char *mntpt;
-       struct stat stbuf;
-       int type;
 #ifdef NFS
 #ifdef NFS
-       register CLIENT *clp;
+       enum clnt_stat clnt_stat;
+       CLIENT *clp;
+       char *delimp, *hostp;
        struct hostent *hp = 0;
        struct sockaddr_in saddr;
        struct timeval pertry, try;
        struct hostent *hp = 0;
        struct sockaddr_in saddr;
        struct timeval pertry, try;
-       enum clnt_stat clnt_stat;
        int so = RPC_ANYSOCK;
        int so = RPC_ANYSOCK;
-       char *hostp, *delimp;
 #endif /* NFS */
 #endif /* NFS */
+       char *mntpt;
+       struct stat stbuf;
+       int type;
+       char rname[MAXPATHLEN];
+
+       if (realpath(name, rname) == NULL) {
+               warn("%s", rname);
+               return (0);
+       }
+
+       name = rname;
 
        if (stat(name, &stbuf) < 0) {
 
        if (stat(name, &stbuf) < 0) {
-               if (getmntname(name, MNTFROM, &type) != 0)
-                       mntpt = name;
-               else if ((mntpt = getmntname(name, MNTON, &type)) == 0) {
-                       fprintf(stderr, "%s: not currently mounted\n", name);
+               if (((mntpt = getmntname(name, MNTFROM, &type)) == 0) &&
+                   ((mntpt = getmntname(name, MNTON, &type)) == 0)) {
+                       warnx("%s: not currently mounted", name);
                        return (0);
                }
        } else if ((stbuf.st_mode & S_IFMT) == S_IFBLK) {
                if ((mntpt = getmntname(name, MNTON, &type)) == 0) {
                        return (0);
                }
        } else if ((stbuf.st_mode & S_IFMT) == S_IFBLK) {
                if ((mntpt = getmntname(name, MNTON, &type)) == 0) {
-                       fprintf(stderr, "%s: not currently mounted\n", name);
+                       warnx("%s: not currently mounted", name);
                        return (0);
                }
        } else if ((stbuf.st_mode & S_IFMT) == S_IFDIR) {
                mntpt = name;
                        return (0);
                }
        } else if ((stbuf.st_mode & S_IFMT) == S_IFDIR) {
                mntpt = name;
-               if (getmntname(mntpt, MNTFROM, &type) == 0) {
-                       fprintf(stderr, "%s: not currently mounted\n", name);
+               if ((name = getmntname(mntpt, MNTFROM, &type)) == 0) {
+                       warnx("%s: not currently mounted", mntpt);
                        return (0);
                }
        } else {
                        return (0);
                }
        } else {
-               fprintf(stderr, "%s: not a directory or special device\n",
-                       name);
+               warnx("%s: not a directory or special device", name);
                return (0);
        }
 
        if (badtype(type, typelist))
                return (0);
        }
 
        if (badtype(type, typelist))
-               return(1);
+               return (1);
 #ifdef NFS
 #ifdef NFS
-       if ((delimp = index(name, '@')) != NULL) {
+       if ((delimp = strchr(name, '@')) != NULL) {
                hostp = delimp + 1;
                *delimp = '\0';
                hp = gethostbyname(hostp);
                *delimp = '@';
                hostp = delimp + 1;
                *delimp = '\0';
                hp = gethostbyname(hostp);
                *delimp = '@';
-       } else if ((delimp = index(name, ':')) != NULL) {
+       } else if ((delimp = strchr(name, ':')) != NULL) {
                *delimp = '\0';
                hostp = name;
                hp = gethostbyname(hostp);
                *delimp = '\0';
                hostp = name;
                hp = gethostbyname(hostp);
@@ -215,21 +229,22 @@ umountfs(name, typelist)
        }
 
        if (!namematch(hp, nfshost))
        }
 
        if (!namematch(hp, nfshost))
-               return(1);
+               return (1);
 #endif /* NFS */
        if (!fake && unmount(mntpt, fflag) < 0) {
 #endif /* NFS */
        if (!fake && unmount(mntpt, fflag) < 0) {
-               perror(mntpt);
+               warn("%s", mntpt);
                return (0);
        }
        if (vflag)
                return (0);
        }
        if (vflag)
-               fprintf(stderr, "%s: Unmounted from %s\n", name, mntpt);
+               warnx("%s: unmounted from %s", name, mntpt);
 
 #ifdef NFS
 
 #ifdef NFS
-       if (!fake && hp != NULL && (fflag & MNT_FORCE) == 0) {
+       if (!fake && (hp != NULL) && ((fflag & MNT_FORCE) == 0)) {
                *delimp = '\0';
                *delimp = '\0';
-               bcopy(hp->h_addr,(caddr_t)&saddr.sin_addr,hp->h_length);
+               bzero(&saddr, sizeof(saddr));
                saddr.sin_family = AF_INET;
                saddr.sin_port = 0;
                saddr.sin_family = AF_INET;
                saddr.sin_port = 0;
+               bcopy(hp->h_addr, &saddr.sin_addr, hp->h_length);
                pertry.tv_sec = 3;
                pertry.tv_usec = 0;
                if ((clp = clntudp_create(&saddr, RPCPROG_MNT, RPCMNT_VER1,
                pertry.tv_sec = 3;
                pertry.tv_usec = 0;
                if ((clp = clntudp_create(&saddr, RPCPROG_MNT, RPCMNT_VER1,
@@ -256,15 +271,15 @@ umountfs(name, typelist)
 char *
 getmntname(name, what, type)
        char *name;
 char *
 getmntname(name, what, type)
        char *name;
-       int what;
+       mntwhat what;
        int *type;
 {
        int *type;
 {
-       int mntsize, i;
+       int i, mntsize;
        struct statfs *mntbuf;
 
        if ((mntsize = getmntinfo(&mntbuf, MNT_NOWAIT)) == 0) {
        struct statfs *mntbuf;
 
        if ((mntsize = getmntinfo(&mntbuf, MNT_NOWAIT)) == 0) {
-               perror("umount");
-               return (0);
+               warn("getmntinfo");
+               return (NULL);
        }
        for (i = 0; i < mntsize; i++) {
                if (what == MNTON && !strcmp(mntbuf[i].f_mntfromname, name)) {
        }
        for (i = 0; i < mntsize; i++) {
                if (what == MNTON && !strcmp(mntbuf[i].f_mntfromname, name)) {
@@ -278,34 +293,37 @@ getmntname(name, what, type)
                        return (mntbuf[i].f_mntfromname);
                }
        }
                        return (mntbuf[i].f_mntfromname);
                }
        }
-       return (0);
+       return (NULL);
 }
 
 static int skipvfs;
 
 }
 
 static int skipvfs;
 
+int
 badtype(type, typelist)
        int type;
        int *typelist;
 {
        if (typelist == 0)
 badtype(type, typelist)
        int type;
        int *typelist;
 {
        if (typelist == 0)
-               return(0);
+               return (0);
+
        while (*typelist) {
                if (type == *typelist)
        while (*typelist) {
                if (type == *typelist)
-                       return(skipvfs);
+                       return (skipvfs);
                typelist++;
        }
                typelist++;
        }
-       return(!skipvfs);
+       return (!skipvfs);
 }
 
 int *
 maketypelist(fslist)
        char *fslist;
 {
 }
 
 int *
 maketypelist(fslist)
        char *fslist;
 {
-       register char *nextcp;
-       register int *av, i;
+       char *nextcp;
+       int *av, i;
 
        if (fslist == NULL)
 
        if (fslist == NULL)
-               return(NULL);
+               return (NULL);
+
        if (fslist[0] == 'n' && fslist[1] == 'o') {
                fslist += 2;
                skipvfs = 1;
        if (fslist[0] == 'n' && fslist[1] == 'o') {
                fslist += 2;
                skipvfs = 1;
@@ -314,18 +332,21 @@ maketypelist(fslist)
        for (i = 0, nextcp = fslist; *nextcp; nextcp++)
                if (*nextcp == ',')
                        i++;
        for (i = 0, nextcp = fslist; *nextcp; nextcp++)
                if (*nextcp == ',')
                        i++;
+
        av = (int *)malloc((i+2) * sizeof(int));
        if (av == NULL)
        av = (int *)malloc((i+2) * sizeof(int));
        if (av == NULL)
-               return(NULL);
+               return (NULL);
+
        for (i = 0; fslist; fslist = nextcp) {
        for (i = 0; fslist; fslist = nextcp) {
-               if (nextcp = index(fslist, ','))
+               if (nextcp = strchr(fslist, ','))
                        *nextcp++ = '\0';
                av[i++] = fsnametotype(fslist);
        }
        av[i++] = 0;
                        *nextcp++ = '\0';
                av[i++] = fsnametotype(fslist);
        }
        av[i++] = 0;
-       return(av);
+       return (av);
 }
 
 }
 
+int
 fsnametotype(name)
        char *name;
 {
 fsnametotype(name)
        char *name;
 {
@@ -337,38 +358,43 @@ fsnametotype(name)
        return (MOUNT_NONE);
 }
 
        return (MOUNT_NONE);
 }
 
-#ifdef NFS
+#ifdef NFS
+int
 namematch(hp, nfshost)
        struct hostent *hp;
        char *nfshost;
 {
 namematch(hp, nfshost)
        struct hostent *hp;
        char *nfshost;
 {
-       register char *cp;
-       register char **np;
+       char *cp;
+       char **np;
 
        if (hp == NULL || nfshost == NULL)
 
        if (hp == NULL || nfshost == NULL)
-               return(1);
+               return (1);
+
        if (strcasecmp(nfshost, hp->h_name) == 0)
        if (strcasecmp(nfshost, hp->h_name) == 0)
-               return(1);
-       if (cp = index(hp->h_name, '.')) {
+               return (1);
+
+       if (cp = strchr(hp->h_name, '.')) {
                *cp = '\0';
                if (strcasecmp(nfshost, hp->h_name) == 0)
                *cp = '\0';
                if (strcasecmp(nfshost, hp->h_name) == 0)
-                       return(1);
+                       return (1);
        }
        for (np = hp->h_aliases; *np; np++) {
                if (strcasecmp(nfshost, *np) == 0)
        }
        for (np = hp->h_aliases; *np; np++) {
                if (strcasecmp(nfshost, *np) == 0)
-                       return(1);
-               if (cp = index(*np, '.')) {
+                       return (1);
+               if (cp = strchr(*np, '.')) {
+
                        *cp = '\0';
                        if (strcasecmp(nfshost, *np) == 0)
                        *cp = '\0';
                        if (strcasecmp(nfshost, *np) == 0)
-                               return(1);
+                               return (1);
                }
        }
                }
        }
-       return(0);
+       return (0);
 }
 
 /*
  * xdr routines for mount rpc's
  */
 }
 
 /*
  * xdr routines for mount rpc's
  */
+int
 xdr_dir(xdrsp, dirp)
        XDR *xdrsp;
        char *dirp;
 xdr_dir(xdrsp, dirp)
        XDR *xdrsp;
        char *dirp;