1 /* mswindows.c -- Windows-specific support
2 Copyright (C) 1995, 1996, 1997, 1998 Free Software Foundation, Inc.
4 This file is part of GNU Wget.
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.
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.
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.
20 In addition, as a special exception, the Free Software Foundation
21 gives permission to link the code of its release of Wget with the
22 OpenSSL project's "OpenSSL" library (or with modified versions of it
23 that use the same license as the "OpenSSL" library), and distribute
24 the linked executables. You must obey the GNU General Public License
25 in all respects for all of the code used other than "OpenSSL". If you
26 modify this file, you may extend this exception to your version of the
27 file, but you are not obligated to do so. If you do not wish to do
28 so, delete this exception statement from your version. */
30 /* #### Someone please document what these functions do! */
42 #ifdef HACK_BCC_UTIME_BUG
48 # ifdef HAVE_SYS_UTIME_H
49 # include <sys/utime.h>
61 #ifndef ES_SYSTEM_REQUIRED
62 #define ES_SYSTEM_REQUIRED 0x00000001
66 #define ES_CONTINUOUS 0x80000000
70 /* Defined in log.c. */
71 void log_request_redirect_output PARAMS ((const char *));
73 static DWORD set_sleep_mode (DWORD mode);
75 static DWORD pwr_mode = 0;
76 static int windows_nt_p;
80 /* Emulation of Unix sleep. */
83 sleep (unsigned seconds)
85 return SleepEx (1000 * seconds, TRUE) ? 0U : 1000 * seconds;
90 /* Emulation of Unix usleep(). This has a granularity of
91 milliseconds, but that's ok because:
93 a) Wget is only using it with milliseconds [not anymore, but b)
96 b) You can't rely on usleep's granularity anyway. If a caller
97 expects usleep to respect every microsecond, he's in for a
101 usleep (unsigned long usec)
103 SleepEx (usec / 1000, TRUE);
106 #endif /* HAVE_USLEEP */
109 windows_main_junk (int *argc, char **argv, char **exec_name)
113 /* Remove .EXE from filename if it has one. */
114 *exec_name = xstrdup (*exec_name);
115 p = strrchr (*exec_name, '.');
127 set_sleep_mode (pwr_mode);
134 log_request_redirect_output ("CTRL+Break");
138 fork_to_background (void)
140 /* Whether we arrange our own version of opt.lfilename here. */
145 opt.lfilename = unique_name (DEFAULT_LOGFILE, 0);
148 printf (_("Continuing in background.\n"));
150 printf (_("Output will be written to `%s'.\n"), opt.lfilename);
158 ws_handler (DWORD dwEvent)
165 #ifdef CTRLBREAK_BACKGND
166 case CTRL_BREAK_EVENT:
168 fork_to_background ();
170 case CTRL_SHUTDOWN_EVENT:
171 case CTRL_CLOSE_EVENT:
172 case CTRL_LOGOFF_EVENT:
180 static char *title_buf = NULL;
181 static char *curr_url = NULL;
182 static int num_urls = 0;
185 ws_changetitle (const char *url, int nurl)
195 title_buf = (char *)xmalloc (strlen (url) + 20);
196 curr_url = xstrdup(url);
197 sprintf(title_buf, "Wget %s%s", url, nurl == 1 ? "" : " ...");
198 SetConsoleTitle(title_buf);
202 ws_percenttitle (double percent)
204 if (num_urls == 1 && title_buf && curr_url && fabs(percent) <= 100.0)
206 sprintf (title_buf, "Wget [%.1f%%] %s", percent, curr_url);
207 SetConsoleTitle (title_buf);
214 static char *wspathsave = NULL;
215 char buffer[MAX_PATH];
223 if (GetModuleFileName (NULL, buffer, MAX_PATH) &&
224 (ptr = strrchr (buffer, PATH_SEPARATOR)) != NULL)
227 wspathsave = xstrdup (buffer);
235 ws_help (const char *name)
237 char *mypath = ws_mypath ();
242 char *buf = (char *)alloca (strlen (mypath) + strlen (name) + 4 + 1);
243 sprintf (buf, "%s%s.HLP", mypath, name);
244 if (stat (buf, &sbuf) == 0)
246 printf (_("Starting WinHelp %s\n"), buf);
247 WinHelp (NULL, buf, HELP_INDEX, NULL);
251 printf ("%s: %s\n", buf, strerror (errno));
264 if (GetVersionEx (&os) == TRUE
265 && os.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS)
268 requested = MAKEWORD (1, 1);
269 err = WSAStartup (requested, &data);
273 fprintf (stderr, _("%s: Couldn't find usable socket driver.\n"),
278 if (data.wVersion < requested)
280 fprintf (stderr, _("%s: Couldn't find usable socket driver.\n"),
286 pwr_mode = set_sleep_mode (0);
287 SetConsoleCtrlHandler (ws_handler, TRUE);
290 /* Replacement utime function for buggy Borland C++Builder 5.5 compiler.
291 (The Borland utime function only works on Windows NT.) */
293 #ifdef HACK_BCC_UTIME_BUG
294 int borland_utime(const char *path, const struct utimbuf *times)
301 if ((fd = open (path, O_RDWR)) < 0)
304 ptr_tm = localtime (×->modtime);
305 ft.ft_tsec = ptr_tm->tm_sec >> 1;
306 ft.ft_min = ptr_tm->tm_min;
307 ft.ft_hour = ptr_tm->tm_hour;
308 ft.ft_day = ptr_tm->tm_mday;
309 ft.ft_month = ptr_tm->tm_mon + 1;
310 ft.ft_year = ptr_tm->tm_year - 80;
311 res = setftime (fd, &ft);
318 * Prevent Windows entering sleep/hibernation-mode while wget is doing a lengthy transfer.
319 * Windows does by default not consider network activity in console-programs as activity !
320 * Works on Win-98/ME/2K and up.
323 DWORD set_sleep_mode (DWORD mode)
325 HMODULE mod = LoadLibrary ("kernel32.dll");
326 DWORD (*_SetThreadExecutionState) (DWORD) = NULL;
327 DWORD rc = (DWORD)-1;
330 (void*)_SetThreadExecutionState = GetProcAddress ((HINSTANCE)mod, "SetThreadExecutionState");
332 if (_SetThreadExecutionState)
334 if (mode == 0) /* first time */
335 mode = (ES_SYSTEM_REQUIRED | ES_CONTINUOUS);
336 rc = (*_SetThreadExecutionState) (mode);
340 DEBUGP (("set_sleep_mode(): mode 0x%08lX, rc 0x%08lX\n", mode, rc));