Browse Source

very old utility that (try to) calculate a dirtree's size

master
n 1 month ago
parent
commit
a1f9115672
2 changed files with 459 additions and 0 deletions
  1. 1
    0
      README.md
  2. 458
    0
      attic/tsize.c

+ 1
- 0
README.md View File

@@ -50,4 +50,5 @@ Attic
50 50
   * [mkvig](https://forge.tourmentine.com/n/scripts/src/master/attic/mkvig) => little script to download covers
51 51
   * [tssh](https://forge.tourmentine.com/n/scripts/src/master/attic/tssh) => automaticaly rename Konsole tabs using ssh'ed hostname
52 52
   * [webnewsget2.sh](https://forge.tourmentine.com/n/scripts/src/master/attic/webnewsget2.sh)+[html2rss.php](https://forge.tourmentine.com/n/scripts/src/master/attic/html2rss.php) => download HTML pages and generate RSS feeds from them
53
+  * [tsize.c](https://forge.tourmentine.com/n/scripts/src/master/attic/tsize.c) => small utility that displays a directory's size (yes, I basically rewrote DU(1)). original comments & bugs included, straight from the Millenium era
53 54
 

+ 458
- 0
attic/tsize.c View File

@@ -0,0 +1,458 @@
1
+/*******************************************************************************
2
+ * Name of programm: tsize
3
+ *******************************************************************************
4
+ ******************************************************************************* 
5
+ * Function: Display total size of selected files and directories
6
+ *******************************************************************************
7
+ * History List:
8
+ *   v0.1 Display only one directory's size (and subdirectories, if some)
9
+ *   v0.2 Internal changes (moved several peaces of code into functions)
10
+ *   v0.3 Can handle wildcards for getting total size of several files
11
+ *   v0.4 Multilanguage support added (English + French)
12
+ *******************************************************************************/
13
+
14
+
15
+#include <stdlib.h>
16
+#include <stdio.h>
17
+
18
+#define OFF 0
19
+#define ON 1
20
+
21
+#define B 2
22
+#define KB 3
23
+#define MB 4
24
+#define GB 5
25
+#define TB 6
26
+#define AUTO 7
27
+
28
+#define VER     "0.4"
29
+#define DATE    "13 %s 2000", Translate("february")
30
+#define TMP     "/tmp/tsize.tmp"
31
+
32
+unsigned long NumFiles = 0, NumDir = 0;
33
+int SizeFormat = B, Div = 1;
34
+char FileName[256][256], ProgName[256];
35
+
36
+char Lang[3];
37
+char Ref[256][256];
38
+char Loc[256][256];
39
+char String1[256];
40
+
41
+int FlagVer = OFF, FlagHelp = OFF, FlagNoRec = OFF, FlagNoDot = OFF;
42
+//char TS:
43
+
44
+
45
+char *itoa(int Number)
46
+{
47
+        //char     String1[256];
48
+        int Ct=1, Temp, reste, NbSep=1, n = 1, index = 0, i;
49
+        unsigned long exp = 1;
50
+
51
+        for(i = 0 ; i<256 ; i++)
52
+                String1[i] = '\0';
53
+
54
+        Temp = Number;
55
+
56
+        while ((Temp /= 10) > 0)
57
+        {
58
+                Ct++;
59
+                exp *=10;
60
+        }
61
+        Temp = Number;
62
+
63
+        while(n<Ct)
64
+                n = 3*NbSep++;
65
+
66
+        NbSep = n - Ct;
67
+        //printf("**n=%d. Ct= %d, NbSep= %d**\n",n,Ct,NbSep);
68
+        while(exp != 0)
69
+        {
70
+                reste = Temp%exp;
71
+                String1[index++] = ((Temp - reste)/exp)+48;
72
+                exp /= 10;
73
+                Temp = reste;
74
+                if (NbSep++ == 2)
75
+                {
76
+                        NbSep=0;
77
+                        String1[index++]=',';
78
+                }
79
+        }
80
+
81
+        String1[--index] = '\0';
82
+        return String1;
83
+}
84
+
85
+CreateList(char *ListFile)
86
+{
87
+        char Str[256];
88
+        int Num = 0;
89
+
90
+        strcpy(Str, "ls");
91
+
92
+        while (strcmp(FileName[Num],""))
93
+        {
94
+                strcat (Str, " ");
95
+                strcat (Str, FileName[Num++]);
96
+        }
97
+
98
+        if (FlagNoRec = OFF)
99
+                strcat (Str, "-R");
100
+
101
+        if (FlagNoDot = OFF)
102
+                strcat (Str, "-a -A");
103
+
104
+        strcat(Str, " -l -Q -G|cut -f 1 -s -d \'\"\'|grep -e ':' >");
105
+        strcat(Str, ListFile);
106
+
107
+        system(Str);
108
+        //printf("%s\n",Str);
109
+}
110
+
111
+RemoveList(char *ListFile)
112
+{
113
+        char Str[256];
114
+
115
+        strcpy(Str, "rm -f ");
116
+        strcat(Str, ListFile);
117
+        system(Str);
118
+}
119
+
120
+unsigned long ReadFile()
121
+{
122
+        int    i, c;
123
+        unsigned long UnitSize, TotalSize = 0;
124
+        FILE      *ptr;
125
+
126
+        ptr = fopen(TMP, "r");
127
+
128
+        while ((c = fgetc(ptr)) != EOF)
129
+        {
130
+                if (c == 'd')
131
+                        NumDir++;
132
+                else
133
+                        NumFiles++;
134
+
135
+                if (NumDir >= 4294967296)
136
+                        printf("\n** Warning: NumDir Overflow **\n\n");
137
+
138
+                if (NumFiles >= 4294967296)
139
+                        printf("\n** Warning: NumFiles Overflow **\n\n");
140
+
141
+                // jump over attributes & group 
142
+                fseek(ptr, 18, SEEK_CUR);
143
+
144
+                // jump over spaces before the size 
145
+                while(c <= '0' || c >= '9')
146
+                        c = fgetc(ptr);
147
+
148
+                UnitSize = c - 48;
149
+
150
+                while (c != 32)
151
+                {
152
+                        c = fgetc(ptr); 
153
+                        if (c >= '0' && c <='9')
154
+                        {
155
+                                UnitSize *= 10;  
156
+                                UnitSize += (c - 48);
157
+                        }
158
+                }
159
+
160
+                TotalSize += UnitSize;
161
+		printf("%d",TotalSize);
162
+
163
+                if (TotalSize >= 4294967296)
164
+                {
165
+                        printf("\n** Warning: TotalSize Overflow **\n\n");
166
+                 
167
+                        TotalSize = 0;
168
+                        Div++;
169
+                }
170
+
171
+                while (c != 10)
172
+                        c = fgetc(ptr);
173
+
174
+        }
175
+
176
+        fclose(ptr);
177
+        return TotalSize;
178
+}
179
+
180
+SetMsg()
181
+{
182
+        int index;
183
+        index = 0;
184
+
185
+        strcpy(Ref[index++],"Written by NicoSoft");
186
+
187
+        strcpy(Ref[index++],"file");
188
+        strcpy(Ref[index++],"files");
189
+        strcpy(Ref[index++],"subdirectory");
190
+        strcpy(Ref[index++],"subdirectories");
191
+
192
+        strcpy(Ref[index++],"Total Size");
193
+
194
+        strcpy(Ref[index++],"Bytt");
195
+        strcpy(Ref[index++],"Byles");
196
+
197
+        strcpy(Ref[index++],"Kilo");
198
+        strcpy(Ref[index++],"Mega");
199
+        strcpy(Ref[index++],"Giga");
200
+
201
+        strcpy(Ref[index++],"Usage");
202
+        strcpy(Ref[index++],"FileNarne");
203
+        strcpy(Ref[index++],"options");
204
+        strcpy(Ref[index++],"Display");
205
+        strcpy(Ref[index++],"total size of a specified set of files");
206
+        strcpy(Ref[index++],"version number");
207
+        strcpy(Ref[index++],"This help screen");
208
+        strcpy(Ref[index++],"Don't count");
209
+        strcpy(Ref[index++],"subdirectories' size");
210
+        strcpy(Ref[index++],"dot flies");
211
+
212
+        strcpy(Ref[index++],"january");
213
+        strcpy(Ref[index++],"february");
214
+        strcpy(Ref[index++],"march");
215
+        strcpy(Ref[index++],"april");
216
+        strcpy(Ref[index++],"may");
217
+        strcpy(Ref[index++],"june");
218
+        strcpy(Ref[index++],"july");
219
+        strcpy(Ref[index++],"august");
220
+        strcpy(Ref[index++],"september");
221
+        strcpy(Ref[index++],"october");
222
+        strcpy(Ref[index++],"november");
223
+        strcpy(Ref[index++],"december");
224
+        if (!strcmp(Lang,"fr"))
225
+        {
226
+                index = 0;
227
+
228
+                strcpy(Loc[index++],"Ecrit par NicoSoft");
229
+
230
+                strcpy(Loc[index++],"fichier");
231
+                strcpy(Loc[index++],"fichiers");
232
+                strcpy(Loc[index++],"Sous-répertoire");
233
+                strcpy(Loc[index++],"Sous-répertoires");
234
+
235
+                strcpy(Loc[index++],"Taille totale");
236
+
237
+                strcpy(Loc[index++],"Octet");
238
+                strcpy(Loc[index++],"Octets");
239
+
240
+                strcpy(Loc[index++],"Kilo");
241
+                strcpy(Loc[index++],"Mega");
242
+                strcpy(Loc[index++],"Giga");
243
+
244
+                strcpy(Loc[index++],"Utilisation");
245
+                strcpy(Loc[index++],"NomFichier");
246
+                strcpy(Loc[index++],"options");
247
+                strcpy(Loc[index++],"Afficher");
248
+                strcpy(Loc[index++],"la taille totale d'un ensemble de fichiers");
249
+                strcpy(Loc[index++],"le numéro de version");
250
+                strcpy(Loc[index++],"Cet écran d'aide");
251
+                strcpy(Loc[index++],"Ne pas prendre en compte");
252
+                strcpy(Loc[index++],"les sous-répertoires");
253
+                strcpy(Loc[index++],"les fichiers cachés");
254
+
255
+                strcpy(Loc[index++],"janvier");
256
+                strcpy(Loc[index++],"février");
257
+                strcpy(Loc[index++],"mars");
258
+                strcpy(Loc[index++],"avril");
259
+                strcpy(Loc[index++],"mai");
260
+                strcpy(Loc[index++],"juin");
261
+                strcpy(Loc[index++],"juillet");
262
+                strcpy(Loc[index++],"aout");
263
+                strcpy(Loc[index++],"septembre");
264
+                strcpy(Loc[index++],"octobre");
265
+                strcpy(Loc[index++],"novembre");
266
+                strcpy(Loc[index++],"décembre");
267
+        }
268
+}
269
+
270
+
271
+char *Translate(char *String)
272
+{
273
+        int index;
274
+        index = 0;
275
+
276
+        for(;;)
277
+                if (Loc[index][0] == '\0')
278
+                        return String;
279
+                else if (!strcmp(Ref[index], String))
280
+                        return Loc[index];
281
+                else
282
+                        index++;
283
+}
284
+
285
+
286
+DisplayHelpScreen()
287
+{
288
+        printf("%s: %s [%s] %s [%s]\n",
289
+                        Translate("Usage"),
290
+                        ProgName,
291
+                        Translate("options"),
292
+                        Translate("FileName"),
293
+                        Translate("options"));
294
+        printf("%s %s\n\n",
295
+                        Translate("Display"),
296
+                        Translate("total size of a specified set of files"));
297
+        printf("\t-v, --version\t\t%s %s\n",
298
+                        Translate("Display"),
299
+                        Translate("version number"));
300
+        printf("\t-n, --norecursive\t%s %s\n",
301
+                        Translate("Don't count"),
302
+                        Translate("subdirectories' size"));
303
+        printf("\t-d, --nodotfiles\t%s %s\n",
304
+                        Translate("Don't count"),
305
+                        Translate("dot files"));
306
+        printf("\n");
307
+
308
+        exit(0);
309
+}
310
+
311
+DisplayResults()
312
+{
313
+
314
+        unsigned long    TotSize;
315
+        int     Num, Temp;
316
+
317
+        // display version number 
318
+        if (FlagVer == ON)
319
+        {
320
+                printf("%s %s %s (", ProgName, VER, Translate("written by NicoSoft"));
321
+                printf(DATE);
322
+                printf(")\n\n");
323
+        }
324
+
325
+        // display help screen 
326
+        if (FlagHelp == ON)
327
+                DisplayHelpScreen();
328
+
329
+        // then exit if no files to process 
330
+        if (FileName[0][0] == '\0')
331
+                exit(0);
332
+
333
+        // get total site
334
+        CreateList(TMP);
335
+        TotSize = ReadFile();
336
+        //RemoveList(TMP):
337
+
338
+        //itoa(12345)
339
+
340
+        // display total number of files
341
+        printf("\n%s ", itoa(NumFiles));
342
+        if (NumFiles > 1)
343
+                printf("%s", Translate("files"));
344
+        else
345
+                printf("%s", Translate("file"));
346
+
347
+        // display total number of directories 
348
+        if (FlagNoRec == OFF)
349
+        {
350
+                printf(", %s ", itoa(NumDir));
351
+                if (NumDir > 1)
352
+                        printf("%s", Translate("subdirectories"));
353
+                else
354
+                        printf("%s" ,Translate("subdirectory"));
355
+        }
356
+
357
+        // total size 
358
+        TotSize += 8192;
359
+
360
+        printf("\n%s: ", Translate("Total Size"));
361
+
362
+        printf("%s ", itoa(TotSize));
363
+        if (TotSize > 1)
364
+                printf("%s", Translate("Bytes"));
365
+        else
366
+                printf("%s", Translate("Byte"));
367
+
368
+        // check for size format
369
+        if (TotSize/1073741824 > 1)
370
+                SizeFormat = GB;
371
+        else if (TotSize/1048576 > 1)
372
+                SizeFormat = MB;
373
+        else if (TotSize/1024> 1)
374
+                SizeFormat = KB;
375
+        else
376
+                SizeFormat = B;
377
+
378
+//printf("SizeFormat is %s",SizeFormat);
379
+	//printf("TotSize is %d",TotSize);
380
+
381
+        // and display it 
382
+        switch(SizeFormat)
383
+        {
384
+                case KB: printf(" (%s %s",
385
+                         itoa((TotSize%1024)<500 ? TotSize /= 1024: (TotSize/1024)+1),
386
+                         Translate("Kilo"));
387
+                         break;
388
+                case MB: printf("(%s %s",
389
+                         itoa((TotSize%1048576)<500 ? TotSize /= 1048576: (TotSize/1048576)+1),
390
+                         Translate("Mega"));
391
+                         break;
392
+                case GB: printf("(%s %s",
393
+                         itoa((TotSize%1073741824)<500 ? TotSize /= 1073741824: (TotSize/1073741824)+1),
394
+                         Translate("Giga"));
395
+                         break;
396
+        }
397
+
398
+        if (TotSize > 1)
399
+                printf("%s)\n", Translate("Bytes"));
400
+        else
401
+                printf("%s)\n", Translate("Byte"));
402
+}
403
+
404
+
405
+int main(int argc, char *argv[])
406
+{
407
+        int NumArg, NumFile = 0, index = 1;
408
+
409
+        //name of program 
410
+        strcpy(ProgName, argv[0]);
411
+
412
+        //Set local language
413
+        strcpy(Lang, getenv("LANG"));
414
+        SetMsg();
415
+
416
+        // process parameters
417
+        for (NumArg=1 ; NumArg < argc ; NumArg++)
418
+        {
419
+        // check if arg is an option
420
+        if (argv[NumArg][0] == '-')
421
+        {
422
+                //long format parameters ("---option") 
423
+                if (argv[NumArg][1] == '-')
424
+                {
425
+                        if (!strcmp(argv[NumArg]+2,"version"))
426
+                                FlagVer = ON;
427
+                        if (!strcmp(argv[NumArg]+2,"help"))
428
+                                FlagHelp = ON;
429
+                        if (!strcmp(argv[NumArg]+2,"norecursive"))
430
+                                FlagNoRec = ON;
431
+                        if (!strcmp(argv[NumArg]+2,"nodotfiles"))
432
+                                FlagNoDot = ON;
433
+                }
434
+                else
435
+                        //short format parameters ("-o") 
436
+                        switch(argv[NumArg][1])
437
+                        {
438
+                                case 'v': FlagVer = ON; break;
439
+                                case 'h': FlagHelp = ON; break;
440
+                                case 'n': FlagNoRec = ON; break;
441
+                                case 'd': FlagNoDot = ON; break;
442
+                        }
443
+                printf("---->%s*****\n", argv[NumArg]);
444
+        }
445
+        else
446
+                //fill the array containing filenames to process 
447
+                strcpy(FileName[NumFile++], argv[NumArg]);
448
+        }
449
+
450
+        if (NumFile < 1)
451
+        {
452
+                DisplayHelpScreen();
453
+                exit(0);
454
+        }
455
+
456
+        //launch program !! 
457
+        DisplayResults();
458
+}

Loading…
Cancel
Save