| 1 | /* Utility and Unix shadow routines for GNU Emacs support programs on NT. |
| 2 | Copyright (C) 1994, 2001, 2002, 2003, 2004, 2005, 2006, 2007, |
| 3 | 2008 Free Software Foundation, Inc. |
| 4 | |
| 5 | This file is part of GNU Emacs. |
| 6 | |
| 7 | GNU Emacs is free software: you can redistribute it and/or modify |
| 8 | it under the terms of the GNU General Public License as published by |
| 9 | the Free Software Foundation, either version 3 of the License, or |
| 10 | (at your option) any later version. |
| 11 | |
| 12 | GNU Emacs is distributed in the hope that it will be useful, |
| 13 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | GNU General Public License for more details. |
| 16 | |
| 17 | You should have received a copy of the GNU General Public License |
| 18 | along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. |
| 19 | |
| 20 | |
| 21 | Geoff Voelker (voelker@cs.washington.edu) 10-8-94 |
| 22 | */ |
| 23 | |
| 24 | #include <windows.h> |
| 25 | #include <stdlib.h> |
| 26 | #include <stdio.h> |
| 27 | #include <time.h> |
| 28 | #include <direct.h> |
| 29 | #include <sys/types.h> |
| 30 | #include <sys/stat.h> |
| 31 | #include <errno.h> |
| 32 | |
| 33 | #include "ntlib.h" |
| 34 | |
| 35 | #define MAXPATHLEN _MAX_PATH |
| 36 | |
| 37 | /* Emulate sleep...we could have done this with a define, but that |
| 38 | would necessitate including windows.h in the files that used it. |
| 39 | This is much easier. */ |
| 40 | void |
| 41 | sleep(unsigned long seconds) |
| 42 | { |
| 43 | Sleep (seconds * 1000); |
| 44 | } |
| 45 | |
| 46 | /* Get the current working directory. */ |
| 47 | char * |
| 48 | getwd (char *dir) |
| 49 | { |
| 50 | if (GetCurrentDirectory (MAXPATHLEN, dir) > 0) |
| 51 | return dir; |
| 52 | return NULL; |
| 53 | } |
| 54 | |
| 55 | static HANDLE getppid_parent; |
| 56 | static int getppid_ppid; |
| 57 | |
| 58 | int |
| 59 | getppid(void) |
| 60 | { |
| 61 | char *ppid; |
| 62 | DWORD result; |
| 63 | |
| 64 | ppid = getenv ("EM_PARENT_PROCESS_ID"); |
| 65 | if (!ppid) |
| 66 | { |
| 67 | printf("no pid.\n"); |
| 68 | return 0; |
| 69 | } |
| 70 | else |
| 71 | { |
| 72 | getppid_ppid = atoi (ppid); |
| 73 | } |
| 74 | |
| 75 | if (!getppid_parent) |
| 76 | { |
| 77 | getppid_parent = OpenProcess (SYNCHRONIZE, FALSE, atoi(ppid)); |
| 78 | if (!getppid_parent) |
| 79 | { |
| 80 | printf ("Failed to open handle to parent process: %d\n", |
| 81 | GetLastError()); |
| 82 | exit (1); |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | result = WaitForSingleObject (getppid_parent, 0); |
| 87 | switch (result) |
| 88 | { |
| 89 | case WAIT_TIMEOUT: |
| 90 | /* The parent is still alive. */ |
| 91 | return getppid_ppid; |
| 92 | case WAIT_OBJECT_0: |
| 93 | /* The parent is gone. Return the pid of Unix init (1). */ |
| 94 | return 1; |
| 95 | case WAIT_FAILED: |
| 96 | default: |
| 97 | printf ("Checking parent status failed: %d\n", GetLastError()); |
| 98 | exit (1); |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | char * |
| 103 | getlogin () |
| 104 | { |
| 105 | static char user_name[256]; |
| 106 | DWORD length = sizeof (user_name); |
| 107 | |
| 108 | if (GetUserName (user_name, &length)) |
| 109 | return user_name; |
| 110 | return NULL; |
| 111 | } |
| 112 | |
| 113 | char * |
| 114 | cuserid (char * s) |
| 115 | { |
| 116 | char * name = getlogin (); |
| 117 | if (s) |
| 118 | return strcpy (s, name ? name : ""); |
| 119 | return name; |
| 120 | } |
| 121 | |
| 122 | int |
| 123 | getuid () |
| 124 | { |
| 125 | return 0; |
| 126 | } |
| 127 | |
| 128 | int |
| 129 | setuid (int uid) |
| 130 | { |
| 131 | return 0; |
| 132 | } |
| 133 | |
| 134 | struct passwd * |
| 135 | getpwuid (int uid) |
| 136 | { |
| 137 | return NULL; |
| 138 | } |
| 139 | |
| 140 | char * |
| 141 | getpass (const char * prompt) |
| 142 | { |
| 143 | static char input[256]; |
| 144 | HANDLE in; |
| 145 | HANDLE err; |
| 146 | DWORD count; |
| 147 | |
| 148 | in = GetStdHandle (STD_INPUT_HANDLE); |
| 149 | err = GetStdHandle (STD_ERROR_HANDLE); |
| 150 | |
| 151 | if (in == INVALID_HANDLE_VALUE || err == INVALID_HANDLE_VALUE) |
| 152 | return NULL; |
| 153 | |
| 154 | if (WriteFile (err, prompt, strlen (prompt), &count, NULL)) |
| 155 | { |
| 156 | int istty = (GetFileType (in) == FILE_TYPE_CHAR); |
| 157 | DWORD old_flags; |
| 158 | int rc; |
| 159 | |
| 160 | if (istty) |
| 161 | { |
| 162 | if (GetConsoleMode (in, &old_flags)) |
| 163 | SetConsoleMode (in, ENABLE_LINE_INPUT | ENABLE_PROCESSED_INPUT); |
| 164 | else |
| 165 | istty = 0; |
| 166 | } |
| 167 | rc = ReadFile (in, input, sizeof (input), &count, NULL); |
| 168 | if (count >= 2 && input[count - 2] == '\r') |
| 169 | input[count - 2] = '\0'; |
| 170 | else |
| 171 | { |
| 172 | char buf[256]; |
| 173 | while (ReadFile (in, buf, sizeof (buf), &count, NULL) > 0) |
| 174 | if (count >= 2 && buf[count - 2] == '\r') |
| 175 | break; |
| 176 | } |
| 177 | WriteFile (err, "\r\n", 2, &count, NULL); |
| 178 | if (istty) |
| 179 | SetConsoleMode (in, old_flags); |
| 180 | if (rc) |
| 181 | return input; |
| 182 | } |
| 183 | |
| 184 | return NULL; |
| 185 | } |
| 186 | |
| 187 | int |
| 188 | fchown (int fd, int uid, int gid) |
| 189 | { |
| 190 | return 0; |
| 191 | } |
| 192 | |
| 193 | /* Place a wrapper around the MSVC version of ctime. It returns NULL |
| 194 | on network directories, so we handle that case here. |
| 195 | (Ulrich Leodolter, 1/11/95). */ |
| 196 | char * |
| 197 | sys_ctime (const time_t *t) |
| 198 | { |
| 199 | char *str = (char *) ctime (t); |
| 200 | return (str ? str : "Sun Jan 01 00:00:00 1970"); |
| 201 | } |
| 202 | |
| 203 | FILE * |
| 204 | sys_fopen(const char * path, const char * mode) |
| 205 | { |
| 206 | return fopen (path, mode); |
| 207 | } |
| 208 | |
| 209 | int |
| 210 | sys_chdir (const char * path) |
| 211 | { |
| 212 | return _chdir (path); |
| 213 | } |
| 214 | |
| 215 | static FILETIME utc_base_ft; |
| 216 | static long double utc_base; |
| 217 | static int init = 0; |
| 218 | |
| 219 | static time_t |
| 220 | convert_time (FILETIME ft) |
| 221 | { |
| 222 | long double ret; |
| 223 | |
| 224 | if (CompareFileTime (&ft, &utc_base_ft) < 0) |
| 225 | return 0; |
| 226 | |
| 227 | ret = (long double) ft.dwHighDateTime |
| 228 | * 4096.0L * 1024.0L * 1024.0L + ft.dwLowDateTime; |
| 229 | ret -= utc_base; |
| 230 | return (time_t) (ret * 1e-7L); |
| 231 | } |
| 232 | |
| 233 | static int |
| 234 | is_exec (const char * name) |
| 235 | { |
| 236 | char * p = strrchr (name, '.'); |
| 237 | return |
| 238 | (p != NULL |
| 239 | && (stricmp (p, ".exe") == 0 || |
| 240 | stricmp (p, ".com") == 0 || |
| 241 | stricmp (p, ".bat") == 0 || |
| 242 | stricmp (p, ".cmd") == 0)); |
| 243 | } |
| 244 | |
| 245 | #define IS_DIRECTORY_SEP(x) ((x) == '/' || (x) == '\\') |
| 246 | |
| 247 | /* We need this because nt/inc/sys/stat.h defines struct stat that is |
| 248 | incompatible with the MS run-time libraries. */ |
| 249 | int |
| 250 | stat (const char * path, struct stat * buf) |
| 251 | { |
| 252 | WIN32_FIND_DATA wfd; |
| 253 | HANDLE fh; |
| 254 | int permission; |
| 255 | int len; |
| 256 | int rootdir = FALSE; |
| 257 | char *name = alloca (FILENAME_MAX); |
| 258 | |
| 259 | if (!init) |
| 260 | { |
| 261 | /* Determine the delta between 1-Jan-1601 and 1-Jan-1970. */ |
| 262 | SYSTEMTIME st; |
| 263 | |
| 264 | st.wYear = 1970; |
| 265 | st.wMonth = 1; |
| 266 | st.wDay = 1; |
| 267 | st.wHour = 0; |
| 268 | st.wMinute = 0; |
| 269 | st.wSecond = 0; |
| 270 | st.wMilliseconds = 0; |
| 271 | |
| 272 | SystemTimeToFileTime (&st, &utc_base_ft); |
| 273 | utc_base = (long double) utc_base_ft.dwHighDateTime |
| 274 | * 4096.0L * 1024.0L * 1024.0L + utc_base_ft.dwLowDateTime; |
| 275 | init = 1; |
| 276 | } |
| 277 | |
| 278 | if (path == NULL || buf == NULL || *path == '\0') |
| 279 | { |
| 280 | errno = EFAULT; |
| 281 | return -1; |
| 282 | } |
| 283 | if (_mbspbrk (path, "*?|<>\"")) |
| 284 | { |
| 285 | errno = ENOENT; |
| 286 | return -1; |
| 287 | } |
| 288 | |
| 289 | strcpy (name, path); |
| 290 | /* Remove trailing directory separator, unless name is the root |
| 291 | directory of a drive in which case ensure there is a trailing |
| 292 | separator. */ |
| 293 | len = strlen (name); |
| 294 | rootdir = IS_DIRECTORY_SEP (name[0]) |
| 295 | || (len == 3 && name[1] == ':' && IS_DIRECTORY_SEP (name[2])); |
| 296 | if (rootdir) |
| 297 | { |
| 298 | if (GetDriveType (name) < 2) |
| 299 | { |
| 300 | errno = ENOENT; |
| 301 | return -1; |
| 302 | } |
| 303 | memset (&wfd, 0, sizeof (wfd)); |
| 304 | wfd.dwFileAttributes = FILE_ATTRIBUTE_DIRECTORY; |
| 305 | wfd.ftCreationTime = utc_base_ft; |
| 306 | wfd.ftLastAccessTime = utc_base_ft; |
| 307 | wfd.ftLastWriteTime = utc_base_ft; |
| 308 | strcpy (wfd.cFileName, name); |
| 309 | } |
| 310 | else |
| 311 | { |
| 312 | if (IS_DIRECTORY_SEP (name[len-1])) |
| 313 | name[len - 1] = 0; |
| 314 | |
| 315 | fh = FindFirstFile (name, &wfd); |
| 316 | if (fh == INVALID_HANDLE_VALUE) |
| 317 | { |
| 318 | errno = ENOENT; |
| 319 | return -1; |
| 320 | } |
| 321 | FindClose (fh); |
| 322 | } |
| 323 | buf->st_mode = (wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) ? |
| 324 | S_IFDIR : S_IFREG; |
| 325 | buf->st_nlink = 1; |
| 326 | buf->st_ino = 0; |
| 327 | |
| 328 | if (name[0] && name[1] == ':') |
| 329 | buf->st_dev = tolower (name[0]) - 'a' + 1; |
| 330 | else |
| 331 | buf->st_dev = _getdrive (); |
| 332 | buf->st_rdev = buf->st_dev; |
| 333 | |
| 334 | buf->st_size = wfd.nFileSizeLow; |
| 335 | |
| 336 | /* Convert timestamps to Unix format. */ |
| 337 | buf->st_mtime = convert_time (wfd.ftLastWriteTime); |
| 338 | buf->st_atime = convert_time (wfd.ftLastAccessTime); |
| 339 | if (buf->st_atime == 0) buf->st_atime = buf->st_mtime; |
| 340 | buf->st_ctime = convert_time (wfd.ftCreationTime); |
| 341 | if (buf->st_ctime == 0) buf->st_ctime = buf->st_mtime; |
| 342 | |
| 343 | /* determine rwx permissions */ |
| 344 | if (wfd.dwFileAttributes & FILE_ATTRIBUTE_READONLY) |
| 345 | permission = S_IREAD; |
| 346 | else |
| 347 | permission = S_IREAD | S_IWRITE; |
| 348 | |
| 349 | if (wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) |
| 350 | permission |= S_IEXEC; |
| 351 | else if (is_exec (name)) |
| 352 | permission |= S_IEXEC; |
| 353 | |
| 354 | buf->st_mode |= permission | (permission >> 3) | (permission >> 6); |
| 355 | |
| 356 | return 0; |
| 357 | } |
| 358 | |
| 359 | /* arch-tag: 7b63fb83-70ee-4124-8822-54e53e5d0773 |
| 360 | (do not change this comment) */ |