Main Page | File List | Globals

src/statfs.c

Go to the documentation of this file.
00001 /*
00002      This file is part of PlibC.
00003      (C) 2005 Nils Durner (and other contributing authors)
00004 
00005            This library is free software; you can redistribute it and/or
00006            modify it under the terms of the GNU Lesser General Public
00007            License as published by the Free Software Foundation; either
00008            version 2.1 of the License, or (at your option) any later version.
00009         
00010            This library is distributed in the hope that it will be useful,
00011            but WITHOUT ANY WARRANTY; without even the implied warranty of
00012            MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013            Lesser General Public License for more details.
00014         
00015            You should have received a copy of the GNU Lesser General Public
00016            License along with this library; if not, write to the Free Software
00017            Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00018 */
00019 
00025 #include "plibc_private.h"
00026 
00032 int statfs(const char *path, struct statfs *buf)
00033 {
00034   HINSTANCE h;
00035   FARPROC f;
00036   char tmp[MAX_PATH], resolved_path[MAX_PATH];
00037   int retval = 0;
00038 
00039   errno = 0;
00040 
00041   realpath(path, resolved_path);
00042   if(!resolved_path)
00043     retval = -1;
00044   else
00045   {
00046     /* check whether GetDiskFreeSpaceExA is supported */
00047     h = LoadLibraryA("kernel32.dll");
00048     if(h)
00049       f = GetProcAddress(h, "GetDiskFreeSpaceExA");
00050     else
00051       f = NULL;
00052     if(f)
00053     {
00054       ULARGE_INTEGER bytes_free, bytes_total, bytes_free2;
00055       if(!f(resolved_path, &bytes_free2, &bytes_total, &bytes_free))
00056       {
00057         errno = ENOENT;
00058         retval = -1;
00059       }
00060       else
00061       {
00062         buf->f_bsize = FAKED_BLOCK_SIZE;
00063         buf->f_bfree = (bytes_free.QuadPart) / FAKED_BLOCK_SIZE;
00064         buf->f_files = buf->f_blocks =
00065           (bytes_total.QuadPart) / FAKED_BLOCK_SIZE;
00066         buf->f_ffree = buf->f_bavail =
00067           (bytes_free2.QuadPart) / FAKED_BLOCK_SIZE;
00068       }
00069     }
00070     else
00071     {
00072       DWORD sectors_per_cluster, bytes_per_sector;
00073       if(h)
00074         FreeLibrary(h);
00075       if(!GetDiskFreeSpaceA(resolved_path, &sectors_per_cluster,
00076                             &bytes_per_sector, &buf->f_bavail,
00077                             &buf->f_blocks))
00078       {
00079         errno = ENOENT;
00080         retval = -1;
00081       }
00082       else
00083       {
00084         buf->f_bsize = sectors_per_cluster * bytes_per_sector;
00085         buf->f_files = buf->f_blocks;
00086         buf->f_ffree = buf->f_bavail;
00087         buf->f_bfree = buf->f_bavail;
00088       }
00089     }
00090     if(h)
00091       FreeLibrary(h);
00092   }
00093 
00094   /* get the FS volume information */
00095   if(strspn(":", resolved_path) > 0)
00096     resolved_path[3] = '\0';    /* we want only the root */
00097   if(GetVolumeInformation
00098      (resolved_path, NULL, 0, &buf->f_fsid, &buf->f_namelen, NULL, tmp,
00099       MAX_PATH))
00100   {
00101     if(strcasecmp("NTFS", tmp) == 0)
00102     {
00103       buf->f_type = NTFS_SUPER_MAGIC;
00104     }
00105     else
00106     {
00107       buf->f_type = MSDOS_SUPER_MAGIC;
00108     }
00109   }
00110   else
00111   {
00112     errno = ENOENT;
00113     retval = -1;
00114   }
00115   return retval;
00116 }
00117 
00118 /* end of statfs.c */

Generated on Sun Sep 4 11:16:47 2005 for PlibC by  doxygen 1.4.2