1 /* fdformat.c - Low-level formats a floppy disk - Werner Almesberger */
3 /* 1999-02-22 Arkadiusz Mi¶kiewicz <misiek@pld.ORG.PL>
4 * - added Native Language Support
5 * 1999-03-20 Arnaldo Carvalho de Melo <acme@conectiva.com.br>
6 & - more i18n/nls translatable strings marked
15 #include <sys/ioctl.h>
23 struct floppy_struct param
;
25 #define SECTOR_SIZE 512
27 static void format_disk(int ctrl
)
29 struct format_descr descr
;
32 printf(_("Formatting ... "));
34 if (ioctl(ctrl
, FDFMTBEG
, NULL
) < 0)
35 err(EXIT_FAILURE
, "\nioctl(FDFMTBEG)");
36 for (track
= 0; track
< param
.track
; track
++) {
39 if (ioctl(ctrl
, FDFMTTRK
, (long)&descr
) < 0)
40 err(EXIT_FAILURE
, "\nioctl(FDFMTTRK)");
42 printf("%3d\b\b\b", track
);
44 if (param
.head
== 2) {
46 if (ioctl(ctrl
, FDFMTTRK
, (long)&descr
) < 0)
47 err(EXIT_FAILURE
, "\nioctl(FDFMTTRK)");
50 if (ioctl(ctrl
, FDFMTEND
, NULL
) < 0)
51 err(EXIT_FAILURE
, "\nioctl(FDFMTEND)");
55 static void verify_disk(char *name
)
59 int fd
, cyl_size
, count
;
61 cyl_size
= param
.sect
* param
.head
* 512;
62 data
= xmalloc(cyl_size
);
63 printf(_("Verifying ... "));
65 if ((fd
= open(name
, O_RDONLY
)) < 0)
66 err(EXIT_FAILURE
, _("cannot open file %s"), name
);
67 for (cyl
= 0; cyl
< param
.track
; cyl
++) {
70 printf("%3d\b\b\b", cyl
);
72 read_bytes
= read(fd
, data
, cyl_size
);
73 if (read_bytes
!= cyl_size
) {
77 _("Problem reading cylinder %d,"
78 " expected %d, read %d\n"),
79 cyl
, cyl_size
, read_bytes
);
83 for (count
= 0; count
< cyl_size
; count
++)
84 if (data
[count
] != FD_FILL_BYTE
) {
85 printf(_("bad data in cyl %d\n"
86 "Continuing ... "), cyl
);
94 err(EXIT_FAILURE
, "close");
97 static void __attribute__ ((__noreturn__
)) usage(FILE * out
)
99 fprintf(out
, _("Usage: %s [options] device\n"),
100 program_invocation_short_name
);
102 fprintf(out
, _("\nOptions:\n"
103 " -n, --no-verify disable the verification after the format\n"
104 " -V, --version output version information and exit\n"
105 " -h, --help display this help and exit\n\n"));
107 exit(out
== stderr
? EXIT_FAILURE
: EXIT_SUCCESS
);
110 int main(int argc
, char **argv
)
117 static const struct option longopts
[] = {
118 {"no-verify", no_argument
, NULL
, 'n'},
119 {"version", no_argument
, NULL
, 'V'},
120 {"help", no_argument
, NULL
, 'h'},
124 setlocale(LC_ALL
, "");
125 bindtextdomain(PACKAGE
, LOCALEDIR
);
128 while ((ch
= getopt_long(argc
, argv
, "nVh", longopts
, NULL
)) != -1)
134 printf(_("%s from %s\n"), program_invocation_short_name
,
148 if (stat(argv
[0], &st
) < 0)
149 err(EXIT_FAILURE
, _("cannot stat file %s"), argv
[0]);
150 if (!S_ISBLK(st
.st_mode
))
151 /* do not test major - perhaps this was an USB floppy */
152 errx(EXIT_FAILURE
, _("%s: not a block device"), argv
[0]);
153 if (access(argv
[0], W_OK
) < 0)
154 err(EXIT_FAILURE
, _("cannot access file %s"), argv
[0]);
156 ctrl
= open(argv
[0], O_WRONLY
);
158 err(EXIT_FAILURE
, _("cannot open file %s"), argv
[0]);
159 if (ioctl(ctrl
, FDGETPRM
, (long)¶m
) < 0)
160 err(EXIT_FAILURE
, _("Could not determine current format type"));
162 printf(_("%s-sided, %d tracks, %d sec/track. Total capacity %d kB.\n"),
163 (param
.head
== 2) ? _("Double") : _("Single"),
164 param
.track
, param
.sect
, param
.size
>> 1);
169 verify_disk(argv
[0]);