| 1 | /* Utility and Unix shadow routines for GNU Emacs support programs on NT. |
| 2 | Copyright (C) 1994 Free Software Foundation, Inc. |
| 3 | |
| 4 | This file is part of GNU Emacs. |
| 5 | |
| 6 | GNU Emacs 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, or (at your option) |
| 9 | any later version. |
| 10 | |
| 11 | GNU Emacs 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 GNU Emacs; see the file COPYING. If not, write to |
| 18 | the Free Software Foundation, Inc., 59 Temple Place - Suite 330, |
| 19 | Boston, MA 02111-1307, USA. |
| 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 | |
| 30 | #include "ntlib.h" |
| 31 | |
| 32 | #define MAXPATHLEN _MAX_PATH |
| 33 | |
| 34 | /* Emulate sleep...we could have done this with a define, but that |
| 35 | would necessitate including windows.h in the files that used it. |
| 36 | This is much easier. */ |
| 37 | void |
| 38 | sleep(unsigned long seconds) |
| 39 | { |
| 40 | Sleep (seconds * 1000); |
| 41 | } |
| 42 | |
| 43 | /* Get the current working directory. */ |
| 44 | char * |
| 45 | getwd (char *dir) |
| 46 | { |
| 47 | if (GetCurrentDirectory (MAXPATHLEN, dir) > 0) |
| 48 | return dir; |
| 49 | return NULL; |
| 50 | } |
| 51 | |
| 52 | static HANDLE getppid_parent; |
| 53 | static int getppid_ppid; |
| 54 | |
| 55 | int |
| 56 | getppid(void) |
| 57 | { |
| 58 | char *ppid; |
| 59 | DWORD result; |
| 60 | |
| 61 | ppid = getenv ("EM_PARENT_PROCESS_ID"); |
| 62 | if (!ppid) |
| 63 | { |
| 64 | printf("no pid.\n"); |
| 65 | return 0; |
| 66 | } |
| 67 | else |
| 68 | { |
| 69 | getppid_ppid = atoi (ppid); |
| 70 | } |
| 71 | |
| 72 | if (!getppid_parent) |
| 73 | { |
| 74 | getppid_parent = OpenProcess (SYNCHRONIZE, FALSE, atoi(ppid)); |
| 75 | if (!getppid_parent) |
| 76 | { |
| 77 | printf ("Failed to open handle to parent process: %d\n", |
| 78 | GetLastError()); |
| 79 | exit (1); |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | result = WaitForSingleObject (getppid_parent, 0); |
| 84 | switch (result) |
| 85 | { |
| 86 | case WAIT_TIMEOUT: |
| 87 | /* The parent is still alive. */ |
| 88 | return getppid_ppid; |
| 89 | case WAIT_OBJECT_0: |
| 90 | /* The parent is gone. Return the pid of Unix init (1). */ |
| 91 | return 1; |
| 92 | case WAIT_FAILED: |
| 93 | default: |
| 94 | printf ("Checking parent status failed: %d\n", GetLastError()); |
| 95 | exit (1); |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | char * |
| 100 | getlogin () |
| 101 | { |
| 102 | static char user_name[256]; |
| 103 | DWORD length = sizeof (user_name); |
| 104 | |
| 105 | if (GetUserName (user_name, &length)) |
| 106 | return user_name; |
| 107 | return NULL; |
| 108 | } |
| 109 | |
| 110 | char * |
| 111 | cuserid (char * s) |
| 112 | { |
| 113 | char * name = getlogin (); |
| 114 | if (s) |
| 115 | return strcpy (s, name ? name : ""); |
| 116 | return name; |
| 117 | } |
| 118 | |
| 119 | int |
| 120 | getuid () |
| 121 | { |
| 122 | return 0; |
| 123 | } |
| 124 | |
| 125 | int |
| 126 | setuid (int uid) |
| 127 | { |
| 128 | return 0; |
| 129 | } |
| 130 | |
| 131 | struct passwd * |
| 132 | getpwuid (int uid) |
| 133 | { |
| 134 | return NULL; |
| 135 | } |
| 136 | |
| 137 | char * |
| 138 | getpass (const char * prompt) |
| 139 | { |
| 140 | static char input[256]; |
| 141 | HANDLE in; |
| 142 | HANDLE err; |
| 143 | DWORD count; |
| 144 | |
| 145 | in = GetStdHandle (STD_INPUT_HANDLE); |
| 146 | err = GetStdHandle (STD_ERROR_HANDLE); |
| 147 | |
| 148 | if (in == INVALID_HANDLE_VALUE || err == INVALID_HANDLE_VALUE) |
| 149 | return NULL; |
| 150 | |
| 151 | if (WriteFile (err, prompt, strlen (prompt), &count, NULL)) |
| 152 | { |
| 153 | int istty = (GetFileType (in) == FILE_TYPE_CHAR); |
| 154 | DWORD old_flags; |
| 155 | int rc; |
| 156 | |
| 157 | if (istty) |
| 158 | { |
| 159 | if (GetConsoleMode (in, &old_flags)) |
| 160 | SetConsoleMode (in, ENABLE_LINE_INPUT | ENABLE_PROCESSED_INPUT); |
| 161 | else |
| 162 | istty = 0; |
| 163 | } |
| 164 | rc = ReadFile (in, input, sizeof (input), &count, NULL); |
| 165 | if (count >= 2 && input[count - 2] == '\r') |
| 166 | input[count - 2] = '\0'; |
| 167 | else |
| 168 | { |
| 169 | char buf[256]; |
| 170 | while (ReadFile (in, buf, sizeof (buf), &count, NULL) > 0) |
| 171 | if (count >= 2 && buf[count - 2] == '\r') |
| 172 | break; |
| 173 | } |
| 174 | WriteFile (err, "\r\n", 2, &count, NULL); |
| 175 | if (istty) |
| 176 | SetConsoleMode (in, old_flags); |
| 177 | if (rc) |
| 178 | return input; |
| 179 | } |
| 180 | |
| 181 | return NULL; |
| 182 | } |
| 183 | |
| 184 | int |
| 185 | fchown (int fd, int uid, int gid) |
| 186 | { |
| 187 | return 0; |
| 188 | } |
| 189 | |
| 190 | /* Place a wrapper around the MSVC version of ctime. It returns NULL |
| 191 | on network directories, so we handle that case here. |
| 192 | (Ulrich Leodolter, 1/11/95). */ |
| 193 | char * |
| 194 | sys_ctime (const time_t *t) |
| 195 | { |
| 196 | char *str = (char *) ctime (t); |
| 197 | return (str ? str : "Sun Jan 01 00:00:00 1970"); |
| 198 | } |
| 199 | |
| 200 | FILE * |
| 201 | sys_fopen(const char * path, const char * mode) |
| 202 | { |
| 203 | return fopen (path, mode); |
| 204 | } |
| 205 | |
| 206 | int |
| 207 | sys_chdir (const char * path) |
| 208 | { |
| 209 | return _chdir (path); |
| 210 | } |