date and time created 83/02/18 00:42:39 by mckusick
[unix-history] / usr / src / sbin / mount / mount.c
index 010fde8..10657af 100644 (file)
@@ -1,23 +1,13 @@
-static char *sccsid = "@(#)mount.c     4.1 (Berkeley) %G%";
-#include <stdio.h>
-#include <fstab.h>
+#ifndef lint
+static char *sccsid = "@(#)mount.c     4.8 (Berkeley) %G%";
+#endif
 
 /*
 
 /*
- *     Mount file systems.
- *
- *     mount -a        Mount all file systems, as determined from the
- *                     file /etc/fstab.
- *     If the name entry in /etc/fstab is "/", don't mount.
- *     If the read only entry in /etc/fstab is "ro", mount read only
- *     The special file names in /etc/fstab are the block devices;
- *             this is what we want to mount.
- *     Tries to mount all of the files in /etc/fstab.
- *     
- *     mount special name      Mount special on name
- *     mount special name -r   Mount special on name, read/write
+ * mount
  */
  */
+#include <stdio.h>
+#include <fstab.h>
 
 
-int    mountall;
 #define        NMOUNT  16
 #define        NAMSIZ  32
 
 #define        NMOUNT  16
 #define        NAMSIZ  32
 
@@ -26,15 +16,19 @@ struct mtab {
        char    spec[NAMSIZ];
 } mtab[NMOUNT];
 
        char    spec[NAMSIZ];
 } mtab[NMOUNT];
 
+int    all;
+int    ro;
+int    fake;
+int    verbose;
+
 main(argc, argv)
 main(argc, argv)
-char **argv;
+       int argc;
+       char **argv;
 {
 {
-       register int ro;
        register struct mtab *mp;
        register char *np;
        int mf;
 
        register struct mtab *mp;
        register char *np;
        int mf;
 
-       mountall = 0;
        mf = open("/etc/mtab", 0);
        read(mf, (char *)mtab, NMOUNT*2*NAMSIZ);
        if (argc==1) {
        mf = open("/etc/mtab", 0);
        read(mf, (char *)mtab, NMOUNT*2*NAMSIZ);
        if (argc==1) {
@@ -43,79 +37,102 @@ char **argv;
                                printf("%s on %s\n", mp->spec, mp->file);
                exit(0);
        }
                                printf("%s on %s\n", mp->spec, mp->file);
                exit(0);
        }
-
-       if (argc == 2){
-               if (strcmp(argv[1], "-a") == 0)
-                       mountall++;
-               else {
-                       fprintf(stderr,"arg count\n");
-                       exit(1);
+top:
+       if (argc > 1) {
+               if (!strcmp(argv[1], "-a")) {
+                       all++;
+                       argc--, argv++;
+                       goto top;
                }
                }
-       }
-
-       if (!mountall){
-               ro = 0;
-               if(argc > 3)
+               if (!strcmp(argv[1], "-r")) {
                        ro++;
                        ro++;
-               if (mountfs(argv[1], argv[2], ro))
-                       exit(1);
-       } else {
-               FILE    *fs_file;
-               struct  fstab   fs;
-               if ( (fs_file = fopen(FSTAB, "r")) == NULL){
-                       perror(FSTAB);
-                       exit(1);
+                       argc--, argv++;
+                       goto top;
+               }
+               if (!strcmp(argv[1], "-f")) {
+                       fake++;
+                       argc--, argv++;
+                       goto top;
                }
                }
-               while (!feof(fs_file)){
-                       fscanf(fs_file, FSTABFMT, FSTABARG(&fs));
-                       if (strcmp(fs.fs_file, "/") == 0)
+               if (!strcmp(argv[1], "-v")) {
+                       verbose++;
+                       argc--, argv++;
+                       goto top;
+               }
+       }
+       if (all) {
+               struct fstab *fsp;
+
+               if (argc > 1)
+                       goto argcnt;
+               close(2); dup(1);
+               if (setfsent() == 0)
+                       perror(FSTAB), exit(1);
+               while ( (fsp = getfsent()) != 0) {
+                       if (strcmp(fsp->fs_file, "/") == 0)
+                               continue;
+                       ro = !strcmp(fsp->fs_type, FSTAB_RO);
+                       if (ro==0 && strcmp(fsp->fs_type, FSTAB_RW))
                                continue;
                                continue;
-                       fprintf(stderr, "Mounting %s on %s %s",
-                               fs.fs_file, fs.fs_spec,
-                               FSRO(&fs) ? "(Read Only)\n" : "\n");
-                       mountfs(fs.fs_spec, fs.fs_file, FSRO(&fs));
+                       mountfs(fsp->fs_spec, fsp->fs_file, ro);
                }
                }
-               fclose(fs_file);
+               exit(0);
        }
        }
-       exit(0);
+       if (argc != 3) {
+argcnt:
+               fprintf(stderr,
+           "usage: mount [ -a ] [ -r ] [ -f ] [ -v ] [ special dir ]\n");
+               exit(1);
+       }
+       mountfs(argv[1], argv[2], ro);
 }
 
 mountfs(spec, name, ro)
 }
 
 mountfs(spec, name, ro)
