121156Sdist /* 221156Sdist * Copyright (c) 1980 Regents of the University of California. 321156Sdist * All rights reserved. The Berkeley software License Agreement 421156Sdist * specifies the terms and conditions for redistribution. 521156Sdist */ 621156Sdist 710811Ssam #ifndef lint 821156Sdist char copyright[] = 921156Sdist "@(#) Copyright (c) 1980 Regents of the University of California.\n\ 1021156Sdist All rights reserved.\n"; 1121156Sdist #endif not lint 121057Sbill 1321156Sdist #ifndef lint 14*35339Sbostic static char sccsid[] = "@(#)mount.c 5.3 (Berkeley) 08/05/88"; 1521156Sdist #endif not lint 1621156Sdist 1712808Ssam #include <sys/param.h> 18*35339Sbostic #include <sys/file.h> 1910811Ssam #include <fstab.h> 2012808Ssam #include <mtab.h> 2116669Ssam #include <errno.h> 22*35339Sbostic #include <stdio.h> 231057Sbill 24*35339Sbostic #define BADTYPE(type) \ 25*35339Sbostic (strcmp(type, FSTAB_RO) && strcmp(type, FSTAB_RW) && \ 26*35339Sbostic strcmp(type, FSTAB_RQ)) 27*35339Sbostic #define SETTYPE(type) \ 28*35339Sbostic (!strcmp(type, FSTAB_RW) || !strcmp(type, FSTAB_RQ)) 291057Sbill 30*35339Sbostic #define MTAB "/etc/mtab" 311057Sbill 32*35339Sbostic static struct mtab mtab[NMOUNT]; 33*35339Sbostic static int fake, verbose; 345073Sroot 351057Sbill main(argc, argv) 365073Sroot int argc; 375073Sroot char **argv; 381057Sbill { 39*35339Sbostic extern char *optarg; 40*35339Sbostic extern int optind; 411057Sbill register struct mtab *mp; 42*35339Sbostic register struct fstab *fs; 43*35339Sbostic register int cnt; 44*35339Sbostic int all, ch, fd; 45*35339Sbostic char *type; 461057Sbill 47*35339Sbostic all = 0; 48*35339Sbostic type = NULL; 49*35339Sbostic while ((ch = getopt(argc, argv, "afrwv")) != EOF) 50*35339Sbostic switch((char)ch) { 51*35339Sbostic case 'a': 52*35339Sbostic all = 1; 53*35339Sbostic break; 54*35339Sbostic case 'f': 55*35339Sbostic fake = 1; 56*35339Sbostic break; 57*35339Sbostic case 'r': 5812808Ssam type = FSTAB_RO; 59*35339Sbostic break; 60*35339Sbostic case 'v': 61*35339Sbostic verbose = 1; 62*35339Sbostic break; 63*35339Sbostic case 'w': 64*35339Sbostic type = FSTAB_RW; 65*35339Sbostic break; 66*35339Sbostic case '?': 67*35339Sbostic default: 68*35339Sbostic usage(); 695073Sroot } 70*35339Sbostic argc -= optind; 71*35339Sbostic argv += optind; 72*35339Sbostic 73*35339Sbostic /* NOSTRICT */ 74*35339Sbostic if ((fd = open(MTAB, O_RDONLY, 0)) >= 0) { 75*35339Sbostic if (read(fd, (char *)mtab, NMOUNT * sizeof(struct mtab)) < 0) 76*35339Sbostic mtaberr(); 77*35339Sbostic (void)close(fd); 781057Sbill } 79*35339Sbostic 804460Sroot if (all) { 81*35339Sbostic while ((fs = getfsent())) 82*35339Sbostic if (strcmp(fs->fs_file, "/") && !BADTYPE(fs->fs_type)) 83*35339Sbostic mountfs(fs->fs_spec, fs->fs_file, 84*35339Sbostic type ? type : fs->fs_type); 85*35339Sbostic exit(0); 86*35339Sbostic } 875073Sroot 88*35339Sbostic if (argc == 0) { 89*35339Sbostic if (verbose || fake || type) 90*35339Sbostic usage(); 91*35339Sbostic for (mp = mtab, cnt = NMOUNT; cnt--; ++mp) 92*35339Sbostic if (*mp->m_path) 93*35339Sbostic prmtab(mp); 944460Sroot exit(0); 951057Sbill } 9612808Ssam 97*35339Sbostic if (all) 98*35339Sbostic usage(); 99*35339Sbostic 100*35339Sbostic if (argc == 1) { 101*35339Sbostic if (!(fs = getfsfile(*argv)) && !(fs = getfsspec(*argv))) { 102*35339Sbostic fprintf(stderr, 103*35339Sbostic "mount: unknown special file or file system %s.\n", 104*35339Sbostic *argv); 105*35339Sbostic exit(1); 106*35339Sbostic } 107*35339Sbostic if (BADTYPE(fs->fs_type)) { 108*35339Sbostic fprintf(stderr, 109*35339Sbostic "mount: %s has unknown file system type.\n", *argv); 110*35339Sbostic exit(1); 111*35339Sbostic } 112*35339Sbostic mountfs(fs->fs_spec, fs->fs_file, type ? type : fs->fs_type); 11312808Ssam exit(0); 11412808Ssam } 1151057Sbill 116*35339Sbostic if (argc != 2) 117*35339Sbostic usage(); 11812808Ssam 119*35339Sbostic mountfs(argv[0], argv[1], type ? type : "rw"); 12012808Ssam } 12112808Ssam 122*35339Sbostic static 12312808Ssam mountfs(spec, name, type) 12412808Ssam char *spec, *name, *type; 12512808Ssam { 126*35339Sbostic extern int errno; 127*35339Sbostic register struct mtab *mp, *space; 128*35339Sbostic register int cnt; 129*35339Sbostic register char *p; 130*35339Sbostic int fd; 131*35339Sbostic char *index(), *rindex(), *strcpy(); 1321057Sbill 13312808Ssam if (!fake) { 134*35339Sbostic if (mount(spec, name, type)) { 135*35339Sbostic fprintf(stderr, "%s on %s: ", spec, name); 13616669Ssam switch (errno) { 13716669Ssam case EMFILE: 138*35339Sbostic fprintf(stderr, "Mount table full\n"); 13916669Ssam break; 14016669Ssam case EINVAL: 141*35339Sbostic fprintf(stderr, "Bogus super block\n"); 14216669Ssam break; 14316669Ssam default: 144*35339Sbostic perror((char *)NULL); 145*35339Sbostic break; 14616669Ssam } 1474460Sroot return; 1484460Sroot } 149*35339Sbostic 15012808Ssam /* we don't do quotas.... */ 15112808Ssam if (strcmp(type, FSTAB_RQ) == 0) 15212808Ssam type = FSTAB_RW; 1534460Sroot } 154*35339Sbostic 155*35339Sbostic /* trim trailing /'s and find last component of name */ 156*35339Sbostic for (p = index(spec, '\0'); *--p == '/';); 157*35339Sbostic *++p = '\0'; 158*35339Sbostic if (p = rindex(spec, '/')) { 159*35339Sbostic *p = '\0'; 160*35339Sbostic spec = p + 1; 16112808Ssam } 162*35339Sbostic 163*35339Sbostic for (mp = mtab, cnt = NMOUNT, space = NULL; cnt--; ++mp) { 164*35339Sbostic if (!strcmp(mp->m_dname, spec)) 165*35339Sbostic break; 166*35339Sbostic if (!space && !*mp->m_path) 167*35339Sbostic space = mp; 168*35339Sbostic } 169*35339Sbostic if (cnt == -1) { 170*35339Sbostic if (!space) { 171*35339Sbostic fprintf(stderr, "mount: no more room in %s.\n", MTAB); 172*35339Sbostic exit(1); 173*35339Sbostic } 174*35339Sbostic mp = space; 175*35339Sbostic } 176*35339Sbostic 177*35339Sbostic #define DNMAX (sizeof(mtab[0].m_dname) - 1) 178*35339Sbostic #define PNMAX (sizeof(mtab[0].m_path) - 1) 179*35339Sbostic 180*35339Sbostic (void)strncpy(mp->m_dname, spec, DNMAX); 18112808Ssam mp->m_dname[DNMAX] = '\0'; 182*35339Sbostic (void)strncpy(mp->m_path, name, PNMAX); 18312808Ssam mp->m_path[PNMAX] = '\0'; 184*35339Sbostic (void)strcpy(mp->m_type, type); 185*35339Sbostic 18612808Ssam if (verbose) 18712808Ssam prmtab(mp); 188*35339Sbostic 189*35339Sbostic for (mp = mtab + NMOUNT - 1; mp > mtab && !*mp->m_path; --mp); 190*35339Sbostic if ((fd = open(MTAB, O_WRONLY|O_CREAT|O_TRUNC, 0644)) < 0) 191*35339Sbostic mtaberr(); 192*35339Sbostic cnt = (mp - mtab + 1) * sizeof(struct mtab); 193*35339Sbostic /* NOSTRICT */ 194*35339Sbostic if (write(fd, (char *)mtab, cnt) != cnt) 195*35339Sbostic mtaberr(); 196*35339Sbostic (void)close(fd); 1971057Sbill } 198*35339Sbostic 199*35339Sbostic static 200*35339Sbostic prmtab(mp) 201*35339Sbostic register struct mtab *mp; 202*35339Sbostic { 203*35339Sbostic printf("%s on %s", mp->m_dname, mp->m_path); 204*35339Sbostic if (!strcmp(mp->m_type, FSTAB_RO)) 205*35339Sbostic printf("\t(read-only)"); 206*35339Sbostic else if (!strcmp(mp->m_type, FSTAB_RQ)) 207*35339Sbostic printf("\t(with quotas)"); 208*35339Sbostic printf("\n"); 209*35339Sbostic } 210*35339Sbostic 211*35339Sbostic static 212*35339Sbostic mtaberr() 213*35339Sbostic { 214*35339Sbostic fprintf(stderr, "mount: %s: ", MTAB); 215*35339Sbostic perror((char *)NULL); 216*35339Sbostic exit(1); 217*35339Sbostic } 218*35339Sbostic 219*35339Sbostic static 220*35339Sbostic usage() 221*35339Sbostic { 222*35339Sbostic fprintf(stderr, "usage: mount [-arw]\nor mount [-rw] special | node\nor mount [-rw] special node\n"); 223*35339Sbostic exit(1); 224*35339Sbostic } 225