]>
git.ipfire.org Git - thirdparty/util-linux.git/blob - disk-utils/fdformat.c
1 /* fdformat.c - Low-level formats a floppy disk. */
10 #include <sys/ioctl.h>
15 struct floppy_struct param
;
17 #define FLOPPY_MAJOR 2
18 #define SECTOR_SIZE 512
19 #define PERROR(msg) { perror(msg); exit(1); }
21 static void format_disk(char *name
)
23 struct format_descr descr
;
26 printf("Formatting ... ");
28 if (ioctl(ctrl
,FDFMTBEG
,NULL
) < 0) PERROR("\nioctl(FDFMTBEG)");
29 for (track
= 0; track
< param
.track
; track
++) {
32 if (ioctl(ctrl
,FDFMTTRK
,(long) &descr
) < 0)
33 PERROR("\nioctl(FDFMTTRK)");
35 printf("%3d\b\b\b",track
);
37 if (param
.head
== 2) {
39 if (ioctl(ctrl
,FDFMTTRK
,(long) &descr
) < 0)
40 PERROR("\nioctl(FDFMTTRK)");
43 if (ioctl(ctrl
,FDFMTEND
,NULL
) < 0) PERROR("\nioctl(FDFMTEND)");
48 static void verify_disk(char *name
)
51 int fd
,cyl_size
,cyl
,count
;
53 cyl_size
= param
.sect
*param
.head
*512;
54 if ((data
= (unsigned char *) malloc(cyl_size
)) == NULL
) PERROR("malloc");
55 printf("Verifying ... ");
57 if ((fd
= open(name
,O_RDONLY
)) < 0) PERROR(name
);
58 for (cyl
= 0; cyl
< param
.track
; cyl
++) {
59 printf("%3d\b\b\b",cyl
);
61 if (read(fd
,data
,cyl_size
) != cyl_size
) PERROR("read");
62 for (count
= 0; count
< cyl_size
; count
++)
63 if (data
[count
] != FD_FILL_BYTE
) {
64 printf("bad data in cyl %d\nContinuing ... ",cyl
);
70 if (close(fd
) < 0) PERROR("close");
74 static void usage(char *name
)
78 if ((this = strrchr(name
,'/')) != NULL
) name
= this+1;
79 fprintf(stderr
,"usage: %s [ -n ] device\n",name
);
84 int main(int argc
,char **argv
)
92 if (argc
> 1 && argv
[1][0] == '-') {
93 if (argv
[1][1] != 'n') usage(name
);
98 if (argc
!= 2) usage(name
);
99 if (stat(argv
[1],&st
) < 0) PERROR(argv
[1]);
100 if (!S_ISBLK(st
.st_mode
) || MAJOR(st
.st_rdev
) != FLOPPY_MAJOR
) {
101 fprintf(stderr
,"%s: not a floppy device\n",argv
[1]);
104 if (access(argv
[1],W_OK
) < 0) PERROR(argv
[1]);
105 if ((ctrl
= open(argv
[1],3)) < 0) PERROR(argv
[1]);
106 if (ioctl(ctrl
,FDGETPRM
,(long) ¶m
) < 0)
107 PERROR("Could not determine current format type");
108 printf("%sle-sided, %d tracks, %d sec/track. Total capacity %d kB.\n",
109 param
.head
? "Doub" : "Sing",param
.track
,param
.sect
,param
.size
>> 1);
110 format_disk(argv
[1]);
111 if (verify
) verify_disk(argv
[1]);