]> git.ipfire.org Git - thirdparty/util-linux.git/blob - disk-utils/fdformat.c
fdformat: coding style
[thirdparty/util-linux.git] / disk-utils / fdformat.c
1 /* fdformat.c - Low-level formats a floppy disk - Werner Almesberger */
2
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
7 */
8
9 #include <errno.h>
10 #include <fcntl.h>
11 #include <getopt.h>
12 #include <linux/fd.h>
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <sys/ioctl.h>
16 #include <sys/stat.h>
17 #include <unistd.h>
18
19 #include "c.h"
20 #include "nls.h"
21 #include "xalloc.h"
22
23 struct floppy_struct param;
24
25 #define SECTOR_SIZE 512
26
27 static void format_disk(int ctrl)
28 {
29 struct format_descr descr;
30 unsigned int track;
31
32 printf(_("Formatting ... "));
33 fflush(stdout);
34 if (ioctl(ctrl, FDFMTBEG, NULL) < 0)
35 err(EXIT_FAILURE, "\nioctl(FDFMTBEG)");
36 for (track = 0; track < param.track; track++) {
37 descr.track = track;
38 descr.head = 0;
39 if (ioctl(ctrl, FDFMTTRK, (long)&descr) < 0)
40 err(EXIT_FAILURE, "\nioctl(FDFMTTRK)");
41
42 printf("%3d\b\b\b", track);
43 fflush(stdout);
44 if (param.head == 2) {
45 descr.head = 1;
46 if (ioctl(ctrl, FDFMTTRK, (long)&descr) < 0)
47 err(EXIT_FAILURE, "\nioctl(FDFMTTRK)");
48 }
49 }
50 if (ioctl(ctrl, FDFMTEND, NULL) < 0)
51 err(EXIT_FAILURE, "\nioctl(FDFMTEND)");
52 printf(_("done\n"));
53 }
54
55 static void verify_disk(char *name)
56 {
57 unsigned char *data;
58 unsigned int cyl;
59 int fd, cyl_size, count;
60
61 cyl_size = param.sect * param.head * 512;
62 data = xmalloc(cyl_size);
63 printf(_("Verifying ... "));
64 fflush(stdout);
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++) {
68 int read_bytes;
69
70 printf("%3d\b\b\b", cyl);
71 fflush(stdout);
72 read_bytes = read(fd, data, cyl_size);
73 if (read_bytes != cyl_size) {
74 if (read_bytes < 0)
75 perror(_("Read: "));
76 fprintf(stderr,
77 _("Problem reading cylinder %d,"
78 " expected %d, read %d\n"),
79 cyl, cyl_size, read_bytes);
80 free(data);
81 exit(EXIT_FAILURE);
82 }
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);
87 fflush(stdout);
88 break;
89 }
90 }
91 free(data);
92 printf(_("done\n"));
93 if (close(fd) < 0)
94 err(EXIT_FAILURE, "close");
95 }
96
97 static void __attribute__ ((__noreturn__)) usage(FILE * out)
98 {
99 fprintf(out, _("Usage: %s [options] device\n"),
100 program_invocation_short_name);
101
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"));
106
107 exit(out == stderr ? EXIT_FAILURE : EXIT_SUCCESS);
108 }
109
110 int main(int argc, char **argv)
111 {
112 int ch;
113 int ctrl;
114 int verify;
115 struct stat st;
116
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'},
121 {NULL, 0, NULL, 0}
122 };
123
124 setlocale(LC_ALL, "");
125 bindtextdomain(PACKAGE, LOCALEDIR);
126 textdomain(PACKAGE);
127
128 while ((ch = getopt_long(argc, argv, "nVh", longopts, NULL)) != -1)
129 switch (ch) {
130 case 'n':
131 verify = 0;
132 break;
133 case 'V':
134 printf(_("%s from %s\n"), program_invocation_short_name,
135 PACKAGE_STRING);
136 exit(EXIT_SUCCESS);
137 case 'h':
138 usage(stdout);
139 default:
140 usage(stderr);
141 }
142
143 argc -= optind;
144 argv += optind;
145
146 if (argc < 1)
147 usage(stderr);
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]);
155
156 ctrl = open(argv[0], O_WRONLY);
157 if (ctrl < 0)
158 err(EXIT_FAILURE, _("cannot open file %s"), argv[0]);
159 if (ioctl(ctrl, FDGETPRM, (long)&param) < 0)
160 err(EXIT_FAILURE, _("Could not determine current format type"));
161
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);
165 format_disk(ctrl);
166 close(ctrl);
167
168 if (verify)
169 verify_disk(argv[0]);
170 return EXIT_SUCCESS;
171 }