home *** CD-ROM | disk | FTP | other *** search
/ Amiga Elysian Archive / AmigaElysianArchive.iso / newc_dev / unix_too.lha / sc.c < prev    next >
C/C++ Source or Header  |  1992-09-20  |  3KB  |  121 lines

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <libraries/dos.h>
  5. #include <exec/memory.h>
  6. #include <proto/dos.h>
  7. #include <proto/exec.h>
  8.  
  9. unsigned long int TotBytes;
  10. unsigned long int TotDirs;
  11. unsigned long int TotFiles;
  12. char GlobalFileName[512];
  13.  
  14. BOOL DoSizeCheck(BPTR);
  15.  
  16. int CXBRK() {return(0);}
  17. int chkabort() {return(0);}
  18.  
  19. void main(argc,argv)
  20. int argc;
  21. char *argv[];
  22. {
  23.    BPTR mylock;
  24.  
  25.    TotBytes=0;
  26.    TotDirs=0;
  27.    TotFiles=0;
  28.  
  29.    printf("sc - size check by Jean-François Stenuit\n");
  30.    if (argc!=2)
  31.    {
  32.       printf("Usage : sc filename|dirname\n");
  33.       exit(0);
  34.    };
  35.    if (argv[1][strlen(argv[1])-1]=='/')
  36.       argv[1][strlen(argv[1])-1]=0;
  37.    if ((mylock=Lock(argv[1],ACCESS_READ))==0)
  38.    {
  39.       printf("Unable to lock %s, DOS error code %d.\n",argv[1],IoErr());
  40.       exit(0);
  41.    };
  42.    strcpy(GlobalFileName,argv[1]);
  43.    if (DoSizeCheck(mylock)==FALSE)
  44.       printf("Recursive routine failed\n");
  45.    else
  46.       printf("%s contains %d files and %d subdirs, summing %d bytes.\n",
  47.               argv[1],    TotFiles,    TotDirs,            TotBytes);
  48.    UnLock(mylock);
  49.    exit(0);
  50. }
  51.  
  52. BOOL DoSizeCheck(ALock)
  53. BPTR ALock;
  54. {
  55.    register struct FileInfoBlock *myFIB;
  56.    register BPTR NewLock;
  57.    register short int i;
  58.    register char tmp;
  59.  
  60.    if ((myFIB=(struct FileInfoBlock *)
  61.         AllocMem(sizeof(struct FileInfoBlock),MEMF_PUBLIC))==0)
  62.       return(FALSE);
  63.    if (Examine(ALock,myFIB)==FALSE)
  64.    {
  65.       FreeMem((APTR)myFIB,sizeof(struct FileInfoBlock));
  66.       return(FALSE);
  67.    };
  68.    if (myFIB->fib_DirEntryType<0)
  69.    /* A file : just add its size to total */
  70.    {
  71.       TotBytes+=myFIB->fib_Size;
  72.       TotFiles++;
  73.       FreeMem((APTR)myFIB,sizeof(struct FileInfoBlock));
  74.       return(TRUE);
  75.    }
  76.    else
  77.    /* A subdir : check the whole contents */
  78.    {
  79.       for(;;)
  80.       {
  81.          if (ExNext(ALock,myFIB)==FALSE)
  82.          {
  83.             FreeMem((APTR)myFIB,sizeof(struct FileInfoBlock));
  84.             if (IoErr()==ERROR_NO_MORE_ENTRIES)
  85.                return(TRUE);
  86.             else
  87.                return(FALSE);
  88.          }
  89.          else
  90.          {
  91.             if (myFIB->fib_DirEntryType<0)
  92.             {
  93.                TotBytes+=myFIB->fib_Size;
  94.                TotFiles++;
  95.             }
  96.             else
  97.             {
  98.                TotDirs++;
  99.                if (GlobalFileName[strlen(GlobalFileName)-1]!=':')
  100.                   strcat(GlobalFileName,"/");
  101.                strcat(GlobalFileName,myFIB->fib_FileName);
  102.                NewLock=Lock(GlobalFileName,ACCESS_READ);
  103.                if (DoSizeCheck(NewLock)==FALSE)
  104.                {
  105.                   UnLock(NewLock);
  106.                   FreeMem((APTR)myFIB,sizeof(struct FileInfoBlock));
  107.                   return(FALSE);
  108.                };
  109.                for (i=strlen(GlobalFileName);
  110.                             ((tmp=GlobalFileName[i])!='/')&&(tmp!=':');
  111.                              i--)
  112.                   GlobalFileName[i]=0;
  113.                if (GlobalFileName[i]!=':')
  114.                   GlobalFileName[i]=0;
  115.                UnLock(NewLock);
  116.             };
  117.          };
  118.       };
  119.    };
  120. }
  121.