459 lines
13 KiB
C
459 lines
13 KiB
C
|
/*******************************************************************************
|
||
|
* Name of programm: tsize
|
||
|
*******************************************************************************
|
||
|
*******************************************************************************
|
||
|
* Function: Display total size of selected files and directories
|
||
|
*******************************************************************************
|
||
|
* History List:
|
||
|
* v0.1 Display only one directory's size (and subdirectories, if some)
|
||
|
* v0.2 Internal changes (moved several peaces of code into functions)
|
||
|
* v0.3 Can handle wildcards for getting total size of several files
|
||
|
* v0.4 Multilanguage support added (English + French)
|
||
|
*******************************************************************************/
|
||
|
|
||
|
|
||
|
#include <stdlib.h>
|
||
|
#include <stdio.h>
|
||
|
|
||
|
#define OFF 0
|
||
|
#define ON 1
|
||
|
|
||
|
#define B 2
|
||
|
#define KB 3
|
||
|
#define MB 4
|
||
|
#define GB 5
|
||
|
#define TB 6
|
||
|
#define AUTO 7
|
||
|
|
||
|
#define VER "0.4"
|
||
|
#define DATE "13 %s 2000", Translate("february")
|
||
|
#define TMP "/tmp/tsize.tmp"
|
||
|
|
||
|
unsigned long NumFiles = 0, NumDir = 0;
|
||
|
int SizeFormat = B, Div = 1;
|
||
|
char FileName[256][256], ProgName[256];
|
||
|
|
||
|
char Lang[3];
|
||
|
char Ref[256][256];
|
||
|
char Loc[256][256];
|
||
|
char String1[256];
|
||
|
|
||
|
int FlagVer = OFF, FlagHelp = OFF, FlagNoRec = OFF, FlagNoDot = OFF;
|
||
|
//char TS:
|
||
|
|
||
|
|
||
|
char *itoa(int Number)
|
||
|
{
|
||
|
//char String1[256];
|
||
|
int Ct=1, Temp, reste, NbSep=1, n = 1, index = 0, i;
|
||
|
unsigned long exp = 1;
|
||
|
|
||
|
for(i = 0 ; i<256 ; i++)
|
||
|
String1[i] = '\0';
|
||
|
|
||
|
Temp = Number;
|
||
|
|
||
|
while ((Temp /= 10) > 0)
|
||
|
{
|
||
|
Ct++;
|
||
|
exp *=10;
|
||
|
}
|
||
|
Temp = Number;
|
||
|
|
||
|
while(n<Ct)
|
||
|
n = 3*NbSep++;
|
||
|
|
||
|
NbSep = n - Ct;
|
||
|
//printf("**n=%d. Ct= %d, NbSep= %d**\n",n,Ct,NbSep);
|
||
|
while(exp != 0)
|
||
|
{
|
||
|
reste = Temp%exp;
|
||
|
String1[index++] = ((Temp - reste)/exp)+48;
|
||
|
exp /= 10;
|
||
|
Temp = reste;
|
||
|
if (NbSep++ == 2)
|
||
|
{
|
||
|
NbSep=0;
|
||
|
String1[index++]=',';
|
||
|
}
|
||
|
}
|
||
|
|
||
|
String1[--index] = '\0';
|
||
|
return String1;
|
||
|
}
|
||
|
|
||
|
CreateList(char *ListFile)
|
||
|
{
|
||
|
char Str[256];
|
||
|
int Num = 0;
|
||
|
|
||
|
strcpy(Str, "ls");
|
||
|
|
||
|
while (strcmp(FileName[Num],""))
|
||
|
{
|
||
|
strcat (Str, " ");
|
||
|
strcat (Str, FileName[Num++]);
|
||
|
}
|
||
|
|
||
|
if (FlagNoRec = OFF)
|
||
|
strcat (Str, "-R");
|
||
|
|
||
|
if (FlagNoDot = OFF)
|
||
|
strcat (Str, "-a -A");
|
||
|
|
||
|
strcat(Str, " -l -Q -G|cut -f 1 -s -d \'\"\'|grep -e ':' >");
|
||
|
strcat(Str, ListFile);
|
||
|
|
||
|
system(Str);
|
||
|
//printf("%s\n",Str);
|
||
|
}
|
||
|
|
||
|
RemoveList(char *ListFile)
|
||
|
{
|
||
|
char Str[256];
|
||
|
|
||
|
strcpy(Str, "rm -f ");
|
||
|
strcat(Str, ListFile);
|
||
|
system(Str);
|
||
|
}
|
||
|
|
||
|
unsigned long ReadFile()
|
||
|
{
|
||
|
int i, c;
|
||
|
unsigned long UnitSize, TotalSize = 0;
|
||
|
FILE *ptr;
|
||
|
|
||
|
ptr = fopen(TMP, "r");
|
||
|
|
||
|
while ((c = fgetc(ptr)) != EOF)
|
||
|
{
|
||
|
if (c == 'd')
|
||
|
NumDir++;
|
||
|
else
|
||
|
NumFiles++;
|
||
|
|
||
|
if (NumDir >= 4294967296)
|
||
|
printf("\n** Warning: NumDir Overflow **\n\n");
|
||
|
|
||
|
if (NumFiles >= 4294967296)
|
||
|
printf("\n** Warning: NumFiles Overflow **\n\n");
|
||
|
|
||
|
// jump over attributes & group
|
||
|
fseek(ptr, 18, SEEK_CUR);
|
||
|
|
||
|
// jump over spaces before the size
|
||
|
while(c <= '0' || c >= '9')
|
||
|
c = fgetc(ptr);
|
||
|
|
||
|
UnitSize = c - 48;
|
||
|
|
||
|
while (c != 32)
|
||
|
{
|
||
|
c = fgetc(ptr);
|
||
|
if (c >= '0' && c <='9')
|
||
|
{
|
||
|
UnitSize *= 10;
|
||
|
UnitSize += (c - 48);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
TotalSize += UnitSize;
|
||
|
printf("%d",TotalSize);
|
||
|
|
||
|
if (TotalSize >= 4294967296)
|
||
|
{
|
||
|
printf("\n** Warning: TotalSize Overflow **\n\n");
|
||
|
|
||
|
TotalSize = 0;
|
||
|
Div++;
|
||
|
}
|
||
|
|
||
|
while (c != 10)
|
||
|
c = fgetc(ptr);
|
||
|
|
||
|
}
|
||
|
|
||
|
fclose(ptr);
|
||
|
return TotalSize;
|
||
|
}
|
||
|
|
||
|
SetMsg()
|
||
|
{
|
||
|
int index;
|
||
|
index = 0;
|
||
|
|
||
|
strcpy(Ref[index++],"Written by NicoSoft");
|
||
|
|
||
|
strcpy(Ref[index++],"file");
|
||
|
strcpy(Ref[index++],"files");
|
||
|
strcpy(Ref[index++],"subdirectory");
|
||
|
strcpy(Ref[index++],"subdirectories");
|
||
|
|
||
|
strcpy(Ref[index++],"Total Size");
|
||
|
|
||
|
strcpy(Ref[index++],"Bytt");
|
||
|
strcpy(Ref[index++],"Byles");
|
||
|
|
||
|
strcpy(Ref[index++],"Kilo");
|
||
|
strcpy(Ref[index++],"Mega");
|
||
|
strcpy(Ref[index++],"Giga");
|
||
|
|
||
|
strcpy(Ref[index++],"Usage");
|
||
|
strcpy(Ref[index++],"FileNarne");
|
||
|
strcpy(Ref[index++],"options");
|
||
|
strcpy(Ref[index++],"Display");
|
||
|
strcpy(Ref[index++],"total size of a specified set of files");
|
||
|
strcpy(Ref[index++],"version number");
|
||
|
strcpy(Ref[index++],"This help screen");
|
||
|
strcpy(Ref[index++],"Don't count");
|
||
|
strcpy(Ref[index++],"subdirectories' size");
|
||
|
strcpy(Ref[index++],"dot flies");
|
||
|
|
||
|
strcpy(Ref[index++],"january");
|
||
|
strcpy(Ref[index++],"february");
|
||
|
strcpy(Ref[index++],"march");
|
||
|
strcpy(Ref[index++],"april");
|
||
|
strcpy(Ref[index++],"may");
|
||
|
strcpy(Ref[index++],"june");
|
||
|
strcpy(Ref[index++],"july");
|
||
|
strcpy(Ref[index++],"august");
|
||
|
strcpy(Ref[index++],"september");
|
||
|
strcpy(Ref[index++],"october");
|
||
|
strcpy(Ref[index++],"november");
|
||
|
strcpy(Ref[index++],"december");
|
||
|
if (!strcmp(Lang,"fr"))
|
||
|
{
|
||
|
index = 0;
|
||
|
|
||
|
strcpy(Loc[index++],"Ecrit par NicoSoft");
|
||
|
|
||
|
strcpy(Loc[index++],"fichier");
|
||
|
strcpy(Loc[index++],"fichiers");
|
||
|
strcpy(Loc[index++],"Sous-répertoire");
|
||
|
strcpy(Loc[index++],"Sous-répertoires");
|
||
|
|
||
|
strcpy(Loc[index++],"Taille totale");
|
||
|
|
||
|
strcpy(Loc[index++],"Octet");
|
||
|
strcpy(Loc[index++],"Octets");
|
||
|
|
||
|
strcpy(Loc[index++],"Kilo");
|
||
|
strcpy(Loc[index++],"Mega");
|
||
|
strcpy(Loc[index++],"Giga");
|
||
|
|
||
|
strcpy(Loc[index++],"Utilisation");
|
||
|
strcpy(Loc[index++],"NomFichier");
|
||
|
strcpy(Loc[index++],"options");
|
||
|
strcpy(Loc[index++],"Afficher");
|
||
|
strcpy(Loc[index++],"la taille totale d'un ensemble de fichiers");
|
||
|
strcpy(Loc[index++],"le numéro de version");
|
||
|
strcpy(Loc[index++],"Cet écran d'aide");
|
||
|
strcpy(Loc[index++],"Ne pas prendre en compte");
|
||
|
strcpy(Loc[index++],"les sous-répertoires");
|
||
|
strcpy(Loc[index++],"les fichiers cachés");
|
||
|
|
||
|
strcpy(Loc[index++],"janvier");
|
||
|
strcpy(Loc[index++],"février");
|
||
|
strcpy(Loc[index++],"mars");
|
||
|
strcpy(Loc[index++],"avril");
|
||
|
strcpy(Loc[index++],"mai");
|
||
|
strcpy(Loc[index++],"juin");
|
||
|
strcpy(Loc[index++],"juillet");
|
||
|
strcpy(Loc[index++],"aout");
|
||
|
strcpy(Loc[index++],"septembre");
|
||
|
strcpy(Loc[index++],"octobre");
|
||
|
strcpy(Loc[index++],"novembre");
|
||
|
strcpy(Loc[index++],"décembre");
|
||
|
}
|
||
|
}
|
||
|
|
||
|
|
||
|
char *Translate(char *String)
|
||
|
{
|
||
|
int index;
|
||
|
index = 0;
|
||
|
|
||
|
for(;;)
|
||
|
if (Loc[index][0] == '\0')
|
||
|
return String;
|
||
|
else if (!strcmp(Ref[index], String))
|
||
|
return Loc[index];
|
||
|
else
|
||
|
index++;
|
||
|
}
|
||
|
|
||
|
|
||
|
DisplayHelpScreen()
|
||
|
{
|
||
|
printf("%s: %s [%s] %s [%s]\n",
|
||
|
Translate("Usage"),
|
||
|
ProgName,
|
||
|
Translate("options"),
|
||
|
Translate("FileName"),
|
||
|
Translate("options"));
|
||
|
printf("%s %s\n\n",
|
||
|
Translate("Display"),
|
||
|
Translate("total size of a specified set of files"));
|
||
|
printf("\t-v, --version\t\t%s %s\n",
|
||
|
Translate("Display"),
|
||
|
Translate("version number"));
|
||
|
printf("\t-n, --norecursive\t%s %s\n",
|
||
|
Translate("Don't count"),
|
||
|
Translate("subdirectories' size"));
|
||
|
printf("\t-d, --nodotfiles\t%s %s\n",
|
||
|
Translate("Don't count"),
|
||
|
Translate("dot files"));
|
||
|
printf("\n");
|
||
|
|
||
|
exit(0);
|
||
|
}
|
||
|
|
||
|
DisplayResults()
|
||
|
{
|
||
|
|
||
|
unsigned long TotSize;
|
||
|
int Num, Temp;
|
||
|
|
||
|
// display version number
|
||
|
if (FlagVer == ON)
|
||
|
{
|
||
|
printf("%s %s %s (", ProgName, VER, Translate("written by NicoSoft"));
|
||
|
printf(DATE);
|
||
|
printf(")\n\n");
|
||
|
}
|
||
|
|
||
|
// display help screen
|
||
|
if (FlagHelp == ON)
|
||
|
DisplayHelpScreen();
|
||
|
|
||
|
// then exit if no files to process
|
||
|
if (FileName[0][0] == '\0')
|
||
|
exit(0);
|
||
|
|
||
|
// get total site
|
||
|
CreateList(TMP);
|
||
|
TotSize = ReadFile();
|
||
|
//RemoveList(TMP):
|
||
|
|
||
|
//itoa(12345)
|
||
|
|
||
|
// display total number of files
|
||
|
printf("\n%s ", itoa(NumFiles));
|
||
|
if (NumFiles > 1)
|
||
|
printf("%s", Translate("files"));
|
||
|
else
|
||
|
printf("%s", Translate("file"));
|
||
|
|
||
|
// display total number of directories
|
||
|
if (FlagNoRec == OFF)
|
||
|
{
|
||
|
printf(", %s ", itoa(NumDir));
|
||
|
if (NumDir > 1)
|
||
|
printf("%s", Translate("subdirectories"));
|
||
|
else
|
||
|
printf("%s" ,Translate("subdirectory"));
|
||
|
}
|
||
|
|
||
|
// total size
|
||
|
TotSize += 8192;
|
||
|
|
||
|
printf("\n%s: ", Translate("Total Size"));
|
||
|
|
||
|
printf("%s ", itoa(TotSize));
|
||
|
if (TotSize > 1)
|
||
|
printf("%s", Translate("Bytes"));
|
||
|
else
|
||
|
printf("%s", Translate("Byte"));
|
||
|
|
||
|
// check for size format
|
||
|
if (TotSize/1073741824 > 1)
|
||
|
SizeFormat = GB;
|
||
|
else if (TotSize/1048576 > 1)
|
||
|
SizeFormat = MB;
|
||
|
else if (TotSize/1024> 1)
|
||
|
SizeFormat = KB;
|
||
|
else
|
||
|
SizeFormat = B;
|
||
|
|
||
|
//printf("SizeFormat is %s",SizeFormat);
|
||
|
//printf("TotSize is %d",TotSize);
|
||
|
|
||
|
// and display it
|
||
|
switch(SizeFormat)
|
||
|
{
|
||
|
case KB: printf(" (%s %s",
|
||
|
itoa((TotSize%1024)<500 ? TotSize /= 1024: (TotSize/1024)+1),
|
||
|
Translate("Kilo"));
|
||
|
break;
|
||
|
case MB: printf("(%s %s",
|
||
|
itoa((TotSize%1048576)<500 ? TotSize /= 1048576: (TotSize/1048576)+1),
|
||
|
Translate("Mega"));
|
||
|
break;
|
||
|
case GB: printf("(%s %s",
|
||
|
itoa((TotSize%1073741824)<500 ? TotSize /= 1073741824: (TotSize/1073741824)+1),
|
||
|
Translate("Giga"));
|
||
|
break;
|
||
|
}
|
||
|
|
||
|
if (TotSize > 1)
|
||
|
printf("%s)\n", Translate("Bytes"));
|
||
|
else
|
||
|
printf("%s)\n", Translate("Byte"));
|
||
|
}
|
||
|
|
||
|
|
||
|
int main(int argc, char *argv[])
|
||
|
{
|
||
|
int NumArg, NumFile = 0, index = 1;
|
||
|
|
||
|
//name of program
|
||
|
strcpy(ProgName, argv[0]);
|
||
|
|
||
|
//Set local language
|
||
|
strcpy(Lang, getenv("LANG"));
|
||
|
SetMsg();
|
||
|
|
||
|
// process parameters
|
||
|
for (NumArg=1 ; NumArg < argc ; NumArg++)
|
||
|
{
|
||
|
// check if arg is an option
|
||
|
if (argv[NumArg][0] == '-')
|
||
|
{
|
||
|
//long format parameters ("---option")
|
||
|
if (argv[NumArg][1] == '-')
|
||
|
{
|
||
|
if (!strcmp(argv[NumArg]+2,"version"))
|
||
|
FlagVer = ON;
|
||
|
if (!strcmp(argv[NumArg]+2,"help"))
|
||
|
FlagHelp = ON;
|
||
|
if (!strcmp(argv[NumArg]+2,"norecursive"))
|
||
|
FlagNoRec = ON;
|
||
|
if (!strcmp(argv[NumArg]+2,"nodotfiles"))
|
||
|
FlagNoDot = ON;
|
||
|
}
|
||
|
else
|
||
|
//short format parameters ("-o")
|
||
|
switch(argv[NumArg][1])
|
||
|
{
|
||
|
case 'v': FlagVer = ON; break;
|
||
|
case 'h': FlagHelp = ON; break;
|
||
|
case 'n': FlagNoRec = ON; break;
|
||
|
case 'd': FlagNoDot = ON; break;
|
||
|
}
|
||
|
printf("---->%s*****\n", argv[NumArg]);
|
||
|
}
|
||
|
else
|
||
|
//fill the array containing filenames to process
|
||
|
strcpy(FileName[NumFile++], argv[NumArg]);
|
||
|
}
|
||
|
|
||
|
if (NumFile < 1)
|
||
|
{
|
||
|
DisplayHelpScreen();
|
||
|
exit(0);
|
||
|
}
|
||
|
|
||
|
//launch program !!
|
||
|
DisplayResults();
|
||
|
}
|