home *** CD-ROM | disk | FTP | other *** search
/ Atari FTP / ATARI_FTP_0693.zip / ATARI_FTP_0693 / Mint / mntlib32.zoo / statfs.c < prev    next >
C/C++ Source or Header  |  1993-06-17  |  3KB  |  124 lines

  1. /*
  2.  * statfs() emulation for MiNT/TOS
  3.  *
  4.  * Written by Adrian Ashley (adrian@secret.uucp)
  5.  * and placed in the public domain.
  6.  */
  7.  
  8. #include <errno.h>
  9. #include <stat.h>
  10. #include <osbind.h>
  11. #include <mintbind.h>
  12. #include <unistd.h> /* for chdir, getcwd */
  13. #include <limits.h> /* for PATH_MAX */
  14. #ifdef __TURBOC__
  15. #include <sys\statfs.h>
  16. #else
  17. #include <sys/statfs.h>
  18. #endif
  19.  
  20. extern int __mint;
  21.  
  22. int statfs(path, buf)
  23.   char *path;
  24.   struct statfs *buf;
  25. {
  26.   int r;
  27.   _DISKINFO free;
  28.   struct stat statbuf;
  29.   struct {
  30.     long ninodes, nzones;
  31.     long finodes, fzones;
  32.     short version;
  33.     short increment;
  34.     long res1, res2, res3, res4;
  35.   } mfsinfo;
  36.  
  37.   if (!buf || !path)
  38.   {
  39.     errno = EFAULT;
  40.     return -1;
  41.   }
  42.  
  43.   r = stat(path, &statbuf);
  44.  
  45.   if (r == -1)
  46.     return -1;
  47.  
  48. /* This bit courtesy of S N Henson:
  49.  * We can do better than Dfree with minix filesystems
  50.  * they do have inodes and the Dcntl function MFS_INFO
  51.  * (0x104) tells us how many exist and how many are free.
  52.  * Also f_type is 1 for V1 filesystems and 2 for V2 (it
  53.  * is zero for TOS).
  54.  */
  55.   if(Dcntl(0x104,path, (long) &mfsinfo)==0)
  56.   {
  57.     buf->f_type = 1+mfsinfo.version;
  58.     buf->f_bsize = 1024;
  59.     buf->f_blocks = mfsinfo.nzones;
  60.     buf->f_bfree = buf->f_bavail = mfsinfo.fzones;
  61.     buf->f_files = mfsinfo.ninodes;
  62.     buf->f_ffree = mfsinfo.finodes;
  63.     buf->f_fsid.val[0] = buf->f_fsid.val[1] = -1L;
  64.   }
  65.   else {
  66.     if ((__mint >= 99) && (statbuf.st_dev >= 32))
  67.     {
  68.       /* Hack by HPP 02/06/1993: since MiNT 0.99 only returns     */
  69.       /* valid dfree info for pseudo-drives if they are the       */
  70.       /* current directory, change directories for the occasion.  */
  71.       char oldpath[PATH_MAX];
  72.     
  73.       if (getcwd(oldpath, PATH_MAX) != NULL)
  74.       {
  75.         chdir(path);
  76.         Dfree(&free, statbuf.st_dev + 1);
  77.         chdir(oldpath);
  78.       }
  79.       else
  80.         Dfree(&free, statbuf.st_dev + 1);
  81.     }
  82.     else
  83.       Dfree(&free, statbuf.st_dev + 1);
  84.  
  85.     buf->f_type = 0;
  86.     buf->f_bsize = free.b_secsiz * free.b_clsiz;
  87.     buf->f_blocks = free.b_total;
  88.     buf->f_bfree = buf->f_bavail = free.b_free;
  89.     buf->f_files = buf->f_ffree = buf->f_fsid.val[0] = buf->f_fsid.val[1] = -1L;
  90.   }
  91.   return 0;
  92. }
  93.  
  94. #ifdef TEST
  95.  
  96. #include <stdio.h>
  97.  
  98. main(int argc, char **argv)
  99. {
  100.   int i = 0, r;
  101.   register char *p;
  102.   struct statfs stbuf;
  103.  
  104.   while (--argc)
  105.   {
  106.     p = argv[++i];
  107.  
  108.     r = statfs(p, &stbuf);
  109.     if (r == -1)
  110.       perror(p);
  111.     else
  112.     {
  113.       printf("statfs(`%s'): %ld free bytes\n", p,
  114.     (long)(stbuf.f_bfree * stbuf.f_bsize));
  115.       printf("Fs type %ld\n",stbuf.f_type);
  116.       printf("%ld zones %ld free zones\n",stbuf.f_blocks,stbuf.f_bfree);
  117.       printf("%ld nodes %ld free nodes\n",stbuf.f_files,stbuf.f_ffree);
  118.     }
  119.   }
  120.   return 0;
  121. }
  122.  
  123. #endif
  124.