source: trunk/minix/commands/i386/mtools-3.9.7/mdu.c@ 9

Last change on this file since 9 was 9, checked in by Mattia Monga, 13 years ago

Minix 3.1.2a

File size: 2.3 KB
RevLine 
[9]1/*
2 * mdu.c:
3 * Display the space occupied by an MSDOS directory
4 */
5
6#include "sysincludes.h"
7#include "msdos.h"
8#include "vfat.h"
9#include "mtools.h"
10#include "file.h"
11#include "mainloop.h"
12#include "fs.h"
13#include "codepage.h"
14
15
16typedef struct Arg_t {
17 int all;
18 int inDir;
19 int summary;
20 struct Arg_t *parent;
21 char *target;
22 char *path;
23 unsigned int blocks;
24 MainParam_t mp;
25} Arg_t;
26
27static void usage(void)
28{
29 fprintf(stderr, "Mtools version %s, dated %s\n",
30 mversion, mdate);
31 fprintf(stderr, "Usage: %s [-as] msdosdirectory\n"
32 "\t-a All (also show individual files)\n"
33 "\t-s Summary for directory only\n",
34 progname);
35 exit(1);
36}
37
38static int file_mdu(direntry_t *entry, MainParam_t *mp)
39{
40 unsigned int blocks;
41 Arg_t * arg = (Arg_t *) (mp->arg);
42
43 blocks = countBlocks(entry->Dir,getStart(entry->Dir, &entry->dir));
44 if(arg->all || !arg->inDir) {
45 printf("%-7d ", blocks);
46 fprintPwd(stdout, entry,0);
47 fputc('\n', stdout);
48 }
49 arg->blocks += blocks;
50 return GOT_ONE;
51}
52
53
54static int dir_mdu(direntry_t *entry, MainParam_t *mp)
55{
56 Arg_t *parentArg = (Arg_t *) (mp->arg);
57 Arg_t arg;
58 int ret;
59
60 arg = *parentArg;
61 arg.mp.arg = (void *) &arg;
62 arg.parent = parentArg;
63 arg.inDir = 1;
64
65 /* account for the space occupied by the directory itself */
66 if(!isRootDir(entry->Dir)) {
67 arg.blocks = countBlocks(entry->Dir,
68 getStart(entry->Dir, &entry->dir));
69 } else {
70 arg.blocks = 0;
71 }
72
73 /* recursion */
74 ret = mp->loop(mp->File, &arg.mp, "*");
75 if(!arg.summary || !parentArg->inDir) {
76 printf("%-7d ", arg.blocks);
77 fprintPwd(stdout, entry,0);
78 fputc('\n', stdout);
79 }
80 arg.parent->blocks += arg.blocks;
81 return ret;
82}
83
84void mdu(int argc, char **argv, int type)
85{
86 Arg_t arg;
87 int c;
88
89 arg.all = 0;
90 arg.inDir = 0;
91 arg.summary = 0;
92 while ((c = getopt(argc, argv, "as")) != EOF) {
93 switch (c) {
94 case 'a':
95 arg.all = 1;
96 break;
97 case 's':
98 arg.summary = 1;
99 break;
100 case '?':
101 usage();
102 }
103 }
104
105 if (optind >= argc)
106 usage();
107
108 if(arg.summary && arg.all) {
109 fprintf(stderr,"-a and -s options are mutually exclusive\n");
110 usage();
111 }
112
113 init_mp(&arg.mp);
114 arg.mp.callback = file_mdu;
115 arg.mp.openflags = O_RDONLY;
116 arg.mp.dirCallback = dir_mdu;
117
118 arg.mp.arg = (void *) &arg;
119 arg.mp.lookupflags = ACCEPT_PLAIN | ACCEPT_DIR | DO_OPEN_DIRS | NO_DOTS;
120 exit(main_loop(&arg.mp, argv + optind, argc - optind));
121}
Note: See TracBrowser for help on using the repository browser.