home *** CD-ROM | disk | FTP | other *** search
/ rtsi.com / 2014.01.www.rtsi.com.tar / www.rtsi.com / OS9 / OSK / SRC / msdos_diskaccess.lzh / MS_DISK_ACCESS / mkdfs.c < prev    next >
C/C++ Source or Header  |  1991-08-04  |  4KB  |  179 lines

  1. #include <stdio.h>
  2. #include <errno.h>
  3. #include <modes.h>
  4. #include "msdos.h"
  5. #include "bootblk.h"
  6.  
  7. #define VOLLBL 0x8
  8. #define FAT 0xf9
  9.  
  10. static char *floppy = "/tos0@" ;
  11. static char disklabel[12] ;
  12.  
  13. void move(), Write(), usage(), formatit() ;
  14.  
  15. static char *progname, buf[MSECSIZ] ;
  16.  
  17. main(argc,argv)
  18.   char **argv;
  19. {
  20.   int c, fd, sec, fd1 = -1, len;
  21.   int fat_len = 3 ;
  22.   int fat = FAT ;
  23.   int dir_len = 7 ;
  24.   int hdflag=0, fflag = 0, preext = 0;
  25.   extern char *optarg;
  26.  
  27.   progname = argv[0] ;
  28.  
  29.   /* Initialize with spaces - modif bd, 06/90 */
  30.   sprintf(disklabel,"%-11.11s","form OS9_ST") ;
  31.  
  32.   while( (c=getopt(argc,argv,"d:hfpxl:?")) != EOF ) {
  33.     switch(c) {
  34.     case 'f' :
  35.       fflag++ ;
  36.       break;
  37.     case 'h' :
  38.       hdflag++ ;
  39.       break;
  40.     case 'p' :
  41.       preext++;
  42.       break;
  43.     case 'x' :
  44.       preext = 2;
  45.       break;
  46.     case 'l' :
  47.       sprintf(disklabel,"%-11.11s",optarg) ;
  48.       break;
  49.     case 'd' :
  50.       floppy = optarg;
  51.       if (floppy[strlen(floppy-1)] != '@') {
  52.           fflag = 0;
  53.       } else {
  54.           fprintf(stderr,"%s: don't init other devives than default. to risky!\n",
  55.               progname);
  56.           exit(1);
  57.       }
  58.       fd1 = 0;
  59.       break;
  60.     default:
  61.       fprintf(stderr,"Unknown option \"%c\"\n",c) ;
  62.     case '?':
  63.       usage() ;
  64.     }
  65.   }
  66.  
  67.   if (fflag) 
  68.     formatit(hdflag) ;
  69.  
  70.   /*  Lets initialize the MSDOS FS */
  71.   if (fd1 == 0)
  72.       unlink(floppy);
  73.   len = (preext) ? 512*9*80*2 : 512 * 9 * 4;
  74.   if (((fd1 != 0) && (fd=open(floppy, S_IREAD+S_IWRITE)) == -1) ||
  75.         ((fd1 == 0) && (fd=create(floppy, S_IWRITE+S_ISIZE,
  76.                                  S_IREAD+S_IWRITE, len)) == -1)) {
  77.     fprintf(stderr,"%s: open: ",progname) ;
  78.     perror(floppy) ;
  79.     exit(1) ;
  80.   }
  81.   if (fd1 == 0 && preext == 2) {
  82.       lseek(fd, len-1, 0);
  83.       write(fd, &floppy, 1);
  84.       lseek(fd, 0, 0);
  85.   }
  86.  
  87.   if (fd1 != 0 && (fd1=open("/d0@",S_IREAD+S_IWRITE)) == -1 ) {
  88.     fprintf(stderr,"%s: open default (/d0@): ",progname) ;
  89.     perror(floppy) ;
  90.     exit(1) ;
  91.   }
  92.   if ( hdflag ) {
  93.     dir_len=14 ;
  94.     fat_len=9 ;
  95.   }
  96.   
  97.   bzero(buf,sizeof(buf)) ;
  98.   bcopy(hdflag?hdboot:ldboot,buf,MSECSIZ) ;   /* Create the boot block */
  99.   if (fd1 > 0) {
  100.       lseek(fd1, 256, 0);
  101.       Write(fd1, buf, 256);
  102.       lseek(fd, MSECSIZ, 0);
  103.   } else {
  104.       Write(fd, buf, MSECSIZ) ;   /* Dump the boot block */
  105.   }
  106.   bzero(buf,sizeof(buf)) ;
  107.   for( c=0; c < 2 ; ++c ) {
  108.     buf[0] = fat ;
  109.     buf[1] = 0xff ;
  110.     buf[2] = 0xff ;
  111.     Write(fd,buf,MSECSIZ) ;    /* First block of FAT */
  112.     bzero(buf,3) ;
  113.     for( sec=fat_len; --sec ; )
  114.       Write(fd,buf,MSECSIZ) ;  /* Rest of FAT */
  115.   }
  116.   strcpy(((struct directory *)buf)->name,disklabel) ;
  117.   ((struct directory *)buf)->attr= VOLLBL ;
  118.   Write(fd,buf,MSECSIZ) ;  /* Root dir */
  119.   bzero(buf,strlen(disklabel)) ;
  120.   ((struct directory *)buf)->attr= 0 ;
  121.   for( ; --dir_len ; )
  122.     Write(fd,buf,MSECSIZ) ;  /* Root dir */
  123.   lseek(fd, 0, 0);
  124. }
  125.  
  126. void 
  127. move(fd,sector)
  128.   int fd, sector ;
  129. {
  130.   if ( lseek(fd,sector*MSECSIZ,0) != sector*MSECSIZ) {
  131.     fprintf(stderr,"%s: lseek: ",progname) ;
  132.     perror(floppy) ;
  133.     exit(1) ;
  134.   }
  135. }
  136.  
  137. void
  138. Write(fd,buf,count)
  139.   int fd,count ;
  140.   char * buf ;
  141. {
  142.   if ( write(fd,buf,count) != count ) {
  143.     fprintf(stderr,"%s: write: ",progname) ;
  144.     perror(floppy) ;
  145.     exit(1) ;
  146.   }
  147. }
  148.  
  149. void
  150. usage() 
  151. {
  152.   fprintf(stderr,"Syntax:   %s <opts>\n",progname) ;
  153.   fprintf(stderr,"Function: Builds an empty DOS filesystem\n") ;
  154.   fprintf(stderr,"\t-d<file> init filesystem on file <file>\n\
  155. \t-f\treformat first\n\
  156. \t(-h\thigh density) currently not supported\n\
  157. \t-l<disklabel>\n\
  158. \t-p\tpreextend file to full disksize (737280 Bytes)\n\
  159. \t-x\tpreextend file to full disksize (737280 Bytes) and show it\n\
  160. \t\tp allocates space, but fileentry shows only used part\n\
  161. \t-?\tthis message\n") ;
  162.   exit(1) ;
  163. }
  164.  
  165. void
  166. formatit(hdq)
  167.   int hdq;
  168. {
  169.   int retval;
  170.  
  171.   system("ex diskcache -d /d0");
  172.   retval = system("ex form -5 -s9 -i1 /d0");
  173.   if (retval != 0) {
  174.     fprintf(stderr,"%s: ",progname);
  175.     fprintf(stderr,"The format operation may have failed\nTry again\n") ;
  176.     exit(retval);
  177.   }
  178. }
  179.