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*35372Sbostic static char sccsid[] = "@(#)mount.c 5.6 (Berkeley) 08/15/88"; 1521156Sdist #endif not lint 1621156Sdist 1712808Ssam #include <sys/param.h> 1835339Sbostic #include <sys/file.h> 1910811Ssam #include <fstab.h> 2012808Ssam #include <mtab.h> 2116669Ssam #include <errno.h> 2235339Sbostic #include <stdio.h> 231057Sbill 2435339Sbostic #define BADTYPE(type) \ 2535339Sbostic (strcmp(type, FSTAB_RO) && strcmp(type, FSTAB_RW) && \ 2635339Sbostic strcmp(type, FSTAB_RQ)) 2735339Sbostic #define SETTYPE(type) \ 2835339Sbostic (!strcmp(type, FSTAB_RW) || !strcmp(type, FSTAB_RQ)) 291057Sbill 3035339Sbostic #define MTAB "/etc/mtab" 311057Sbill 3235339Sbostic static struct mtab mtab[NMOUNT]; 3335369Sbostic static int fake, verbose; 345073Sroot 351057Sbill main(argc, argv) 365073Sroot int argc; 375073Sroot char **argv; 381057Sbill { 3935339Sbostic extern char *optarg; 4035339Sbostic extern int optind; 411057Sbill register struct mtab *mp; 4235339Sbostic register struct fstab *fs; 4335339Sbostic register int cnt; 44*35372Sbostic int all, ch, fd, rval, sfake; 4535339Sbostic char *type; 461057Sbill 4735339Sbostic all = 0; 4835339Sbostic type = NULL; 4935339Sbostic while ((ch = getopt(argc, argv, "afrwv")) != EOF) 5035339Sbostic switch((char)ch) { 5135339Sbostic case 'a': 5235339Sbostic all = 1; 5335339Sbostic break; 5435339Sbostic case 'f': 5535339Sbostic fake = 1; 5635339Sbostic break; 5735339Sbostic case 'r': 5812808Ssam type = FSTAB_RO; 5935339Sbostic break; 6035339Sbostic case 'v': 6135339Sbostic verbose = 1; 6235339Sbostic break; 6335339Sbostic case 'w': 6435339Sbostic type = FSTAB_RW; 6535339Sbostic break; 6635339Sbostic case '?': 6735339Sbostic default: 6835339Sbostic usage(); 695073Sroot } 7035339Sbostic argc -= optind; 7135339Sbostic argv += optind; 7235339Sbostic 7335339Sbostic /* NOSTRICT */ 7435339Sbostic if ((fd = open(MTAB, O_RDONLY, 0)) >= 0) { 7535339Sbostic if (read(fd, (char *)mtab, NMOUNT * sizeof(struct mtab)) < 0) 7635339Sbostic mtaberr(); 7735339Sbostic (void)close(fd); 781057Sbill } 7935339Sbostic 804460Sroot if (all) { 8135369Sbostic rval = 0; 82*35372Sbostic for (sfake = fake; fs = getfsent(); fake = sfake) { 83*35372Sbostic if (BADTYPE(fs->fs_type)) 84*35372Sbostic continue; 85*35372Sbostic /* `/' is special, it's always mounted */ 86*35372Sbostic if (!strcmp(fs->fs_file, "/")) 87*35372Sbostic fake = 1; 88*35372Sbostic rval |= mountfs(fs->fs_spec, fs->fs_file, 89*35372Sbostic type ? type : fs->fs_type); 90*35372Sbostic } 9135341Sbostic exit(rval); 9235339Sbostic } 935073Sroot 9435339Sbostic if (argc == 0) { 9535339Sbostic if (verbose || fake || type) 9635339Sbostic usage(); 9735339Sbostic for (mp = mtab, cnt = NMOUNT; cnt--; ++mp) 9835339Sbostic if (*mp->m_path) 9935339Sbostic prmtab(mp); 1004460Sroot exit(0); 1011057Sbill } 10212808Ssam 10335339Sbostic if (all) 10435339Sbostic usage(); 10535339Sbostic 10635339Sbostic if (argc == 1) { 10735339Sbostic if (!(fs = getfsfile(*argv)) && !(fs = getfsspec(*argv))) { 10835339Sbostic fprintf(stderr, 10935339Sbostic "mount: unknown special file or file system %s.\n", 11035339Sbostic *argv); 11135339Sbostic exit(1); 11235339Sbostic } 11335339Sbostic if (BADTYPE(fs->fs_type)) { 11435339Sbostic fprintf(stderr, 11535339Sbostic "mount: %s has unknown file system type.\n", *argv); 11635339Sbostic exit(1); 11735339Sbostic } 11835369Sbostic exit(mountfs(fs->fs_spec, fs->fs_file, 11935369Sbostic type ? type : fs->fs_type)); 12012808Ssam } 1211057Sbill 12235339Sbostic if (argc != 2) 12335339Sbostic usage(); 12412808Ssam 12535369Sbostic exit(mountfs(argv[0], argv[1], type ? type : "rw")); 12612808Ssam } 12712808Ssam 12835339Sbostic static 12912808Ssam mountfs(spec, name, type) 13012808Ssam char *spec, *name, *type; 13112808Ssam { 13235339Sbostic extern int errno; 13335339Sbostic register struct mtab *mp, *space; 13435339Sbostic register int cnt; 13535339Sbostic register char *p; 13635339Sbostic int fd; 13735339Sbostic char *index(), *rindex(), *strcpy(); 1381057Sbill 13912808Ssam if (!fake) { 14035369Sbostic if (mount(spec, name, !strcmp(type, FSTAB_RO))) { 14135339Sbostic fprintf(stderr, "%s on %s: ", spec, name); 14216669Ssam switch (errno) { 14316669Ssam case EMFILE: 14435339Sbostic fprintf(stderr, "Mount table full\n"); 14516669Ssam break; 14616669Ssam case EINVAL: 14735339Sbostic fprintf(stderr, "Bogus super block\n"); 14816669Ssam break; 14916669Ssam default: 15035339Sbostic perror((char *)NULL); 15135339Sbostic break; 15216669Ssam } 15335369Sbostic return(1); 1544460Sroot } 15535339Sbostic 15612808Ssam /* we don't do quotas.... */ 15712808Ssam if (strcmp(type, FSTAB_RQ) == 0) 15812808Ssam type = FSTAB_RW; 1594460Sroot } 16035339Sbostic 16135339Sbostic /* trim trailing /'s and find last component of name */ 16235339Sbostic for (p = index(spec, '\0'); *--p == '/';); 16335339Sbostic *++p = '\0'; 16435339Sbostic if (p = rindex(spec, '/')) { 16535339Sbostic *p = '\0'; 16635339Sbostic spec = p + 1; 16712808Ssam } 16835339Sbostic 16935339Sbostic for (mp = mtab, cnt = NMOUNT, space = NULL; cnt--; ++mp) { 17035339Sbostic if (!strcmp(mp->m_dname, spec)) 17135339Sbostic break; 17235339Sbostic if (!space && !*mp->m_path) 17335339Sbostic space = mp; 17435339Sbostic } 17535339Sbostic if (cnt == -1) { 17635339Sbostic if (!space) { 17735339Sbostic fprintf(stderr, "mount: no more room in %s.\n", MTAB); 17835339Sbostic exit(1); 17935339Sbostic } 18035339Sbostic mp = space; 18135339Sbostic } 18235339Sbostic 18335339Sbostic #define DNMAX (sizeof(mtab[0].m_dname) - 1) 18435339Sbostic #define PNMAX (sizeof(mtab[0].m_path) - 1) 18535339Sbostic 18635339Sbostic (void)strncpy(mp->m_dname, spec, DNMAX); 18712808Ssam mp->m_dname[DNMAX] = '\0'; 18835339Sbostic (void)strncpy(mp->m_path, name, PNMAX); 18912808Ssam mp->m_path[PNMAX] = '\0'; 19035339Sbostic (void)strcpy(mp->m_type, type); 19135339Sbostic 19212808Ssam if (verbose) 19312808Ssam prmtab(mp); 19435339Sbostic 19535339Sbostic for (mp = mtab + NMOUNT - 1; mp > mtab && !*mp->m_path; --mp); 19635339Sbostic if ((fd = open(MTAB, O_WRONLY|O_CREAT|O_TRUNC, 0644)) < 0) 19735339Sbostic mtaberr(); 19835339Sbostic cnt = (mp - mtab + 1) * sizeof(struct mtab); 19935339Sbostic /* NOSTRICT */ 20035339Sbostic if (write(fd, (char *)mtab, cnt) != cnt) 20135339Sbostic mtaberr(); 20235339Sbostic (void)close(fd); 20335369Sbostic return(0); 2041057Sbill } 20535339Sbostic 20635339Sbostic static 20735339Sbostic prmtab(mp) 20835339Sbostic register struct mtab *mp; 20935339Sbostic { 21035339Sbostic printf("%s on %s", mp->m_dname, mp->m_path); 21135339Sbostic if (!strcmp(mp->m_type, FSTAB_RO)) 21235339Sbostic printf("\t(read-only)"); 21335339Sbostic else if (!strcmp(mp->m_type, FSTAB_RQ)) 21435339Sbostic printf("\t(with quotas)"); 21535339Sbostic printf("\n"); 21635339Sbostic } 21735339Sbostic 21835339Sbostic static 21935339Sbostic mtaberr() 22035339Sbostic { 22135339Sbostic fprintf(stderr, "mount: %s: ", MTAB); 22235339Sbostic perror((char *)NULL); 22335339Sbostic exit(1); 22435339Sbostic } 22535339Sbostic 22635339Sbostic static 22735339Sbostic usage() 22835339Sbostic { 22935369Sbostic fprintf(stderr, "usage: mount [-afrw]\nor mount [-frw] special | node\nor mount [-frw] special node\n"); 23035339Sbostic exit(1); 23135339Sbostic } 232