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