home *** CD-ROM | disk | FTP | other *** search
/ The Developer Connection…ice Driver Kit for OS/2 3 / DEV3-D1.ISO / source / util2src / du.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-10-01  |  9.0 KB  |  230 lines

  1. /*============================================================================*
  2.  * main() module: du.c - Directory usage.                    OS/2 2.1 version.
  3.  *
  4.  * This program lists directory usage. Output is displayed in bytes (not
  5.  * blocks). Optionally, subdirectories may be recursed to yield the bytes
  6.  * in an entire tree.
  7.  *
  8.  * The default is to list all files in the current directory. Use -s and -h
  9.  * to include system and hidden files, respectively. Use -R to recurse sub-
  10.  * directories. Enter -? for help.
  11.  *
  12.  * 12/08/92 - Created by Brian Yoder.
  13.  * 12/08/92 - Initial version.
  14.  * 10/01/93 - Ported to OS/2 2.1.
  15.  *============================================================================*/
  16.  
  17. #include <stdio.h>
  18. #include <stdlib.h>
  19. #include <string.h>
  20. #include <sys/types.h>
  21. //#include <dos.h>
  22.  
  23. #include "util.h"
  24.  
  25. static  ulong    fcnt    = 0L;      /* Total no. of files matched */
  26. static  ulong    tsize = 0L;        /* Total no. of bytes in all files */
  27.  
  28. /*----------------------------------------------------------------------------*
  29.  * Default argc/argv: If no file specs entered, then assume "*"
  30.  *----------------------------------------------------------------------------*/
  31.  
  32. static  int      dargc = 1;
  33. static  char    *dargv[1] = { "*" };
  34.  
  35. /*----------------------------------------------------------------------------*
  36.  * Structure for flags
  37.  *----------------------------------------------------------------------------*/
  38.  
  39. typedef struct {
  40.  
  41.      int        s_flag;             /* include system files */
  42.      int        h_flag;             /* include hidden files */
  43.  
  44. } DUFLAGS;
  45.  
  46. static DUFLAGS flags = {            /* Flags and their initial settings */
  47.  
  48.      FALSE, FALSE
  49. };
  50.  
  51. /*============================================================================*
  52.  * Function prototypes for internal subroutines
  53.  *============================================================================*/
  54.  
  55. static void       syntax       ( void );
  56.  
  57. /*============================================================================*
  58.  * Main Program Entry Point
  59.  *============================================================================*/
  60.  
  61. main(argc, argv)
  62.  
  63. int argc;                           /* arg count */
  64. char *argv[];                       /* arg pointers */
  65.  
  66. {
  67.   int     rc;                       /* Return code store area */
  68.   long    lrc;                      /* Long return code */
  69.   char   *flagstr;                  /* Pointer to string of flags */
  70.   char    cf;                       /* Current flag character (if any) */
  71.  
  72.   int     uniq;                     /* 'uniq' flag for slmake() */
  73.   ushort  mflags;                   /* Match flags for slrewind() */
  74.   ushort  recurse;                  /* Match flags for slrewind() */
  75.  
  76.   SLPATH *speclist;                 /* Pointer to linked list of path specs */
  77.  
  78.   FINFO  *fdata;                    /* Pointers returned by slmatch() */
  79.   SLPATH *pdata;
  80.   SLNAME *ndata;
  81.  
  82.   uniq = TRUE;                      /* Init: build SLPATHs only for unique paths */
  83.   recurse = FALSE;                  /* Init: don't recursively search dirs */
  84.  
  85.  /*---------------------------------------------------------------------------*
  86.   * Process flags, if any
  87.   *---------------------------------------------------------------------------*/
  88.  
  89.   argc--;                           /* Ignore 1st argument (program name) */
  90.   argv++;
  91.  
  92.   while (argc != 0)                 /* For each argument: */
  93.   {
  94.      flagstr = *argv;                  /* Point 'flagstr' to argument */
  95.      if (*flagstr != '-')              /* If it doesn't begin with '-': */
  96.         break;                              /* Then it's the first filespec */
  97.      else                              /* Otherwise: It's a set of flags: */
  98.      {
  99.         flagstr++;                          /* Point past the dash */
  100.  
  101.         if (*flagstr == '?')                /* If help is requested: */
  102.            syntax();                             /* Show help */
  103.  
  104.         while (*flagstr != '\0')            /* For each character in flag string: */
  105.         {
  106.            switch (*flagstr)
  107.            {
  108.               case '?':                          /* If we see ? in the list: */
  109.                  syntax();                       /* then user wants help */
  110.                  return(2);
  111.                  break;
  112.  
  113.               case 's':
  114.                  flags.s_flag = TRUE;
  115.                  break;
  116.  
  117.               case 'h':
  118.                  flags.h_flag = TRUE;
  119.                  break;
  120.  
  121.               case 'R':
  122.                  recurse = TRUE;
  123.                  break;
  124.  
  125.               default:
  126.                  fprintf(stderr, "du: Invalid flag '%c'.  For help, enter 'du -?'\n",
  127.                     *flagstr);
  128.                  exit(2);
  129.                  break;
  130.            }
  131.            flagstr++;                            /* Check next character */
  132.         } /* end of while stmt to process all flag characters in current argv */
  133.  
  134.         argc--;                             /* Done with flags: Discard them */
  135.         argv++;
  136.      } /* end of if stmt to process flag string */
  137.   } /* end of while stmt to process all flag strings */
  138.  
  139.  /*---------------------------------------------------------------------------*
  140.   * Build the mflags variable: will be passed to slrewind() to restrict
  141.   * the names matched by slmatch().
  142.   *---------------------------------------------------------------------------*/
  143.  
  144.   mflags = SL_NORMAL;               /* Include normal files. Then: */
  145.  
  146.   if (flags.s_flag == TRUE)         /* Include system files, too? */
  147.      mflags = mflags | SL_SYSTEM;
  148.  
  149.   if (flags.h_flag == TRUE)         /* Include hidden files, too? */
  150.      mflags = mflags | SL_HIDDEN;
  151.  
  152.  /*---------------------------------------------------------------------------*
  153.   * Process file specifications and build specification list
  154.   *---------------------------------------------------------------------------*/
  155.  
  156.   if (argc != 0)                    /* If there are command line args: */
  157.      rc = slmake(&speclist, uniq, TRUE, argc, argv);     /* Process them */
  158.   else                              /* If there are NO command line args: */
  159.      rc = slmake(&speclist, uniq, TRUE, dargc, dargv);   /* Then assume "*" */
  160.  
  161.   if (rc != 0)                      /* If there was an error: */
  162.   {                                 /* Analyze rc, show msg, and return */
  163.      if (rc == REGX_MEMORY)
  164.         fprintf(stderr, "Out of memory while processing '%s'.\n",
  165.            slerrspec());
  166.      else
  167.         fprintf(stderr, "Invalid filespec: '%s'.\n",
  168.            slerrspec());
  169.  
  170.      return(2);
  171.   }
  172.  
  173.  /*---------------------------------------------------------------------------*
  174.   * Attempt to match files to the specification list
  175.   *---------------------------------------------------------------------------*/
  176.  
  177.   slrewind(speclist, mflags,        /* Initialize slmatch() */
  178.            recurse);
  179.   for (;;)                          /* Loop to find all matching DOS files: */
  180.   {
  181.     /*--------------------------------------------------*
  182.      * Get next matching file. Increment file count and
  183.      * add the file's size to the total size
  184.      *--------------------------------------------------*/
  185.  
  186.      fdata = slmatch(&pdata,             /* Get next matching DOS file */
  187.                      &ndata);
  188.      if (fdata == NULL)                  /* If none: */
  189.         break;                                /* Done */
  190.  
  191.      fcnt++;                             /* Increment file count */
  192.  
  193.      tsize += fdata->Fsize;              /* Add file's size to total */
  194.  
  195.   } /* end of for loop for all matching files */
  196.  
  197.  /*---------------------------------------------------------------------------*
  198.   * Display total size and number of files on stdout
  199.   *---------------------------------------------------------------------------*/
  200.  
  201.   printf("%lu bytes in %lu files\n", tsize, fcnt);
  202.  
  203.  /*---------------------------------------------------------------------------*
  204.   * Display (on stderr) all filespecs that had no matching files
  205.   *---------------------------------------------------------------------------*/
  206.  
  207.   lrc = slnotfound(speclist);       /* Display path\names w/no matching files */
  208.   if (lrc == 0L)                    /* If all fspecs were matched: */
  209.      return(0);                          /* Return successfully */
  210.   else                              /* Otherwise:  One or more not found: */
  211.      return(2);                          /* Return with error */
  212.  
  213. } /* end of main() */
  214.  
  215. /*============================================================================*
  216.  * syntax() - Display command syntax and exit to operating system!
  217.  *============================================================================*/
  218. static void syntax()
  219. {
  220.   fprintf(stderr, "Usage:  du [-shR] [fspec ...]\n");
  221.   fprintf(stderr, "Flags:\n");
  222.  
  223.   fprintf(stderr, "  s  Include system files.\n");
  224.   fprintf(stderr, "  h  Include hidden files.\n");
  225.  
  226.   fprintf(stderr, "  R  Recursively descend subdirectories.\n");
  227.  
  228.   exit(2);
  229. }
  230.