#include #include #include #include #include #include #include typedef double Size; static int FilesToo=0; static long Koot=1024; static int Info=1; static int PrintTotal=0; static int UsePrepare=1; #ifdef DJGPP int __opendir_flags = __OPENDIR_FIND_HIDDEN; #endif static int EdKoko=0; static void _Display(FILE *fp, Size size, char *s) { int a; while((s[0]=='/'||s[0]=='\\') &&(s[1]=='/'||s[1]=='\\'))s++; if(Koot==1) fprintf(fp, "%-11.0f%s", size/Koot, s); else fprintf(fp, "%-8.0f%s", size/Koot, s); for(a=EdKoko-strlen(s); a>0; a--)fputc(' ', fp); } static void Display(Size size, char *s) { _Display(stdout, size, s); printf("\n"); EdKoko=0; } static void Prepare(Size size, char *s) { _Display(stderr, size, s); fprintf(stderr, "\r"); fflush(stderr); EdKoko=strlen(s); } static Size Laske(char *dn) { DIR *dir = opendir(dn); struct dirent *ent; Size size; struct stat st; if(stat(dn, &st)) { printf("du: Can't stat '%s' (%s)\n", dn, sys_errlist[errno]); return 0; } if(!(st.st_mode&S_IFDIR)) { if(FilesToo)Display(st.st_size, dn); return st.st_size; } if(!dir) { printf("du: Can't open '%s' (%s)\n", dn, sys_errlist[errno]); return 0; } size=0; while((ent = readdir(dir)) != NULL) { char *Buf; if(!strcmp(ent->d_name, ".") || !strcmp(ent->d_name, ".."))continue; Buf = (char *)malloc(strlen(dn)+strlen(ent->d_name)+2); strcpy(Buf, dn); strcat(Buf, "/"); strcat(Buf, ent->d_name); size += Laske(Buf); if(UsePrepare)Prepare(size, Buf); free(Buf); } closedir(dir); if(Info)Display(size, dn); return size; } static void A(void) { FilesToo=1; } static void B(void) { Koot=1; } static void C(void) { PrintTotal=1; } static void K(void) { Koot=1024; } static void N(void) { UsePrepare=0; } static void S(void) { Info=0; } static void V(void) { printf("FreeDU v1.00, copyright (C) 1992,1999 Bisqwit (http://iki.fi/bisqwit/)\n"); exit(0); } static void H(void); static struct { void (*Func)(void); char c; char *txt; char *descr; } Options[] = { {A,'a', "all", "write counts for all files, not just directories"}, {B,'b', "bytes", "print size in bytes"}, {C,'c', "total", "produce a grand total"}, {K,'k', "kilobytes", "print size in 1024 byte blocks"}, {N,'n', "nostderr", "disable stderr process indicator"}, {S,'s', "summarize", "display only a total for each argument"}, {H,'h', "help", "display this help and exit"}, {V,'v', "version", "output version information and exit"}, {NULL,0,NULL,NULL} }; static void H(void) { int a; printf("Usage: du [OPTION]... [PATH]...\n\n"); for(a=0; Options[a].c; a++) printf(" -%c, --%-10s%s\n", Options[a].c, Options[a].txt, Options[a].descr); exit(0); } static void ArgError(int a, char *b) { if(a)printf("du: unrecognized option `--%s'", b); else printf("du: illegal option -- %c", *b); printf("\nTry `du --help' for more information.\n"); exit(1); } void SigInt(int c) { exit(c=c=255); } int main(int argc, char **argv) { int a, e; char *s; Size Total; signal(SIGINT, SigInt); #ifdef DJGPP /* Some speed optimization for djgpp */ _djstat_flags = ((unsigned short)-1); #endif for(e=0, a=1; a