mkfs: fast format option
authorBrett Gordon <beretta42@gmail.com>
Thu, 16 Jul 2015 13:30:07 +0000 (09:30 -0400)
committerAlan Cox <alan@linux.intel.com>
Thu, 16 Jul 2015 19:05:03 +0000 (20:05 +0100)
--089e0103e64ae50a2e051afe11ce
Content-Type: text/plain; charset=UTF-8

a new mkfs w/ fast formating (just i blocks) using getopt()

--
Brett M. Gordon,
beretta42@gmail.com

<div dir="ltr">a new mkfs w/ fast formating (just i blocks) using getopt()<br clear="all"><div><br></div>-- <br><div class="gmail_signature">Brett M. Gordon,<br><a href="mailto:beretta42@gmail.com" target="_blank">beretta42@gmail.com</a></div>
</div>

Applications/util/mkfs.c

index 54e8013..23d51ac 100644 (file)
@@ -23,6 +23,7 @@ UZI (Unix Z80 Implementation) Utilities:  mkfs.c
 #include <unistd.h>
 #include <fcntl.h>
 
+
 typedef uint16_t blkno_t;
 
 struct dinode {
@@ -63,6 +64,9 @@ typedef struct direct {
     char     d_name[FILENAME_LEN];
 } direct;
 
+
+uint8_t fast=0;     /* flag for fast formatting option */
+
 int dev;
 
 #define ROOTINODE 1       /* Inode # of / for all mounted filesystems. */
@@ -113,13 +117,14 @@ void mkfs(uint16_t fsize, uint16_t isize)
     printf("Zeroizing i-blocks...\n");
     zeros = zerobuf();         /* Get a zero filled buffer */
 
-#if 1
-    for (j = 0; j < fsize; ++j)
-       dwrite(j, zeros);
-#else
-    for (j = 0; j < isize; ++j)
-       dwrite(j, zeros);
-#endif
+    if( !fast ){
+           for (j = 0; j < fsize; ++j)
+                   dwrite(j, zeros);
+    }
+    else{
+           for (j = 0; j < isize; ++j)
+                   dwrite(j, zeros);
+    }
 
     /* Initialize the super-block */
     fs_tab.s_mounted = SMOUNTED;       /* Magic number */
@@ -169,28 +174,45 @@ void mkfs(uint16_t fsize, uint16_t isize)
     printf("Done.\n");
 }
 
+void printopts( )
+{
+       fprintf( stderr, "usage: mkfs [options] device isize fsize\n");
+       exit(-1);
+}
+
+
 int main(int argc, char *argv[])
 {
     uint16_t fsize, isize;
     struct stat statbuf;
-
-    if (argc != 4) {
-       fprintf(stderr, "usage: mkfs device isize fsize\n");
-       exit(-1);
+    int option;
+
+    while( (option=getopt( argc, argv, "f" ))>0 ){
+           switch( option ){
+           case 'f':
+                   fast=1;
+                   break;
+           case '?':
+           default:
+                   printopts();
+           }
     }
 
-    if (stat(argv[1], &statbuf) != 0) {
-        fprintf(stderr, "mkfs: can't stat %s\n", argv[1]);
+    if (argc-optind < 3) printopts();
+    
+    
+    if (stat(argv[optind], &statbuf) != 0) {
+        fprintf(stderr, "mkfs: can't stat %s\n", argv[optind]);
         exit(-1);
     }
-    
+
     if (!S_ISBLK(statbuf.st_mode)) {
-        fprintf(stderr, "mkfs: %s is not a block device\n", argv[1]);
+        fprintf(stderr, "mkfs: %s is not a block device\n", argv[optind]);
         exit(-1);
     }
 
-    isize = (uint16_t) atoi(argv[2]);
-    fsize = (uint16_t) atoi(argv[3]);
+    isize = (uint16_t) atoi(argv[optind+1]);
+    fsize = (uint16_t) atoi(argv[optind+2]);
 
     if (fsize < 3 || isize < 2 || isize >= fsize) {
        fprintf(stderr, "mkfs: bad parameter values\n");
@@ -198,13 +220,13 @@ int main(int argc, char *argv[])
     }
 
     printf("Making filesystem on device %s with isize %u fsize %u. Confirm? ",
-          argv[1], isize, fsize);
+          argv[optind], isize, fsize);
     if (!yes())
        exit(-1);
 
-    dev = open(argv[1], O_RDWR);
+    dev = open(argv[optind], O_RDWR);
     if (dev < 0) {
-        fprintf(stderr, "mkfs: can't open device %s\n", argv[1]);
+        fprintf(stderr, "mkfs: can't open device %s\n", argv[optind]);
         exit(-1);
     }