]> sjero.net Git - wget/blob - src/mswindows.c
[svn] Windows fixes by Herold Heiko.
[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 GNU Wget.
5
6 GNU Wget 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 GNU Wget 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 Wget; 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
46 unsigned int
47 sleep (unsigned seconds)
48 {
49   return SleepEx (1000 * seconds, TRUE) ? 0U : 1000 * seconds;
50 }
51
52 /* Emulation of Unix usleep().  This has a granularity of
53    milliseconds, but that's ok because:
54
55    a) Wget is only using it with milliseconds;
56
57    b) You can't rely on usleep's granularity anyway.  If a caller
58    expects usleep to respect every microsecond, he's in for a
59    surprise.  */
60
61 int
62 usleep (unsigned long usec)
63 {
64   SleepEx (usec / 1000, TRUE);
65   return 0;
66 }
67
68 static char *
69 read_registry (HKEY hkey, char *subkey, char *valuename, char *buf, int *len)
70 {
71   HKEY result;
72   DWORD size = *len;
73   DWORD type = REG_SZ;
74   if (RegOpenKeyEx (hkey, subkey, NULL, KEY_READ, &result) != ERROR_SUCCESS)
75     return NULL;
76   if (RegQueryValueEx (result, valuename, NULL, &type, buf, &size) != ERROR_SUCCESS)
77     buf = NULL;
78   *len = size;
79   RegCloseKey (result);
80   return buf;
81 }
82
83 void
84 windows_main_junk (int *argc, char **argv, char **exec_name)
85 {
86   char *p;
87
88   /* Remove .EXE from filename if it has one.  */
89   *exec_name = xstrdup (*exec_name);
90   p = strrchr (*exec_name, '.');
91   if (p)
92     *p = '\0';
93 }
94 \f
95 /* Winsock stuff. */
96
97 static void
98 ws_cleanup (void)
99 {
100   WSACleanup ();
101 }
102
103 static void
104 ws_hangup (void)
105 {
106   redirect_output (_("\n\
107 CTRL+Break received, redirecting output to `%s'.\n\
108 Execution continued in background.\n\
109 You may stop Wget by pressing CTRL+ALT+DELETE.\n"));
110 }
111
112 void
113 fork_to_background (void)
114 {
115   /* Whether we arrange our own version of opt.lfilename here.  */
116   int changedp = 0;
117
118   if (!opt.lfilename)
119     {
120       opt.lfilename = unique_name (DEFAULT_LOGFILE);
121       changedp = 1;
122     }
123   printf (_("Continuing in background.\n"));
124   if (changedp)
125     printf (_("Output will be written to `%s'.\n"), opt.lfilename);
126
127   ws_hangup ();
128   if (!windows_nt_p)
129     FreeConsole ();
130 }
131
132 static BOOL WINAPI
133 ws_handler (DWORD dwEvent)
134 {
135   switch (dwEvent)
136     {
137 #ifdef CTRLC_BACKGND
138     case CTRL_C_EVENT:
139 #endif
140 #ifdef CTRLBREAK_BACKGND
141     case CTRL_BREAK_EVENT:
142 #endif
143       fork_to_background ();
144       break;
145     case CTRL_SHUTDOWN_EVENT:
146     case CTRL_CLOSE_EVENT:
147     case CTRL_LOGOFF_EVENT:
148     default:
149       WSACleanup ();
150       return FALSE;
151     }
152   return TRUE;
153 }
154
155 void
156 ws_changetitle (char *url, int nurl)
157 {
158   char *title_buf;
159   if (!nurl)
160     return;
161
162   title_buf = (char *)alloca (strlen (url) + 20);
163   sprintf (title_buf, "Wget %s%s", url, nurl == 1 ? "" : " ...");
164   SetConsoleTitle (title_buf);
165 }
166
167 char *
168 ws_mypath (void)
169 {
170   static char *wspathsave;
171   char buffer[MAX_PATH];
172   char *ptr;
173
174   if (wspathsave)
175     {
176       return wspathsave;
177     }
178
179   GetModuleFileName (NULL, buffer, MAX_PATH);
180
181   ptr = strrchr (buffer, '\\');
182   if (ptr)
183     {
184       *(ptr + 1) = '\0';
185       wspathsave = (char*) xmalloc (strlen (buffer) + 1);
186       strcpy (wspathsave, buffer);
187     }
188   else
189     wspathsave = NULL;
190   return wspathsave;
191 }
192
193 void
194 ws_help (const char *name)
195 {
196   char *mypath = ws_mypath ();
197
198   if (mypath)
199     {
200       struct stat sbuf;
201       char *buf = (char *)alloca (strlen (mypath) + strlen (name) + 4 + 1);
202       sprintf (buf, "%s%s.HLP", mypath, name);
203       if (stat (buf, &sbuf) == 0) 
204         {
205           printf (_("Starting WinHelp %s\n"), buf);
206           WinHelp (NULL, buf, HELP_INDEX, NULL);
207         }
208       else
209         {
210           printf ("%s: %s\n", buf, strerror (errno));
211         }
212     }
213 }
214
215 void
216 ws_startup (void)
217 {
218   WORD requested;
219   WSADATA data;
220   int err;
221   OSVERSIONINFO os;
222
223   if (GetVersionEx (&os) == TRUE
224       && os.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS)
225     windows_nt_p = 1;
226
227   requested = MAKEWORD (1, 1);
228   err = WSAStartup (requested, &data);
229
230   if (err != 0)
231     {
232       fprintf (stderr, _("%s: Couldn't find usable socket driver.\n"),
233                exec_name);
234       exit (1);
235     }
236
237   if (data.wVersion < requested)
238     {
239       fprintf (stderr, _("%s: Couldn't find usable socket driver.\n"),
240                exec_name);
241       WSACleanup ();
242       exit (1);
243     }
244   atexit (ws_cleanup);
245   SetConsoleCtrlHandler (ws_handler, TRUE);
246 }