home *** CD-ROM | disk | FTP | other *** search
/ Chip 1995 March / CHIP3.mdf / slackwar / a / util / util-lin.2 / util-lin / util-linux-2.2 / disk-utils / fdformat.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-02-22  |  2.7 KB  |  110 lines

  1. /* fdformat.c  -  Low-level formats a floppy disk. */
  2.  
  3. #include <stdio.h>
  4. #include <string.h>
  5. #include <fcntl.h>
  6. #include <errno.h>
  7. #include <unistd.h>
  8. #include <stdlib.h>
  9. #include <sys/stat.h>
  10. #include <linux/fd.h>
  11. #include <linux/fs.h>
  12.  
  13. static int ctrl;
  14. struct floppy_struct param;
  15.  
  16. #define FLOPPY_MAJOR 2
  17. #define SECTOR_SIZE 512
  18. #define PERROR(msg) { perror(msg); exit(1); }
  19.  
  20. static void format_disk(char *name)
  21. {
  22.     struct format_descr descr;
  23.     int track;
  24.  
  25.     printf("Formatting ... ");
  26.     fflush(stdout);
  27.     if (ioctl(ctrl,FDFMTBEG,NULL) < 0) PERROR("\nioctl(FDFMTBEG)");
  28.     for (track = 0; track < param.track; track++) {
  29.     descr.track = track;
  30.     descr.head = 0;
  31.     if (ioctl(ctrl,FDFMTTRK,(int) &descr) < 0) PERROR("\nioctl(FDFMTTRK)");
  32.     printf("%3d\b\b\b",track);
  33.     fflush(stdout);
  34.     if (param.head == 2) {
  35.         descr.head = 1;
  36.         if (ioctl(ctrl,FDFMTTRK,(int) &descr) < 0)
  37.         PERROR("\nioctl(FDFMTTRK)");
  38.     }
  39.     }
  40.     if (ioctl(ctrl,FDFMTEND,NULL) < 0) PERROR("\nioctl(FDFMTEND)");
  41.     printf("done\n");
  42. }
  43.  
  44.  
  45. static void verify_disk(char *name)
  46. {
  47.     unsigned char *data;
  48.     int fd,cyl_size,cyl,count;
  49.  
  50.     cyl_size = param.sect*param.head*512;
  51.     if ((data = (unsigned char *) malloc(cyl_size)) == NULL) PERROR("malloc");
  52.     printf("Verifying ... ");
  53.     fflush(stdout);
  54.     if ((fd = open(name,O_RDONLY)) < 0) PERROR(name);
  55.     for (cyl = 0; cyl < param.track; cyl++) {
  56.     printf("%3d\b\b\b",cyl);
  57.     fflush(stdout);
  58.     if (read(fd,data,cyl_size) != cyl_size) PERROR("read");
  59.     for (count = 0; count < cyl_size; count++)
  60.         if (data[count] != FD_FILL_BYTE) {
  61.         printf("bad data in cyl %d\nContinuing ... ",cyl);
  62.         fflush(stdout);
  63.         break;
  64.         }
  65.     }
  66.     printf("done\n");
  67.     if (close(fd) < 0) PERROR("close");
  68. }
  69.  
  70.  
  71. static void usage(char *name)
  72. {
  73.     char *this;
  74.  
  75.     if (this = strrchr(name,'/')) name = this+1;
  76.     fprintf(stderr,"usage: %s [ -n ] device\n",name);
  77.     exit(1);
  78. }
  79.  
  80.  
  81. int main(int argc,char **argv)
  82. {
  83.     int verify;
  84.     char *name;
  85.     struct stat st;
  86.  
  87.     name = argv[0];
  88.     verify = 1;
  89.     if (argc > 1 && argv[1][0] == '-') {
  90.     if (argv[1][1] != 'n') usage(name);
  91.     verify = 0;
  92.     argc--;
  93.     argv++;
  94.     }
  95.     if (argc != 2) usage(name);
  96.     if (lstat(argv[1],&st) < 0) PERROR(argv[1]);
  97.     if (!S_ISBLK(st.st_mode) || MAJOR(st.st_rdev) != FLOPPY_MAJOR) {
  98.     fprintf(stderr,"%s: not a floppy device\n",argv[1]);
  99.     exit(1);
  100.     }
  101.     if (access(argv[1],W_OK) < 0) PERROR(argv[1]);
  102.     if ((ctrl = open(argv[1],3)) < 0) PERROR(argv[1]);
  103.     if (ioctl(ctrl,FDGETPRM,(int) ¶m) < 0) PERROR("ioctl(FDGETPRM)");
  104.     printf("%sle-sided, %d tracks, %d sec/track. Total capacity %d kB.\n",
  105.       param.head ? "Doub" : "Sing",param.track,param.sect,param.size >> 1);
  106.     format_disk(argv[1]);
  107.     if (verify) verify_disk(argv[1]);
  108.     return 0;
  109. }
  110.