home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / misc / volume30 / qdos_utils / part01 / du.c next >
Encoding:
C/C++ Source or Header  |  1992-07-06  |  2.8 KB  |  118 lines

  1. #include <stdio.h>
  2. #include <errno.h>
  3. #include <fido.h>
  4. #include <qdos.h>
  5. #include <ctype.h>
  6. #include <time.h>
  7. #include <dirent.h>
  8.  
  9. typedef unsigned char       u_char;
  10. typedef unsigned short  int u_short;
  11. typedef unsigned long   int u_long;
  12. typedef unsigned        int u_int;
  13.  
  14. /*
  15.     du.c 1.00 copyleft 1992 Erik Slagter (v882022@si.hhs.nl)
  16.     This source may be distributed and modified freely if this message stays
  17.     intact.
  18.  
  19.     compile with c68k 2.00 or higher.
  20.  
  21.     usage: ex du;"[-[sabkm]] {directory}"
  22.  
  23.     flags:
  24.  
  25.     -s short; do not output directory names
  26.     -a all  ; output all file names and sizes
  27.     -b bytes; output in bytes
  28.     -k kbyte; output in kilobytes
  29.     -m mega ; output in megabytes
  30.  
  31.     multiple file and directory names and wildcards may be supplied
  32.  
  33.     extremely useful in conjunction with harddisk or level II device driver.
  34.  
  35.     Modification history of du
  36.  
  37.     1.00: 920410:   First version
  38.  
  39. */
  40.  
  41. static  u_char  format          = 10;
  42. static  u_char  which           = 1;
  43. char    _prog_name[]            = "du";
  44. long    _mneed                  = 16384;
  45. static  u_char *_version        = "1.00";
  46. static  u_char *sccs            = "(#)du.c 1.01 920327 Qdos Minerva C68 2.00\n";
  47.  
  48. #define oops(s) (perror(s), exit(ERR_BP))
  49.  
  50. u_int count(dir)
  51. u_char *dir;
  52. {
  53.     chanid_t    chanid;
  54.     struct      qdirect qd;
  55.     u_char      tmp[1024];
  56.     u_char      file[1024];
  57.     u_int       total = 0;
  58.  
  59.     sprintf(tmp, "%s*", dir);
  60.  
  61.     chanid = open_qdir(dir);
  62.     while(read_qdir(chanid, tmp, file, &qd, 0))
  63.         if(qd.d_type == QF_DIR_TYPE)
  64.             total += count(file);
  65.     io_close(chanid);
  66.  
  67.     chanid = open_qdir(dir);
  68.     while(read_qdir(chanid, tmp, file, &qd, 0))
  69.         if(qd.d_type != QF_DIR_TYPE)
  70.         {
  71.             total += qd.d_length;
  72.             if(which > 1)
  73.                 printf("%5d %s\n", qd.d_length >> format, file);
  74.         }
  75.     io_close(chanid);
  76.  
  77.     if(which)
  78.         printf("%5d %s\n", total >> format, *dir ? dir : ".");
  79.  
  80.     return(total);
  81. }
  82.  
  83. long main(argc, argv)
  84. int argc;
  85. char **argv;
  86. {
  87.     u_int   total = 0;
  88.     u_int   dirs  = 0;
  89.  
  90.     while(*++argv)
  91.         if(argv[0][0] == '-')
  92.             switch(argv[0][1])
  93.             {
  94.                 case('s'):
  95.                     which   = 0;    break;
  96.                 case('a'):
  97.                     which   = 2;    break;
  98.                 case('b'):
  99.                     format  = 0;    break;
  100.                 case('k'):
  101.                     format  = 10;   break;
  102.                 case('m'):
  103.                     format  = 20;   break;
  104.                 default:
  105.                     fprintf(stderr, "usage: %s [-[sabkm]] {directory}\n", _prog_name);
  106.                     exit(ERR_BP);
  107.             }
  108.         else
  109.             total += count(*argv), dirs++;
  110.  
  111.     if(!dirs)
  112.         total = count("");
  113.  
  114.     if((dirs > 1) || !which)
  115.         printf("Total %5d\n", total >> format);
  116. }
  117.  
  118.