-       char    *spec, *name;
-       int     ro;
+       char *spec, *name;
 {
 {
-       register        char    *np;
-       register        struct  mtab    *mp;
-       int     mf;
+       register char *np;
+       register struct mtab *mp;
+       int mf;
 
 
-       if(mount(spec, name, ro) < 0) {
-               perror("mount");
-               return(1);
+       if (fake==0) {
+               if (mount(spec, name, ro) < 0) {
+                       fprintf(stderr, "%s on ", spec);
+                       perror(name);
+                       return;
+               }
        }
        }
+       if (verbose)
+               fprintf(stderr, "%s on %s%s\n", spec, name,
+                   ro ? " read only" : "");
        np = spec;
        np = spec;
-       while(*np++)
+       while (*np++)
                ;
        np--;
                ;
        np--;
-       while(*--np == '/')
+       while (*--np == '/')
                *np = '\0';
                *np = '\0';
-       while(np > spec && *--np != '/')
+       while (np > spec && *--np != '/')
                ;
                ;
-       if(*np == '/')
+       if (*np == '/')
                np++;
        spec = np;
                np++;
        spec = np;
-       for (mp = mtab; mp < &mtab[NMOUNT]; mp++) {
-               if (mp->file[0] == 0) {
-                       for (np = mp->spec; np < &mp->spec[NAMSIZ-1];)
-                               if ((*np++ = *spec++) == 0)
-                                       spec--;
-                       for (np = mp->file; np < &mp->file[NAMSIZ-1];)
-                               if ((*np++ = *name++) == 0)
-                                       name--;
-                       mp = &mtab[NMOUNT];
-                       while ((--mp)->file[0] == 0);
-                       mf = creat("/etc/mtab", 0644);
-                       write(mf, (char *)mtab, (mp-mtab+1)*2*NAMSIZ);
-               }
-       }
-       return(0);
+       for (mp = mtab; mp < &mtab[NMOUNT]; mp++)
+               if (!strcmp(mp->spec, spec))
+                       goto replace;
+       for (mp = mtab; mp < &mtab[NMOUNT]; mp++)
+               if (mp->file[0] == 0)
+                       goto replace;
+       return;
+replace:
+       for (np = mp->spec; np < &mp->spec[NAMSIZ-1];)
+               if ((*np++ = *spec++) == 0)
+                       spec--;
+       for (np = mp->file; np < &mp->file[NAMSIZ-1];)
+               if ((*np++ = *name++) == 0)
+                       name--;
+       mp = &mtab[NMOUNT];
+       while ((--mp)->file[0] == 0);
+       mf = creat("/etc/mtab", 0644);
+       write(mf, (char *)mtab, (mp-mtab+1)*2*NAMSIZ);
+       close(mf);
+       return;
 }
 }