8000 minix/minix/commands/format/format.c at R3.3.0 · zmdo/minix · GitHub
[go: up one dir, main page]

Skip to content

Latest commit

 

History

History
421 lines (362 loc) · 10.8 KB

format.c

File metadata and controls

421 lines (362 loc) · 10.8 KB
}
int main(int argc, char **argv)
{
char *device;
unsigned drive;
unsigned type;
unsigned media_size;
unsigned drive_size;
int verify= 0;
struct stat st0, st;
char special[PATH_MAX + 1], mounted_on[PATH_MAX + 1];
char version[MNTNAMELEN], rw_flag[MNTFLAGLEN];
/* Option -v. */
while (argc > 1 && argv[1][0] == '-') {
char *p;
for (p= argv[1]; *p == '-' || *p == 'v'; p++) {
if (*p == 'v') verify= 1;
}
if (*p != 0) usage();
argc--;
argv++;
if (strcmp(argv[0], "--") == 0) break;
}
if (argc < 2 || argc > 4) usage();
/* Check if the caller has read-write permission. Use the access()
* call to check with the real uid & gid. This program is usually
* set-uid root.
*/
device= argv[1];
if (stat(device, &st0) < 0
|| access(device, R_OK|W_OK) < 0
|| stat(device, &st) < 0
|| (errno= EACCES, 0) /* set errno for following tests */
|| st.st_dev != st0.st_dev
|| st.st_ino != st0.st_ino
) {
fatal(device);
}
if (!S_ISBLK(st.st_mode) || !isfloppy(st.st_rdev)) {
fprintf(stderr, "format: %s: not a floppy device\n", device);
exit(1);
}
drive= fl_drive(st.st_rdev);
type= fl_type(st.st_rdev);
/* The drive should not be mounted. */
if (load_mtab("mkfs") < 0) exit(1);
while (get_mtab_entry(special, mounted_on, version, rw_flag) == 0) {
if (stat(special, &st) >= 0 && isfloppy(st.st_rdev)
&& fl_drive(st.st_rdev) == drive) {
fprintf(stderr, "format: %s is mounted on %s\n",
device, mounted_on);
exit(1);
}
}
if (isflauto(type)) {
/* Auto type 0 requires size(s). */
unsigned long lmedia, ldrive;
char *end;
if (argc < 3) {
fprintf(stderr,
"format: no size specified for auto floppy device %s\n",
device);
usage();
}
lmedia= strtoul(argv[2], &end, 10);
if (end == argv[2] || *end != 0 || lmedia > 20 * 1024)
usage();
if (argc == 4) {
ldrive= strtoul(argv[3], &end, 10);
if (end == argv[3] || *end != 0 || ldrive > 20 * 1024)
usage();
} else {
ldrive= lmedia;
}
/* Silently correct wrong ordered sizes. */
if (lmedia > ldrive) {
media_size= ldrive;
drive_size= lmedia;
} else {
media_size= lmedia;
drive_size= ldrive;
}
/* A 1.44M drive can do 720k diskettes with no extra tricks.
* Diddle with the 720k params so it is found.
*/
if (media_size == 720 && drive_size == 1440)
parameters[4 - 1].drive_size= 1440;
/* Translate the auto type to a known type. */
for (type= 1; type <= NR_TYPES; type++) {
if (parameters[type - 1].media_size == media_size
&& parameters[type - 1].drive_size == drive_size
) break;
}
if (!isfltyped(type)) {
fprintf(stderr,
"format: can't format a %uk floppy in a %uk drive\n",
media_size, drive_size);
exit(1);
}
} else
if (isfltyped(type)) {
/* No sizes needed for a non-auto type. */
if (argc > 2) {
fprintf(stderr,
"format: no sizes need to be specified for non-auto floppy device %s\n",
device);
usage();
}
} else
if (isflpart(type)) {
fprintf(stderr,
"format: floppy partition %s can't be formatted\n",
device);
exit(1);
} else {
fprintf(stderr,
"format: %s: can't format strange type %d\n",
device, type);
}
format_device(drive, type, verify);
exit(0);
}
0