]> sjero.net Git - wget/blob - src/mswindows.c
[svn] Small cleanup of mswindows.c by Paul Bludov.
[wget] / src / mswindows.c
1 /* mswindows.c -- Windows-specific support
2    Copyright (C) 1995, 1996, 1997, 1998  Free Software Foundation, Inc.
3
4 This file is part of Wget.
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
19
20 /* #### Someone please document what these functions do!  */
21
22 #include <config.h>
23
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <winsock.h>
27 #include <string.h>
28 #include <assert.h>
29 #include <errno.h>
30
31 #include "wget.h"
32 #include "url.h"
33
34 #ifndef errno
35 extern int errno;
36 #endif
37
38 /* Defined in log.c.  */
39 void redirect_output (const char *);
40
41 static int windows_nt_p;
42
43
44 /* Emulation of Unix sleep.  */
45 unsigned int
46 sleep (unsigned seconds)
47 {
48   return SleepEx (1000 * seconds, TRUE) ? 0U : 1000 * seconds;
49 }
50
51 static char *
52 read_registry (HKEY hkey, char *subkey, char *valuename, char *buf, int *len)
53 {
54   HKEY result;
55   DWORD size = *len;
56   DWORD type = REG_SZ;
57   if (RegOpenKeyEx (hkey, subkey, NULL, KEY_READ, &result) != ERROR_SUCCESS)
58     return NULL;
59   if (RegQueryValueEx (result, valuename, NULL, &type, buf, &size) != ERROR_SUCCESS)
60     buf = NULL;
61   *len = size;
62   RegCloseKey (result);
63   return buf;
64 }
65
66 char *
67 pwd_cuserid (char *where)
68 {
69   char buf[32], *ptr;
70   int len = sizeof (buf);
71   if (GetUserName (buf, (LPDWORD) &len) == TRUE)
72     {
73       ;
74     }
75   else if (!!(ptr = getenv ("USERNAME")))
76     {
77       strcpy (buf, ptr);
78     }
79   else if (!read_registry (HKEY_LOCAL_MACHINE, "Network\\Logon",
80                           "username", buf, &len))
81     {
82       return NULL;
83     }
84   if (where)
85     {
86       strncpy (where, buf, len);
87       return where;
88     }
89   return xstrdup (buf);
90 }
91
92 void
93 windows_main_junk (int *argc, char **argv, char **exec_name)
94 {
95   char *p;
96
97   /* Remove .EXE from filename if it has one.  */
98   *exec_name = xstrdup (*exec_name);
99   p = strrchr (*exec_name, '.');
100   if (p)
101     *p = '\0';
102 }
103 \f
104 /* Winsock stuff. */
105
106 static void
107 ws_cleanup (void)
108 {
109   WSACleanup ();
110 }
111
112 static void
113 ws_hangup (void)
114 {
115   redirect_output (_("\n\
116 CTRL+Break received, redirecting output to `%s'.\n\
117 Execution continued in background.\n\
118 You may stop Wget by pressing CTRL+ALT+DELETE.\n"));
119 }
120
121 void
122 fork_to_background (void)
123 {
124   /* Whether we arrange our own version of opt.lfilename here.  */
125   int changedp = 0;
126
127   if (!opt.lfilename)
128     {
129       opt.lfilename = unique_name (DEFAULT_LOGFILE);
130       changedp = 1;
131     }
132   printf (_("Continuing in background.\n"));
133   if (changedp)
134     printf (_("Output will be written to `%s'.\n"), opt.lfilename);
135
136   ws_hangup ();
137   if (!windows_nt_p)
138     FreeConsole ();
139 }
140
141 static BOOL WINAPI
142 ws_handler (DWORD dwEvent)
143 {
144   switch (dwEvent)
145     {
146 #ifdef CTRLC_BACKGND
147     case CTRL_C_EVENT:
148 #endif
149 #ifdef CTRLBREAK_BACKGND
150     case CTRL_BREAK_EVENT:
151 #endif
152       fork_to_background ();
153       break;
154     case CTRL_SHUTDOWN_EVENT:
155     case CTRL_CLOSE_EVENT:
156     case CTRL_LOGOFF_EVENT:
157     default:
158       WSACleanup ();
159       return FALSE;
160     }
161   return TRUE;
162 }
163
164 void
165 ws_changetitle (char *url, int nurl)
166 {
167   char *title_buf;
168   if (!nurl)
169     return;
170
171   title_buf = (char *)alloca (strlen (url) + 20);
172   sprintf (title_buf, "Wget %s%s", url, nurl == 1 ? "" : " ...");
173   SetConsoleTitle (title_buf);
174 }
175
176 char *
177 ws_mypath (void)
178 {
179   static char *wspathsave;
180   char buffer[MAX_PATH];
181   char *ptr;
182
183   if (wspathsave)
184     {
185       return wspathsave;
186     }
187
188   GetModuleFileName (NULL, buffer, MAX_PATH);
189
190   ptr = strrchr (buffer, '\\');
191   if (ptr)
192     {
193       *(ptr + 1) = '\0';
194       wspathsave = (char*) xmalloc (strlen (buffer) + 1);
195       strcpy (wspathsave, buffer);
196     }
197   else
198     wspathsave = NULL;
199   return wspathsave;
200 }
201
202 void
203 ws_help (const char *name)
204 {
205   char *mypath = ws_mypath ();
206
207   if (mypath)
208     {
209       struct stat sbuf;
210       char *buf = (char *)alloca (strlen (mypath) + strlen (name) + 4 + 1);
211       sprintf (buf, "%s%s.HLP", mypath, name);
212       if (stat (buf, &sbuf) == 0) 
213         {
214           printf (_("Starting WinHelp %s\n"), buf);
215           WinHelp (NULL, buf, HELP_INDEX, NULL);
216         }
217       else
218         {
219           printf ("%s: %s\n", buf, strerror (errno));
220         }
221     }
222 }
223
224 void
225 ws_startup (void)
226 {
227   WORD requested;
228   WSADATA data;
229   int err;
230   OSVERSIONINFO os;
231
232   if (GetVersionEx (&os) == TRUE
233       && os.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS)
234     windows_nt_p = 1;
235
236   requested = MAKEWORD (1, 1);
237   err = WSAStartup (requested, &data);
238
239   if (err != 0)
240     {
241       fprintf (stderr, _("%s: Couldn't find usable socket driver.\n"),
242                exec_name);
243       exit (1);
244     }
245
246   if (data.wVersion < requested)
247     {
248       fprintf (stderr, _("%s: Couldn't find usable socket driver.\n"),
249                exec_name);
250       WSACleanup ();
251       exit (1);
252     }
253   atexit (ws_cleanup);
254   SetConsoleCtrlHandler (ws_handler, TRUE);
255 }