Upload Tizen:Base source
[framework/base/util-linux-ng.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 <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 struct floppy_struct param;
22
23 #define SECTOR_SIZE 512
24 #define PERROR(msg) { perror(msg); exit(1); }
25
26 static void format_disk(int ctrl, char *name)
27 {
28     struct format_descr descr;
29     int track;
30
31     printf(_("Formatting ... "));
32     fflush(stdout);
33     if (ioctl(ctrl,FDFMTBEG,NULL) < 0) PERROR("\nioctl(FDFMTBEG)");
34     for (track = 0; track < param.track; track++) {
35         descr.track = track;
36         descr.head = 0;
37         if (ioctl(ctrl,FDFMTTRK,(long) &descr) < 0)
38           PERROR("\nioctl(FDFMTTRK)");
39
40         printf("%3d\b\b\b",track);
41         fflush(stdout);
42         if (param.head == 2) {
43             descr.head = 1;
44             if (ioctl(ctrl,FDFMTTRK,(long) &descr) < 0)
45               PERROR("\nioctl(FDFMTTRK)");
46         }
47     }
48     if (ioctl(ctrl,FDFMTEND,NULL) < 0) PERROR("\nioctl(FDFMTEND)");
49     printf(_("done\n"));
50 }
51
52
53 static void verify_disk(char *name)
54 {
55     unsigned char *data;
56     int fd,cyl_size,cyl,count;
57
58     cyl_size = param.sect*param.head*512;
59     if ((data = (unsigned char *) malloc(cyl_size)) == NULL) PERROR("malloc");
60     printf(_("Verifying ... "));
61     fflush(stdout);
62     if ((fd = open(name,O_RDONLY)) < 0) PERROR(name);
63     for (cyl = 0; cyl < param.track; cyl++) {
64         int read_bytes;
65
66         printf("%3d\b\b\b",cyl);
67         fflush(stdout);
68         read_bytes = read(fd,data,cyl_size);
69         if(read_bytes != cyl_size) {
70             if(read_bytes < 0)
71                     perror(_("Read: "));
72             fprintf(stderr,
73                     _("Problem reading cylinder %d, expected %d, read %d\n"),
74                     cyl, cyl_size, read_bytes);
75             free(data);
76             exit(1);
77         }
78         for (count = 0; count < cyl_size; count++)
79             if (data[count] != FD_FILL_BYTE) {
80                 printf(_("bad data in cyl %d\nContinuing ... "),cyl);
81                 fflush(stdout);
82                 break;
83             }
84     }
85     free(data);
86     printf(_("done\n"));
87     if (close(fd) < 0) PERROR("close");
88 }
89
90
91 static void usage(char *name)
92 {
93     char *this;
94
95     if ((this = strrchr(name,'/')) != NULL) name = this+1;
96     fprintf(stderr,_("usage: %s [ -n ] device\n"),name);
97     exit(1);
98 }
99
100
101 int main(int argc,char **argv)
102 {
103     int ctrl;
104     int verify;
105     struct stat st;
106     char *progname, *p;
107
108     progname = argv[0];
109     if ((p = strrchr(progname, '/')) != NULL)
110             progname = p+1;
111
112     setlocale(LC_ALL, "");
113     bindtextdomain(PACKAGE, LOCALEDIR);
114     textdomain(PACKAGE);
115
116     if (argc == 2 &&
117         (!strcmp(argv[1], "-V") || !strcmp(argv[1], "--version"))) {
118             printf(_("%s (%s)\n"), progname, PACKAGE_STRING);
119             exit(0);
120     }
121
122     verify = 1;
123     if (argc > 1 && argv[1][0] == '-') {
124         if (argv[1][1] != 'n') usage(progname);
125         verify = 0;
126         argc--;
127         argv++;
128     }
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]);
133         exit(1);
134         /* do not test major - perhaps this was an USB floppy */
135     }
136     if (access(argv[1],W_OK) < 0) PERROR(argv[1]);
137
138     ctrl = open(argv[1],O_WRONLY);
139     if (ctrl < 0)
140             PERROR(argv[1]);
141     if (ioctl(ctrl,FDGETPRM,(long) &param) < 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]);
147     close(ctrl);
148
149     if (verify)
150             verify_disk(argv[1]);
151     return 0;
152 }