]> git.ipfire.org Git - thirdparty/util-linux.git/blob - disk-utils/fdformat.c
Imported from util-linux-2.10s tarball.
[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@misiek.eu.org>
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 <stdio.h>
10 #include <string.h>
11 #include <fcntl.h>
12 #include <errno.h>
13 #include <unistd.h>
14 #include <stdlib.h>
15 #include <sys/stat.h>
16 #include <sys/ioctl.h>
17 #include <linux/fd.h>
18
19 #include "nls.h"
20
21 /* cannot include <linux/fs.h> */
22 #define MAJOR(a) ((a)>>8)
23
24 static int ctrl;
25 struct floppy_struct param;
26
27 #define FLOPPY_MAJOR 2
28 #define SECTOR_SIZE 512
29 #define PERROR(msg) { perror(msg); exit(1); }
30
31 static void format_disk(char *name)
32 {
33 struct format_descr descr;
34 int track;
35
36 printf(_("Formatting ... "));
37 fflush(stdout);
38 if (ioctl(ctrl,FDFMTBEG,NULL) < 0) PERROR("\nioctl(FDFMTBEG)");
39 for (track = 0; track < param.track; track++) {
40 descr.track = track;
41 descr.head = 0;
42 if (ioctl(ctrl,FDFMTTRK,(long) &descr) < 0)
43 PERROR("\nioctl(FDFMTTRK)");
44
45 printf("%3d\b\b\b",track);
46 fflush(stdout);
47 if (param.head == 2) {
48 descr.head = 1;
49 if (ioctl(ctrl,FDFMTTRK,(long) &descr) < 0)
50 PERROR("\nioctl(FDFMTTRK)");
51 }
52 }
53 if (ioctl(ctrl,FDFMTEND,NULL) < 0) PERROR("\nioctl(FDFMTEND)");
54 printf(_("done\n"));
55 }
56
57
58 static void verify_disk(char *name)
59 {
60 unsigned char *data;
61 int fd,cyl_size,cyl,count;
62
63 cyl_size = param.sect*param.head*512;
64 if ((data = (unsigned char *) malloc(cyl_size)) == NULL) PERROR("malloc");
65 printf(_("Verifying ... "));
66 fflush(stdout);
67 if ((fd = open(name,O_RDONLY)) < 0) PERROR(name);
68 for (cyl = 0; cyl < param.track; cyl++) {
69 int read_bytes;
70
71 printf("%3d\b\b\b",cyl);
72 fflush(stdout);
73 read_bytes = read(fd,data,cyl_size);
74 if(read_bytes != cyl_size) {
75 if(read_bytes < 0)
76 perror(_("Read: "));
77 fprintf(stderr,
78 _("Problem reading cylinder %d, expected %d, read %d\n"),
79 cyl, cyl_size, read_bytes);
80 exit(1);
81 }
82 for (count = 0; count < cyl_size; count++)
83 if (data[count] != FD_FILL_BYTE) {
84 printf(_("bad data in cyl %d\nContinuing ... "),cyl);
85 fflush(stdout);
86 break;
87 }
88 }
89 printf(_("done\n"));
90 if (close(fd) < 0) PERROR("close");
91 }
92
93
94 static void usage(char *name)
95 {
96 char *this;
97
98 if ((this = strrchr(name,'/')) != NULL) name = this+1;
99 fprintf(stderr,_("usage: %s [ -n ] device\n"),name);
100 exit(1);
101 }
102
103
104 int main(int argc,char **argv)
105 {
106 int verify;
107 struct stat st;
108 char *progname, *p;
109
110 progname = argv[0];
111 if ((p = strrchr(progname, '/')) != NULL)
112 progname = p+1;
113
114 setlocale(LC_ALL, "");
115 bindtextdomain(PACKAGE, LOCALEDIR);
116 textdomain(PACKAGE);
117
118 if (argc == 2 &&
119 (!strcmp(argv[1], "-V") || !strcmp(argv[1], "--version"))) {
120 printf(_("%s from %s\n"), progname, util_linux_version);
121 exit(0);
122 }
123
124 verify = 1;
125 if (argc > 1 && argv[1][0] == '-') {
126 if (argv[1][1] != 'n') usage(progname);
127 verify = 0;
128 argc--;
129 argv++;
130 }
131 if (argc != 2) usage(progname);
132 if (stat(argv[1],&st) < 0) PERROR(argv[1]);
133 if (!S_ISBLK(st.st_mode) || MAJOR(st.st_rdev) != FLOPPY_MAJOR) {
134 fprintf(stderr,_("%s: not a floppy device\n"),argv[1]);
135 exit(1);
136 }
137 if (access(argv[1],W_OK) < 0) PERROR(argv[1]);
138 if ((ctrl = open(argv[1],O_WRONLY)) < 0) PERROR(argv[1]);
139 if (ioctl(ctrl,FDGETPRM,(long) &param) < 0)
140 PERROR(_("Could not determine current format type"));
141 printf(_("%s-sided, %d tracks, %d sec/track. Total capacity %d kB.\n"),
142 (param.head == 2) ? _("Double") : _("Single"),
143 param.track, param.sect,param.size >> 1);
144 format_disk(argv[1]);
145 if (verify) verify_disk(argv[1]);
146 return 0;
147 }