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
12 #define COUNT_OF(a) (sizeof(a) / sizeof(a[0]))
16 EnumerateConnections(LPCWSTR Local
)
25 printf("%S\t\t\t%S\t\t\t\t%S\n", L
"Local", L
"Remote", L
"Provider");
27 dRet
= WNetOpenEnum(RESOURCE_CONNECTED
, RESOURCETYPE_DISK
, 0, NULL
, &hEnum
);
28 if (dRet
!= WN_SUCCESS
)
33 lpRes
= HeapAlloc(GetProcessHeap(), 0, dSize
);
45 memset(lpRes
, 0, dSize
);
46 dRet
= WNetEnumResource(hEnum
, &dCount
, lpRes
, &dSize
);
47 if (dRet
== WN_SUCCESS
|| dRet
== WN_MORE_DATA
)
50 for (; dCount
; dCount
--)
52 if (!Local
|| wcsicmp(lpCur
->lpLocalName
, Local
) == 0)
54 printf("%S\t\t\t%S\t\t%S\n", lpCur
->lpLocalName
, lpCur
->lpRemoteName
, lpCur
->lpProvider
);
60 } while (dRet
!= WN_NO_MORE_ENTRIES
);
62 HeapFree(GetProcessHeap(), 0, lpRes
);
77 Status
= EnumerateConnections(NULL
);
78 printf("Status: %lu\n", Status
);
83 Len
= wcslen(argv
[2]);
86 PrintResourceString(IDS_ERROR_INVALID_OPTION_VALUE
, L
"DeviceName");
90 if (!iswalpha(argv
[2][0]) || argv
[2][1] != L
':')
92 PrintResourceString(IDS_ERROR_INVALID_OPTION_VALUE
, L
"DeviceName");
96 Status
= EnumerateConnections(argv
[2]);
97 printf("Status: %lu\n", Status
);
101 Len
= wcslen(argv
[2]);
102 if (Len
!= 1 && Len
!= 2)
104 PrintResourceString(IDS_ERROR_INVALID_OPTION_VALUE
, L
"DeviceName");
105 printf("Len: %lu\n", Len
);
109 if (Len
== 2 && argv
[2][1] != L
':')
111 PrintResourceString(IDS_ERROR_INVALID_OPTION_VALUE
, L
"DeviceName");
115 if (argv
[2][0] != L
'*' && !iswalpha(argv
[2][0]))
117 PrintResourceString(IDS_ERROR_INVALID_OPTION_VALUE
, L
"DeviceName");
121 if (wcsicmp(argv
[3], L
"/DELETE") == 0)
123 if (argv
[2][0] == L
'*')
125 PrintResourceString(IDS_ERROR_INVALID_OPTION_VALUE
, L
"DeviceName");
129 return WNetCancelConnection(argv
[2], FALSE
);
133 BOOL Persist
= FALSE
;
136 DWORD OutFlags
= 0, Size
= COUNT_OF(Access
);
138 Len
= wcslen(argv
[3]);
141 PrintResourceString(IDS_ERROR_INVALID_OPTION_VALUE
, L
"Name");
145 if (argv
[3][0] != L
'\\' || argv
[3][1] != L
'\\')
147 PrintResourceString(IDS_ERROR_INVALID_OPTION_VALUE
, L
"Name");
154 Len
= wcslen(argv
[4]);
157 Cpy
= HeapAlloc(GetProcessHeap(), 0, (Len
+ 1) * sizeof(WCHAR
));
161 for (i
= 0; i
< Len
; ++i
)
162 Cpy
[i
] = towupper(argv
[4][i
]);
164 if (wcsstr(Cpy
, L
"/PERSISTENT:") == Cpy
)
166 LPWSTR Arg
= Cpy
+ 12;
167 if (Len
== 14 && Arg
[0] == 'N' && Arg
[1] == 'O')
171 else if (Len
== 15 && Arg
[0] == 'Y' && Arg
[1] == 'E' && Arg
[2] == 'S')
177 HeapFree(GetProcessHeap(), 0, Cpy
);
178 PrintResourceString(IDS_ERROR_INVALID_OPTION_VALUE
, L
"Persistent");
182 HeapFree(GetProcessHeap(), 0, Cpy
);
188 lpNet
.dwType
= RESOURCETYPE_DISK
;
189 lpNet
.lpLocalName
= (argv
[2][0] != L
'*') ? argv
[2] : NULL
;
190 lpNet
.lpRemoteName
= argv
[3];
191 lpNet
.lpProvider
= NULL
;
193 Status
= WNetUseConnection(NULL
, &lpNet
, NULL
, NULL
, CONNECT_REDIRECT
| (Persist
? CONNECT_UPDATE_PROFILE
: 0), Access
, &Size
, &OutFlags
);
194 if (argv
[2][0] == L
'*' && Status
== NO_ERROR
&& OutFlags
== CONNECT_LOCALDRIVE
)
195 printf("%S is now connected to %S\n", argv
[3], Access
);