cryptdlg: Add Swedish translation.
[wine.git] / programs / net / net.c
blobe5e47c88320d06466c3d0ec7dd1bde94a627d7ce
1 /*
2 * Copyright 2007 Tim Schwartz
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19 #include <stdio.h>
20 #include <string.h>
21 #include <windows.h>
22 #include <lm.h>
23 #include "resources.h"
25 #define NET_START 0001
26 #define NET_STOP 0002
28 static int output_string(int msg, ...)
30 char msg_buffer[8192];
31 va_list arguments;
33 LoadStringA(GetModuleHandleW(NULL), msg, msg_buffer, sizeof(msg_buffer));
34 va_start(arguments, msg);
35 vprintf(msg_buffer, arguments);
36 va_end(arguments);
37 return 0;
40 static BOOL output_error_string(DWORD error)
42 LPSTR pBuffer;
43 if (FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM |
44 FORMAT_MESSAGE_IGNORE_INSERTS | FORMAT_MESSAGE_ALLOCATE_BUFFER,
45 NULL, error, 0, (LPSTR)&pBuffer, 0, NULL))
47 fputs(pBuffer, stdout);
48 LocalFree(pBuffer);
49 return TRUE;
51 return FALSE;
54 static BOOL net_use(int argc, char *argv[])
56 USE_INFO_2 *buffer, *connection;
57 DWORD read, total, resume_handle, rc, i;
58 const char *status_description[] = { "OK", "Paused", "Disconnected", "An error occurred",
59 "A network error occurred", "Connection is being made",
60 "Reconnecting" };
61 resume_handle = 0;
62 buffer = NULL;
64 if(argc<3)
66 do {
67 rc = NetUseEnum(NULL, 2, (BYTE **) &buffer, 2048, &read, &total, &resume_handle);
68 if (rc != ERROR_MORE_DATA && rc != ERROR_SUCCESS)
70 break;
73 if(total == 0)
75 output_string(STRING_NO_ENTRIES);
76 break;
79 output_string(STRING_USE_HEADER);
80 for (i = 0, connection = buffer; i < read; ++i, ++connection)
81 output_string(STRING_USE_ENTRY, status_description[connection->ui2_status], connection->ui2_local,
82 connection->ui2_remote, connection->ui2_refcount);
84 if (buffer != NULL) NetApiBufferFree(buffer);
85 } while (rc == ERROR_MORE_DATA);
87 return TRUE;
90 return FALSE;
93 static BOOL StopService(SC_HANDLE SCManager, SC_HANDLE serviceHandle)
95 LPENUM_SERVICE_STATUSW dependencies = NULL;
96 DWORD buffer_size = 0;
97 DWORD count = 0, counter;
98 BOOL result;
99 SC_HANDLE dependent_serviceHandle;
100 SERVICE_STATUS_PROCESS ssp;
102 result = EnumDependentServicesW(serviceHandle, SERVICE_ACTIVE, dependencies, buffer_size, &buffer_size, &count);
104 if(!result && (GetLastError() == ERROR_MORE_DATA))
106 dependencies = HeapAlloc(GetProcessHeap(), 0, buffer_size);
107 if(EnumDependentServicesW(serviceHandle, SERVICE_ACTIVE, dependencies, buffer_size, &buffer_size, &count))
109 for(counter = 0; counter < count; counter++)
111 output_string(STRING_STOP_DEP, dependencies[counter].lpDisplayName);
112 dependent_serviceHandle = OpenServiceW(SCManager, dependencies[counter].lpServiceName, SC_MANAGER_ALL_ACCESS);
113 if(dependent_serviceHandle) result = StopService(SCManager, dependent_serviceHandle);
114 CloseServiceHandle(dependent_serviceHandle);
115 if(!result) output_string(STRING_CANT_STOP, dependencies[counter].lpDisplayName);
120 if(result) result = ControlService(serviceHandle, SERVICE_CONTROL_STOP, (LPSERVICE_STATUS)&ssp);
121 HeapFree(GetProcessHeap(), 0, dependencies);
122 return result;
125 static BOOL net_service(int operation, char *service_name)
127 SC_HANDLE SCManager, serviceHandle;
128 BOOL result = 0;
129 char service_display_name[4096];
130 DWORD buffer_size = sizeof(service_display_name);
132 SCManager = OpenSCManagerW(NULL, NULL, SC_MANAGER_ALL_ACCESS);
133 if(!SCManager)
135 output_string(STRING_NO_SCM);
136 return FALSE;
138 serviceHandle = OpenServiceA(SCManager, service_name, SC_MANAGER_ALL_ACCESS);
139 if(!serviceHandle)
141 output_string(STRING_NO_SVCHANDLE);
142 CloseServiceHandle(SCManager);
143 return FALSE;
147 GetServiceDisplayNameA(SCManager, service_name, service_display_name, &buffer_size);
148 if (!service_display_name[0]) strcpy(service_display_name, service_name);
150 switch(operation)
152 case NET_START:
153 output_string(STRING_START_SVC, service_display_name);
154 result = StartServiceW(serviceHandle, 0, NULL);
156 if(result) output_string(STRING_START_SVC_SUCCESS, service_display_name);
157 else
159 if (!output_error_string(GetLastError()))
160 output_string(STRING_START_SVC_FAIL, service_display_name);
162 break;
163 case NET_STOP:
164 output_string(STRING_STOP_SVC, service_display_name);
165 result = StopService(SCManager, serviceHandle);
167 if(result) output_string(STRING_STOP_SVC_SUCCESS, service_display_name);
168 else
170 if (!output_error_string(GetLastError()))
171 output_string(STRING_STOP_SVC_FAIL, service_display_name);
173 break;
176 CloseServiceHandle(serviceHandle);
177 CloseServiceHandle(SCManager);
178 return result;
181 int main(int argc, char *argv[])
183 if (argc < 2)
185 output_string(STRING_USAGE);
186 return 1;
189 if(!strcasecmp(argv[1], "help"))
191 output_string(STRING_HELP_USAGE);
194 if(!strcasecmp(argv[1], "start"))
196 if(argc < 3)
198 output_string(STRING_START_USAGE);
199 return 1;
202 if(!net_service(NET_START, argv[2]))
204 return 1;
206 return 0;
209 if(!strcasecmp(argv[1], "stop"))
211 if(argc < 3)
213 output_string(STRING_STOP_USAGE);
214 return 1;
217 if(!net_service(NET_STOP, argv[2]))
219 return 1;
221 return 0;
224 if(!strcasecmp(argv[1], "use"))
226 if(!net_use(argc, argv)) return 1;
229 return 0;