home *** CD-ROM | disk | FTP | other *** search
/ World of A1200 / World_Of_A1200.iso / programs / monitors / rsys / goodies / free / free.c < prev    next >
C/C++ Source or Header  |  1995-02-27  |  4KB  |  207 lines

  1. #include <string.h>
  2. #include <stdlib.h>
  3.  
  4. #include <exec/types.h>
  5. #include <exec/memory.h>
  6.  
  7. #include <dos/dos.h>
  8. #include <dos/dosextens.h>
  9.  
  10. #include <clib/dos_protos.h>
  11. #include <clib/alib_protos.h>
  12. #include <clib/exec_protos.h>
  13.  
  14. #include <dos/stdio.h>
  15.  
  16. struct Library *DosBase = NULL;
  17. BPTR  stdout;
  18.  
  19. char *v = "$VER: MyFree 0.2 (19.6.93) (c) 1993 by Rolf Böhme";
  20.  
  21. long maxfree = 0L, maxvol = 0L;
  22.  
  23. #define null(type) ((type)NULL)
  24. #define ENTRY     ((UBYTE *)"%-20.20s %8lD %8lD %5lD%% %5s\n")
  25. #define ENTRY2    ((UBYTE *)"\33[32;41m%-20.20s %8lD %8lD %5lD%% %5s\33[0m\n")
  26. #define HEADER    ((UBYTE *)"\n%-20.20s  KB-%4s  KB-%4s %5s%% %5s\n%s\n")
  27.  
  28. void
  29. FreeBytes(char *Vol)
  30. {
  31.    BPTR  lock = null(BPTR);
  32.    register struct InfoData *ID = NULL;
  33.    long  num = 0L;
  34.    long free, used, usedpercent;
  35.    char state[6];
  36.  
  37.    free = 0L;
  38.    used = 0L;
  39.    usedpercent = 0;
  40.    *state = 0;
  41.  
  42.    if ((ID = (struct InfoData *)
  43.         AllocVec(sizeof(struct InfoData), MEMF_CLEAR | MEMF_PUBLIC)) != NULL)
  44.    {
  45.       if (lock = Lock((UBYTE *) Vol, ACCESS_READ))
  46.       {
  47.          if (Info(lock, ID))
  48.          {
  49.             free = (((ID->id_NumBlocks - ID->id_NumBlocksUsed) *
  50.                      ID->id_BytesPerBlock) >> 10);
  51.  
  52.             maxfree += free;
  53.  
  54.             used = ((ID->id_NumBlocksUsed * ID->id_BytesPerBlock) >> 10);
  55.  
  56.             maxvol += (free + used);
  57.  
  58.             usedpercent = (100 * ID->id_NumBlocksUsed) / ID->id_NumBlocks;
  59.  
  60.             switch (ID->id_DiskState)
  61.             {
  62.                case ID_WRITE_PROTECTED:
  63.                   strcpy(state, "R/O");
  64.                   break;
  65.  
  66.                case ID_VALIDATING:
  67.                   strcpy(state, "VAL");
  68.                   break;
  69.  
  70.                case ID_VALIDATED:
  71.                   strcpy(state, "R/W");
  72.                   break;
  73.  
  74.                default:
  75.                   strcpy(state, "???");
  76.                   break;
  77.             }
  78.          }
  79.          else
  80.             strcpy(state, "NST");
  81.  
  82.          UnLock(lock);
  83.       }
  84.       else
  85.          strcpy(state, "NOL");
  86.  
  87.       FreeVec(ID);
  88.  
  89.       Printf((usedpercent > 90) || !strcmp(state,"NOL") ? ENTRY : ENTRY2,
  90.              Vol, free, used, usedpercent, state);
  91.    }
  92.  
  93.    return;
  94. }
  95.  
  96. void
  97. DisableSysRequest(int disable)
  98. {
  99.    static APTR wind;
  100.    struct Process *p = (struct Process *) FindTask(NULL);
  101.  
  102.    if (disable)
  103.    {
  104.       wind = (APTR) p->pr_WindowPtr;
  105.       p->pr_WindowPtr = (APTR) (-1);
  106.    }
  107.    else
  108.       p->pr_WindowPtr = wind;
  109.  
  110.    return;
  111. }
  112.  
  113.    /*
  114.     * B2CStr() wandelt einen BCPL-String in einen C-String um
  115.     */
  116. char *
  117. B2CStr(char *ret, BSTR inp, int len)
  118. {
  119.    register int i;
  120.    char *help = (char *)BADDR(inp);
  121.  
  122.    for (i = 0; (i < *help) && i < (len - 1); i++)
  123.       *(ret + i) = *(help + i + 1);
  124.  
  125.    *(ret + i) = '\0';
  126.  
  127.    return (ret);
  128. }
  129.  
  130. void
  131. MakeVolumeList(void)
  132. {
  133.    struct DosList *dl;
  134.    char  name[100];
  135.    ULONG mask = LDF_VOLUMES|LDF_READ;
  136.  
  137.    DisableSysRequest(TRUE);
  138.  
  139.    dl = LockDosList(mask);
  140.  
  141.    while (dl = NextDosEntry(dl, mask))
  142.    {
  143.       B2CStr(name, dl->dol_Name, 100);
  144.       strcat(name, ":");
  145.  
  146.       FreeBytes(name);
  147.    }
  148.  
  149.    UnLockDosList(mask);
  150.  
  151.    DisableSysRequest(FALSE);
  152.  
  153.    return;
  154. }
  155.  
  156. void
  157. MakeVolume(char *vol)
  158. {
  159.    DisableSysRequest(TRUE);
  160.  
  161.    FreeBytes(vol);
  162.  
  163.    DisableSysRequest(FALSE);
  164.  
  165.    return;
  166. }
  167.  
  168. void
  169. main(int argc, char *argv[])
  170. {
  171.    char  line[52];
  172.    long fast, chip, total;
  173.  
  174.    if ((argc != 0) && (DosBase = OpenLibrary((STRPTR) "dos.library", 37)))
  175.    {
  176.       stdout = Output();
  177.  
  178.       memset(line, '-', sizeof(line));
  179.       line[51] = '\0';
  180.  
  181.       Printf((UBYTE *)"%s\n%s",(ULONG)(&v[6]), line);
  182.  
  183.       Printf(HEADER, (ULONG) "Volume", "Free", "Used", "Used", "State",
  184.              line);
  185.  
  186.       if (argc != 2)
  187.          MakeVolumeList();
  188.       else
  189.          MakeVolume(argv[1]);
  190.  
  191.       Forbid();
  192.       chip = AvailMem(MEMF_CHIP) >> 10;
  193.       fast = AvailMem(MEMF_FAST) >> 10;
  194.       total = chip + fast;
  195.       Permit();
  196.  
  197.       Printf((UBYTE *)"%s\nMax. free: %lD KB - Max. avail: %lD KB\n%s\n"
  198.                       "CHIP: %lD KB - FAST: %lD KB - Total: %lD KB\n",
  199.              (ULONG)line,
  200.              maxfree, maxvol,
  201.              line,
  202.              chip, fast, total);
  203.  
  204.       CloseLibrary(DosBase);
  205.    }
  206. }
  207.