]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sbin/mount/mount.c
This commit was generated by cvs2svn to compensate for changes in r155094,
[FreeBSD/FreeBSD.git] / sbin / mount / mount.c
1 /*-
2  * Copyright (c) 1980, 1989, 1993, 1994
3  *      The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 4. Neither the name of the University nor the names of its contributors
14  *    may be used to endorse or promote products derived from this software
15  *    without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  */
29
30 #ifndef lint
31 static const char copyright[] =
32 "@(#) Copyright (c) 1980, 1989, 1993, 1994\n\
33         The Regents of the University of California.  All rights reserved.\n";
34 #endif /* not lint */
35
36 #ifndef lint
37 #if 0
38 static char sccsid[] = "@(#)mount.c     8.25 (Berkeley) 5/8/95";
39 #endif
40 static const char rcsid[] =
41   "$FreeBSD$";
42 #endif /* not lint */
43
44 #include <sys/param.h>
45 #include <sys/mount.h>
46 #include <sys/stat.h>
47 #include <sys/wait.h>
48
49 #include <ctype.h>
50 #include <err.h>
51 #include <errno.h>
52 #include <fstab.h>
53 #include <paths.h>
54 #include <pwd.h>
55 #include <signal.h>
56 #include <stdint.h>
57 #include <stdio.h>
58 #include <stdlib.h>
59 #include <string.h>
60 #include <unistd.h>
61
62 #include "extern.h"
63 #include "mntopts.h"
64 #include "pathnames.h"
65
66 /* `meta' options */
67 #define MOUNT_META_OPTION_FSTAB         "fstab"
68 #define MOUNT_META_OPTION_CURRENT       "current"
69
70 int debug, fstab_style, verbose;
71
72 char   *catopt(char *, const char *);
73 struct statfs *getmntpt(const char *);
74 int     hasopt(const char *, const char *);
75 int     ismounted(struct fstab *, struct statfs *, int);
76 int     isremountable(const char *);
77 void    mangle(char *, int *, char **);
78 char   *update_options(char *, char *, int);
79 int     mountfs(const char *, const char *, const char *,
80                         int, const char *, const char *);
81 void    remopt(char *, const char *);
82 void    prmount(struct statfs *);
83 void    putfsent(const struct statfs *);
84 void    usage(void);
85 char   *flags2opts(int);
86
87 /* Map from mount options to printable formats. */
88 static struct opt {
89         int o_opt;
90         const char *o_name;
91 } optnames[] = {
92         { MNT_ASYNC,            "asynchronous" },
93         { MNT_EXPORTED,         "NFS exported" },
94         { MNT_LOCAL,            "local" },
95         { MNT_NOATIME,          "noatime" },
96         { MNT_NOEXEC,           "noexec" },
97         { MNT_NOSUID,           "nosuid" },
98         { MNT_NOSYMFOLLOW,      "nosymfollow" },
99         { MNT_QUOTA,            "with quotas" },
100         { MNT_RDONLY,           "read-only" },
101         { MNT_SYNCHRONOUS,      "synchronous" },
102         { MNT_UNION,            "union" },
103         { MNT_NOCLUSTERR,       "noclusterr" },
104         { MNT_NOCLUSTERW,       "noclusterw" },
105         { MNT_SUIDDIR,          "suiddir" },
106         { MNT_SOFTDEP,          "soft-updates" },
107         { MNT_MULTILABEL,       "multilabel" },
108         { MNT_ACLS,             "acls" },
109         { 0, NULL }
110 };
111
112 /*
113  * List of VFS types that can be remounted without becoming mounted on top
114  * of each other.
115  * XXX Is this list correct?
116  */
117 static const char *
118 remountable_fs_names[] = {
119         "ufs", "ffs", "ext2fs",
120         0
121 };
122
123 static int
124 use_mountprog(const char *vfstype)
125 {
126         /* XXX: We need to get away from implementing external mount
127          *      programs for every filesystem, and move towards having
128          *      each filesystem properly implement the nmount() system call.
129          */
130         unsigned int i;
131         const char *fs[] = {
132         "cd9660", "mfs", "msdosfs", "nfs", "nfs4", "ntfs",
133         "nwfs", "nullfs", "portalfs", "smbfs", "udf", "umapfs",
134         "unionfs",
135         NULL
136         };
137
138         for (i=0; fs[i] != NULL; ++i) {
139                 if (strcmp(vfstype, fs[i]) == 0)
140                         return 1;
141         }
142         
143         return 0;
144 }
145
146 static int
147 exec_mountprog(const char *name, const char *execname,
148         char *const argv[])
149 {
150         pid_t pid;
151         int status;
152
153         switch (pid = fork()) {
154         case -1:                                /* Error. */
155                 warn("fork");
156                 exit (1);
157         case 0:                                 /* Child. */
158                 /* Go find an executable. */
159                 execvP(execname, _PATH_SYSPATH, argv);
160                 if (errno == ENOENT) {
161                         warn("exec %s not found in %s", execname,
162                             _PATH_SYSPATH);
163                 }
164                 exit(1);
165         default:                                /* Parent. */
166                 if (waitpid(pid, &status, 0) < 0) {
167                         warn("waitpid");
168                         return (1);
169                 }
170
171                 if (WIFEXITED(status)) {
172                         if (WEXITSTATUS(status) != 0)
173                                 return (WEXITSTATUS(status));
174                 } else if (WIFSIGNALED(status)) {
175                         warnx("%s: %s", name, sys_siglist[WTERMSIG(status)]);
176                         return (1);
177                 }
178                 break;
179         }
180
181         return (0);
182 }
183
184 int
185 main(int argc, char *argv[])
186 {
187         const char *mntfromname, **vfslist, *vfstype;
188         struct fstab *fs;
189         struct statfs *mntbuf;
190         FILE *mountdfp;
191         pid_t pid;
192         int all, ch, i, init_flags, mntsize, rval, have_fstab;
193         char *cp, *ep, *options;
194
195         all = init_flags = 0;
196         options = NULL;
197         vfslist = NULL;
198         vfstype = "ufs";
199         while ((ch = getopt(argc, argv, "adF:fo:prwt:uv")) != -1)
200                 switch (ch) {
201                 case 'a':
202                         all = 1;
203                         break;
204                 case 'd':
205                         debug = 1;
206                         break;
207                 case 'F':
208                         setfstab(optarg);
209                         break;
210                 case 'f':
211                         init_flags |= MNT_FORCE;
212                         break;
213                 case 'o':
214                         if (*optarg)
215                                 options = catopt(options, optarg);
216                         break;
217                 case 'p':
218                         fstab_style = 1;
219                         verbose = 1;
220                         break;
221                 case 'r':
222                         options = catopt(options, "ro");
223                         break;
224                 case 't':
225                         if (vfslist != NULL)
226                                 errx(1, "only one -t option may be specified");
227                         vfslist = makevfslist(optarg);
228                         vfstype = optarg;
229                         break;
230                 case 'u':
231                         init_flags |= MNT_UPDATE;
232                         break;
233                 case 'v':
234                         verbose = 1;
235                         break;
236                 case 'w':
237                         options = catopt(options, "noro");
238                         break;
239                 case '?':
240                 default:
241                         usage();
242                         /* NOTREACHED */
243                 }
244         argc -= optind;
245         argv += optind;
246
247 #define BADTYPE(type)                                                   \
248         (strcmp(type, FSTAB_RO) &&                                      \
249             strcmp(type, FSTAB_RW) && strcmp(type, FSTAB_RQ))
250
251         rval = 0;
252         switch (argc) {
253         case 0:
254                 if ((mntsize = getmntinfo(&mntbuf, MNT_NOWAIT)) == 0)
255                         err(1, "getmntinfo");
256                 if (all) {
257                         while ((fs = getfsent()) != NULL) {
258                                 if (BADTYPE(fs->fs_type))
259                                         continue;
260                                 if (checkvfsname(fs->fs_vfstype, vfslist))
261                                         continue;
262                                 if (hasopt(fs->fs_mntops, "noauto"))
263                                         continue;
264                                 if (!(init_flags & MNT_UPDATE) &&
265                                     ismounted(fs, mntbuf, mntsize))
266                                         continue;
267                                 options = update_options(options, fs->fs_mntops,
268                                     mntbuf->f_flags);
269                                 if (mountfs(fs->fs_vfstype, fs->fs_spec,
270                                     fs->fs_file, init_flags, options,
271                                     fs->fs_mntops))
272                                         rval = 1;
273                         }
274                 } else if (fstab_style) {
275                         for (i = 0; i < mntsize; i++) {
276                                 if (checkvfsname(mntbuf[i].f_fstypename, vfslist))
277                                         continue;
278                                 putfsent(&mntbuf[i]);
279                         }
280                 } else {
281                         for (i = 0; i < mntsize; i++) {
282                                 if (checkvfsname(mntbuf[i].f_fstypename,
283                                     vfslist))
284                                         continue;
285                                 prmount(&mntbuf[i]);
286                         }
287                 }
288                 exit(rval);
289         case 1:
290                 if (vfslist != NULL)
291                         usage();
292
293                 rmslashes(*argv, *argv);
294                 if (init_flags & MNT_UPDATE) {
295                         mntfromname = NULL;
296                         have_fstab = 0;
297                         if ((mntbuf = getmntpt(*argv)) == NULL)
298                                 errx(1, "not currently mounted %s", *argv);
299                         /*
300                          * Only get the mntflags from fstab if both mntpoint
301                          * and mntspec are identical. Also handle the special
302                          * case where just '/' is mounted and 'spec' is not
303                          * identical with the one from fstab ('/dev' is missing
304                          * in the spec-string at boot-time).
305                          */
306                         if ((fs = getfsfile(mntbuf->f_mntonname)) != NULL) {
307                                 if (strcmp(fs->fs_spec,
308                                     mntbuf->f_mntfromname) == 0 &&
309                                     strcmp(fs->fs_file,
310                                     mntbuf->f_mntonname) == 0) {
311                                         have_fstab = 1;
312                                         mntfromname = mntbuf->f_mntfromname;
313                                 } else if (argv[0][0] == '/' &&
314                                     argv[0][1] == '\0') {
315                                         fs = getfsfile("/");
316                                         have_fstab = 1;
317                                         mntfromname = fs->fs_spec;
318                                 }
319                         }
320                         if (have_fstab) {
321                                 options = update_options(options, fs->fs_mntops,
322                                     mntbuf->f_flags);
323                         } else {
324                                 mntfromname = mntbuf->f_mntfromname;
325                                 options = update_options(options, NULL,
326                                     mntbuf->f_flags);
327                         }
328                         rval = mountfs(mntbuf->f_fstypename, mntfromname,
329                             mntbuf->f_mntonname, init_flags, options, 0);
330                         break;
331                 }
332                 if ((fs = getfsfile(*argv)) == NULL &&
333                     (fs = getfsspec(*argv)) == NULL)
334                         errx(1, "%s: unknown special file or file system",
335                             *argv);
336                 if (BADTYPE(fs->fs_type))
337                         errx(1, "%s has unknown file system type",
338                             *argv);
339                 rval = mountfs(fs->fs_vfstype, fs->fs_spec, fs->fs_file,
340                     init_flags, options, fs->fs_mntops);
341                 break;
342         case 2:
343                 /*
344                  * If -t flag has not been specified, the path cannot be
345                  * found, spec contains either a ':' or a '@', then assume
346                  * that an NFS file system is being specified ala Sun.
347                  * Check if the hostname contains only allowed characters
348                  * to reduce false positives.  IPv6 addresses containing
349                  * ':' will be correctly parsed only if the separator is '@'.
350                  * The definition of a valid hostname is taken from RFC 1034.
351                  */
352                 if (vfslist == NULL && ((ep = strchr(argv[0], '@')) != NULL ||
353                     (ep = strchr(argv[0], ':')) != NULL)) {
354                         if (*ep == '@') {
355                                 cp = ep + 1;
356                                 ep = cp + strlen(cp);
357                         } else
358                                 cp = argv[0];
359                         while (cp != ep) {
360                                 if (!isdigit(*cp) && !isalpha(*cp) &&
361                                     *cp != '.' && *cp != '-' && *cp != ':')
362                                         break;
363                                 cp++;
364                         }
365                         if (cp == ep)
366                                 vfstype = "nfs";
367                 }
368                 rval = mountfs(vfstype,
369                     argv[0], argv[1], init_flags, options, NULL);
370                 break;
371         default:
372                 usage();
373                 /* NOTREACHED */
374         }
375
376         /*
377          * If the mount was successfully, and done by root, tell mountd the
378          * good news.  Pid checks are probably unnecessary, but don't hurt.
379          */
380         if (rval == 0 && getuid() == 0 &&
381             (mountdfp = fopen(_PATH_MOUNTDPID, "r")) != NULL) {
382                 if (fscanf(mountdfp, "%d", &pid) == 1 &&
383                      pid > 0 && kill(pid, SIGHUP) == -1 && errno != ESRCH)
384                         err(1, "signal mountd");
385                 (void)fclose(mountdfp);
386         }
387
388         exit(rval);
389 }
390
391 int
392 ismounted(struct fstab *fs, struct statfs *mntbuf, int mntsize)
393 {
394         char realfsfile[PATH_MAX];
395         int i;
396
397         if (fs->fs_file[0] == '/' && fs->fs_file[1] == '\0')
398                 /* the root file system can always be remounted */
399                 return (0);
400
401         /* The user may have specified a symlink in fstab, resolve the path */
402         if (realpath(fs->fs_file, realfsfile) == NULL) {
403                 /* Cannot resolve the path, use original one */
404                 strlcpy(realfsfile, fs->fs_file, sizeof(realfsfile));
405         }
406
407         for (i = mntsize - 1; i >= 0; --i)
408                 if (strcmp(realfsfile, mntbuf[i].f_mntonname) == 0 &&
409                     (!isremountable(fs->fs_vfstype) ||
410                      strcmp(fs->fs_spec, mntbuf[i].f_mntfromname) == 0))
411                         return (1);
412         return (0);
413 }
414
415 int
416 isremountable(const char *vfsname)
417 {
418         const char **cp;
419
420         for (cp = remountable_fs_names; *cp; cp++)
421                 if (strcmp(*cp, vfsname) == 0)
422                         return (1);
423         return (0);
424 }
425
426 int
427 hasopt(const char *mntopts, const char *option)
428 {
429         int negative, found;
430         char *opt, *optbuf;
431
432         if (option[0] == 'n' && option[1] == 'o') {
433                 negative = 1;
434                 option += 2;
435         } else
436                 negative = 0;
437         optbuf = strdup(mntopts);
438         found = 0;
439         for (opt = optbuf; (opt = strtok(opt, ",")) != NULL; opt = NULL) {
440                 if (opt[0] == 'n' && opt[1] == 'o') {
441                         if (!strcasecmp(opt + 2, option))
442                                 found = negative;
443                 } else if (!strcasecmp(opt, option))
444                         found = !negative;
445         }
446         free(optbuf);
447         return (found);
448 }
449
450 int
451 mountfs(const char *vfstype, const char *spec, const char *name, int flags,
452         const char *options, const char *mntopts)
453 {
454         char *argv[100];
455         struct statfs sf;
456         int argc, i, ret;
457         char *optbuf, execname[PATH_MAX], mntpath[PATH_MAX];
458
459         /* resolve the mountpoint with realpath(3) */
460         (void)checkpath(name, mntpath);
461         name = mntpath;
462
463         if (mntopts == NULL)
464                 mntopts = "";
465         if (options == NULL) {
466                 if (*mntopts == '\0') {
467                         options = "rw";
468                 } else {
469                         options = mntopts;
470                         mntopts = "";
471                 }
472         }
473         optbuf = catopt(strdup(mntopts), options);
474
475         if (strcmp(name, "/") == 0)
476                 flags |= MNT_UPDATE;
477         if (flags & MNT_FORCE)
478                 optbuf = catopt(optbuf, "force");
479         if (flags & MNT_RDONLY)
480                 optbuf = catopt(optbuf, "ro");
481         /*
482          * XXX
483          * The mount_mfs (newfs) command uses -o to select the
484          * optimization mode.  We don't pass the default "-o rw"
485          * for that reason.
486          */
487         if (flags & MNT_UPDATE)
488                 optbuf = catopt(optbuf, "update");
489
490         /* Compatibility glue. */
491         if (strcmp(vfstype, "msdos") == 0)
492                 vfstype = "msdosfs";
493
494         /* Construct the name of the appropriate mount command */
495         (void)snprintf(execname, sizeof(execname), "mount_%s", vfstype);
496
497         argc = 0;
498         argv[argc++] = execname;
499         mangle(optbuf, &argc, argv);
500         argv[argc++] = strdup(spec);
501         argv[argc++] = strdup(name);
502         argv[argc] = NULL;
503
504         if (debug) {
505                 (void)printf("exec: mount_%s", vfstype);
506                 for (i = 1; i < argc; i++)
507                         (void)printf(" %s", argv[i]);
508                 (void)printf("\n");
509                 return (0);
510         }
511
512         if (use_mountprog(vfstype)) {
513                 ret = exec_mountprog(name, execname, argv);
514         } else {
515                 ret = mount_fs(vfstype, argc, argv); 
516         }
517
518         free(optbuf);
519
520         if (verbose) {
521                 if (statfs(name, &sf) < 0) {
522                         warn("statfs %s", name);
523                         return (1);
524                 }
525                 if (fstab_style)
526                         putfsent(&sf);
527                 else
528                         prmount(&sf);
529         }
530
531         return (0);
532 }
533
534 void
535 prmount(struct statfs *sfp)
536 {
537         int flags;
538         unsigned int i;
539         struct opt *o;
540         struct passwd *pw;
541
542         (void)printf("%s on %s (%s", sfp->f_mntfromname, sfp->f_mntonname,
543             sfp->f_fstypename);
544
545         flags = sfp->f_flags & MNT_VISFLAGMASK;
546         for (o = optnames; flags && o->o_opt; o++)
547                 if (flags & o->o_opt) {
548                         (void)printf(", %s", o->o_name);
549                         flags &= ~o->o_opt;
550                 }
551         /*
552          * Inform when file system is mounted by an unprivileged user
553          * or privileged non-root user.
554          */
555         if ((flags & MNT_USER) != 0 || sfp->f_owner != 0) {
556                 (void)printf(", mounted by ");
557                 if ((pw = getpwuid(sfp->f_owner)) != NULL)
558                         (void)printf("%s", pw->pw_name);
559                 else
560                         (void)printf("%d", sfp->f_owner);
561         }
562         if (verbose) {
563                 if (sfp->f_syncwrites != 0 || sfp->f_asyncwrites != 0)
564                         (void)printf(", writes: sync %ju async %ju",
565                             (uintmax_t)sfp->f_syncwrites,
566                             (uintmax_t)sfp->f_asyncwrites);
567                 if (sfp->f_syncreads != 0 || sfp->f_asyncreads != 0)
568                         (void)printf(", reads: sync %ju async %ju",
569                             (uintmax_t)sfp->f_syncreads,
570                             (uintmax_t)sfp->f_asyncreads);
571                 if (sfp->f_fsid.val[0] != 0 || sfp->f_fsid.val[1] != 0) {
572                         printf(", fsid ");
573                         for (i = 0; i < sizeof(sfp->f_fsid); i++)
574                                 printf("%02x", ((u_char *)&sfp->f_fsid)[i]);
575                 }
576         }
577         (void)printf(")\n");
578 }
579
580 struct statfs *
581 getmntpt(const char *name)
582 {
583         struct statfs *mntbuf;
584         int i, mntsize;
585
586         mntsize = getmntinfo(&mntbuf, MNT_NOWAIT);
587         for (i = mntsize - 1; i >= 0; i--) {
588                 if (strcmp(mntbuf[i].f_mntfromname, name) == 0 ||
589                     strcmp(mntbuf[i].f_mntonname, name) == 0)
590                         return (&mntbuf[i]);
591         }
592         return (NULL);
593 }
594
595 char *
596 catopt(char *s0, const char *s1)
597 {
598         size_t i;
599         char *cp;
600
601         if (s1 == NULL || *s1 == '\0')
602                 return s0;
603
604         if (s0 && *s0) {
605                 i = strlen(s0) + strlen(s1) + 1 + 1;
606                 if ((cp = malloc(i)) == NULL)
607                         errx(1, "malloc failed");
608                 (void)snprintf(cp, i, "%s,%s", s0, s1);
609         } else
610                 cp = strdup(s1);
611
612         if (s0)
613                 free(s0);
614         return (cp);
615 }
616
617 void
618 mangle(options, argcp, argv)
619         char *options;
620         int *argcp;
621         char **argv;
622 {
623         char *p, *s;
624         int argc;
625
626         argc = *argcp;
627         for (s = options; (p = strsep(&s, ",")) != NULL;)
628                 if (*p != '\0') {
629                         if (strcmp(p, "noauto") == 0) {
630                                 /*
631                                  * Do not pass noauto option to nmount().
632                                  * or external mount program.  noauto is
633                                  * only used to prevent mounting a filesystem
634                                  * when 'mount -a' is specified, and is
635                                  * not a real mount option.
636                                  */
637                                 continue;
638                         } else if (strcmp(p, "userquota") == 0) {
639                                 continue;
640                         } else if (strcmp(p, "groupquota") == 0) {
641                                 continue;
642                         } else if (*p == '-') {
643                                 argv[argc++] = p;
644                                 p = strchr(p, '=');
645                                 if (p != NULL) {
646                                         *p = '\0';
647                                         argv[argc++] = p+1;
648                                 }
649                         } else {
650                                 argv[argc++] = strdup("-o");
651                                 argv[argc++] = p;
652                         }
653                 }
654
655         *argcp = argc;
656 }
657
658
659 char *
660 update_options(opts, fstab, curflags)
661         char *opts;
662         char *fstab;
663         int curflags;
664 {
665         char *o, *p;
666         char *cur;
667         char *expopt, *newopt, *tmpopt;
668
669         if (opts == NULL)
670                 return strdup("");
671
672         /* remove meta options from list */
673         remopt(fstab, MOUNT_META_OPTION_FSTAB);
674         remopt(fstab, MOUNT_META_OPTION_CURRENT);
675         cur = flags2opts(curflags);
676
677         /*
678          * Expand all meta-options passed to us first.
679          */
680         expopt = NULL;
681         for (p = opts; (o = strsep(&p, ",")) != NULL;) {
682                 if (strcmp(MOUNT_META_OPTION_FSTAB, o) == 0)
683                         expopt = catopt(expopt, fstab);
684                 else if (strcmp(MOUNT_META_OPTION_CURRENT, o) == 0)
685                         expopt = catopt(expopt, cur);
686                 else
687                         expopt = catopt(expopt, o);
688         }
689         free(cur);
690         free(opts);
691
692         /*
693          * Remove previous contradictory arguments. Given option "foo" we
694          * remove all the "nofoo" options. Given "nofoo" we remove "nonofoo"
695          * and "foo" - so we can deal with possible options like "notice".
696          */
697         newopt = NULL;
698         for (p = expopt; (o = strsep(&p, ",")) != NULL;) {
699                 if ((tmpopt = malloc( strlen(o) + 2 + 1 )) == NULL)
700                         errx(1, "malloc failed");
701
702                 strcpy(tmpopt, "no");
703                 strcat(tmpopt, o);
704                 remopt(newopt, tmpopt);
705                 free(tmpopt);
706
707                 if (strncmp("no", o, 2) == 0)
708                         remopt(newopt, o+2);
709
710                 newopt = catopt(newopt, o);
711         }
712         free(expopt);
713
714         return newopt;
715 }
716
717 void
718 remopt(string, opt)
719         char *string;
720         const char *opt;
721 {
722         char *o, *p, *r;
723
724         if (string == NULL || *string == '\0' || opt == NULL || *opt == '\0')
725                 return;
726
727         r = string;
728
729         for (p = string; (o = strsep(&p, ",")) != NULL;) {
730                 if (strcmp(opt, o) != 0) {
731                         if (*r == ',' && *o != '\0')
732                                 r++;
733                         while ((*r++ = *o++) != '\0')
734                             ;
735                         *--r = ',';
736                 }
737         }
738         *r = '\0';
739 }
740
741 void
742 usage()
743 {
744
745         (void)fprintf(stderr, "%s\n%s\n%s\n",
746 "usage: mount [-adfpruvw] [-F fstab] [-o options] [-t ufs | external_type]",
747 "       mount [-dfpruvw] special | node",
748 "       mount [-dfpruvw] [-o options] [-t ufs | external_type] special node");
749         exit(1);
750 }
751
752 void
753 putfsent(ent)
754         const struct statfs *ent;
755 {
756         struct fstab *fst;
757         char *opts;
758
759         opts = flags2opts(ent->f_flags);
760         printf("%s\t%s\t%s %s", ent->f_mntfromname, ent->f_mntonname,
761             ent->f_fstypename, opts);
762         free(opts);
763
764         if ((fst = getfsspec(ent->f_mntfromname)))
765                 printf("\t%u %u\n", fst->fs_freq, fst->fs_passno);
766         else if ((fst = getfsfile(ent->f_mntonname)))
767                 printf("\t%u %u\n", fst->fs_freq, fst->fs_passno);
768         else if (strcmp(ent->f_fstypename, "ufs") == 0) {
769                 if (strcmp(ent->f_mntonname, "/") == 0)
770                         printf("\t1 1\n");
771                 else
772                         printf("\t2 2\n");
773         } else
774                 printf("\t0 0\n");
775 }
776
777
778 char *
779 flags2opts(flags)
780         int flags;
781 {
782         char *res;
783
784         res = NULL;
785
786         res = catopt(res, (flags & MNT_RDONLY) ? "ro" : "rw");
787
788         if (flags & MNT_SYNCHRONOUS)    res = catopt(res, "sync");
789         if (flags & MNT_NOEXEC)         res = catopt(res, "noexec");
790         if (flags & MNT_NOSUID)         res = catopt(res, "nosuid");
791         if (flags & MNT_UNION)          res = catopt(res, "union");
792         if (flags & MNT_ASYNC)          res = catopt(res, "async");
793         if (flags & MNT_NOATIME)        res = catopt(res, "noatime");
794         if (flags & MNT_NOCLUSTERR)     res = catopt(res, "noclusterr");
795         if (flags & MNT_NOCLUSTERW)     res = catopt(res, "noclusterw");
796         if (flags & MNT_NOSYMFOLLOW)    res = catopt(res, "nosymfollow");
797         if (flags & MNT_SUIDDIR)        res = catopt(res, "suiddir");
798         if (flags & MNT_MULTILABEL)     res = catopt(res, "multilabel");
799         if (flags & MNT_ACLS)           res = catopt(res, "acls");
800
801         return res;
802 }