- } else
- fso = -1;
- fsi = open(special, O_RDONLY);
- if (fsi < 0)
- fatal("%s: %s", special, strerror(errno));
- if (fstat(fsi, &st) < 0)
- fatal("%s: %s", special, strerror(errno));
- if ((st.st_mode & S_IFMT) != S_IFCHR)
- printf("%s: not a character device\n", special);
- cp = index(argv[0], '\0') - 1;
- if (cp == 0 || (*cp < 'a' || *cp > 'h') && !isdigit(*cp))
- fatal("%s: can't figure out file system partition", argv[0]);
+
+ /* Bail if target special is mounted */
+ n = getmntinfo(&mp, MNT_NOWAIT);
+ if (n == 0)
+ fatal("%s: getmntinfo: %s", special, strerror(errno));
+
+ len = sizeof(_PATH_DEV) - 1;
+ s1 = special;
+ if (strncmp(_PATH_DEV, s1, len) == 0)
+ s1 += len;
+
+ while (--n >= 0) {
+ s2 = mp->f_mntfromname;
+ if (strncmp(_PATH_DEV, s2, len) == 0) {
+ s2 += len - 1;
+ *s2 = 'r';
+ }
+ if (strcmp(s1, s2) == 0 || strcmp(s1, &s2[1]) == 0)
+ fatal("%s is mounted on %s",
+ special, mp->f_mntonname);
+ ++mp;
+ }
+ }
+ if (mfs && disktype != NULL) {
+ lp = (struct disklabel *)getdiskbyname(disktype);
+ if (lp == NULL)
+ fatal("%s: unknown disk type", disktype);
+ pp = &lp->d_partitions[1];
+ } else {
+ fsi = open(special, O_RDONLY);
+ if (fsi < 0)
+ fatal("%s: %s", special, strerror(errno));
+ if (fstat(fsi, &st) < 0)
+ fatal("%s: %s", special, strerror(errno));
+ if ((st.st_mode & S_IFMT) != S_IFCHR && !mfs)
+ printf("%s: %s: not a character-special device\n",
+ progname, special);
+ cp = strchr(argv[0], '\0') - 1;
+ if (cp == 0 || (*cp < 'a' || *cp > 'h') && !isdigit(*cp))
+ fatal("%s: can't figure out file system partition",
+ argv[0]);