ReactOS  0.4.15-dev-439-g292f67a
label.c
Go to the documentation of this file.
1 /*
2  * LABEL.C - label internal command.
3  *
4  *
5  * History:
6  *
7  * 10-Dec-1998 (Eric Kohl)
8  * Started.
9  *
10  * 11-Dec-1998 (Eric Kohl)
11  * Finished.
12  *
13  * 19-Jan-1998 (Eric Kohl)
14  * Unicode ready!
15  *
16  * 28-Apr-2005 (Magnus Olsen <magnus@greatlord.com>)
17  * Remove all hardcoded strings in En.rc
18  */
19 
20 #include <stdio.h>
21 #include <stdlib.h>
22 
23 #include <windef.h>
24 #include <winbase.h>
25 #include <wincon.h>
26 #include <winnls.h>
27 #include <winuser.h>
28 
29 #include <conutils.h>
30 
31 #include "resource.h"
32 
33 #define MAX_LABEL_LENGTH 32
34 #define MAX_DRIVE_LENGTH 2
35 
36 
37 static
38 VOID
40 {
41  va_list arg_ptr;
42 
43  va_start(arg_ptr, MessageId);
46  NULL,
47  MessageId,
49  &arg_ptr);
50  va_end(arg_ptr);
51 }
52 
53 
54 static
55 VOID
57 {
58  DWORD dwOldMode;
59  DWORD dwRead = 0;
60  HANDLE hFile;
61  LPWSTR p;
62  PCHAR pBuf;
63 
65 
67  GetConsoleMode(hFile, &dwOldMode);
68 
70 
71  ReadFile(hFile, (PVOID)pBuf, dwLength - 1, &dwRead, NULL);
72 
73  MultiByteToWideChar(GetConsoleCP(), 0, pBuf, dwRead, lpInput, dwLength - 1);
74  HeapFree(GetProcessHeap(), 0, pBuf);
75 
76  for (p = lpInput; *p; p++)
77  {
78  if (*p == L'\x0d')
79  {
80  *p = UNICODE_NULL;
81  break;
82  }
83  }
84 
85  SetConsoleMode(hFile, dwOldMode);
86 }
87 
88 
89 static
90 BOOL
92 {
93  WCHAR szOldPath[MAX_PATH];
94  BOOL bResult;
95 
96  GetCurrentDirectoryW(MAX_PATH, szOldPath);
97  bResult = SetCurrentDirectoryW(pszPath);
98 
99  SetCurrentDirectoryW(szOldPath);
100 
101  return bResult;
102 }
103 
104 
105 static
106 BOOL
108 {
109  WCHAR szOptions[4];
110  WCHAR szInput[16];
111  BOOL bResult = FALSE;
112 
114 
115  for (;;)
116  {
117  ConPuts(StdOut, L"\n");
119 
120  ConInString(szInput, ARRAYSIZE(szInput));
121 
122  if (towupper(szInput[0]) == szOptions[0])
123  {
124  bResult = TRUE;
125  break;
126  }
127  else if (towupper(szInput[0]) == szOptions[1])
128  {
129  bResult = FALSE;
130  break;
131  }
132 
133  ConPuts(StdOut, L"\n");
134  }
135 
136  ConPuts(StdOut, L"\n");
137 
138  return bResult;
139 }
140 
141 
142 int wmain(int argc, WCHAR *argv[])
143 {
144  WCHAR szRootPath[] = L" :\\";
145  WCHAR szBuffer[80] = L"";
146  WCHAR szLabel[80] = L"";
147  WCHAR szOldLabel[80];
148  DWORD dwSerialNr;
149  INT len, i;
150 
151  /* Initialize the Console Standard Streams */
153 
154  /* print help */
155  if (argc > 1 && wcscmp(argv[1], L"/?") == 0)
156  {
158  return 0;
159  }
160 
161  if (argc > 1)
162  {
163  len = 0;
164  for (i = 1; i < argc; i++)
165  {
166  if (i > 1)
167  len++;
168  len += wcslen(argv[i]);
169  }
170 
172  {
174  return 1;
175  }
176 
177  for (i = 1; i < argc; i++)
178  {
179  if (i > 1)
180  wcscat(szBuffer, L" ");
181  wcscat(szBuffer, argv[i]);
182  }
183  }
184 
185  if (wcslen(szBuffer) > 0)
186  {
187  if (szBuffer[1] == L':')
188  {
189  szRootPath[0] = towupper(szBuffer[0]);
190  wcscpy(szLabel, &szBuffer[2]);
191  }
192  else
193  {
194  wcscpy(szLabel, szBuffer);
195  }
196  }
197 
198  if (wcslen(szLabel) > MAX_LABEL_LENGTH)
199  {
201  return 1;
202  }
203 
204  if (szRootPath[0] == L' ')
205  {
206  /* get label of current drive */
207  WCHAR szCurPath[MAX_PATH];
208  GetCurrentDirectoryW(MAX_PATH, szCurPath);
209  szRootPath[0] = szCurPath[0];
210  }
211 
212  /* check root path */
213  if (!IsValidPathName(szRootPath))
214  {
216  return 1;
217  }
218 
219  if (wcslen(szLabel) == 0)
220  {
221  GetVolumeInformationW(szRootPath, szOldLabel, ARRAYSIZE(szOldLabel), &dwSerialNr,
222  NULL, NULL, NULL, 0);
223 
224  /* print drive info */
225  if (szOldLabel[0] != UNICODE_NULL)
226  {
227  ConResPrintf(StdOut, STRING_LABEL_TEXT1, towupper(szRootPath[0]), szOldLabel);
228  }
229  else
230  {
232  }
233 
234  /* print the volume serial number */
235  ConResPrintf(StdOut, STRING_LABEL_TEXT3, HIWORD(dwSerialNr), LOWORD(dwSerialNr));
236 
238 
239  ConInString(szLabel, ARRAYSIZE(szLabel));
240  ConPuts(StdOut, L"\n");
241 
242  if (wcslen(szLabel) == 0)
243  {
244  if (PromptYesNo() == FALSE)
245  return 0;
246  }
247  }
248 
249  if (!SetVolumeLabelW(szRootPath, szLabel))
250  {
252  return 1;
253  }
254 
255  return 0;
256 }
257 
258 /* EOF */
signed char * PCHAR
Definition: retypes.h:7
static int argc
Definition: ServiceArgs.c:12
#define TRUE
Definition: types.h:120
const WCHAR * LPCWSTR
Definition: xmlstorage.h:185
BOOL WINAPI GetConsoleMode(HANDLE hConsoleHandle, LPDWORD lpMode)
Definition: console.c:1571
#define GetCurrentDirectoryW(x, y)
Definition: compat.h:422
static VOID ConInString(LPWSTR lpInput, DWORD dwLength)
Definition: label.c:56
#define ARRAYSIZE(array)
Definition: filtermapper.c:47
DWORD WINAPI GetLastError(VOID)
Definition: except.c:1059
HANDLE WINAPI GetStdHandle(IN DWORD nStdHandle)
Definition: console.c:200
#define STRING_LABEL_PROMPT
Definition: resource.h:10
#define argv
Definition: mplay32.c:18
int32_t INT
Definition: typedefs.h:57
#define STRING_ERROR_INVALID_DRIVE
Definition: resource.h:13
INT ConMsgPrintfV(IN PCON_STREAM Stream, IN DWORD dwFlags, IN LPCVOID lpSource OPTIONAL, IN DWORD dwMessageId, IN DWORD dwLanguageId, IN va_list *Arguments OPTIONAL)
Definition: outstream.c:1030
#define ENABLE_ECHO_INPUT
Definition: wincon.h:80
GLsizei GLenum const GLvoid GLsizei GLenum GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLint GLint GLint GLshort GLshort GLshort GLubyte GLubyte GLubyte GLuint GLuint GLuint GLushort GLushort GLushort GLbyte GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLfloat GLint GLint GLint GLint GLshort GLshort GLshort GLshort GLubyte GLubyte GLubyte GLubyte GLuint GLuint GLuint GLuint GLushort GLushort GLushort GLushort GLboolean const GLdouble const GLfloat const GLint const GLshort const GLbyte const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLdouble const GLfloat const GLfloat const GLint const GLint const GLshort const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort GLenum GLenum GLenum GLfloat GLenum GLint GLenum GLenum GLenum GLfloat GLenum GLenum GLint GLenum GLfloat GLenum GLint GLint GLushort GLenum GLenum GLfloat GLenum GLenum GLint GLfloat const GLubyte GLenum GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLint GLint GLsizei GLsizei GLint GLenum GLenum const GLvoid GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLenum const GLdouble GLenum GLenum const GLfloat GLenum GLenum const GLint GLsizei GLuint GLfloat GLuint GLbitfield GLfloat GLint GLuint GLboolean GLenum GLfloat GLenum GLbitfield GLenum GLfloat GLfloat GLint GLint const GLfloat GLenum GLfloat GLfloat GLint GLint GLfloat GLfloat GLint GLint const GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat const GLdouble const GLfloat const GLdouble const GLfloat GLint i
Definition: glfuncs.h:248
#define ConInitStdStreams()
Definition: stream.h:122
#define va_end(ap)
Definition: acmsvcex.h:90
#define STRING_ERROR_INVALID_LABEL
Definition: resource.h:14
#define UNICODE_NULL
#define STRING_LABEL_TEXT4
Definition: resource.h:9
unsigned int BOOL
Definition: ntddk_ex.h:94
BOOL WINAPI DECLSPEC_HOTPATCH SetConsoleMode(HANDLE hConsoleHandle, DWORD dwMode)
Definition: console.c:1608
#define STRING_LABEL_TEXT1
Definition: resource.h:6
smooth NULL
Definition: ftsmooth.c:416
char * va_list
Definition: acmsvcex.h:78
#define MAX_DRIVE_LENGTH
Definition: label.c:34
UINT WINAPI DECLSPEC_HOTPATCH GetConsoleCP(VOID)
Definition: console.c:2393
#define STD_INPUT_HANDLE
Definition: winbase.h:264
#define FORMAT_MESSAGE_FROM_SYSTEM
Definition: winbase.h:404
#define STRING_LABEL_TEXT2
Definition: resource.h:7
#define PCHAR
Definition: match.c:90
INT __cdecl ConResPrintf(IN PCON_STREAM Stream, IN UINT uID,...)
Definition: outstream.c:781
#define GetProcessHeap()
Definition: compat.h:404
PVOID WINAPI HeapAlloc(HANDLE, DWORD, SIZE_T)
__wchar_t WCHAR
Definition: xmlstorage.h:180
static DWORD DWORD * dwLength
Definition: fusion.c:85
BOOL WINAPI SetVolumeLabelW(IN LPCWSTR lpRootPathName, IN LPCWSTR lpVolumeName OPTIONAL)
Definition: volume.c:503
#define MAX_PATH
Definition: compat.h:26
unsigned long DWORD
Definition: ntddk_ex.h:95
#define ENABLE_LINE_INPUT
Definition: wincon.h:79
#define StdErr
Definition: stream.h:77
#define MAX_LABEL_LENGTH
Definition: label.c:33
INT ConResPuts(IN PCON_STREAM Stream, IN UINT uID)
Definition: outstream.c:610
#define STRING_LABEL_HELP
Definition: resource.h:5
_CRTIMP wchar_t *__cdecl wcscpy(_Out_writes_z_(_String_length_(_Source)+1) wchar_t *_Dest, _In_z_ const wchar_t *_Source)
static const WCHAR L[]
Definition: oid.c:1250
GLenum GLsizei len
Definition: glext.h:6722
_In_ HANDLE hFile
Definition: mswsock.h:90
#define STRING_LABEL_OPTIONS
Definition: resource.h:11
BOOL WINAPI GetVolumeInformationW(IN LPCWSTR lpRootPathName, IN LPWSTR lpVolumeNameBuffer, IN DWORD nVolumeNameSize, OUT LPDWORD lpVolumeSerialNumber OPTIONAL, OUT LPDWORD lpMaximumComponentLength OPTIONAL, OUT LPDWORD lpFileSystemFlags OPTIONAL, OUT LPWSTR lpFileSystemNameBuffer OPTIONAL, IN DWORD nFileSystemNameSize)
Definition: volume.c:226
_Check_return_ _CRTIMP int __cdecl wcscmp(_In_z_ const wchar_t *_Str1, _In_z_ const wchar_t *_Str2)
#define GetModuleHandle
Definition: winbase.h:3667
BOOL WINAPI SetCurrentDirectoryW(IN LPCWSTR lpPathName)
Definition: path.c:2248
_CRTIMP wchar_t *__cdecl wcscat(_Inout_updates_z_(_String_length_(_Dest)+_String_length_(_Source)+1) wchar_t *_Dest, _In_z_ const wchar_t *_Source)
#define va_start(ap, A)
Definition: acmsvcex.h:91
#define HEAP_ZERO_MEMORY
Definition: compat.h:123
INT ConPuts(IN PCON_STREAM Stream, IN LPWSTR szStr)
Definition: outstream.c:427
#define MultiByteToWideChar
Definition: compat.h:100
int wmain(int argc, WCHAR *argv[])
Definition: label.c:142
#define STRING_LABEL_TEXT3
Definition: resource.h:8
static VOID ConFormatMessage(PCON_STREAM Stream, DWORD MessageId,...)
Definition: label.c:39
#define StdOut
Definition: stream.h:76
#define HIWORD(l)
Definition: typedefs.h:246
static BOOL IsValidPathName(LPCWSTR pszPath)
Definition: label.c:91
#define towupper(c)
Definition: wctype.h:99
GLfloat GLfloat p
Definition: glext.h:8902
WCHAR * LPWSTR
Definition: xmlstorage.h:184
BOOL WINAPI ReadFile(IN HANDLE hFile, IN LPVOID lpBuffer, IN DWORD nNumberOfBytesToRead, OUT LPDWORD lpNumberOfBytesRead OPTIONAL, IN LPOVERLAPPED lpOverlapped OPTIONAL)
Definition: rw.c:123
#define LOWORD(l)
Definition: pedump.c:82
size_t __cdecl wcslen(_In_z_ const wchar_t *_Str)
#define HeapFree(x, y, z)
Definition: compat.h:403
_Inout_opt_ PUNICODE_STRING _Inout_opt_ PUNICODE_STRING Stream
Definition: fltkernel.h:1092
static BOOL PromptYesNo(VOID)
Definition: label.c:107
#define LoadString
Definition: winuser.h:5794
#define LANG_USER_DEFAULT
Definition: tnerror.cpp:50