1 /*
2  * COPYRIGHT:       See COPYING in the top level directory
3  * PROJECT:         ReactOS net command
4  * FILE:            base/applications/network/net/cmdShare.c
5  * PROGRAMMERS:     Eric Kohl <eric.kohl@reactos.org>
6  */
7 
8 #include "net.h"
9 
10 
11 NET_API_STATUS
EnumerateShares(VOID)12 EnumerateShares(VOID)
13 {
14     PSHARE_INFO_2 pBuffer = NULL;
15     DWORD dwRead = 0, dwTotal = 0;
16     DWORD ResumeHandle = 0, i;
17     NET_API_STATUS Status;
18 
19     ConPuts(StdOut, L"\n");
20     PrintMessageString(4730);
21     ConPuts(StdOut, L"\n");
22     PrintPadding(L'-', 79);
23     ConPuts(StdOut, L"\n");
24 
25     do
26     {
27         Status = NetShareEnum(NULL,
28                               2,
29                               (LPBYTE*)&pBuffer,
30                               MAX_PREFERRED_LENGTH,
31                               &dwRead,
32                               &dwTotal,
33                               &ResumeHandle);
34         if ((Status != NERR_Success) && (Status != ERROR_MORE_DATA))
35             return Status;
36 
37         for (i = 0; i < dwRead; i++)
38         {
39             ConPrintf(StdOut, L"%-12s %-31s %s\n", pBuffer[i].shi2_netname, pBuffer[i].shi2_path, pBuffer[i].shi2_remark);
40         }
41 
42         NetApiBufferFree(pBuffer);
43         pBuffer = NULL;
44     }
45     while (Status == ERROR_MORE_DATA);
46 
47     return NERR_Success;
48 }
49 
50 
51 NET_API_STATUS
DisplayShare(PWSTR pShareName)52 DisplayShare(
53     PWSTR pShareName)
54 {
55     PSHARE_INFO_2 pBuffer = NULL;
56     INT nPaddedLength = 22;
57     NET_API_STATUS Status;
58 
59     Status = NetShareGetInfo(NULL,
60                              pShareName,
61                              2,
62                              (LPBYTE*)&pBuffer);
63     if (Status != NERR_Success)
64         return Status;
65 
66     PrintPaddedMessageString(4731, nPaddedLength);
67     ConPrintf(StdOut, L"%s\n", pBuffer->shi2_netname);
68 
69     PrintPaddedMessageString(4339, nPaddedLength);
70     ConPrintf(StdOut, L"%s\n", pBuffer->shi2_path);
71 
72     PrintPaddedMessageString(4334, nPaddedLength);
73     ConPrintf(StdOut, L"%s\n", pBuffer->shi2_remark);
74 
75     PrintPaddedMessageString(4735, nPaddedLength);
76     if (pBuffer->shi2_max_uses == (DWORD)-1)
77         PrintMessageString(4736);
78     else
79         ConPrintf(StdOut, L"%lu", pBuffer->shi2_max_uses);
80     ConPrintf(StdOut, L"\n");
81 
82     PrintPaddedMessageString(4737, nPaddedLength);
83     if (pBuffer->shi2_current_uses > 0)
84         ConPrintf(StdOut, L"%lu", pBuffer->shi2_current_uses);
85     ConPrintf(StdOut, L"\n");
86 
87     NetApiBufferFree(pBuffer);
88 
89     return NERR_Success;
90 }
91 
92 
93 INT
cmdShare(INT argc,WCHAR ** argv)94 cmdShare(
95     INT argc,
96     WCHAR **argv)
97 {
98     SHARE_INFO_2 ShareInfo;
99     PWSTR pszShareName = NULL;
100     PWSTR pszSharePath = NULL;
101     PWSTR ptr;
102     BOOL bDelete = FALSE;
103     INT len;
104     INT i, result = 0;
105     NET_API_STATUS Status;
106 
107     i = 2;
108     if (argc > 2 && argv[i][0] != L'/')
109     {
110         ptr = wcschr(argv[i], L'=');
111         if (ptr != NULL)
112         {
113             if (ptr[1] != UNICODE_NULL)
114             {
115                 len = wcslen(&ptr[i]);
116                 pszSharePath = HeapAlloc(GetProcessHeap(),
117                                          HEAP_ZERO_MEMORY,
118                                          (len + 1) * sizeof(WCHAR));
119                 if (pszSharePath == NULL)
120                 {
121                     // FIXME: Proper error code!
122                     return 1;
123                 }
124 
125                 wcscpy(pszSharePath, &ptr[1]);
126             }
127 
128             len = ((INT_PTR)ptr - (INT_PTR)argv[i]) / sizeof(WCHAR);
129             pszShareName = HeapAlloc(GetProcessHeap(),
130                                      HEAP_ZERO_MEMORY,
131                                      (len + 1) * sizeof(WCHAR));
132             if (pszShareName == NULL)
133             {
134                 // FIXME: Proper error code!
135                 return 1;
136             }
137 
138             wcsncpy(pszShareName, argv[i], len);
139         }
140         else
141         {
142             len = wcslen(argv[i]);
143             pszShareName = HeapAlloc(GetProcessHeap(),
144                                      HEAP_ZERO_MEMORY,
145                                      (len + 1) * sizeof(WCHAR));
146             if (pszShareName == NULL)
147             {
148                 // FIXME: Proper error code!
149                 return 1;
150             }
151 
152             wcscpy(pszShareName, argv[i]);
153         }
154 
155         i++;
156     }
157 
158     for (; i < argc; i++)
159     {
160         if (_wcsicmp(argv[i], L"/help") == 0)
161         {
162             /* Print full help text*/
163             PrintMessageString(4381);
164             ConPuts(StdOut, L"\n");
165             PrintNetMessage(MSG_SHARE_SYNTAX);
166             PrintNetMessage(MSG_SHARE_HELP);
167             return 0;
168         }
169         else if (_wcsicmp(argv[i], L"/delete") == 0)
170         {
171             bDelete = TRUE;
172         }
173     }
174 
175     printf("pszShareName: '%S'\n", pszShareName);
176     printf("pszSharePath: '%S'\n", pszSharePath);
177 
178     if (pszShareName == NULL && pszSharePath == NULL)
179     {
180         Status = EnumerateShares();
181         ConPrintf(StdOut, L"Status: %lu\n", Status);
182     }
183     else if (pszShareName != NULL && pszSharePath == NULL)
184     {
185         if (bDelete == TRUE)
186         {
187             Status = NetShareDel(NULL,
188                                  pszShareName,
189                                  0);
190         }
191         else
192         {
193             Status = DisplayShare(pszShareName);
194         }
195 
196         ConPrintf(StdOut, L"Status: %lu\n", Status);
197     }
198     else if (pszShareName != NULL && pszSharePath != NULL)
199     {
200         ZeroMemory(&ShareInfo, sizeof(SHARE_INFO_2));
201         ShareInfo.shi2_netname = pszShareName;
202         ShareInfo.shi2_path = pszSharePath;
203 
204         Status = NetShareAdd(NULL,
205                              2,
206                              (LPBYTE)&ShareInfo,
207                              NULL);
208 
209         ConPrintf(StdOut, L"Status: %lu\n", Status);
210     }
211 
212     if (pszSharePath != NULL)
213         HeapFree(GetProcessHeap(), 0, pszSharePath);
214 
215     if (pszShareName != NULL)
216         HeapFree(GetProcessHeap(), 0, pszShareName);
217 
218     return result;
219 }
220 
221 /* EOF */
222