various scripts for various tasks.
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

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