Copy wininet to branch
[reactos.git] / reactos / subsys / system / cmd / free.c
1 /*
2 * FREE.C - internal command.
3 *
4 *
5 * History:
6 *
7 * 01-Sep-1999 (Eric Kohl)
8 * Started.
9 */
10
11 #include "precomp.h"
12
13 #ifdef INCLUDE_CMD_FREE
14
15
16 /*
17 * convert
18 *
19 * insert commas into a number
20 */
21
22 static INT
23 ConvertULargeInteger (ULARGE_INTEGER num, LPTSTR des, INT len)
24 {
25 TCHAR temp[32];
26 INT c = 0;
27 INT n = 0;
28
29 if (num.QuadPart == 0)
30 {
31 des[0] = _T('0');
32 des[1] = _T('\0');
33 n = 1;
34 }
35 else
36 {
37 temp[31] = 0;
38 while (num.QuadPart > 0)
39 {
40 if (((c + 1) % (nNumberGroups + 1)) == 0)
41 temp[30 - c++] = cThousandSeparator;
42 temp[30 - c++] = (TCHAR)(num.QuadPart % 10) + _T('0');
43 num.QuadPart /= 10;
44 }
45
46 for (n = 0; n <= c; n++)
47 des[n] = temp[31 - c + n];
48 }
49
50 return n;
51 }
52
53
54 static VOID
55 PrintDiskInfo (LPTSTR szDisk)
56 {
57 TCHAR szRootPath[4] = _T("A:\\");
58 TCHAR szDrive[2] = _T("A");
59 TCHAR szVolume[64];
60 TCHAR szSerial[10];
61 TCHAR szTotal[40];
62 TCHAR szUsed[40];
63 TCHAR szFree[40];
64 DWORD dwSerial;
65 ULARGE_INTEGER uliSize;
66 DWORD dwSecPerCl;
67 DWORD dwBytPerSec;
68 DWORD dwFreeCl;
69 DWORD dwTotCl;
70
71 if (_tcslen (szDisk) < 2 || szDisk[1] != _T(':'))
72 {
73 ConErrPrintf (_T("Invalid drive %s\n"), szDisk);
74 return;
75 }
76
77 szRootPath[0] = szDisk[0];
78 szDrive[0] = _totupper (szRootPath[0]);
79
80 if (!GetVolumeInformation (szRootPath, szVolume, 64, &dwSerial,
81 NULL, NULL, NULL, 0))
82 {
83 ConErrPrintf (_T("Invalid drive %s:\n"), szDrive);
84 return;
85 }
86
87 if (szVolume[0] == _T('\0'))
88 _tcscpy (szVolume, _T("unlabeled"));
89
90 _stprintf (szSerial,
91 _T("%04X-%04X"),
92 HIWORD(dwSerial),
93 LOWORD(dwSerial));
94
95 if (!GetDiskFreeSpace (szRootPath, &dwSecPerCl,
96 &dwBytPerSec, &dwFreeCl, &dwTotCl))
97 {
98 ConErrPrintf (_T("Invalid drive %s:\n"), szDrive);
99 return;
100 }
101
102 uliSize.QuadPart = dwSecPerCl * dwBytPerSec * dwTotCl;
103 ConvertULargeInteger (uliSize, szTotal, 40);
104
105 uliSize.QuadPart = dwSecPerCl * dwBytPerSec * (dwTotCl - dwFreeCl);
106 ConvertULargeInteger (uliSize, szUsed, 40);
107
108 uliSize.QuadPart = dwSecPerCl * dwBytPerSec * dwFreeCl;
109 ConvertULargeInteger (uliSize, szFree, 40);
110
111 ConOutPrintf (_T("\n"
112 " Volume in drive %s is %-11s Serial number is %s\n"
113 " %16s bytes total disk space\n"
114 " %16s bytes used\n"
115 " %16s bytes free\n"),
116 szDrive, szVolume, szSerial,
117 szTotal, szUsed, szFree);
118 }
119
120
121 INT CommandFree (LPTSTR cmd, LPTSTR param)
122 {
123 LPTSTR szParam;
124 TCHAR szDefPath[MAX_PATH];
125 INT argc, i;
126 LPTSTR *arg;
127
128 if (!_tcsncmp (param, _T("/?"), 2))
129 {
130 ConOutPuts (_T("Displays drive information.\n"
131 "\n"
132 "FREE [drive: ...]"));
133 return 0;
134 }
135
136 if (!param || *param == _T('\0'))
137 {
138 GetCurrentDirectory (MAX_PATH, szDefPath);
139 szDefPath[2] = _T('\0');
140 szParam = szDefPath;
141 }
142 else
143 szParam = param;
144
145 arg = split (szParam, &argc, FALSE);
146
147 for (i = 0; i < argc; i++)
148 PrintDiskInfo (arg[i]);
149
150 freep (arg);
151
152 return 0;
153 }
154
155 #endif /* INCLUDE_CMD_FREE */
156
157 /* EOF */