ReactOS  0.4.14-dev-49-gfb4591c
cmdUse.c
Go to the documentation of this file.
1 /*
2  * COPYRIGHT: See COPYING in the top level directory
3  * PROJECT: ReactOS net command
4  * FILE: base/applications/network/net/cmdUse.c
5  * PURPOSE:
6  *
7  * PROGRAMMERS: Pierre Schweitzer
8  */
9 
10 #include "net.h"
11 
12 static
13 DWORD
15 {
16  DWORD dRet;
17  HANDLE hEnum;
18  LPNETRESOURCE lpRes;
19  DWORD dSize = 0x1000;
20  DWORD dCount = -1;
21  LPNETRESOURCE lpCur;
22 
23  ConPrintf(StdOut, L"%s\t\t\t%s\t\t\t\t%s\n", L"Local", L"Remote", L"Provider");
24 
26  if (dRet != WN_SUCCESS)
27  {
28  return 1;
29  }
30 
31  lpRes = HeapAlloc(GetProcessHeap(), 0, dSize);
32  if (!lpRes)
33  {
34  WNetCloseEnum(hEnum);
35  return 1;
36  }
37 
38  do
39  {
40  dSize = 0x1000;
41  dCount = -1;
42 
43  memset(lpRes, 0, dSize);
44  dRet = WNetEnumResource(hEnum, &dCount, lpRes, &dSize);
45  if (dRet == WN_SUCCESS || dRet == WN_MORE_DATA)
46  {
47  lpCur = lpRes;
48  for (; dCount; dCount--)
49  {
50  if (!Local || wcsicmp(lpCur->lpLocalName, Local) == 0)
51  {
52  ConPrintf(StdOut, L"%s\t\t\t%s\t\t%s\n", lpCur->lpLocalName, lpCur->lpRemoteName, lpCur->lpProvider);
53  }
54 
55  lpCur++;
56  }
57  }
58  } while (dRet != WN_NO_MORE_ENTRIES);
59 
60  HeapFree(GetProcessHeap(), 0, lpRes);
61  WNetCloseEnum(hEnum);
62 
63  return 0;
64 }
65 
66 static
67 VOID
69 {
70  WCHAR szStatusBuffer[16];
71  LPWSTR Buffer;
72 
73  swprintf(szStatusBuffer, L"%lu", Status);
74  PrintMessageStringV(3502, szStatusBuffer);
75 
77  {
78  ConPrintf(StdErr, L"\n%s", Buffer);
80  }
81 }
82 
83 static
84 BOOL
86 {
87  DWORD Len;
88 
89  Len = wcslen(DevName);
90  if (Len != 2)
91  {
92  return FALSE;
93  }
94 
95  if (!iswalpha(DevName[0]) || DevName[1] != L':')
96  {
97  return FALSE;
98  }
99 
100  return TRUE;
101 }
102 
103 INT
105  INT argc,
106  WCHAR **argv)
107 {
109 
110  if (argc == 2)
111  {
113  if (Status == NO_ERROR)
115  else
117 
118  return 0;
119  }
120  else if (argc == 3)
121  {
122  if (!ValidateDeviceName(argv[2]))
123  {
124  PrintMessageStringV(3952, L"DeviceName");
125  return 1;
126  }
127 
129  if (Status == NO_ERROR)
131  else
133 
134  return 0;
135  }
136 
137  Delete = 0;
138  if (wcsicmp(argv[2], L"/DELETE") == 0)
139  {
140  Delete = 3;
141  }
142  else
143  {
144  if ((argv[2][0] != '*' && argv[2][1] != 0) &&
146  {
147  PrintMessageStringV(3952, L"DeviceName");
148  return 1;
149  }
150  }
151 
152  if (wcsicmp(argv[3], L"/DELETE") == 0)
153  {
154  Delete = 2;
155  }
156 
157  if (Delete != 0)
158  {
159  if (!ValidateDeviceName(argv[Delete]) || argv[Delete][0] == L'*')
160  {
161  PrintMessageStringV(3952, L"DeviceName");
162  return 1;
163  }
164 
166  if (Status != NO_ERROR)
168 
169  return Status;
170  }
171  else
172  {
173  BOOL Persist = FALSE;
174  NETRESOURCE lpNet;
175  WCHAR Access[256];
176  DWORD OutFlags = 0, Size = ARRAYSIZE(Access);
177 
178  Len = wcslen(argv[3]);
179  if (Len < 4)
180  {
181  PrintMessageStringV(3952, L"Name");
182  return 1;
183  }
184 
185  if (argv[3][0] != L'\\' || argv[3][1] != L'\\')
186  {
187  PrintMessageStringV(3952, L"Name");
188  return 1;
189  }
190 
191  if (argc > 4)
192  {
193  LPWSTR Cpy;
194  Len = wcslen(argv[4]);
195  if (Len > 12)
196  {
197  Cpy = HeapAlloc(GetProcessHeap(), 0, (Len + 1) * sizeof(WCHAR));
198  if (Cpy)
199  {
200  INT i;
201  for (i = 0; i < Len; ++i)
202  Cpy[i] = towupper(argv[4][i]);
203 
204  if (wcsstr(Cpy, L"/PERSISTENT:") == Cpy)
205  {
206  LPWSTR Arg = Cpy + 12;
207  if (Len == 14 && Arg[0] == 'N' && Arg[1] == 'O')
208  {
209  Persist = FALSE;
210  }
211  else if (Len == 15 && Arg[0] == 'Y' && Arg[1] == 'E' && Arg[2] == 'S')
212  {
213  Persist = TRUE;
214  }
215  else
216  {
217  HeapFree(GetProcessHeap(), 0, Cpy);
218  PrintMessageStringV(3952, L"Persistent");
219  return 1;
220  }
221  }
222  HeapFree(GetProcessHeap(), 0, Cpy);
223  }
224  }
225 
226  }
227 
228  lpNet.dwType = RESOURCETYPE_DISK;
229  lpNet.lpLocalName = (argv[2][0] != L'*') ? argv[2] : NULL;
230  lpNet.lpRemoteName = argv[3];
231  lpNet.lpProvider = NULL;
232 
233  Status = WNetUseConnection(NULL, &lpNet, NULL, NULL, CONNECT_REDIRECT | (Persist ? CONNECT_UPDATE_PROFILE : 0), Access, &Size, &OutFlags);
234  if (argv[2][0] == L'*' && Status == NO_ERROR && OutFlags == CONNECT_LOCALDRIVE)
236  else if (Status != NO_ERROR)
238 
239  return Status;
240  }
241 }
static int argc
Definition: ServiceArgs.c:12
#define TRUE
Definition: types.h:120
VOID PrintMessageStringV(DWORD dwMessage,...)
Definition: main.c:82
DWORD dwType
Definition: winnetwk.h:168
#define CONNECT_UPDATE_PROFILE
Definition: winnetwk.h:87
#define ERROR_SUCCESS
Definition: deptool.c:10
const WCHAR * LPCWSTR
Definition: xmlstorage.h:185
#define WN_SUCCESS
Definition: winnetwk.h:111
_CONST_RETURN wchar_t *__cdecl wcsstr(_In_z_ const wchar_t *_Str, _In_z_ const wchar_t *_SubStr)
uint16_t * PWSTR
Definition: typedefs.h:54
#define WNetCancelConnection2
Definition: winnetwk.h:607
LPSTR lpLocalName
Definition: winnetwk.h:171
#define ARRAYSIZE(array)
Definition: filtermapper.c:47
#define WNetUseConnection
Definition: winnetwk.h:609
#define iswalpha(_c)
Definition: ctype.h:664
#define argv
Definition: mplay32.c:18
#define NO_ERROR
Definition: dderror.h:5
int32_t INT
Definition: typedefs.h:56
#define RESOURCE_CONNECTED
Definition: winnetwk.h:58
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
unsigned int BOOL
Definition: ntddk_ex.h:94
static IPersist Persist
Definition: atl.c:503
#define FORMAT_MESSAGE_ALLOCATE_BUFFER
Definition: winbase.h:400
INT __cdecl ConPrintf(IN PCON_STREAM Stream, IN LPWSTR szStr,...)
Definition: outstream.c:520
smooth NULL
Definition: ftsmooth.c:416
Definition: bufpool.h:45
#define FORMAT_MESSAGE_FROM_SYSTEM
Definition: winbase.h:404
LPSTR lpProvider
Definition: winnetwk.h:174
#define WNetOpenEnum
Definition: winnetwk.h:598
INT __cdecl ConResPrintf(IN PCON_STREAM Stream, IN UINT uID,...)
Definition: outstream.c:781
static DWORD EnumerateConnections(LPCWSTR Local)
Definition: cmdUse.c:14
#define GetProcessHeap()
Definition: compat.h:395
PVOID WINAPI HeapAlloc(HANDLE, DWORD, SIZE_T)
#define CONNECT_REDIRECT
Definition: winnetwk.h:94
__wchar_t WCHAR
Definition: xmlstorage.h:180
#define swprintf(buf, format,...)
Definition: sprintf.c:56
unsigned long DWORD
Definition: ntddk_ex.h:95
static VOID PrintError(DWORD Status)
Definition: cmdUse.c:68
#define Len
Definition: deflate.h:82
#define StdErr
Definition: stream.h:77
#define FormatMessage
Definition: winbase.h:3612
static const WCHAR L[]
Definition: oid.c:1250
#define IDS_USE_NOW_CONNECTED
Definition: resource.h:3
INT cmdUse(INT argc, WCHAR **argv)
Definition: cmdUse.c:104
DWORD WINAPI WNetCloseEnum(HANDLE hEnum)
Definition: wnet.c:1621
IN PVOID IN PVOID IN USHORT IN USHORT Size
Definition: pci.h:359
#define wcsicmp
Definition: string.h:1152
#define WNetEnumResource
Definition: winnetwk.h:599
#define RESOURCETYPE_DISK
Definition: winnetwk.h:64
Status
Definition: gdiplustypes.h:24
static BOOL ValidateDeviceName(PWSTR DevName)
Definition: cmdUse.c:85
#define FORMAT_MESSAGE_IGNORE_INSERTS
Definition: winbase.h:401
static VOID PrintErrorMessage(DWORD dwError)
Definition: at.c:308
HLOCAL NTAPI LocalFree(HLOCAL hMem)
Definition: heapmem.c:1577
#define WN_NO_MORE_ENTRIES
Definition: winnetwk.h:146
LPSTR lpRemoteName
Definition: winnetwk.h:172
#define CONNECT_LOCALDRIVE
Definition: winnetwk.h:95
#define StdOut
Definition: stream.h:76
#define WN_MORE_DATA
Definition: winnetwk.h:117
#define towupper(c)
Definition: wctype.h:99
WCHAR * LPWSTR
Definition: xmlstorage.h:184
IN BOOLEAN OUT PSTR Buffer
Definition: progress.h:34
#define memset(x, y, z)
Definition: compat.h:39
BOOL Delete(LPCTSTR ServiceName)
Definition: delete.c:12
size_t __cdecl wcslen(_In_z_ const wchar_t *_Str)
#define HeapFree(x, y, z)
Definition: compat.h:394