home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 152.lha / byter.c < prev    next >
C/C++ Source or Header  |  1988-04-26  |  6KB  |  220 lines

  1. /* BYTER 2.0                            7-7-88              Ed Holzknecht
  2. *
  3. *  Byter is a program that will display the directory, the size of each
  4. *   file, and bytes and blocks free on all disk devices in the system,
  5. *   including VD0: and DH0:. The new addition to this version is 
  6. *   that Byter is now compatible with WorkBench. By using extended selection
  7. *   (holding down the shift key while clicking on an icon), you can execute
  8. *   Byter. Hold down the shift key, click on a drawer or disk icon, and 
  9. *   then double-click on the Byter icon.  
  10. *
  11. *  USAGE...byter <drive>. If <drive> is not specified, the current directory
  12. *          is the directory listed.
  13. *  
  14. *  Copyright (C) 1988 Ed Holzknecht. All rights reserved. This program
  15. *    is Freeware. If you can make it better, go for it; just keep my name
  16. *    on the original.
  17. *    
  18. *  This program may be included in Fred Fish and Amicus disks (if they want
  19. *    it) with the author's compliments. Please keep this notice intact!   
  20. *
  21. *  Note:  Found out how to get rid of nasty requesters by looking at Charles
  22. *         McManis' 'Info' command in C.  Thanks, Charles!
  23. *
  24. ***************************************************************************/
  25. #include <functions.h>
  26. #include <exec/memory.h>
  27. #include <workbench/startup.h>
  28. #include <intuition/intuition.h>
  29. #include <libraries/dos.h>
  30. #include <libraries/dosextens.h>
  31.  
  32. #define  print(str)    Write(Output(), str, (LONG)strlen(str))
  33.  
  34. extern  struct WBStartup *WBenchMsg;
  35. struct  DirData {
  36.     char  filename[50];
  37.     LONG  filesize;
  38. } mydata[100];
  39.  
  40. char    *device[5]  =  {"VD0:", "DH0:", "DF2:", "DF1:", "DF0:"};
  41. char    s[8];
  42. void    rtjust();
  43.  
  44. main(argc, argv)
  45. int   argc;
  46. char  *argv[];
  47. {
  48. struct  Process   *pro = NULL;
  49. struct  InfoData  *myinfo = NULL;
  50. struct  FileLock  *mylock =NULL;
  51. void    getdir();
  52. LONG    blocks, bytes, i = 0;
  53. char    *diskname, *sblocks, *sbytes, *itoa();
  54. int     end;
  55. BOOL    flag = 1;
  56. APTR    pnt;
  57.  
  58.    if(argc == 0) {
  59.       if(WBenchMsg->sm_NumArgs == 1) {
  60.          exit(0);
  61.       }
  62.       diskname = WBenchMsg->sm_ArgList[1].wa_Name;
  63.    }
  64.    else if(argc == 1) diskname = "";
  65.    else if(argc == 2) diskname = argv[1];
  66.    else {
  67.       print("USAGE..byter <drive>\n");
  68.       exit(0);
  69.    }
  70.    getdir(diskname);
  71.    pro = (struct Process *)FindTask(NULL);
  72.    pnt = pro->pr_WindowPtr;   
  73.    pro->pr_WindowPtr = (APTR) -1; 
  74.    myinfo = (struct InfoData *)AllocMem((LONG)sizeof(struct InfoData), 0L);
  75.    while(i < 5) {
  76.       if(mylock = (struct FileLock *)Lock(device[i], ACCESS_READ)) {
  77.          Info(mylock, myinfo); 
  78.          blocks = myinfo->id_NumBlocks - myinfo->id_NumBlocksUsed;
  79.          bytes  = blocks * 512L;  
  80.          sbytes = itoa(&bytes);
  81.          if(flag) {
  82.             print("DRIVE           BYTES FREE     BLOCKS FREE\n");
  83.             flag = 0;
  84.          }
  85.          print(device[i]); print("\t\t");
  86.          rtjust(sbytes, 9L);
  87.          print(sbytes);print("\t");
  88.          sblocks = itoa(&blocks);
  89.          rtjust(sblocks, 6L);
  90.          print(sblocks);print("\n");
  91.       }   
  92.       if(mylock) UnLock(mylock);
  93.       i++;
  94.    }
  95.    if(WBenchMsg) { 
  96.      print("\nHit <RETURN> to end!\0");
  97.      getchar();
  98.    }
  99.    if(myinfo) FreeMem(myinfo, ((LONG)(sizeof(struct InfoData))));
  100.    pro->pr_WindowPtr = pnt;
  101.    exit(0);
  102. }
  103.  
  104. void  getdir(name)
  105. char  *name;
  106. {
  107. void    sort();
  108. struct  FileInfoBlock  *myfib = NULL;
  109. struct  FileLock       *myotherlock = NULL, *oldlock = NULL;
  110. char    *sfilesize;      
  111. LONG    len, filecount, i = 0, colcount = 0, Enable_Abort = 0;
  112.  
  113.    if(WBenchMsg) {
  114.       myotherlock = (struct FileLock *)WBenchMsg->sm_ArgList[1].wa_Lock;
  115.       oldlock = CurrentDir(myotherlock);
  116.    }
  117.    else 
  118.       myotherlock = (struct FileLock *)Lock(name, ACCESS_READ);
  119.    myfib = (struct FileInfoBlock *)AllocMem((LONG)sizeof(struct FileInfoBlock), 0L);
  120.    if(!Examine(myotherlock, myfib))
  121.       exit(20);
  122.    Enable_Abort = 1;
  123.    while(ExNext(myotherlock, myfib) != 0L || IoErr() != ERROR_NO_MORE_ENTRIES) {
  124.       if(myfib -> fib_DirEntryType > 0L) {
  125.          print("\t");
  126.          print(myfib -> fib_FileName);
  127.          print(" (dir)\n");
  128.       }
  129.       else {
  130.          strcpy(mydata[i].filename, myfib -> fib_FileName);
  131.          mydata[i].filesize = myfib -> fib_Size;
  132.          i++;
  133.       }
  134.    }
  135.    sort(mydata, i);
  136.    for(filecount = 0;filecount < i;filecount++) {
  137.       print(mydata[filecount].filename);
  138.       len = strlen(mydata[filecount].filename);
  139.       for(;len < 25L; len++) 
  140.          print(" ");
  141.       sfilesize = itoa(&mydata[filecount].filesize);
  142.       rtjust(sfilesize, 7L);
  143.       print(sfilesize); 
  144.       print("     ");
  145.       if(colcount % 2L == 1L)
  146.          print("\n");
  147.       colcount++;
  148.       Chk_Abort();
  149.    }
  150.    colcount % 2L == 1L ? print("\n\n") : print("\n");
  151.    if(myotherlock && (!WBenchMsg)) {
  152.       if(oldlock) 
  153.          myotherlock = CurrentDir(oldlock);
  154.       UnLock((struct FileLock *)myotherlock);
  155.    }
  156.    if(myfib) FreeMem(myfib, ((LONG)(sizeof(struct FileInfoBlock)))); 
  157. }
  158.  
  159. char  *itoa(n)
  160. LONG   *n;
  161. {
  162. LONG   c, i, j, k;
  163.  
  164.  
  165.    k = 0;
  166.    do {
  167.       s[k++] = *n % 10L + '0';
  168.    } while ((*n /= 10L) > 0L);
  169.    s[k] = '\0';
  170.    for(i = 0, j = (LONG)strlen(s)-1L; i < j; i++, j--) {
  171.       c = s[i];
  172.       s[i] = s[j];
  173.       s[j] = c;
  174.    }
  175.    return(s);
  176. }
  177.  
  178. void  rtjust(string, len)
  179. char  *string;
  180. LONG  len;
  181. {
  182. LONG   x, y;
  183.  
  184.    x = (LONG)strlen(string);
  185.    if(x >= len)
  186.       return;
  187.    for(y = len;y >= 0L;y--) 
  188.       *(string+y) = (x < 0L) ? ' ' : *(string+x--);
  189.  
  190. void sort(mydata, count)
  191. struct  DirData  mydata[];
  192. LONG    count;
  193. {
  194. LONG      i, j, k, s, w;
  195. char      a[5];
  196. struct  DirData  x;
  197.  
  198.    a[0]=9L; a[1]=5L;a[2]=3L;a[3]=2L;a[4]=1L;
  199.    
  200.    for(w=0; w<5; w++) {
  201.       k=a[w]; s= -k;
  202.       for(i=k; i<count; ++i) {
  203.          x = mydata[i];
  204.          j=i-k;
  205.          if(s==0) {
  206.             s= -k;
  207.             s++;
  208.             mydata[s] = x;
  209.          }
  210.          while((strcmp(&x.filename, mydata[j].filename)< 0) && j >= 0 && j <= count) {
  211.             mydata[j+k] = mydata[j];
  212.             j = j-k;
  213.          }
  214.          mydata[j + k] = x;
  215.       }
  216.    }
  217. }
  218.  
  219.