home *** CD-ROM | disk | FTP | other *** search
/ The Fred Fish Collection 1.5 / ffcollection-1-5-1992-11.iso / ff_disks / 700-799 / ff713.lha / free / source / main.c < prev    next >
C/C++ Source or Header  |  1992-08-19  |  6KB  |  246 lines

  1. /***************************************************************************
  2.  * FREE:    Display free space on your disk devices.
  3.  *        Author:  Daniel J. Barrett, barrett@cs.umass.edu.
  4.  *        Inspired by "FREE" by Tom Smythe on Fred Fish Disk #66,
  5.  *         but totally rewritten.  One function, AvailSpace(), is an
  6.  *         updated version of Smythe's function avail().
  7.  *
  8.  * This program is Freely Distributable.  Make all the copies you want
  9.  * and give them away.  Use this code in any way you like.
  10.  *
  11.  * Changes/Improvements over Tom Smythe's program are:
  12.  *
  13.  *    o    Does not use self-modifying code, so it should work on
  14.  *        ALL Amigas, not just 68000-based systems.
  15.  *    o    You can specify a device list on the command line.
  16.  *    o    You can specify a device list using an environment
  17.  *        variable.
  18.  *    o    If no device list is specified under AmigaOS 2.0 or higher,
  19.  *        all disk volumes are listed.
  20.  *    o    Requestors are turned off, so non-mounted devices are just
  21.  *        skipped.  (See -m option.)
  22.  *    o    Data is written in correct columns, regardless of the 
  23.  *        lengths of the volume names.  (See -l option.)
  24.  *    o    Total memory free is shown, along with CHIP and FAST
  25.  *        subtotals.
  26.  *    o    Command-line options added so the user can customize the
  27.  *        program.
  28.  *    o    Written in ANSI C, in a modular and easy-to-read style.
  29.  *
  30.  * Daniel J. Barrett makes no claims about the suitability of this program
  31.  * for any particular purpose.  Any damages incurred through the use of
  32.  * this program are entirely the responsibility of the USER.  Use at your
  33.  * own risk.  So there!   :-)
  34.  *
  35.  ***************************************************************************/
  36.  
  37. #include "free.h"
  38. #include <intuition/intuitionbase.h>
  39. #include "version.h"
  40.  
  41.  
  42. char *version = "$VER: Free " VERSION " " VERSION_DATE;
  43.  
  44. static char *outArray = NULL;
  45. struct IntuitionBase *IntuitionBase;
  46.  
  47.  
  48. main(int argc, char *argv[])
  49. {
  50.     InitializeGlobals();
  51.  
  52.     if ((argc == 2) && (EQUAL(argv[1], HELP_ARG)))
  53.         Usage(argv[0]);
  54.     else
  55.         DoFree(argc, argv);
  56.  
  57.     ExitCleanly(NO_ERROR, RETURN_OK);
  58. }
  59.  
  60.  
  61. /* Process the user's command-line options.
  62.  * Allocate the out[] array for output.
  63.  * Disable requestors, if necessary.
  64.  * Measure the available RAM.
  65.  * Then, measure free space on each desired device. 
  66.  * Print the output in a quick manner, using Write().
  67.  * Turn requestors back on, if they were turned off.
  68.  */
  69.  
  70. void DoFree(int argc, char *argv[])
  71. {
  72.     long chipRam, fastRam;
  73.     int argIndex;
  74.  
  75.     argIndex = GetOptions(argc, argv);
  76.     outArray = MakeOutArray();
  77.  
  78.     if (!(flags & FLAG_REQUESTORS))
  79.          DisableRequestors();
  80.  
  81.     GetFreeRam(&chipRam, &fastRam);
  82.  
  83.     if (argIndex >= argc)
  84.         FreeUsingEnv(chipRam, fastRam);
  85.     else
  86.         FreeFromArgs(argv+argIndex, chipRam, fastRam);
  87.  
  88.     Write(Output(), outArray, (long)strlen(outArray));  /* For speed. */
  89.  
  90.     if (!(flags & FLAG_REQUESTORS))
  91.          EnableRequestors();
  92. }
  93.  
  94.  
  95. /* The user specified no volumes as command-line arguments.  So, check
  96.  * the environment variable.  If it has no value, then use DEFAULT_VOLUMES
  97.  * instead.
  98.  * We use the WONDERFUL function strtok() iteratively, to get the name of
  99.  * each volume, one at a time. */
  100.  
  101. void FreeUsingEnv(long chipRam, long fastRam)
  102. {
  103.     char *volume, *volumeList;
  104.     static char *defaultVolumes = DEFAULT_VOLUMES_OLD;
  105.     BOOL newversion = FALSE;
  106.     
  107.     IntuitionBase = (struct IntuitionBase *)
  108.             OpenLibrary("intuition.library", 36);
  109.     if (IntuitionBase)
  110.     {
  111.         newversion = TRUE;
  112.         CloseLibrary(IntuitionBase);
  113.     }
  114.  
  115.     volumeList = getenv(ENV_VARIABLE);
  116.     if (!volumeList && newversion)
  117.         FreeUsingDosList(chipRam, fastRam);
  118.     else
  119.     {
  120.         if (!volumeList)
  121.             volumeList = defaultVolumes;
  122.  
  123.         volume = strtok(volumeList, ENV_SEPARATOR);
  124.         while (volume)
  125.         {
  126.             ShowFree(volume, chipRam, fastRam, outArray);
  127.             volume = strtok(NULL, ENV_SEPARATOR);
  128.         }
  129.     }
  130. }
  131.  
  132.  
  133. void FreeUsingDosList(long chipRam, long fastRam)
  134. {
  135.     struct DosList *dlist, *saveDList;
  136.     char *name;
  137.     long len, maxLen = 0;
  138.     char volume[MAX_NAME_LEN + 1];
  139.     
  140.     dlist = (struct DosList *)LockDosList(LDF_VOLUMES | LDF_READ);
  141.     if (!dlist)
  142.         ExitCleanly(ERROR_DOSLIST, RETURN_FAIL);
  143.     else
  144.         saveDList = dlist;
  145.  
  146.     while (dlist = (struct DosList *)NextDosEntry(dlist, LDF_VOLUMES))
  147.     {
  148.         name = BADDR(dlist->dol_Name);
  149.         if (name[0] > volumeNameLen)
  150.             maxLen = (name[0] > maxLen) ? name[0] : maxLen;
  151.     }
  152.  
  153.     if (maxLen > volumeNameLen)
  154.         volumeNameLen = maxLen;
  155.  
  156.     ShowFree("RAM:", chipRam, fastRam, outArray);
  157.     
  158.     dlist = saveDList;
  159.     while (dlist = (struct DosList *)NextDosEntry(dlist, LDF_VOLUMES))
  160.     {
  161.         name    = BADDR(dlist->dol_Name);
  162.         len    = name[0];
  163.         name++;
  164.  
  165.         if (name)
  166.         {
  167.             strncpy(volume, name, len);
  168.             volume[len]    = ':';
  169.             volume[len+1]    = '\0';
  170.             ShowFree(volume, chipRam, fastRam, outArray);
  171.             
  172.         }
  173.     }
  174.  
  175.     UnLockDosList(LDF_VOLUMES | LDF_READ);
  176. }
  177.  
  178.  
  179. /* The user specified volumes on the command-line.  Check each one. */
  180.  
  181. void FreeFromArgs(char *argv[], long chipRam, long fastRam)
  182. {
  183.     while (*argv)
  184.         ShowFree(*argv++, chipRam, fastRam, outArray);
  185. }
  186.  
  187.  
  188. /* Process the user's command-line options.
  189.  * Return the index of the first argument appearing AFTER all options. */
  190.  
  191. int GetOptions(int argc, char *argv[])
  192. {
  193.     register char c;
  194.     long m;
  195.  
  196.     while ((c = getopt(argc, argv, OPTSTRING)) != EOF)
  197.     {
  198.         switch (c)
  199.         {
  200.             case OPT_BLOCKS:
  201.                 flags |= FLAG_BLOCKS;
  202.                 break;
  203.             case OPT_REQUESTORS:
  204.                 flags |= FLAG_REQUESTORS;
  205.                 break;
  206.             case OPT_MALLOC:
  207.                 flags |= FLAG_MALLOC;
  208.                 if ((m = atoi(optarg)) > 0)
  209.                     memSize = m;
  210.                 break;
  211.             case OPT_VOLUME_NAME_LEN:
  212.                 flags |= FLAG_VOLUME_NAME_LEN;
  213.                 volumeNameLen = atoi(optarg);
  214.                 CheckVolumeNameLen(&volumeNameLen);
  215.                 break;
  216.             case OPT_UNKNOWN:
  217.                 break;
  218.             default:    /* Sanity check! */
  219.                 ExitCleanly(ERROR_IMPOSSIBLE, RETURN_FAIL);
  220.                 break;
  221.         }
  222.     }
  223.     return(optind);
  224. }
  225.  
  226.  
  227. /* Deallocate memory, print a message, and exit. */
  228. void ExitCleanly(ERROR errorCode, int exitCode)
  229. {
  230.     if (outArray)
  231.         free(outArray);
  232.     if (errorCode)
  233.         ErrorMsg(errorCode);
  234.     exit(exitCode);
  235. }
  236.  
  237.  
  238. /* Like the name says... initialize our global variables. */
  239.  
  240. void InitializeGlobals()
  241. {
  242.     flags        = 0L;
  243.     memSize        = DEFAULT_MEMORY_LIMIT;
  244.     volumeNameLen    = DEFAULT_NAME_LEN;
  245. }
  246.