2 * COPYRIGHT: See COPYING in the top level directory
3 * PROJECT: ReactOS net command
4 * FILE: base/applications/network/net/cmdUse.c
7 * PROGRAMMERS: Pierre Schweitzer
14 EnumerateConnections(LPCWSTR Local
)
23 ConPrintf(StdOut
, L
"%s\t\t\t%s\t\t\t\t%s\n", L
"Local", L
"Remote", L
"Provider");
25 dRet
= WNetOpenEnum(RESOURCE_CONNECTED
, RESOURCETYPE_DISK
, 0, NULL
, &hEnum
);
26 if (dRet
!= WN_SUCCESS
)
31 lpRes
= HeapAlloc(GetProcessHeap(), 0, dSize
);
43 memset(lpRes
, 0, dSize
);
44 dRet
= WNetEnumResource(hEnum
, &dCount
, lpRes
, &dSize
);
45 if (dRet
== WN_SUCCESS
|| dRet
== WN_MORE_DATA
)
48 for (; dCount
; dCount
--)
50 if (!Local
|| wcsicmp(lpCur
->lpLocalName
, Local
) == 0)
52 ConPrintf(StdOut
, L
"%s\t\t\t%s\t\t%s\n", lpCur
->lpLocalName
, lpCur
->lpRemoteName
, lpCur
->lpProvider
);
58 } while (dRet
!= WN_NO_MORE_ENTRIES
);
60 HeapFree(GetProcessHeap(), 0, lpRes
);
68 PrintError(DWORD Status
)
72 ConResPrintf(StdErr
, IDS_ERROR_SYSTEM_ERROR
, Status
);
74 if (FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER
| FORMAT_MESSAGE_FROM_SYSTEM
| FORMAT_MESSAGE_IGNORE_INSERTS
, NULL
, Status
, 0, (LPWSTR
)&Buffer
, 0, NULL
))
76 ConPrintf(StdErr
, L
"\n%s", Buffer
);
83 ValidateDeviceName(PWSTR DevName
)
87 Len
= wcslen(DevName
);
93 if (!iswalpha(DevName
[0]) || DevName
[1] != L
':')
106 DWORD Status
, Len
, Delete
;
110 Status
= EnumerateConnections(NULL
);
111 if (Status
== NO_ERROR
)
112 PrintErrorMessage(ERROR_SUCCESS
);
120 if (!ValidateDeviceName(argv
[2]))
122 ConResPrintf(StdErr
, IDS_ERROR_INVALID_OPTION_VALUE
, L
"DeviceName");
126 Status
= EnumerateConnections(argv
[2]);
127 if (Status
== NO_ERROR
)
128 PrintErrorMessage(ERROR_SUCCESS
);
136 if (wcsicmp(argv
[2], L
"/DELETE") == 0)
142 if ((argv
[2][0] != '*' && argv
[2][1] != 0) &&
143 !ValidateDeviceName(argv
[2]))
145 ConResPrintf(StdErr
, IDS_ERROR_INVALID_OPTION_VALUE
, L
"DeviceName");
150 if (wcsicmp(argv
[3], L
"/DELETE") == 0)
157 if (!ValidateDeviceName(argv
[Delete
]) || argv
[Delete
][0] == L
'*')
159 ConResPrintf(StdErr
, IDS_ERROR_INVALID_OPTION_VALUE
, L
"DeviceName");
163 Status
= WNetCancelConnection2(argv
[Delete
], CONNECT_UPDATE_PROFILE
, FALSE
);
164 if (Status
!= NO_ERROR
)
171 BOOL Persist
= FALSE
;
174 DWORD OutFlags
= 0, Size
= ARRAYSIZE(Access
);
176 Len
= wcslen(argv
[3]);
179 ConResPrintf(StdErr
, IDS_ERROR_INVALID_OPTION_VALUE
, L
"Name");
183 if (argv
[3][0] != L
'\\' || argv
[3][1] != L
'\\')
185 ConResPrintf(StdErr
, IDS_ERROR_INVALID_OPTION_VALUE
, L
"Name");
192 Len
= wcslen(argv
[4]);
195 Cpy
= HeapAlloc(GetProcessHeap(), 0, (Len
+ 1) * sizeof(WCHAR
));
199 for (i
= 0; i
< Len
; ++i
)
200 Cpy
[i
] = towupper(argv
[4][i
]);
202 if (wcsstr(Cpy
, L
"/PERSISTENT:") == Cpy
)
204 LPWSTR Arg
= Cpy
+ 12;
205 if (Len
== 14 && Arg
[0] == 'N' && Arg
[1] == 'O')
209 else if (Len
== 15 && Arg
[0] == 'Y' && Arg
[1] == 'E' && Arg
[2] == 'S')
215 HeapFree(GetProcessHeap(), 0, Cpy
);
216 ConResPrintf(StdErr
, IDS_ERROR_INVALID_OPTION_VALUE
, L
"Persistent");
220 HeapFree(GetProcessHeap(), 0, Cpy
);
226 lpNet
.dwType
= RESOURCETYPE_DISK
;
227 lpNet
.lpLocalName
= (argv
[2][0] != L
'*') ? argv
[2] : NULL
;
228 lpNet
.lpRemoteName
= argv
[3];
229 lpNet
.lpProvider
= NULL
;
231 Status
= WNetUseConnection(NULL
, &lpNet
, NULL
, NULL
, CONNECT_REDIRECT
| (Persist
? CONNECT_UPDATE_PROFILE
: 0), Access
, &Size
, &OutFlags
);
232 if (argv
[2][0] == L
'*' && Status
== NO_ERROR
&& OutFlags
== CONNECT_LOCALDRIVE
)
233 ConResPrintf(StdOut
, IDS_USE_NOW_CONNECTED
, argv
[3], Access
);
234 else if (Status
!= NO_ERROR
)