mdu.c

Go to the documentation of this file.
00001 /*
00002  * mdu.c:
00003  * Display the space occupied by an MSDOS directory
00004  */
00005 
00006 #include "sysincludes.h"
00007 #include "msdos.h"
00008 #include "vfat.h"
00009 #include "mtools.h"
00010 #include "file.h"
00011 #include "mainloop.h"
00012 #include "fs.h"
00013 #include "codepage.h"
00014 
00015 
00016 typedef struct Arg_t {
00017         int all;
00018         int inDir;
00019         int summary;
00020         struct Arg_t *parent;
00021         char *target;
00022         char *path;
00023         unsigned int blocks;
00024         MainParam_t mp;
00025 } Arg_t;
00026 
00027 static void usage(void)
00028 {
00029                 fprintf(stderr, "Mtools version %s, dated %s\n",
00030                         mversion, mdate);
00031                 fprintf(stderr, "Usage: %s [-as] msdosdirectory\n"
00032                         "\t-a All (also show individual files)\n"
00033                         "\t-s Summary for directory only\n",
00034                         progname);
00035                 exit(1);
00036 }
00037 
00038 static int file_mdu(direntry_t *entry, MainParam_t *mp)
00039 {
00040         unsigned int blocks;
00041         Arg_t * arg = (Arg_t *) (mp->arg);
00042 
00043         blocks = countBlocks(entry->Dir,getStart(entry->Dir, &entry->dir));
00044         if(arg->all || !arg->inDir) {
00045                 printf("%-7d ", blocks);
00046                 fprintPwd(stdout, entry,0);
00047                 fputc('\n', stdout);
00048         }
00049         arg->blocks += blocks;
00050         return GOT_ONE;
00051 }
00052 
00053 
00054 static int dir_mdu(direntry_t *entry, MainParam_t *mp)
00055 {
00056         Arg_t *parentArg = (Arg_t *) (mp->arg);
00057         Arg_t arg;
00058         int ret;
00059         
00060         arg = *parentArg;
00061         arg.mp.arg = (void *) &arg;
00062         arg.parent = parentArg;
00063         arg.inDir = 1;
00064 
00065         /* account for the space occupied by the directory itself */
00066         if(!isRootDir(entry->Dir)) {
00067                 arg.blocks = countBlocks(entry->Dir,
00068                                          getStart(entry->Dir, &entry->dir));
00069         } else {
00070                 arg.blocks = 0;
00071         }
00072 
00073         /* recursion */
00074         ret = mp->loop(mp->File, &arg.mp, "*");
00075         if(!arg.summary || !parentArg->inDir) {
00076                 printf("%-7d ", arg.blocks);
00077                 fprintPwd(stdout, entry,0);
00078                 fputc('\n', stdout);
00079         }
00080         arg.parent->blocks += arg.blocks;
00081         return ret;
00082 }
00083 
00084 void mdu(int argc, char **argv, int type)
00085 {
00086         Arg_t arg;
00087         int c;
00088 
00089         arg.all = 0;
00090         arg.inDir = 0;
00091         arg.summary = 0;
00092         while ((c = getopt(argc, argv, "as")) != EOF) {
00093                 switch (c) {
00094                         case 'a':
00095                                 arg.all = 1;
00096                                 break;
00097                         case 's':
00098                                 arg.summary = 1;
00099                                 break;
00100                         case '?':
00101                                 usage();
00102                 }
00103         }
00104 
00105         if (optind >= argc)
00106                 usage();
00107 
00108         if(arg.summary && arg.all) {
00109                 fprintf(stderr,"-a and -s options are mutually exclusive\n");
00110                 usage();
00111         }
00112 
00113         init_mp(&arg.mp);
00114         arg.mp.callback = file_mdu;
00115         arg.mp.openflags = O_RDONLY;
00116         arg.mp.dirCallback = dir_mdu;
00117 
00118         arg.mp.arg = (void *) &arg;
00119         arg.mp.lookupflags = ACCEPT_PLAIN | ACCEPT_DIR | DO_OPEN_DIRS | NO_DOTS;
00120         exit(main_loop(&arg.mp, argv + optind, argc - optind));
00121 }

Generated on Fri Apr 14 22:56:55 2006 for minix by  doxygen 1.4.6