home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 229.lha / free / Free.c < prev    next >
C/C++ Source or Header  |  1989-04-07  |  3KB  |  108 lines

  1. /*   Free.c  -  (c) 1989  Mike 'Sirius' Stilson - Sirius Software
  2.  *   Ver1.0    January 1989.  Written, Tested, Debugged, Re-Tested, and Finished.
  3.  *
  4.  *            Display free/total bytes for a volume.
  5.  *        
  6.  *    Notice:
  7.  *        The author makes no claims or guarantees of the suitability of this
  8.  * program for any given application, and I assume no responsibility or
  9.  * liability for damages incurred through its usage.
  10.  *
  11.  *    Feel free to steal any or all of this code and make millions of dollars
  12.  * from its sale and commercial use, but please..  Give credit where credit is
  13.  * due.
  14.  *
  15.  *   Any similarity of this program, living, dead, deleted, or undead is purely
  16.  * coincidental and unintentional.
  17.  *
  18.  */
  19.  
  20. #include <exec/types.h>
  21. #include <exec/memory.h>
  22. #include <libraries/dos.h>
  23. #include <libraries/dosextens.h>
  24. #include <functions.h>
  25.  
  26. struct InfoData *idata = NULL;
  27. struct FileLock *mylock = NULL;
  28.  
  29. _wb_parse() {}        /* We don't run from the wb.. */
  30.  
  31. main(ac,av) int ac; char **av;
  32. {
  33.     char *GetName();
  34.  
  35.    if(!(idata=(struct InfoData *)
  36.         AllocMem((long)sizeof(struct InfoData),(long)MEMF_CHIP|MEMF_CLEAR))) {
  37.         puts("Not enough memory.\n");
  38.         exit(20);
  39.     }
  40.  
  41. if(ac==1) goto once;
  42.     while(--ac > 0) {
  43. once:
  44.    if(!(mylock=Lock(*++av,ACCESS_READ))) {
  45.         printf("Attempt to lock %s failed.\n",*av);
  46.       FreeMem(idata,(long)sizeof(struct InfoData));
  47.       exit(20);
  48.    }
  49.  
  50.    if(!(Info(mylock,idata))) {
  51.         printf(Output(),"Unable to obtain info for device %s.\n",*av);
  52.       FreeMem(idata,(long)sizeof(struct InfoData));
  53.         UnLock(mylock);
  54.       exit(20);
  55.    }
  56.  
  57.    printf("Volume Name: \2334;31;40m%s\2330;0;0m\t",GetName(mylock));
  58.  
  59.     GetFree();
  60.    UnLock(mylock);
  61.     }
  62.  
  63.    FreeMem(idata,(long)sizeof(struct InfoData));
  64.    exit(0);
  65. }
  66.  
  67.  
  68. /* Macro to convert a BPTR to a CString.  Just for readability */
  69. #define BPTR_TO_C(cast, var) ((struct cast *)(BADDR((ULONG)var)))
  70.  
  71. char *
  72. GetName(lock)
  73. struct FileLock *lock;
  74. {
  75.     struct DeviceList *dl=NULL;
  76.     register char *p=NULL;
  77.     register char buf[80];
  78.  
  79.     Forbid();
  80.  
  81. /* Since the InfoData structure points to a NULL BSTR all the time, this is
  82.    the only way I can think to get the volume name */
  83.  
  84.         lock = BPTR_TO_C(FileLock, lock);
  85.         dl = BPTR_TO_C(DeviceList, lock->fl_Volume);
  86.         p = (char *)BADDR(dl->dl_Name);
  87.         movmem(p+1, buf, p[0]);
  88.         buf[p[0]] = '\0';
  89.  
  90.     Permit();
  91.     return(buf);
  92. }
  93.  
  94.  
  95. GetFree()
  96. {
  97.     register ULONG usedbytes=0, totalbytes=0, freebytes=0, bpb=0;
  98.  
  99.     bpb=idata->id_BytesPerBlock;
  100.     usedbytes=bpb * idata->id_NumBlocksUsed;
  101.     totalbytes=bpb * idata->id_NumBlocks;
  102.     freebytes=totalbytes-usedbytes;
  103.     printf("Bytes per block: %-7ld\n",bpb);
  104.     printf("Total bytes: %-7ld\t\tFree Bytes: %-7ld\n",totalbytes,freebytes);
  105.     printf("Total blocks: %-7ld\t\tFree Blocks: %-7ld\n\n",idata->id_NumBlocks,
  106.         (idata->id_NumBlocks - idata->id_NumBlocksUsed));
  107. }
  108.