]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sbin/mount/mount.c
This commit was generated by cvs2svn to compensate for changes in r159825,
[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 const char userquotaeq[] = "userquota=";
124 static const char groupquotaeq[] = "groupquota=";
125
126 static int
127 use_mountprog(const char *vfstype)
128 {
129         /* XXX: We need to get away from implementing external mount
130          *      programs for every filesystem, and move towards having
131          *      each filesystem properly implement the nmount() system call.
132          */
133         unsigned int i;
134         const char *fs[] = {
135         "cd9660", "mfs", "msdosfs", "nfs", "nfs4", "ntfs",
136         "nwfs", "nullfs", "portalfs", "smbfs", "udf", "umapfs",
137         "unionfs",
138         NULL
139         };
140
141         for (i = 0; fs[i] != NULL; ++i) {
142                 if (strcmp(vfstype, fs[i]) == 0)
143                         return (1);
144         }
145         
146         return (0);
147 }
148
149 static int
150 exec_mountprog(const char *name, const char *execname, char *const argv[])
151 {
152         pid_t pid;
153         int status;
154
155         switch (pid = fork()) {
156         case -1:                                /* Error. */
157                 warn("fork");
158                 exit (1);
159         case 0:                                 /* Child. */
160                 /* Go find an executable. */
161                 execvP(execname, _PATH_SYSPATH, argv);
162                 if (errno == ENOENT) {
163                         warn("exec %s not found in %s", execname,
164                             _PATH_SYSPATH);
165                 }
166                 exit(1);
167         default:                                /* Parent. */
168                 if (waitpid(pid, &status, 0) < 0) {
169                         warn("waitpid");
170                         return (1);
171                 }
172
173                 if (WIFEXITED(status)) {
174                         if (WEXITSTATUS(status) != 0)
175                                 return (WEXITSTATUS(status));
176                 } else if (WIFSIGNALED(status)) {
177                         warnx("%s: %s", name, sys_siglist[WTERMSIG(status)]);
178                         return (1);
179                 }
180                 break;
181         }
182
183         return (0);
184 }
185
186 int
187 main(int argc, char *argv[])
188 {
189         const char *mntfromname, **vfslist, *vfstype;
190         struct fstab *fs;
191         struct statfs *mntbuf;
192         FILE *mountdfp;
193         pid_t pid;
194         int all, ch, i, init_flags, mntsize, rval, have_fstab;
195         char *cp, *ep, *options;
196
197         options = strdup("noro");
198         if (options == NULL)
199                 errx(1, "malloc failed");
200
201         all = init_flags = 0;
202         vfslist = NULL;
203         vfstype = "ufs";
204         while ((ch = getopt(argc, argv, "adF:fo:prwt:uv")) != -1)
205                 switch (ch) {
206                 case 'a':
207                         all = 1;
208                         break;
209                 case 'd':
210                         debug = 1;
211                         break;
212                 case 'F':
213                         setfstab(optarg);
214                         break;
215                 case 'f':
216                         init_flags |= MNT_FORCE;
217                         break;
218                 case 'o':
219                         options = catopt(options, optarg);
220                         break;
221                 case 'p':
222                         fstab_style = 1;
223                         verbose = 1;
224                         break;
225                 case 'r':
226                         options = catopt(options, "ro");
227                         break;
228                 case 't':
229                         if (vfslist != NULL)
230                                 errx(1, "only one -t option may be specified");
231                         vfslist = makevfslist(optarg);
232                         vfstype = optarg;
233                         break;
234                 case 'u':
235                         init_flags |= MNT_UPDATE;
236                         break;
237                 case 'v':
238                         verbose = 1;
239                         break;
240                 case 'w':
241                         options = catopt(options, "noro");
242                         break;
243                 case '?':
244                 default:
245                         usage();
246                         /* NOTREACHED */
247                 }
248         argc -= optind;
249         argv += optind;
250
251 #define BADTYPE(type)                                                   \
252         (strcmp(type, FSTAB_RO) &&                                      \
253             strcmp(type, FSTAB_RW) && strcmp(type, FSTAB_RQ))
254
255         rval = 0;
256         switch (argc) {
257         case 0:
258                 if ((mntsize = getmntinfo(&mntbuf, MNT_NOWAIT)) == 0)
259                         err(1, "getmntinfo");
260                 if (all) {
261                         while ((fs = getfsent()) != NULL) {
262                                 if (BADTYPE(fs->fs_type))
263                                         continue;
264                                 if (checkvfsname(fs->fs_vfstype, vfslist))
265                                         continue;
266                                 if (hasopt(fs->fs_mntops, "noauto"))
267                                         continue;
268                                 if (!(init_flags & MNT_UPDATE) &&
269                                     ismounted(fs, mntbuf, mntsize))
270                                         continue;
271                                 options = update_options(options, fs->fs_mntops,
272                                     mntbuf->f_flags);
273                                 if (mountfs(fs->fs_vfstype, fs->fs_spec,
274                                     fs->fs_file, init_flags, options,
275                                     fs->fs_mntops))
276                                         rval = 1;
277                         }
278                 } else if (fstab_style) {
279                         for (i = 0; i < mntsize; i++) {
280                                 if (checkvfsname(mntbuf[i].f_fstypename, vfslist))
281                                         continue;
282                                 putfsent(&mntbuf[i]);
283                         }
284                 } else {
285                         for (i = 0; i < mntsize; i++) {
286                                 if (checkvfsname(mntbuf[i].f_fstypename,
287                                     vfslist))
288                                         continue;
289                                 prmount(&mntbuf[i]);
290                         }
291                 }
292                 exit(rval);
293         case 1:
294                 if (vfslist != NULL)
295                         usage();
296
297                 rmslashes(*argv, *argv);
298                 if (init_flags & MNT_UPDATE) {
299                         mntfromname = NULL;
300                         have_fstab = 0;
301                         if ((mntbuf = getmntpt(*argv)) == NULL)
302                                 errx(1, "not currently mounted %s", *argv);
303                         /*
304                          * Only get the mntflags from fstab if both mntpoint
305                          * and mntspec are identical. Also handle the special
306                          * case where just '/' is mounted and 'spec' is not
307                          * identical with the one from fstab ('/dev' is missing
308                          * in the spec-string at boot-time).
309                          */
310                         if ((fs = getfsfile(mntbuf->f_mntonname)) != NULL) {
311                                 if (strcmp(fs->fs_spec,
312                                     mntbuf->f_mntfromname) == 0 &&
313                                     strcmp(fs->fs_file,
314                                     mntbuf->f_mntonname) == 0) {
315                                         have_fstab = 1;
316                                         mntfromname = mntbuf->f_mntfromname;
317                                 } else if (argv[0][0] == '/' &&
318                                     argv[0][1] == '\0') {
319                                         fs = getfsfile("/");
320                                         have_fstab = 1;
321                                         mntfromname = fs->fs_spec;
322                                 }
323                         }
324                         if (have_fstab) {
325                                 options = update_options(options, fs->fs_mntops,
326                                     mntbuf->f_flags);
327                         } else {
328                                 mntfromname = mntbuf->f_mntfromname;
329                                 options = update_options(options, NULL,
330                                     mntbuf->f_flags);
331                         }
332                         rval = mountfs(mntbuf->f_fstypename, mntfromname,
333                             mntbuf->f_mntonname, init_flags, options, 0);
334                         break;
335                 }
336                 if ((fs = getfsfile(*argv)) == NULL &&
337                     (fs = getfsspec(*argv)) == NULL)
338                         errx(1, "%s: unknown special file or file system",
339                             *argv);
340                 if (BADTYPE(fs->fs_type))
341                         errx(1, "%s has unknown file system type",
342                             *argv);
343                 rval = mountfs(fs->fs_vfstype, fs->fs_spec, fs->fs_file,
344                     init_flags, options, fs->fs_mntops);
345                 break;
346         case 2:
347                 /*
348                  * If -t flag has not been specified, the path cannot be
349                  * found, spec contains either a ':' or a '@', then assume
350                  * that an NFS file system is being specified ala Sun.
351                  * Check if the hostname contains only allowed characters
352                  * to reduce false positives.  IPv6 addresses containing
353                  * ':' will be correctly parsed only if the separator is '@'.
354                  * The definition of a valid hostname is taken from RFC 1034.
355                  */
356                 if (vfslist == NULL && ((ep = strchr(argv[0], '@')) != NULL ||
357                     (ep = strchr(argv[0], ':')) != NULL)) {
358                         if (*ep == '@') {
359                                 cp = ep + 1;
360                                 ep = cp + strlen(cp);
361                         } else
362                                 cp = argv[0];
363                         while (cp != ep) {
364                                 if (!isdigit(*cp) && !isalpha(*cp) &&
365                                     *cp != '.' && *cp != '-' && *cp != ':')
366                                         break;
367                                 cp++;
368                         }
369                         if (cp == ep)
370                                 vfstype = "nfs";
371                 }
372                 rval = mountfs(vfstype,
373                     argv[0], argv[1], init_flags, options, NULL);
374                 break;
375         default:
376                 usage();
377                 /* NOTREACHED */
378         }
379
380         /*
381          * If the mount was successfully, and done by root, tell mountd the
382          * good news.  Pid checks are probably unnecessary, but don't hurt.
383          */
384         if (rval == 0 && getuid() == 0 &&
385             (mountdfp = fopen(_PATH_MOUNTDPID, "r")) != NULL) {
386                 if (fscanf(mountdfp, "%d", &pid) == 1 &&
387                      pid > 0 && kill(pid, SIGHUP) == -1 && errno != ESRCH)
388                         err(1, "signal mountd");
389                 (void)fclose(mountdfp);
390         }
391
392         exit(rval);
393 }
394
395 int
396 ismounted(struct fstab *fs, struct statfs *mntbuf, int mntsize)
397 {
398         char realfsfile[PATH_MAX];
399         int i;
400
401         if (fs->fs_file[0] == '/' && fs->fs_file[1] == '\0')
402                 /* the root file system can always be remounted */
403                 return (0);
404
405         /* The user may have specified a symlink in fstab, resolve the path */
406         if (realpath(fs->fs_file, realfsfile) == NULL) {
407                 /* Cannot resolve the path, use original one */
408                 strlcpy(realfsfile, fs->fs_file, sizeof(realfsfile));
409         }
410
411         for (i = mntsize - 1; i >= 0; --i)
412                 if (strcmp(realfsfile, mntbuf[i].f_mntonname) == 0 &&
413                     (!isremountable(fs->fs_vfstype) ||
414                      strcmp(fs->fs_spec, mntbuf[i].f_mntfromname) == 0))
415                         return (1);
416         return (0);
417 }
418
419 int
420 isremountable(const char *vfsname)
421 {
422         const char **cp;
423
424         for (cp = remountable_fs_names; *cp; cp++)
425                 if (strcmp(*cp, vfsname) == 0)
426                         return (1);
427         return (0);
428 }
429
430 int
431 hasopt(const char *mntopts, const char *option)
432 {
433         int negative, found;
434         char *opt, *optbuf;
435
436         if (option[0] == 'n' && option[1] == 'o') {
437                 negative = 1;
438                 option += 2;
439         } else
440                 negative = 0;
441         optbuf = strdup(mntopts);
442         found = 0;
443         for (opt = optbuf; (opt = strtok(opt, ",")) != NULL; opt = NULL) {
444                 if (opt[0] == 'n' && opt[1] == 'o') {
445                         if (!strcasecmp(opt + 2, option))
446                                 found = negative;
447                 } else if (!strcasecmp(opt, option))
448                         found = !negative;
449         }
450         free(optbuf);
451         return (found);
452 }
453
454 int
455 mountfs(const char *vfstype, const char *spec, const char *name, int flags,
456         const char *options, const char *mntopts)
457 {
458         char *argv[100];
459         struct statfs sf;
460         int argc, i, ret;
461         char *optbuf, execname[PATH_MAX], mntpath[PATH_MAX];
462
463         /* resolve the mountpoint with realpath(3) */
464         (void)checkpath(name, mntpath);
465         name = mntpath;
466
467         if (mntopts == NULL)
468                 mntopts = "";
469         optbuf = catopt(strdup(mntopts), options);
470
471         if (strcmp(name, "/") == 0)
472                 flags |= MNT_UPDATE;
473         if (flags & MNT_FORCE)
474                 optbuf = catopt(optbuf, "force");
475         if (flags & MNT_RDONLY)
476                 optbuf = catopt(optbuf, "ro");
477         /*
478          * XXX
479          * The mount_mfs (newfs) command uses -o to select the
480          * optimization mode.  We don't pass the default "-o rw"
481          * for that reason.
482          */
483         if (flags & MNT_UPDATE)
484                 optbuf = catopt(optbuf, "update");
485
486         /* Compatibility glue. */
487         if (strcmp(vfstype, "msdos") == 0)
488                 vfstype = "msdosfs";
489
490         /* Construct the name of the appropriate mount command */
491         (void)snprintf(execname, sizeof(execname), "mount_%s", vfstype);
492
493         argc = 0;
494         argv[argc++] = execname;
495         mangle(optbuf, &argc, argv);
496         argv[argc++] = strdup(spec);
497         argv[argc++] = strdup(name);
498         argv[argc] = NULL;
499
500         if (debug) {
501                 (void)printf("exec: mount_%s", vfstype);
502                 for (i = 1; i < argc; i++)
503                         (void)printf(" %s", argv[i]);
504                 (void)printf("\n");
505                 return (0);
506         }
507
508         if (use_mountprog(vfstype)) {
509                 ret = exec_mountprog(name, execname, argv);
510         } else {
511                 ret = mount_fs(vfstype, argc, argv); 
512         }
513
514         free(optbuf);
515
516         if (verbose) {
517                 if (statfs(name, &sf) < 0) {
518                         warn("statfs %s", name);
519                         return (1);
520                 }
521                 if (fstab_style)
522                         putfsent(&sf);
523                 else
524                         prmount(&sf);
525         }
526
527         return (0);
528 }
529
530 void
531 prmount(struct statfs *sfp)
532 {
533         int flags;
534         unsigned int i;
535         struct opt *o;
536         struct passwd *pw;
537
538         (void)printf("%s on %s (%s", sfp->f_mntfromname, sfp->f_mntonname,
539             sfp->f_fstypename);
540
541         flags = sfp->f_flags & MNT_VISFLAGMASK;
542         for (o = optnames; flags && o->o_opt; o++)
543                 if (flags & o->o_opt) {
544                         (void)printf(", %s", o->o_name);
545                         flags &= ~o->o_opt;
546                 }
547         /*
548          * Inform when file system is mounted by an unprivileged user
549          * or privileged non-root user.
550          */
551         if ((flags & MNT_USER) != 0 || sfp->f_owner != 0) {
552                 (void)printf(", mounted by ");
553                 if ((pw = getpwuid(sfp->f_owner)) != NULL)
554                         (void)printf("%s", pw->pw_name);
555                 else
556                         (void)printf("%d", sfp->f_owner);
557         }
558         if (verbose) {
559                 if (sfp->f_syncwrites != 0 || sfp->f_asyncwrites != 0)
560                         (void)printf(", writes: sync %ju async %ju",
561                             (uintmax_t)sfp->f_syncwrites,
562                             (uintmax_t)sfp->f_asyncwrites);
563                 if (sfp->f_syncreads != 0 || sfp->f_asyncreads != 0)
564                         (void)printf(", reads: sync %ju async %ju",
565                             (uintmax_t)sfp->f_syncreads,
566                             (uintmax_t)sfp->f_asyncreads);
567                 if (sfp->f_fsid.val[0] != 0 || sfp->f_fsid.val[1] != 0) {
568                         printf(", fsid ");
569                         for (i = 0; i < sizeof(sfp->f_fsid); i++)
570                                 printf("%02x", ((u_char *)&sfp->f_fsid)[i]);
571                 }
572         }
573         (void)printf(")\n");
574 }
575
576 struct statfs *
577 getmntpt(const char *name)
578 {
579         struct statfs *mntbuf;
580         int i, mntsize;
581
582         mntsize = getmntinfo(&mntbuf, MNT_NOWAIT);
583         for (i = mntsize - 1; i >= 0; i--) {
584                 if (strcmp(mntbuf[i].f_mntfromname, name) == 0 ||
585                     strcmp(mntbuf[i].f_mntonname, name) == 0)
586                         return (&mntbuf[i]);
587         }
588         return (NULL);
589 }
590
591 char *
592 catopt(char *s0, const char *s1)
593 {
594         size_t i;
595         char *cp;
596
597         if (s1 == NULL || *s1 == '\0')
598                 return (s0);
599
600         if (s0 && *s0) {
601                 i = strlen(s0) + strlen(s1) + 1 + 1;
602                 if ((cp = malloc(i)) == NULL)
603                         errx(1, "malloc failed");
604                 (void)snprintf(cp, i, "%s,%s", s0, s1);
605         } else
606                 cp = strdup(s1);
607
608         if (s0)
609                 free(s0);
610         return (cp);
611 }
612
613 void
614 mangle(char *options, int *argcp, char *argv[])
615 {
616         char *p, *s;
617         int argc;
618
619         argc = *argcp;
620         for (s = options; (p = strsep(&s, ",")) != NULL;)
621                 if (*p != '\0') {
622                         if (strcmp(p, "noauto") == 0) {
623                                 /*
624                                  * Do not pass noauto option to nmount().
625                                  * or external mount program.  noauto is
626                                  * only used to prevent mounting a filesystem
627                                  * when 'mount -a' is specified, and is
628                                  * not a real mount option.
629                                  */
630                                 continue;
631                         } else if (strcmp(p, "userquota") == 0) {
632                                 continue;
633                         } else if (strncmp(p, userquotaeq,
634                             sizeof(userquotaeq) - 1) == 0) {
635                                 continue;
636                         } else if (strcmp(p, "groupquota") == 0) {
637                                 continue;
638                         } else if (strncmp(p, groupquotaeq,
639                             sizeof(groupquotaeq) - 1) == 0) {
640                                 continue;
641                         } else if (*p == '-') {
642                                 argv[argc++] = p;
643                                 p = strchr(p, '=');
644                                 if (p != NULL) {
645                                         *p = '\0';
646                                         argv[argc++] = p+1;
647                                 }
648                         } else {
649                                 argv[argc++] = strdup("-o");
650                                 argv[argc++] = p;
651                         }
652                 }
653
654         *argcp = argc;
655 }
656
657
658 char *
659 update_options(char *opts, char *fstab, int curflags)
660 {
661         char *o, *p;
662         char *cur;
663         char *expopt, *newopt, *tmpopt;
664
665         if (opts == NULL)
666                 return (strdup(""));
667
668         /* remove meta options from list */
669         remopt(fstab, MOUNT_META_OPTION_FSTAB);
670         remopt(fstab, MOUNT_META_OPTION_CURRENT);
671         cur = flags2opts(curflags);
672
673         /*
674          * Expand all meta-options passed to us first.
675          */
676         expopt = NULL;
677         for (p = opts; (o = strsep(&p, ",")) != NULL;) {
678                 if (strcmp(MOUNT_META_OPTION_FSTAB, o) == 0)
679                         expopt = catopt(expopt, fstab);
680                 else if (strcmp(MOUNT_META_OPTION_CURRENT, o) == 0)
681                         expopt = catopt(expopt, cur);
682                 else
683                         expopt = catopt(expopt, o);
684         }
685         free(cur);
686         free(opts);
687
688         /*
689          * Remove previous contradictory arguments. Given option "foo" we
690          * remove all the "nofoo" options. Given "nofoo" we remove "nonofoo"
691          * and "foo" - so we can deal with possible options like "notice".
692          */
693         newopt = NULL;
694         for (p = expopt; (o = strsep(&p, ",")) != NULL;) {
695                 if ((tmpopt = malloc( strlen(o) + 2 + 1 )) == NULL)
696                         errx(1, "malloc failed");
697
698                 strcpy(tmpopt, "no");
699                 strcat(tmpopt, o);
700                 remopt(newopt, tmpopt);
701                 free(tmpopt);
702
703                 if (strncmp("no", o, 2) == 0)
704                         remopt(newopt, o+2);
705
706                 newopt = catopt(newopt, o);
707         }
708         free(expopt);
709
710         return (newopt);
711 }
712
713 void
714 remopt(char *string, const char *opt)
715 {
716         char *o, *p, *r;
717
718         if (string == NULL || *string == '\0' || opt == NULL || *opt == '\0')
719                 return;
720
721         r = string;
722
723         for (p = string; (o = strsep(&p, ",")) != NULL;) {
724                 if (strcmp(opt, o) != 0) {
725                         if (*r == ',' && *o != '\0')
726                                 r++;
727                         while ((*r++ = *o++) != '\0')
728                             ;
729                         *--r = ',';
730                 }
731         }
732         *r = '\0';
733 }
734
735 void
736 usage(void)
737 {
738
739         (void)fprintf(stderr, "%s\n%s\n%s\n",
740 "usage: mount [-adfpruvw] [-F fstab] [-o options] [-t ufs | external_type]",
741 "       mount [-dfpruvw] special | node",
742 "       mount [-dfpruvw] [-o options] [-t ufs | external_type] special node");
743         exit(1);
744 }
745
746 void
747 putfsent(const struct statfs *ent)
748 {
749         struct fstab *fst;
750         char *opts;
751
752         opts = flags2opts(ent->f_flags);
753
754         /*
755          * "rw" is not a real mount option; this is why we print NULL as "rw"
756          * if opts is still NULL here.
757          */
758         printf("%s\t%s\t%s %s", ent->f_mntfromname, ent->f_mntonname,
759             ent->f_fstypename, opts ? opts : "rw");
760         free(opts);
761
762         if ((fst = getfsspec(ent->f_mntfromname)))
763                 printf("\t%u %u\n", fst->fs_freq, fst->fs_passno);
764         else if ((fst = getfsfile(ent->f_mntonname)))
765                 printf("\t%u %u\n", fst->fs_freq, fst->fs_passno);
766         else if (strcmp(ent->f_fstypename, "ufs") == 0) {
767                 if (strcmp(ent->f_mntonname, "/") == 0)
768                         printf("\t1 1\n");
769                 else
770                         printf("\t2 2\n");
771         } else
772                 printf("\t0 0\n");
773 }
774
775
776 char *
777 flags2opts(int flags)
778 {
779         char *res;
780
781         res = NULL;
782
783         if (flags & MNT_RDONLY)         res = catopt(res, "ro");
784         if (flags & MNT_SYNCHRONOUS)    res = catopt(res, "sync");
785         if (flags & MNT_NOEXEC)         res = catopt(res, "noexec");
786         if (flags & MNT_NOSUID)         res = catopt(res, "nosuid");
787         if (flags & MNT_UNION)          res = catopt(res, "union");
788         if (flags & MNT_ASYNC)          res = catopt(res, "async");
789         if (flags & MNT_NOATIME)        res = catopt(res, "noatime");
790         if (flags & MNT_NOCLUSTERR)     res = catopt(res, "noclusterr");
791         if (flags & MNT_NOCLUSTERW)     res = catopt(res, "noclusterw");
792         if (flags & MNT_NOSYMFOLLOW)    res = catopt(res, "nosymfollow");
793         if (flags & MNT_SUIDDIR)        res = catopt(res, "suiddir");
794         if (flags & MNT_MULTILABEL)     res = catopt(res, "multilabel");
795         if (flags & MNT_ACLS)           res = catopt(res, "acls");
796
797         return (res);
798 }