@@ -52,10 +52,10 @@ static unsigned long long to_blocks(unsigned long long bytes, Unit unit) {
5252 }
5353}
5454
55- static const char * get_fs_type (const char * root) {
55+ static std::string get_fs_type_str (const char * root) {
5656 char fs_name[256 ] = {};
5757 if (GetVolumeInformationA (root, NULL , 0 , NULL , NULL , NULL , fs_name, sizeof (fs_name)))
58- return _strdup ( fs_name) ;
58+ return fs_name;
5959 return " unknown" ;
6060}
6161
@@ -80,24 +80,54 @@ int main(int argc, char* argv[]) {
8080 for (int i = 1 ; i < argc; i++) {
8181 char * a = argv[i];
8282 if (strcmp (a, " --help" ) == 0 ) { usage (); return 0 ; }
83- else if (strcmp (a, " -a" ) == 0 || strcmp (a, " --all" ) == 0 ) opt_all = true ;
84- else if (strcmp (a, " -h" ) == 0 || strcmp (a, " --human-readable" ) == 0 ) { opt_h = true ; unit = HUMAN_1024; }
85- else if (strcmp (a, " -H" ) == 0 || strcmp (a, " --si" ) == 0 ) { opt_H = true ; unit = HUMAN_SI; }
86- else if (strcmp (a, " -i" ) == 0 || strcmp (a, " --inodes" ) == 0 ) opt_i = true ;
87- else if (strcmp (a, " -k" ) == 0 ) { opt_k = true ; unit = BLOCK_1K; }
88- else if (strcmp (a, " -l" ) == 0 || strcmp (a, " --local" ) == 0 ) opt_l = true ;
89- else if (strcmp (a, " -T" ) == 0 || strcmp (a, " --print-type" ) == 0 ) opt_T = true ;
90- else if (strcmp (a, " -P" ) == 0 || strcmp (a, " --portability" ) == 0 ) opt_P = true ;
91- else if ((strcmp (a, " -t" ) == 0 || strcmp (a, " --type" ) == 0 ) && i+1 < argc)
83+ else if (strcmp (a, " --all" ) == 0 ) opt_all = true ;
84+ else if (strcmp (a, " --human-readable" ) == 0 ) { opt_h = true ; unit = HUMAN_1024; }
85+ else if (strcmp (a, " --si" ) == 0 ) { opt_H = true ; unit = HUMAN_SI; }
86+ else if (strcmp (a, " --inodes" ) == 0 ) opt_i = true ;
87+ else if (strcmp (a, " --local" ) == 0 ) opt_l = true ;
88+ else if (strcmp (a, " --print-type" ) == 0 ) opt_T = true ;
89+ else if (strcmp (a, " --portability" ) == 0 ) opt_P = true ;
90+ else if (strcmp (a, " --type" ) == 0 && i+1 < argc)
9291 filter_types.push_back (argv[++i]);
9392 else if (strncmp (a, " --type=" , 7 ) == 0 ) filter_types.push_back (a+7 );
94- else if (( strcmp (a, " -x " ) == 0 || strcmp (a, " -- exclude-type" ) == 0 ) && i+1 < argc)
93+ else if (strcmp (a, " -- exclude-type" ) == 0 && i+1 < argc)
9594 exclude_types.push_back (argv[++i]);
9695 else if (strncmp (a, " --exclude-type=" , 15 ) == 0 ) exclude_types.push_back (a+15 );
97- else if (strncmp (a, " -B" , 2 ) == 0 || strncmp (a, " --block-size=" , 13 ) == 0 ) {
98- const char * s = (a[1 ]==' B' && strlen (a)>2 ) ? a+2 : (strncmp (a," --block-size=" ,13 )==0 ? a+13 : NULL );
99- if (!s && i+1 < argc) s = argv[++i];
100- if (s) block_size = (unsigned long long )atoll (s);
96+ else if (strncmp (a, " --block-size=" , 13 ) == 0 ) {
97+ block_size = (unsigned long long )atoll (a+13 );
98+ }
99+ else if (a[0 ] == ' -' && a[1 ] != ' -' ) {
100+ // Handle combined short flags: -Th, -ahT, etc.
101+ bool err = false ;
102+ for (int j = 1 ; a[j] && !err; j++) {
103+ switch (a[j]) {
104+ case ' a' : opt_all = true ; break ;
105+ case ' h' : opt_h = true ; unit = HUMAN_1024; break ;
106+ case ' H' : opt_H = true ; unit = HUMAN_SI; break ;
107+ case ' i' : opt_i = true ; break ;
108+ case ' k' : opt_k = true ; unit = BLOCK_1K; break ;
109+ case ' l' : opt_l = true ; break ;
110+ case ' T' : opt_T = true ; break ;
111+ case ' P' : opt_P = true ; break ;
112+ case ' t' :
113+ if (a[j+1 ]) { filter_types.push_back (a+j+1 ); j=(int )strlen (a)-1 ; }
114+ else if (i+1 < argc) filter_types.push_back (argv[++i]);
115+ break ;
116+ case ' x' :
117+ if (a[j+1 ]) { exclude_types.push_back (a+j+1 ); j=(int )strlen (a)-1 ; }
118+ else if (i+1 < argc) exclude_types.push_back (argv[++i]);
119+ break ;
120+ case ' B' :
121+ if (a[j+1 ]) { block_size=(unsigned long long )atoll (a+j+1 ); j=(int )strlen (a)-1 ; }
122+ else if (i+1 < argc) block_size=(unsigned long long )atoll (argv[++i]);
123+ break ;
124+ default :
125+ fprintf (stderr, " df: invalid option -- '%c'\n " , a[j]);
126+ fprintf (stderr, " Try 'df --help' for more information.\n " );
127+ err = true ;
128+ }
129+ }
130+ if (err) return 1 ;
101131 }
102132 else if (a[0 ] != ' -' ) paths.push_back (a);
103133 else {
@@ -129,17 +159,17 @@ int main(int argc, char* argv[]) {
129159 UINT dtype = GetDriveTypeA (root);
130160 if (opt_l && dtype == DRIVE_REMOTE) return ;
131161
132- const char * fstype = get_fs_type (root);
162+ std::string fstype = get_fs_type_str (root);
133163
134164 // filter by type
135165 if (!filter_types.empty ()) {
136166 bool found = false ;
137167 for (auto & t : filter_types)
138- if (_stricmp (t.c_str (), fstype) == 0 ) { found = true ; break ; }
168+ if (_stricmp (t.c_str (), fstype. c_str () ) == 0 ) { found = true ; break ; }
139169 if (!found) return ;
140170 }
141171 for (auto & t : exclude_types)
142- if (_stricmp (t.c_str (), fstype) == 0 ) return ;
172+ if (_stricmp (t.c_str (), fstype. c_str () ) == 0 ) return ;
143173
144174 unsigned long long total = totalBytes.QuadPart ;
145175 unsigned long long avail = freeBytesAvail.QuadPart ;
@@ -181,7 +211,7 @@ int main(int argc, char* argv[]) {
181211
182212 if (opt_T) {
183213 printf (" %-20s %-10s %12s %12s %12s %5s %s\n " ,
184- root_trimmed, fstype, size_str, used_str, avail_str, pct_str, mount);
214+ root_trimmed, fstype. c_str () , size_str, used_str, avail_str, pct_str, mount);
185215 } else {
186216 printf (" %-20s %12s %12s %12s %5s %s\n " ,
187217 root_trimmed, size_str, used_str, avail_str, pct_str, mount);
0 commit comments