]>
git.ipfire.org Git - thirdparty/util-linux.git/blob - disk-utils/fdformat.c
1 /* fdformat.c - Low-level formats a floppy disk - Werner Almesberger */
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
16 #include <sys/ioctl.h>
21 /* cannot include <linux/fs.h> */
22 #define MAJOR(a) ((a)>>8)
25 struct floppy_struct param
;
27 #define FLOPPY_MAJOR 2
28 #define SECTOR_SIZE 512
29 #define PERROR(msg) { perror(msg); exit(1); }
31 static void format_disk(char *name
)
33 struct format_descr descr
;
36 printf(_("Formatting ... "));
38 if (ioctl(ctrl
,FDFMTBEG
,NULL
) < 0) PERROR("\nioctl(FDFMTBEG)");
39 for (track
= 0; track
< param
.track
; track
++) {
42 if (ioctl(ctrl
,FDFMTTRK
,(long) &descr
) < 0)
43 PERROR("\nioctl(FDFMTTRK)");
45 printf("%3d\b\b\b",track
);
47 if (param
.head
== 2) {
49 if (ioctl(ctrl
,FDFMTTRK
,(long) &descr
) < 0)
50 PERROR("\nioctl(FDFMTTRK)");
53 if (ioctl(ctrl
,FDFMTEND
,NULL
) < 0) PERROR("\nioctl(FDFMTEND)");
58 static void verify_disk(char *name
)
61 int fd
,cyl_size
,cyl
,count
;
63 cyl_size
= param
.sect
*param
.head
*512;
64 if ((data
= (unsigned char *) malloc(cyl_size
)) == NULL
) PERROR("malloc");
65 printf(_("Verifying ... "));
67 if ((fd
= open(name
,O_RDONLY
)) < 0) PERROR(name
);
68 for (cyl
= 0; cyl
< param
.track
; cyl
++) {
71 printf("%3d\b\b\b",cyl
);
73 read_bytes
= read(fd
,data
,cyl_size
);
74 if(read_bytes
!= cyl_size
) {
78 _("Problem reading cylinder %d, expected %d, read %d\n"),
79 cyl
, cyl_size
, read_bytes
);
82 for (count
= 0; count
< cyl_size
; count
++)
83 if (data
[count
] != FD_FILL_BYTE
) {
84 printf(_("bad data in cyl %d\nContinuing ... "),cyl
);
90 if (close(fd
) < 0) PERROR("close");
94 static void usage(char *name
)
98 if ((this = strrchr(name
,'/')) != NULL
) name
= this+1;
99 fprintf(stderr
,_("usage: %s [ -n ] device\n"),name
);
104 int main(int argc
,char **argv
)
111 if ((p
= strrchr(progname
, '/')) != NULL
)
114 setlocale(LC_ALL
, "");
115 bindtextdomain(PACKAGE
, LOCALEDIR
);
119 (!strcmp(argv
[1], "-V") || !strcmp(argv
[1], "--version"))) {
120 printf(_("%s from %s\n"), progname
, util_linux_version
);
125 if (argc
> 1 && argv
[1][0] == '-') {
126 if (argv
[1][1] != 'n') usage(progname
);
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]);
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) ¶m
) < 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]);