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
2
* fdformat.c - Low-level formats a floppy disk - Werner Almesberger
10
#include <sys/ioctl.h>
15
11
#include <sys/stat.h>
16
#include <sys/ioctl.h>
21
18
struct floppy_struct param;
23
20
#define SECTOR_SIZE 512
24
#define PERROR(msg) { perror(msg); exit(1); }
26
static void format_disk(int ctrl, char *name)
22
static void format_disk(int ctrl)
28
struct format_descr descr;
31
printf(_("Formatting ... "));
33
if (ioctl(ctrl,FDFMTBEG,NULL) < 0) PERROR("\nioctl(FDFMTBEG)");
34
for (track = 0; track < param.track; track++) {
37
if (ioctl(ctrl,FDFMTTRK,(long) &descr) < 0)
38
PERROR("\nioctl(FDFMTTRK)");
40
printf("%3d\b\b\b",track);
24
struct format_descr descr;
27
printf(_("Formatting ... "));
42
if (param.head == 2) {
44
if (ioctl(ctrl,FDFMTTRK,(long) &descr) < 0)
45
PERROR("\nioctl(FDFMTTRK)");
29
if (ioctl(ctrl, FDFMTBEG, NULL) < 0)
30
err(EXIT_FAILURE, "ioctl: FDFMTBEG");
31
for (track = 0; track < param.track; track++) {
34
if (ioctl(ctrl, FDFMTTRK, (long) &descr) < 0)
35
err(EXIT_FAILURE, "ioctl: FDFMTTRK");
37
printf("%3d\b\b\b", track);
39
if (param.head == 2) {
41
if (ioctl(ctrl, FDFMTTRK, (long)&descr) < 0)
42
err(EXIT_FAILURE, "ioctl: FDFMTTRK");
48
if (ioctl(ctrl,FDFMTEND,NULL) < 0) PERROR("\nioctl(FDFMTEND)");
45
if (ioctl(ctrl, FDFMTEND, NULL) < 0)
46
err(EXIT_FAILURE, "ioctl: FDFMTEND");
53
50
static void verify_disk(char *name)
56
int fd,cyl_size,cyl,count;
58
cyl_size = param.sect*param.head*512;
59
if ((data = (unsigned char *) malloc(cyl_size)) == NULL) PERROR("malloc");
60
printf(_("Verifying ... "));
62
if ((fd = open(name,O_RDONLY)) < 0) PERROR(name);
63
for (cyl = 0; cyl < param.track; cyl++) {
66
printf("%3d\b\b\b",cyl);
54
int fd, cyl_size, count;
56
cyl_size = param.sect * param.head * 512;
57
data = xmalloc(cyl_size);
58
printf(_("Verifying ... "));
68
read_bytes = read(fd,data,cyl_size);
69
if(read_bytes != cyl_size) {
73
_("Problem reading cylinder %d, expected %d, read %d\n"),
74
cyl, cyl_size, read_bytes);
60
if ((fd = open(name, O_RDONLY)) < 0)
61
err(EXIT_FAILURE, _("cannot open file %s"), name);
62
for (cyl = 0; cyl < param.track; cyl++) {
65
printf("%3d\b\b\b", cyl);
67
read_bytes = read(fd, data, cyl_size);
68
if (read_bytes != cyl_size) {
72
_("Problem reading cylinder %d,"
73
" expected %d, read %d\n"),
74
cyl, cyl_size, read_bytes);
78
for (count = 0; count < cyl_size; count++)
79
if (data[count] != FD_FILL_BYTE) {
80
printf(_("bad data in cyl %d\n"
81
"Continuing ... "), cyl);
78
for (count = 0; count < cyl_size; count++)
79
if (data[count] != FD_FILL_BYTE) {
80
printf(_("bad data in cyl %d\nContinuing ... "),cyl);
87
if (close(fd) < 0) PERROR("close");
91
static void usage(char *name)
95
if ((this = strrchr(name,'/')) != NULL) name = this+1;
96
fprintf(stderr,_("usage: %s [ -n ] device\n"),name);
101
int main(int argc,char **argv)
109
if ((p = strrchr(progname, '/')) != NULL)
112
setlocale(LC_ALL, "");
113
bindtextdomain(PACKAGE, LOCALEDIR);
117
(!strcmp(argv[1], "-V") || !strcmp(argv[1], "--version"))) {
118
printf(_("%s (%s)\n"), progname, PACKAGE_STRING);
123
if (argc > 1 && argv[1][0] == '-') {
124
if (argv[1][1] != 'n') usage(progname);
129
if (argc != 2) usage(progname);
130
if (stat(argv[1],&st) < 0) PERROR(argv[1]);
131
if (!S_ISBLK(st.st_mode)) {
132
fprintf(stderr,_("%s: not a block device\n"),argv[1]);
134
/* do not test major - perhaps this was an USB floppy */
136
if (access(argv[1],W_OK) < 0) PERROR(argv[1]);
138
ctrl = open(argv[1],O_WRONLY);
141
if (ioctl(ctrl,FDGETPRM,(long) ¶m) < 0)
142
PERROR(_("Could not determine current format type"));
143
printf(_("%s-sided, %d tracks, %d sec/track. Total capacity %d kB.\n"),
144
(param.head == 2) ? _("Double") : _("Single"),
145
param.track, param.sect,param.size >> 1);
146
format_disk(ctrl, argv[1]);
150
verify_disk(argv[1]);
89
err(EXIT_FAILURE, "close");
92
static void __attribute__ ((__noreturn__)) usage(FILE * out)
94
fprintf(out, _("Usage: %s [options] device\n"),
95
program_invocation_short_name);
97
fprintf(out, _("\nOptions:\n"
98
" -n, --no-verify disable the verification after the format\n"
99
" -V, --version output version information and exit\n"
100
" -h, --help display this help and exit\n\n"));
102
exit(out == stderr ? EXIT_FAILURE : EXIT_SUCCESS);
105
int main(int argc, char **argv)
112
static const struct option longopts[] = {
113
{"no-verify", no_argument, NULL, 'n'},
114
{"version", no_argument, NULL, 'V'},
115
{"help", no_argument, NULL, 'h'},
119
setlocale(LC_ALL, "");
120
bindtextdomain(PACKAGE, LOCALEDIR);
123
while ((ch = getopt_long(argc, argv, "nVh", longopts, NULL)) != -1)
129
printf(_("%s from %s\n"), program_invocation_short_name,
143
if (stat(argv[0], &st) < 0)
144
err(EXIT_FAILURE, _("cannot stat file %s"), argv[0]);
145
if (!S_ISBLK(st.st_mode))
146
/* do not test major - perhaps this was an USB floppy */
147
errx(EXIT_FAILURE, _("%s: not a block device"), argv[0]);
148
if (access(argv[0], W_OK) < 0)
149
err(EXIT_FAILURE, _("cannot access file %s"), argv[0]);
151
ctrl = open(argv[0], O_WRONLY);
153
err(EXIT_FAILURE, _("cannot open file %s"), argv[0]);
154
if (ioctl(ctrl, FDGETPRM, (long)¶m) < 0)
155
err(EXIT_FAILURE, _("Could not determine current format type"));
157
printf(_("%s-sided, %d tracks, %d sec/track. Total capacity %d kB.\n"),
158
(param.head == 2) ? _("Double") : _("Single"),
159
param.track, param.sect, param.size >> 1);
164
verify_disk(argv[0]);