home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / misc / volume11 / sparc-mtools / part02 / mkdfs.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-03-25  |  3.9 KB  |  185 lines

  1. #ifndef sun
  2.     /*
  3.          * FIX ME!
  4.      * All we need is a convenient way for the user to choose the
  5.          * DOS layout parameters for the FS,  and yet I did not want
  6.          * to hard code tables for the various drives.
  7.          * Something like a /etc/dosformat.dat file would be nice
  8.      * the user can then use mkdfs <drivename>,  this involes
  9.      * table parsing routines etc.,  not too hard,  just unpleasant.
  10.      */
  11.     main() {
  12.         printf("Do not know how to format disks on your system\n";
  13.         exit(1) ;
  14.     }
  15. #else
  16.  
  17. #include <stdio.h>
  18. #include <fcntl.h>
  19. #include <errno.h>
  20. #include <sys/wait.h>
  21. #include <sys/file.h>
  22. #include <assert.h>
  23. #include "msdos.h"
  24. #include "bootblk.h"
  25.  
  26. #define VOLLBL 0x8
  27. #define FAT720 0xf9
  28. #define FAT1440 0xf0
  29. static char floppy[] = "/dev/rfd0c" ;
  30. static char disklabel[] = "ANYDSKLABEL" ;
  31.  
  32. void move(), Write(), usage(), formatit() ;
  33.  
  34. static char *progname, buf[MSECSIZ] ;
  35.  
  36. main(argc,argv)
  37.   char **argv;
  38. {
  39.   int c, fd, sec ;
  40.   int fat_len = 3 ;
  41.   int fat = FAT720 ;
  42.   int dir_len = 7 ;
  43.   int hdflag=0, fflag = 0 ;
  44.  
  45.   progname = argv[0] ;
  46.  
  47.   while( (c=getopt(argc,argv,"hf")) != EOF ) {
  48.     switch(c) {
  49.     case 'f' :
  50.       fflag++ ;
  51.       break;
  52.     case 'h' :
  53.       hdflag++ ;
  54.       break;
  55.     case '?':
  56.     default:
  57.       fprintf(stderr,"Unknown option \"%c\"\n",c) ;
  58.       usage() ;
  59.     }
  60.   }
  61.  
  62.   if (fflag) 
  63.     formatit(hdflag) ;
  64.  
  65.   /*  Lets initialize the MSDOS FS */
  66.  
  67.   if ( (fd=open(floppy,O_RDWR)) == -1 ) {
  68.     fprintf(stderr,"%s: open: ",progname) ;
  69.     perror(floppy) ;
  70.     exit(1) ;
  71.   }
  72.  
  73.   if ( hdflag ) {
  74.     dir_len=14 ;
  75.     fat_len=9 ;
  76.     fat=FAT1440 ;
  77.   }
  78.   
  79.   bzero(buf,sizeof(buf)) ;
  80.   bcopy(hdflag?hdboot:ldboot,buf,MSECSIZ) ;   /* Create the boot block */
  81.   Write(fd,buf,MSECSIZ) ;   /* Dump the boot block */
  82.   bzero(buf,sizeof(buf)) ;
  83.   for( c=0; c < 2 ; ++c ) {
  84.     buf[0] = fat ;
  85.     buf[1] = 0xff ;
  86.     buf[2] = 0xff ;
  87.     Write(fd,buf,MSECSIZ) ;    /* First block of FAT */
  88.     bzero(buf,3) ;
  89.     for( sec=fat_len; --sec ; )
  90.       Write(fd,buf,MSECSIZ) ;  /* Rest of FAT */
  91.   }
  92.   strcpy(((struct directory *)buf)->name,disklabel) ;
  93.   ((struct directory *)buf)->attr= VOLLBL ;
  94.   Write(fd,buf,MSECSIZ) ;  /* Root dir */
  95.   bzero(buf,strlen(disklabel)) ;
  96.   ((struct directory *)buf)->attr= 0 ;
  97.   for( ; --dir_len ; )
  98.     Write(fd,buf,MSECSIZ) ;  /* Root dir */
  99. }
  100.  
  101. void 
  102. move(fd,sector)
  103.   int fd, sector ;
  104. {
  105.   if ( lseek(fd,sector*MSECSIZ,L_SET) != sector*MSECSIZ) {
  106.     fprintf(stderr,"%s: lseek: ",progname) ;
  107.     perror(floppy) ;
  108.     exit(1) ;
  109.   }
  110. }
  111.  
  112. void
  113. Write(fd,buf,count)
  114.   int fd,count ;
  115.   char * buf ;
  116. {
  117.   if ( write(fd,buf,count) != count ) {
  118.     fprintf(stderr,"%s: write: ",progname) ;
  119.     perror(floppy) ;
  120.     exit(1) ;
  121.   }
  122. }
  123.  
  124. void
  125. usage() 
  126. {
  127.   fprintf(stderr,"Usage: %s [-h] [-f]\n",progname) ;
  128.   fprintf(stderr,"\tBuilds an empty DOS filesystem\n") ;
  129.   fprintf(stderr,"\t-h: high density\n\t-f: reformat first\n") ;
  130.   exit(1) ;
  131. }
  132.  
  133. void
  134. formatit(hdq)
  135.   int hdq;
  136. {
  137.   int pid;
  138.   struct wait w;
  139.   int retval;
  140.  
  141.   if( (pid=fork()) == -1 ) {
  142.     fprintf(stderr,"%s: ",progname) ;
  143.     perror("fork") ;
  144.     exit(1) ;
  145.   }
  146.  
  147.   if ( !pid ) {
  148.     if ( hdq ) 
  149.       execl("/bin/fdformat","fdformat",floppy,0) ;
  150.     else
  151.       execl("/bin/fdformat","fdformat","-l",floppy,0) ;
  152.  
  153.     fprintf(stderr,"%s: ",progname) ;
  154.     perror("exec") ;
  155.     exit(1) ;
  156.   }
  157.  
  158.   while ( (retval=wait4(pid,&w,0,0)) == -1 && errno == EINTR ) ;
  159.  
  160.   if (retval == -1) {
  161.     fprintf(stderr,"%s: ",progname) ;
  162.     perror("wait4") ;
  163.     fprintf(stderr,"The format operation may have failed\nTry again\n") ;
  164.     exit(1) ;
  165.   }
  166.  
  167.   if ( WIFSIGNALED(w) ) {
  168.     fprintf(stderr,"%s: ",progname) ;
  169.     psignal(w.w_termsig,"/bin/fdformat") ;
  170.     if (w.w_coredump)
  171.       fprintf(stderr,"Core dumped\n") ;
  172.     exit(1) ;
  173.   }
  174.  
  175.   assert( WIFEXITED(w) ) ; /* If not signalled,  must be exited
  176.                               we are not tracing stopped processes */
  177.  
  178.   if ( w.w_retcode ) {
  179.     fprintf(stderr,"%s: /bin/fdformat exited with non-zero exit code %d\n",
  180.       progname,w.w_retcode) ;
  181.     exit(1) ;
  182.   }
  183. }
  184. #endif
  185.