17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate * CDDL HEADER START
37c478bd9Sstevel@tonic-gate *
47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the
57c478bd9Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate * with the License.
87c478bd9Sstevel@tonic-gate *
97c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate * and limitations under the License.
137c478bd9Sstevel@tonic-gate *
147c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate *
207c478bd9Sstevel@tonic-gate * CDDL HEADER END
217c478bd9Sstevel@tonic-gate */
227c478bd9Sstevel@tonic-gate /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
237c478bd9Sstevel@tonic-gate /* All Rights Reserved */
247c478bd9Sstevel@tonic-gate
257c478bd9Sstevel@tonic-gate
267c478bd9Sstevel@tonic-gate /*
273e1bd7a2Ssjelinek * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
287c478bd9Sstevel@tonic-gate * Use is subject to license terms.
297c478bd9Sstevel@tonic-gate */
307c478bd9Sstevel@tonic-gate
317c478bd9Sstevel@tonic-gate #include <stdio.h>
327c478bd9Sstevel@tonic-gate #include <limits.h>
337c478bd9Sstevel@tonic-gate #include <locale.h>
347c478bd9Sstevel@tonic-gate #include <libintl.h>
357c478bd9Sstevel@tonic-gate #include <sys/fstyp.h>
367c478bd9Sstevel@tonic-gate #include <errno.h>
377c478bd9Sstevel@tonic-gate #include <sys/vfstab.h>
387c478bd9Sstevel@tonic-gate #include <sys/types.h>
397c478bd9Sstevel@tonic-gate #include <sys/stat.h>
407c478bd9Sstevel@tonic-gate #include <fcntl.h>
417c478bd9Sstevel@tonic-gate #include <string.h>
423e1bd7a2Ssjelinek #include <libdiskmgt.h>
433e1bd7a2Ssjelinek #include "fslib.h"
443e1bd7a2Ssjelinek
453e1bd7a2Ssjelinek
463e1bd7a2Ssjelinek static int match(char **opts, char *s);
47*82d71480Ssjelinek static int has_Nflag(char *opts);
487c478bd9Sstevel@tonic-gate
497c478bd9Sstevel@tonic-gate #define FSTYPE_MAX 8
507c478bd9Sstevel@tonic-gate #define ARGV_MAX 1024
517c478bd9Sstevel@tonic-gate #define VFS_PATH "/usr/lib/fs"
527c478bd9Sstevel@tonic-gate #define ALT_PATH "/etc/fs"
537c478bd9Sstevel@tonic-gate
547c478bd9Sstevel@tonic-gate extern char *default_fstype();
557c478bd9Sstevel@tonic-gate void stat_snap(char *, char *, char *);
567c478bd9Sstevel@tonic-gate char *special = NULL; /* device special name */
577c478bd9Sstevel@tonic-gate char *fstype = NULL; /* fstype name is filled in here */
587c478bd9Sstevel@tonic-gate char *cbasename; /* name of command */
597c478bd9Sstevel@tonic-gate char *newargv[ARGV_MAX]; /* args for the fstype specific command */
607c478bd9Sstevel@tonic-gate char vfstab[] = VFSTAB;
617c478bd9Sstevel@tonic-gate int newargc = 2;
627c478bd9Sstevel@tonic-gate
637c478bd9Sstevel@tonic-gate /*
647c478bd9Sstevel@tonic-gate * TRANSLATION_NOTE - the usage strings in the c_usgstr[] of the
657c478bd9Sstevel@tonic-gate * following structures should be given a translation; the call to gettext
667c478bd9Sstevel@tonic-gate * is in the usage() function. The strings are the ones containing
677c478bd9Sstevel@tonic-gate * "[-F FSType]".
687c478bd9Sstevel@tonic-gate */
697c478bd9Sstevel@tonic-gate
707c478bd9Sstevel@tonic-gate struct commands {
717c478bd9Sstevel@tonic-gate char *c_basename;
727c478bd9Sstevel@tonic-gate char *c_optstr;
737c478bd9Sstevel@tonic-gate char *c_usgstr[4]; /* make sure as large as largest array size */
747c478bd9Sstevel@tonic-gate } cmd_data[] = {
757c478bd9Sstevel@tonic-gate "clri", "F:o:?V",
767c478bd9Sstevel@tonic-gate {
777c478bd9Sstevel@tonic-gate "[-F FSType] [-V] special inumber ...",
787c478bd9Sstevel@tonic-gate NULL
797c478bd9Sstevel@tonic-gate },
807c478bd9Sstevel@tonic-gate "mkfs", "F:o:mb:?V",
817c478bd9Sstevel@tonic-gate {
827c478bd9Sstevel@tonic-gate "[-F FSType] [-V] [-m] [-o specific_options] special ",
837c478bd9Sstevel@tonic-gate "[operands]", NULL
847c478bd9Sstevel@tonic-gate },
857c478bd9Sstevel@tonic-gate "dcopy", "F:o:?V",
867c478bd9Sstevel@tonic-gate {
877c478bd9Sstevel@tonic-gate "[-F FSType] [-V] special inumber ...",
887c478bd9Sstevel@tonic-gate NULL
897c478bd9Sstevel@tonic-gate },
907c478bd9Sstevel@tonic-gate "fsdb", "F:o:z:?V",
917c478bd9Sstevel@tonic-gate {
927c478bd9Sstevel@tonic-gate "[-F FSType] [-V] [-o specific_options] special",
937c478bd9Sstevel@tonic-gate NULL
947c478bd9Sstevel@tonic-gate },
957c478bd9Sstevel@tonic-gate "fssnap", "F:dio:?V",
967c478bd9Sstevel@tonic-gate {
977c478bd9Sstevel@tonic-gate "[-F FSType] [-V] -o special_options /mount/point",
987c478bd9Sstevel@tonic-gate "-d [-F FSType] [-V] /mount/point | dev",
997c478bd9Sstevel@tonic-gate "-i [-F FSType] [-V] [-o special-options] [/mount/point | dev]",
1007c478bd9Sstevel@tonic-gate NULL
1017c478bd9Sstevel@tonic-gate },
1027c478bd9Sstevel@tonic-gate "labelit", "F:o:?nV",
1037c478bd9Sstevel@tonic-gate {
1047c478bd9Sstevel@tonic-gate "[-F FSType] [-V] [-o specific_options] special [operands]",
1057c478bd9Sstevel@tonic-gate NULL
1067c478bd9Sstevel@tonic-gate },
1077c478bd9Sstevel@tonic-gate NULL, "F:o:?V",
1087c478bd9Sstevel@tonic-gate {
1097c478bd9Sstevel@tonic-gate "[-F FSType] [-V] [-o specific_options] special [operands]",
1107c478bd9Sstevel@tonic-gate NULL
1117c478bd9Sstevel@tonic-gate }
1127c478bd9Sstevel@tonic-gate };
1137c478bd9Sstevel@tonic-gate struct commands *c_ptr;
1147c478bd9Sstevel@tonic-gate
11508190127Sdh static void usage(char *cmd, char **usg);
11608190127Sdh static void lookup(void);
11708190127Sdh
11808190127Sdh int
main(int argc,char * argv[])11908190127Sdh main(int argc, char *argv[])
1207c478bd9Sstevel@tonic-gate {
12108190127Sdh char *ptr;
1227c478bd9Sstevel@tonic-gate char full_path[PATH_MAX];
1237c478bd9Sstevel@tonic-gate char *vfs_path = VFS_PATH;
1247c478bd9Sstevel@tonic-gate char *alt_path = ALT_PATH;
1257c478bd9Sstevel@tonic-gate int i;
1263e1bd7a2Ssjelinek int j;
1277c478bd9Sstevel@tonic-gate int verbose = 0; /* set if -V is specified */
1287c478bd9Sstevel@tonic-gate int F_flg = 0;
1297c478bd9Sstevel@tonic-gate int mflag = 0;
1303e1bd7a2Ssjelinek int Nflag = 0;
1317c478bd9Sstevel@tonic-gate char *oopts = NULL;
1323e1bd7a2Ssjelinek char *tmpopts = NULL; /* used for in use checking */
1337c478bd9Sstevel@tonic-gate int iflag = 0;
1347c478bd9Sstevel@tonic-gate int usgflag = 0;
1357c478bd9Sstevel@tonic-gate int arg; /* argument from getopt() */
1363e1bd7a2Ssjelinek char *msg;
1373e1bd7a2Ssjelinek int error;
1387c478bd9Sstevel@tonic-gate extern char *optarg; /* getopt specific */
1397c478bd9Sstevel@tonic-gate extern int optind;
1407c478bd9Sstevel@tonic-gate extern int opterr;
1417c478bd9Sstevel@tonic-gate
1427c478bd9Sstevel@tonic-gate (void) setlocale(LC_ALL, "");
1437c478bd9Sstevel@tonic-gate
1447c478bd9Sstevel@tonic-gate #if !defined(TEXT_DOMAIN) /* Should be defined by cc -D */
1457c478bd9Sstevel@tonic-gate #define TEXT_DOMAIN "SYS_TEST" /* Use this only if it weren't */
1467c478bd9Sstevel@tonic-gate #endif
1477c478bd9Sstevel@tonic-gate
1487c478bd9Sstevel@tonic-gate (void) textdomain(TEXT_DOMAIN);
1497c478bd9Sstevel@tonic-gate
1507c478bd9Sstevel@tonic-gate cbasename = ptr = argv[0];
1517c478bd9Sstevel@tonic-gate while (*ptr) {
1527c478bd9Sstevel@tonic-gate if (*ptr++ == '/')
1537c478bd9Sstevel@tonic-gate cbasename = ptr;
1547c478bd9Sstevel@tonic-gate }
1557c478bd9Sstevel@tonic-gate
1567c478bd9Sstevel@tonic-gate
1577c478bd9Sstevel@tonic-gate if (argc == 1) {
1587c478bd9Sstevel@tonic-gate for (c_ptr = cmd_data; ((c_ptr->c_basename != NULL) &&
1597c478bd9Sstevel@tonic-gate (strcmp(c_ptr->c_basename, cbasename) != 0)); c_ptr++)
1607c478bd9Sstevel@tonic-gate ;
1617c478bd9Sstevel@tonic-gate usage(cbasename, c_ptr->c_usgstr);
1627c478bd9Sstevel@tonic-gate exit(2);
1637c478bd9Sstevel@tonic-gate }
1647c478bd9Sstevel@tonic-gate
1657c478bd9Sstevel@tonic-gate for (c_ptr = cmd_data; ((c_ptr->c_basename != NULL) &&
1667c478bd9Sstevel@tonic-gate (strcmp(c_ptr->c_basename, cbasename) != 0)); c_ptr++)
1677c478bd9Sstevel@tonic-gate ;
1687c478bd9Sstevel@tonic-gate while ((arg = getopt(argc, argv, c_ptr->c_optstr)) != -1) {
1697c478bd9Sstevel@tonic-gate switch (arg) {
1707c478bd9Sstevel@tonic-gate case 'V': /* echo complete command line */
1717c478bd9Sstevel@tonic-gate verbose = 1;
1727c478bd9Sstevel@tonic-gate break;
1737c478bd9Sstevel@tonic-gate case 'F': /* FSType specified */
1747c478bd9Sstevel@tonic-gate F_flg++;
1757c478bd9Sstevel@tonic-gate fstype = optarg;
1767c478bd9Sstevel@tonic-gate break;
1777c478bd9Sstevel@tonic-gate case 'o': /* FSType specific arguments */
1787c478bd9Sstevel@tonic-gate newargv[newargc++] = "-o";
1797c478bd9Sstevel@tonic-gate newargv[newargc++] = optarg;
1807c478bd9Sstevel@tonic-gate oopts = optarg;
1813e1bd7a2Ssjelinek if (!Nflag) {
1823e1bd7a2Ssjelinek tmpopts = optarg;
1833e1bd7a2Ssjelinek Nflag = has_Nflag(tmpopts);
1843e1bd7a2Ssjelinek }
1857c478bd9Sstevel@tonic-gate break;
1867c478bd9Sstevel@tonic-gate case '?': /* print usage message */
1877c478bd9Sstevel@tonic-gate newargv[newargc++] = "-?";
1887c478bd9Sstevel@tonic-gate usgflag = 1;
1897c478bd9Sstevel@tonic-gate break;
1907c478bd9Sstevel@tonic-gate case 'm': /* FSType specific arguments */
1917c478bd9Sstevel@tonic-gate mflag = 1;
1927c478bd9Sstevel@tonic-gate newargv[newargc] = (char *)malloc(3);
1937c478bd9Sstevel@tonic-gate sprintf(newargv[newargc++], "-%c", arg);
1947c478bd9Sstevel@tonic-gate if (optarg)
1957c478bd9Sstevel@tonic-gate newargv[newargc++] = optarg;
1967c478bd9Sstevel@tonic-gate break;
1977c478bd9Sstevel@tonic-gate case 'i': /* fssnap only */
1987c478bd9Sstevel@tonic-gate iflag = 1;
1997c478bd9Sstevel@tonic-gate /*FALLTHROUGH*/
2007c478bd9Sstevel@tonic-gate default:
2017c478bd9Sstevel@tonic-gate newargv[newargc] = (char *)malloc(3);
2027c478bd9Sstevel@tonic-gate sprintf(newargv[newargc++], "-%c", arg);
2037c478bd9Sstevel@tonic-gate if (optarg)
2047c478bd9Sstevel@tonic-gate newargv[newargc++] = optarg;
2057c478bd9Sstevel@tonic-gate break;
2067c478bd9Sstevel@tonic-gate }
2077c478bd9Sstevel@tonic-gate optarg = NULL;
2087c478bd9Sstevel@tonic-gate }
2097c478bd9Sstevel@tonic-gate if (F_flg > 1) {
2103e1bd7a2Ssjelinek (void) fprintf(stderr,
2113e1bd7a2Ssjelinek gettext("%s: more than one FSType specified\n"),
2123e1bd7a2Ssjelinek cbasename);
2137c478bd9Sstevel@tonic-gate usage(cbasename, c_ptr->c_usgstr);
2147c478bd9Sstevel@tonic-gate exit(2);
2157c478bd9Sstevel@tonic-gate }
2167c478bd9Sstevel@tonic-gate if (fstype != NULL) {
2177c478bd9Sstevel@tonic-gate if (strlen(fstype) > FSTYPE_MAX) {
2183e1bd7a2Ssjelinek (void) fprintf(stderr,
2197c478bd9Sstevel@tonic-gate gettext("%s: FSType %s exceeds %d characters\n"),
2207c478bd9Sstevel@tonic-gate cbasename, fstype, FSTYPE_MAX);
2217c478bd9Sstevel@tonic-gate exit(2);
2227c478bd9Sstevel@tonic-gate }
2237c478bd9Sstevel@tonic-gate }
2247c478bd9Sstevel@tonic-gate
2257c478bd9Sstevel@tonic-gate /* perform a lookup if fstype is not specified */
2267c478bd9Sstevel@tonic-gate special = argv[optind];
2277c478bd9Sstevel@tonic-gate optind++;
2287c478bd9Sstevel@tonic-gate
2297c478bd9Sstevel@tonic-gate /* handle -i (fssnap command only) */
2307c478bd9Sstevel@tonic-gate if (iflag) {
2317c478bd9Sstevel@tonic-gate int diff = argc - optind;
2327c478bd9Sstevel@tonic-gate /*
2337c478bd9Sstevel@tonic-gate * There is no reason to ever call a file system specific
2347c478bd9Sstevel@tonic-gate * version since its all in kstats.
2357c478bd9Sstevel@tonic-gate */
2367c478bd9Sstevel@tonic-gate if (diff > 0) /* gave more than one mountpoint or device */
2377c478bd9Sstevel@tonic-gate usage(cbasename, c_ptr->c_usgstr);
2387c478bd9Sstevel@tonic-gate stat_snap(cbasename, diff == 0 ? argv[argc-1] : NULL, oopts);
2397c478bd9Sstevel@tonic-gate exit(0);
2407c478bd9Sstevel@tonic-gate }
2417c478bd9Sstevel@tonic-gate
2427c478bd9Sstevel@tonic-gate if ((special == NULL) && (!usgflag)) {
2433e1bd7a2Ssjelinek (void) fprintf(stderr, gettext("%s: special not specified\n"),
2447c478bd9Sstevel@tonic-gate cbasename);
2457c478bd9Sstevel@tonic-gate usage(cbasename, c_ptr->c_usgstr);
2467c478bd9Sstevel@tonic-gate exit(2);
2477c478bd9Sstevel@tonic-gate }
2483e1bd7a2Ssjelinek
2497c478bd9Sstevel@tonic-gate if ((fstype == NULL) && (usgflag))
2507c478bd9Sstevel@tonic-gate usage(cbasename, c_ptr->c_usgstr);
2517c478bd9Sstevel@tonic-gate if (fstype == NULL)
2527c478bd9Sstevel@tonic-gate lookup();
2537c478bd9Sstevel@tonic-gate if (fstype == NULL) {
2543e1bd7a2Ssjelinek (void) fprintf(stderr,
2553e1bd7a2Ssjelinek gettext("%s: FSType cannot be identified\n"), cbasename);
2567c478bd9Sstevel@tonic-gate usage(cbasename, c_ptr->c_usgstr);
2577c478bd9Sstevel@tonic-gate exit(2);
2587c478bd9Sstevel@tonic-gate }
2597c478bd9Sstevel@tonic-gate newargv[newargc++] = special;
2607c478bd9Sstevel@tonic-gate for (; optind < argc; optind++)
2617c478bd9Sstevel@tonic-gate newargv[newargc++] = argv[optind];
2627c478bd9Sstevel@tonic-gate
2637c478bd9Sstevel@tonic-gate /* build the full pathname of the fstype dependent command */
2647c478bd9Sstevel@tonic-gate sprintf(full_path, "%s/%s/%s", vfs_path, fstype, cbasename);
2657c478bd9Sstevel@tonic-gate
2667c478bd9Sstevel@tonic-gate newargv[1] = cbasename;
2677c478bd9Sstevel@tonic-gate
2687c478bd9Sstevel@tonic-gate if (verbose) {
2697c478bd9Sstevel@tonic-gate printf("%s -F %s ", cbasename, fstype);
2707c478bd9Sstevel@tonic-gate for (i = 2; newargv[i]; i++)
2717c478bd9Sstevel@tonic-gate printf("%s ", newargv[i]);
2727c478bd9Sstevel@tonic-gate printf("\n");
2737c478bd9Sstevel@tonic-gate exit(0);
2747c478bd9Sstevel@tonic-gate }
2757c478bd9Sstevel@tonic-gate
2763e1bd7a2Ssjelinek /*
2773e1bd7a2Ssjelinek * Prior to executing the command for mkfs check for device in use.
2783e1bd7a2Ssjelinek * If the mflag is set, user wants to see command that created
2793e1bd7a2Ssjelinek * an already existing filesystem. Do not check for in use in this
2803e1bd7a2Ssjelinek * case. If Nflag is set user wants to see what the parameters
2813e1bd7a2Ssjelinek * would be to create the filesystem. Do not check for in use in
2823e1bd7a2Ssjelinek * this case.
2833e1bd7a2Ssjelinek */
2843e1bd7a2Ssjelinek if (strcmp(cbasename, "mkfs") == 0 && !mflag && !Nflag) {
2853e1bd7a2Ssjelinek if (dm_inuse(special, &msg, DM_WHO_MKFS, &error) ||
2863e1bd7a2Ssjelinek error) {
2873e1bd7a2Ssjelinek if (error != 0) {
2883e1bd7a2Ssjelinek (void) fprintf(stderr, gettext("Error occurred"
2893e1bd7a2Ssjelinek " with device in use checking: %s\n"),
2903e1bd7a2Ssjelinek strerror(error));
2913e1bd7a2Ssjelinek } else {
2923e1bd7a2Ssjelinek (void) fprintf(stderr, "%s", msg);
2933e1bd7a2Ssjelinek free(msg);
2943e1bd7a2Ssjelinek exit(2);
2953e1bd7a2Ssjelinek }
2963e1bd7a2Ssjelinek }
2973e1bd7a2Ssjelinek }
2983e1bd7a2Ssjelinek
2997c478bd9Sstevel@tonic-gate /*
3007c478bd9Sstevel@tonic-gate * Execute the FSType specific command.
3017c478bd9Sstevel@tonic-gate */
3027c478bd9Sstevel@tonic-gate execv(full_path, &newargv[1]);
3037c478bd9Sstevel@tonic-gate if ((errno == ENOENT) || (errno == EACCES)) {
3047c478bd9Sstevel@tonic-gate /* build the alternate pathname */
3057c478bd9Sstevel@tonic-gate sprintf(full_path, "%s/%s/%s", alt_path, fstype, cbasename);
3067c478bd9Sstevel@tonic-gate if (verbose) {
3077c478bd9Sstevel@tonic-gate printf("%s -F %s ", cbasename, fstype);
3087c478bd9Sstevel@tonic-gate for (i = 2; newargv[i]; i++)
3097c478bd9Sstevel@tonic-gate printf("%s ", newargv[i]);
3107c478bd9Sstevel@tonic-gate printf("\n");
3117c478bd9Sstevel@tonic-gate exit(0);
3127c478bd9Sstevel@tonic-gate }
3137c478bd9Sstevel@tonic-gate execv(full_path, &newargv[1]);
3147c478bd9Sstevel@tonic-gate }
3157c478bd9Sstevel@tonic-gate if (errno == ENOEXEC) {
3167c478bd9Sstevel@tonic-gate newargv[0] = "sh";
3177c478bd9Sstevel@tonic-gate newargv[1] = full_path;
3187c478bd9Sstevel@tonic-gate execv("/sbin/sh", &newargv[0]);
3197c478bd9Sstevel@tonic-gate }
3207c478bd9Sstevel@tonic-gate if (errno != ENOENT) {
3217c478bd9Sstevel@tonic-gate perror(cbasename);
3223e1bd7a2Ssjelinek (void) fprintf(stderr, gettext("%s: cannot execute %s\n"),
3233e1bd7a2Ssjelinek cbasename, full_path);
3247c478bd9Sstevel@tonic-gate exit(2);
3257c478bd9Sstevel@tonic-gate }
3267c478bd9Sstevel@tonic-gate
3277c478bd9Sstevel@tonic-gate if (sysfs(GETFSIND, fstype) == (-1)) {
3283e1bd7a2Ssjelinek (void) fprintf(stderr,
3297c478bd9Sstevel@tonic-gate gettext("%s: FSType %s not installed in the kernel\n"),
3307c478bd9Sstevel@tonic-gate cbasename, fstype);
3317c478bd9Sstevel@tonic-gate exit(2);
3327c478bd9Sstevel@tonic-gate }
3333e1bd7a2Ssjelinek (void) fprintf(stderr,
3347c478bd9Sstevel@tonic-gate gettext("%s: Operation not applicable for FSType %s \n"),
3357c478bd9Sstevel@tonic-gate cbasename, fstype);
33608190127Sdh return (2);
3377c478bd9Sstevel@tonic-gate }
3387c478bd9Sstevel@tonic-gate
33908190127Sdh static void
usage(char * cmd,char ** usg)34008190127Sdh usage(char *cmd, char **usg)
3417c478bd9Sstevel@tonic-gate {
3427c478bd9Sstevel@tonic-gate int i;
3433e1bd7a2Ssjelinek (void) fprintf(stderr, gettext("Usage:\n"));
3447c478bd9Sstevel@tonic-gate for (i = 0; usg[i] != NULL; i++)
3453e1bd7a2Ssjelinek (void) fprintf(stderr, "%s %s\n", gettext(cmd),
3463e1bd7a2Ssjelinek gettext(usg[i]));
3477c478bd9Sstevel@tonic-gate exit(2);
3487c478bd9Sstevel@tonic-gate }
3497c478bd9Sstevel@tonic-gate
3507c478bd9Sstevel@tonic-gate
3517c478bd9Sstevel@tonic-gate /*
3527c478bd9Sstevel@tonic-gate * This looks up the /etc/vfstab entry given the device 'special'.
3537c478bd9Sstevel@tonic-gate * It is called when the fstype is not specified on the command line.
3547c478bd9Sstevel@tonic-gate *
3557c478bd9Sstevel@tonic-gate * The following global variables are used:
3567c478bd9Sstevel@tonic-gate * special, fstype
3577c478bd9Sstevel@tonic-gate */
3587c478bd9Sstevel@tonic-gate
35908190127Sdh static void
lookup(void)36008190127Sdh lookup(void)
3617c478bd9Sstevel@tonic-gate {
3627c478bd9Sstevel@tonic-gate FILE *fd;
3637c478bd9Sstevel@tonic-gate int ret;
3647c478bd9Sstevel@tonic-gate struct vfstab vget, vref;
3657c478bd9Sstevel@tonic-gate
3667c478bd9Sstevel@tonic-gate if ((fd = fopen(vfstab, "r")) == NULL) {
3673e1bd7a2Ssjelinek (void) fprintf(stderr, gettext("%s: cannot open vfstab\n"),
3683e1bd7a2Ssjelinek cbasename);
3697c478bd9Sstevel@tonic-gate exit(1);
3707c478bd9Sstevel@tonic-gate }
3717c478bd9Sstevel@tonic-gate vfsnull(&vref);
3727c478bd9Sstevel@tonic-gate vref.vfs_special = special;
3737c478bd9Sstevel@tonic-gate ret = getvfsany(fd, &vget, &vref);
3747c478bd9Sstevel@tonic-gate if (ret == -1) {
3757c478bd9Sstevel@tonic-gate rewind(fd);
3767c478bd9Sstevel@tonic-gate vfsnull(&vref);
3777c478bd9Sstevel@tonic-gate vref.vfs_fsckdev = special;
3787c478bd9Sstevel@tonic-gate ret = getvfsany(fd, &vget, &vref);
3797c478bd9Sstevel@tonic-gate }
3807c478bd9Sstevel@tonic-gate fclose(fd);
3817c478bd9Sstevel@tonic-gate
3827c478bd9Sstevel@tonic-gate switch (ret) {
3837c478bd9Sstevel@tonic-gate case -1:
3847c478bd9Sstevel@tonic-gate fstype = default_fstype(special);
3857c478bd9Sstevel@tonic-gate break;
3867c478bd9Sstevel@tonic-gate case 0:
3877c478bd9Sstevel@tonic-gate fstype = vget.vfs_fstype;
3887c478bd9Sstevel@tonic-gate break;
3897c478bd9Sstevel@tonic-gate case VFS_TOOLONG:
3903e1bd7a2Ssjelinek (void) fprintf(stderr,
3917c478bd9Sstevel@tonic-gate gettext("%s: line in vfstab exceeds %d characters\n"),
3927c478bd9Sstevel@tonic-gate cbasename, VFS_LINE_MAX-2);
3937c478bd9Sstevel@tonic-gate exit(1);
3947c478bd9Sstevel@tonic-gate break;
3957c478bd9Sstevel@tonic-gate case VFS_TOOFEW:
3963e1bd7a2Ssjelinek (void) fprintf(stderr,
3977c478bd9Sstevel@tonic-gate gettext("%s: line in vfstab has too few entries\n"),
3987c478bd9Sstevel@tonic-gate cbasename);
3997c478bd9Sstevel@tonic-gate exit(1);
4007c478bd9Sstevel@tonic-gate break;
4017c478bd9Sstevel@tonic-gate }
4027c478bd9Sstevel@tonic-gate }
4037c478bd9Sstevel@tonic-gate
4047c478bd9Sstevel@tonic-gate void
stat_snap(char * cmd,char * mountpoint,char * opts)40508190127Sdh stat_snap(char *cmd, char *mountpoint, char *opts)
4067c478bd9Sstevel@tonic-gate {
4077c478bd9Sstevel@tonic-gate int fd; /* check mount point if given */
4087c478bd9Sstevel@tonic-gate int en;
4097c478bd9Sstevel@tonic-gate char *errstr;
4107c478bd9Sstevel@tonic-gate
4117c478bd9Sstevel@tonic-gate if (mountpoint) {
4127c478bd9Sstevel@tonic-gate if ((fd = open(mountpoint, O_RDONLY)) < 0) {
4137c478bd9Sstevel@tonic-gate en = errno;
4147c478bd9Sstevel@tonic-gate errstr = strerror(errno);
4157c478bd9Sstevel@tonic-gate if (errstr == NULL)
4167c478bd9Sstevel@tonic-gate errstr = gettext("Unknown error");
4177c478bd9Sstevel@tonic-gate
4183e1bd7a2Ssjelinek (void) fprintf(stderr,
4193e1bd7a2Ssjelinek gettext("%s: %s: error %d: %s\n"),
4203e1bd7a2Ssjelinek cmd, mountpoint, en, errstr);
4217c478bd9Sstevel@tonic-gate
4227c478bd9Sstevel@tonic-gate exit(2);
4237c478bd9Sstevel@tonic-gate }
4247c478bd9Sstevel@tonic-gate close(fd);
4257c478bd9Sstevel@tonic-gate }
4267c478bd9Sstevel@tonic-gate fssnap_show_status(mountpoint, opts, 1, (opts ? 0 : 1));
4277c478bd9Sstevel@tonic-gate }
4283e1bd7a2Ssjelinek static int
has_Nflag(char * opts)4293e1bd7a2Ssjelinek has_Nflag(char *opts)
4303e1bd7a2Ssjelinek {
4313e1bd7a2Ssjelinek while (opts != NULL && *opts != '\0') {
4323e1bd7a2Ssjelinek if (match(&opts, "N")) {
4333e1bd7a2Ssjelinek return (1);
4343e1bd7a2Ssjelinek }
4353e1bd7a2Ssjelinek if (!opts)
4363e1bd7a2Ssjelinek break;
4373e1bd7a2Ssjelinek if (*opts == ',')
4383e1bd7a2Ssjelinek opts ++;
4393e1bd7a2Ssjelinek if (*opts == ' ')
4403e1bd7a2Ssjelinek opts ++;
4413e1bd7a2Ssjelinek }
4423e1bd7a2Ssjelinek return (0);
4433e1bd7a2Ssjelinek }
4443e1bd7a2Ssjelinek /*
4453e1bd7a2Ssjelinek * Parses the -o [fs specific options string] to search for the UFS -N flag.
4463e1bd7a2Ssjelinek * Return the opts string pointing to the next position in the string if
4473e1bd7a2Ssjelinek * match is not found. A delimiter of , or ' ' can be used depending on the
4483e1bd7a2Ssjelinek * caller, newfs or mkfs.
4493e1bd7a2Ssjelinek */
4503e1bd7a2Ssjelinek static int
match(char ** opts,char * s)4513e1bd7a2Ssjelinek match(char **opts, char *s)
4523e1bd7a2Ssjelinek {
4533e1bd7a2Ssjelinek char *cs;
4543e1bd7a2Ssjelinek char *tmp_str;
4553e1bd7a2Ssjelinek
4563e1bd7a2Ssjelinek cs = *opts;
4573e1bd7a2Ssjelinek
4583e1bd7a2Ssjelinek while (*cs++ == *s) {
4593e1bd7a2Ssjelinek if (*s++ == '\0') {
4603e1bd7a2Ssjelinek goto true;
4613e1bd7a2Ssjelinek }
4623e1bd7a2Ssjelinek }
4633e1bd7a2Ssjelinek if (*s != '\0') {
4643e1bd7a2Ssjelinek /*
4653e1bd7a2Ssjelinek * If we cannot find the delimiter it means we
4663e1bd7a2Ssjelinek * have hit the end of the string.
4673e1bd7a2Ssjelinek */
4683e1bd7a2Ssjelinek tmp_str = strchr(*opts, ',');
4693e1bd7a2Ssjelinek if (!tmp_str)
4703e1bd7a2Ssjelinek tmp_str = strchr(*opts, ' ');
4713e1bd7a2Ssjelinek
4723e1bd7a2Ssjelinek *opts = tmp_str;
4733e1bd7a2Ssjelinek return (0);
4743e1bd7a2Ssjelinek }
4753e1bd7a2Ssjelinek true:
4763e1bd7a2Ssjelinek cs--;
4773e1bd7a2Ssjelinek *opts = cs;
4783e1bd7a2Ssjelinek return (1);
4793e1bd7a2Ssjelinek }
480