home *** CD-ROM | disk | FTP | other *** search
/ Unix System Administration Handbook 1997 October / usah_oct97.iso / news / cnews.tar / util / dostatfs.c < prev    next >
C/C++ Source or Header  |  1995-04-27  |  2KB  |  110 lines

  1. /*
  2.  * dostatfs - the heart of spacefor.statfs
  3.  */
  4.  
  5. #include <stdio.h>
  6. #include <string.h>
  7. #include <sys/types.h>
  8.  
  9. /*
  10.  * Unfortunately, the whereabouts of the necessary struct, the exact
  11.  * calling convention for statfs(), and the name of the "units in which
  12.  * free space is expressed" member, are rather system-specific.  Here's
  13.  * a few attempts...
  14.  */
  15.  
  16. /* First, pick up a few popular headers on general principles. */
  17. #include <sys/param.h>
  18. #include <sys/mount.h>
  19.  
  20. /* Second, assorted variations... */
  21. #ifdef linux
  22. #define    sun    1    /* Linux happens to be the same as Sun for this... */
  23. #else
  24. #ifdef __linux__
  25. #define    sun    1    /* a Linux by any other name... */
  26. #endif
  27. #endif
  28. #ifdef hpux
  29. #define    sun    1    /* likewise HP */
  30. #endif
  31. #ifdef __FreeBSD__
  32. #define    BSD4_4    1    /* and FreeBSD is sort of 4.4 */
  33. #endif
  34.  
  35. #ifdef sun
  36. #include <sys/vfs.h>
  37. #define    UNIT    f_bsize
  38. #endif
  39.  
  40. #ifdef _AIX
  41. #include <sys/statfs.h>
  42. #define    UNIT    f_fsize
  43. #endif
  44.  
  45. #ifdef M_XENIX        /* SCO */
  46. #include <sys/statfs.h>
  47. #define    STATFS(fs, result)    statfs(fs, &result, (int)sizeof(result), 0)
  48. #define    UNIT    f_fsize
  49. #define    f_bavail    f_bfree    /* talk about kludges */
  50. #endif
  51.  
  52. #ifdef BSD4_4
  53. #define    UNIT    f_bsize
  54. #endif
  55.  
  56. /* Finally, some defaults to simplify the above. */
  57. #ifndef UNIT
  58. #define    UNIT    f_fsize
  59. #endif
  60. #ifndef STATFS
  61. #define    STATFS(fs, result)    statfs(fs, &result)
  62. #endif
  63.  
  64.  
  65. extern int debug;
  66. extern void error();
  67.  
  68. /*
  69.  - spacefor - do the work
  70.  */
  71. long
  72. spacefor(filesize, fileonfs, wantspace, wantinodes, bperi)
  73. long filesize;
  74. char *fileonfs;
  75. long wantspace;
  76. long wantinodes;
  77. long bperi;
  78. {
  79.     struct statfs info;
  80.     register long n;
  81. #    define    LOTS    10000
  82.     register long iperfile = filesize/bperi + 1;
  83.  
  84.     if (STATFS(fileonfs, info) < 0)
  85.         error("cannot do statfs(%s)", fileonfs);
  86.     if (debug)
  87.         fprintf(stderr, "bsize %ld, avail %ld, inodes %ld\n",
  88.                 info.UNIT, info.f_bavail, info.f_ffree);
  89.  
  90.     n = LOTS;
  91.     if (info.f_bavail <= wantspace)
  92.         n = 0;
  93.     else if (info.UNIT > 0 && filesize > 0)
  94.         n = (info.f_bavail - wantspace) / (filesize/info.UNIT + 1);
  95.  
  96.     if (info.f_ffree < 0)        /* information unavailable */
  97.         ;            /* bypass check, and pray */
  98.     else if (info.f_ffree <= wantinodes)
  99.         n = 0;
  100.     else if ((info.f_ffree - wantinodes) / iperfile < n)
  101.         n = (info.f_ffree - wantinodes) / iperfile;
  102.  
  103.     if (n < 0)
  104.         n = 0;
  105.     else if (n > LOTS)
  106.         n = LOTS;    /* to avert 16-bit trouble elsewhere */
  107.  
  108.     return(n);
  109. }
  110.