OSDN Git Service

Modify documents.
[ffftp/ffftp.git] / diskfree.c
1 /*=============================================================================\r
2 *\r
3 *                                               DISKのフリースペースを得る\r
4 *\r
5 ===============================================================================\r
6 / Copyright (C) 1997-2007 Sota. All rights reserved.\r
7 /\r
8 / Redistribution and use in source and binary forms, with or without \r
9 / modification, are permitted provided that the following conditions \r
10 / are met:\r
11 /\r
12 /  1. Redistributions of source code must retain the above copyright \r
13 /     notice, this list of conditions and the following disclaimer.\r
14 /  2. Redistributions in binary form must reproduce the above copyright \r
15 /     notice, this list of conditions and the following disclaimer in the \r
16 /     documentation and/or other materials provided with the distribution.\r
17 /\r
18 / THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR \r
19 / IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES \r
20 / OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. \r
21 / IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, \r
22 / INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, \r
23 / BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF \r
24 / USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON \r
25 / ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT \r
26 / (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF \r
27 / THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r
28 /============================================================================*/\r
29 \r
30 #define  STRICT\r
31 #include <windows.h>\r
32 #include <stdio.h>\r
33 #include <stdlib.h>\r
34 #include <string.h>\r
35 #include <mbstring.h>\r
36 #include <malloc.h>\r
37 #include <windowsx.h>\r
38 #include <winsock.h>\r
39 \r
40 #include "common.h"\r
41 #include "resource.h"\r
42 \r
43 \r
44 typedef DWORD (WINAPI*FUNC_GETDISKFREESPACEEX) (LPCTSTR, PULARGE_INTEGER, PULARGE_INTEGER, PULARGE_INTEGER);\r
45 \r
46 /*===== ローカルなワーク =====*/\r
47 \r
48 static HINSTANCE m_hDll = NULL;\r
49 \r
50 static FUNC_GETDISKFREESPACEEX m_GetDiskFreeSpaceEx = NULL;\r
51 \r
52 \r
53 \r
54 /*----- KERNEL32をロードする --------------------------------------------------\r
55 *\r
56 *       Parameter\r
57 *               なし\r
58 *\r
59 *       Return Value\r
60 *               なし\r
61 *----------------------------------------------------------------------------*/\r
62 \r
63 void LoadKernelLib(void)\r
64 {\r
65         OSVERSIONINFO VerInfo;\r
66 \r
67         VerInfo.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);\r
68         GetVersionEx(&VerInfo);\r
69         if(((VerInfo.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS) && (VerInfo.dwBuildNumber > 1000)) ||\r
70            (VerInfo.dwPlatformId == VER_PLATFORM_WIN32_NT))\r
71         {\r
72                 if((m_hDll = LoadLibrary("kernel32.dll")) != NULL)\r
73                 {\r
74                         m_GetDiskFreeSpaceEx = (FUNC_GETDISKFREESPACEEX)GetProcAddress(m_hDll, "GetDiskFreeSpaceExA");\r
75 \r
76                         if(m_GetDiskFreeSpaceEx == NULL)\r
77                         {\r
78                                 FreeLibrary(m_hDll);\r
79                                 m_hDll = NULL;\r
80                         }\r
81                 }\r
82         }\r
83         return;\r
84 }\r
85 \r
86 \r
87 /*----- KERNEL32をリリースする -------------------------------------------------\r
88 *\r
89 *       Parameter\r
90 *               なし\r
91 *\r
92 *       Return Value\r
93 *               なし\r
94 *----------------------------------------------------------------------------*/\r
95 \r
96 void ReleaseKernelLib(void)\r
97 {\r
98         if(m_hDll != NULL)\r
99                 FreeLibrary(m_hDll);\r
100         m_hDll = NULL;\r
101 \r
102         return;\r
103 }\r
104 \r
105 \r
106 /*----- フリーエリアのサイズを表わす文字列を返す-------------------------------\r
107 *\r
108 *       Parameter\r
109 *               char *Path : パス名\r
110 *\r
111 *       Return Value\r
112 *               char *文字列\r
113 *----------------------------------------------------------------------------*/\r
114 \r
115 char *AskLocalFreeSpace(char *Path)\r
116 {\r
117         DWORD SectClus;\r
118         DWORD ByteSect;\r
119         DWORD FreeClus;\r
120         DWORD AllClus;\r
121         ULARGE_INTEGER a;\r
122         ULARGE_INTEGER b;\r
123         ULARGE_INTEGER c;\r
124         double Free;\r
125         static char Buf[40];\r
126 \r
127         strcpy(Buf, "??");\r
128         if(*(Path+1) == ':')\r
129         {\r
130                 strncpy(Buf, Path, 2);\r
131                 strcpy(Buf+2, "\\");\r
132 \r
133                 if(m_GetDiskFreeSpaceEx != NULL)\r
134                 {\r
135                         if((*m_GetDiskFreeSpaceEx)(Buf, &a, &b, &c) != 0)\r
136                         {\r
137                                 _ui64toa(a.QuadPart, Buf, 10);\r
138                                 Free = atof(Buf);\r
139                                 MakeSizeString(Free, Buf);\r
140                         }\r
141                         else\r
142                                 strcpy(Buf, "??");\r
143                 }\r
144                 else\r
145                 {\r
146                         if(GetDiskFreeSpace(Buf, &SectClus, &ByteSect, &FreeClus, &AllClus) == TRUE)\r
147                         {\r
148                                 Free = SectClus * ByteSect * FreeClus;\r
149                                 MakeSizeString(Free, Buf);\r
150                         }\r
151                         else\r
152                                 strcpy(Buf, "??");\r
153                 }\r
154         }\r
155         return(Buf);\r
156 }\r
157 \r
158 \r