home *** CD-ROM | disk | FTP | other *** search
/ Unix System Administration Handbook 1997 October / usah_oct97.iso / news / cnews.tar / util / doustat.c < prev    next >
C/C++ Source or Header  |  1994-10-17  |  1KB  |  63 lines

  1. /*
  2.  * doustat - the heart of spacefor.ustat
  3.  */
  4.  
  5. #include <stdio.h>
  6. #include <string.h>
  7. #include <sys/types.h>
  8. #include <sys/stat.h>
  9. #include <ustat.h>
  10.  
  11. extern int debug;
  12.  
  13. extern void error();
  14.  
  15. /*
  16.  - spacefor - do the work
  17.  */
  18. long
  19. spacefor(filesize, fileonfs, wantspace, wantinodes, bperi)
  20. long filesize;
  21. char *fileonfs;
  22. long wantspace;
  23. long wantinodes;
  24. long bperi;
  25. {
  26.     struct stat silly;
  27.     struct ustat info;
  28.     register long n;
  29. #    define    LOTS    10000
  30. #ifndef USTATBS
  31. #    define    USTATBS    (512)
  32. #endif
  33.     register long iperfile = filesize/bperi + 1;
  34.  
  35.     if (stat(fileonfs, &silly) < 0)
  36.         error("cannot do stat(%s)", fileonfs);
  37.     if (ustat(silly.st_dev, &info) < 0)
  38.         error("cannot do ustat(%s)", fileonfs);
  39.     if (debug)
  40.         fprintf(stderr, "bsize %ld, avail %ld, inodes %ld\n",
  41.                 USTATBS, info.f_tfree, (long)info.f_tinode);
  42.  
  43.     n = LOTS;
  44.     if (info.f_tfree <= wantspace)
  45.         n = 0;
  46.     else if (USTATBS > 0 && filesize > 0)
  47.         n = (info.f_tfree - wantspace) / (filesize/USTATBS + 1);
  48.  
  49.     if (info.f_tinode < 0)        /* information unavailable */
  50.         ;            /* bypass check, and pray */
  51.     else if (info.f_tinode <= wantinodes)
  52.         n = 0;
  53.     else if ((info.f_tinode - wantinodes) / iperfile < n)
  54.         n = (info.f_tinode - wantinodes) / iperfile;
  55.  
  56.     if (n < 0)
  57.         n = 0;
  58.     else if (n > LOTS)
  59.         n = LOTS;    /* to avert 16-bit trouble elsewhere */
  60.  
  61.     return(n);
  62. }
  63.