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

  1. /*
  2.  * doultrix - the heart of spacefor.ultrix
  3.  */
  4.  
  5. #include <stdio.h>
  6. #include <string.h>
  7. #include <sys/types.h>
  8. #include <sys/param.h>
  9. #include <sys/mount.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 fs_data info;
  27.     register long n;
  28. #    define    LOTS    10000
  29. #ifndef ULTRIXBS
  30. #    define    ULTRIXBS    (1024)
  31. #endif
  32.     register long iperfile = filesize/bperi + 1;
  33.  
  34.     if (statfs(fileonfs, &info) <= 0)
  35.         error("cannot do statfs(%s)", fileonfs);
  36.     if (debug)
  37.         fprintf(stderr, "bsize %ld, avail %ld, inodes %ld\n",
  38.                 ULTRIXBS, info.fd_req.bfreen, info.fd_req.gfree);
  39.  
  40.     n = LOTS;
  41.     if (info.fd_req.bfreen <= wantspace)
  42.         n = 0;
  43.     else if (ULTRIXBS > 0 && filesize > 0)
  44.         n = (info.fd_req.bfreen - wantspace) / (filesize/ULTRIXBS + 1);
  45.     if (info.fd_req.gfree < 0)    /* information unavailable */
  46.         ;            /* bypass check, and pray */
  47.     else if (info.fd_req.gfree <= wantinodes)
  48.         n = 0;
  49.     else if ((info.fd_req.gfree - wantinodes) / iperfile < n)
  50.         n = (info.fd_req.gfree - wantinodes) / iperfile;
  51.  
  52.     if (n < 0)
  53.         n = 0;
  54.     else if (n > LOTS)
  55.         n = LOTS;    /* to avert 16-bit trouble elsewhere */
  56.  
  57.     return(n);
  58. }
  59.