Trailing whitespace deleted.
[bpt/emacs.git] / lib-src / ntlib.c
CommitLineData
95ed0025
RS
1/* Utility and Unix shadow routines for GNU Emacs support programs on NT.
2 Copyright (C) 1994 Free Software Foundation, Inc.
3
3b7ad313
EN
4This file is part of GNU Emacs.
5
6GNU Emacs is free software; you can redistribute it and/or modify
7it under the terms of the GNU General Public License as published by
8the Free Software Foundation; either version 2, or (at your option)
9any later version.
10
11GNU Emacs is distributed in the hope that it will be useful,
12but WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14GNU General Public License for more details.
15
16You should have received a copy of the GNU General Public License
17along with GNU Emacs; see the file COPYING. If not, write to
18the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19Boston, MA 02111-1307, USA.
95ed0025
RS
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>
14f29224
GV
27#include <time.h>
28#include <direct.h>
29
30#include "ntlib.h"
95ed0025
RS
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. */
37void
c09c2c99 38sleep(unsigned long seconds)
95ed0025
RS
39{
40 Sleep (seconds * 1000);
41}
42
43/* Get the current working directory. */
133319ab 44char *
95ed0025
RS
45getwd (char *dir)
46{
14f29224
GV
47 if (GetCurrentDirectory (MAXPATHLEN, dir) > 0)
48 return dir;
49 return NULL;
95ed0025
RS
50}
51
52static HANDLE getppid_parent;
53static int getppid_ppid;
54
55int
56getppid(void)
57{
58 char *ppid;
59 DWORD result;
60
97e3c91b 61 ppid = getenv ("EM_PARENT_PROCESS_ID");
177c0ea7 62 if (!ppid)
95ed0025
RS
63 {
64 printf("no pid.\n");
65 return 0;
177c0ea7
JB
66 }
67 else
95ed0025
RS
68 {
69 getppid_ppid = atoi (ppid);
70 }
71
177c0ea7 72 if (!getppid_parent)
95ed0025
RS
73 {
74 getppid_parent = OpenProcess (SYNCHRONIZE, FALSE, atoi(ppid));
177c0ea7 75 if (!getppid_parent)
95ed0025
RS
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);
177c0ea7 84 switch (result)
95ed0025
RS
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}
14f29224
GV
98
99char *
100getlogin ()
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
110char *
111cuserid (char * s)
112{
113 char * name = getlogin ();
114 if (s)
115 return strcpy (s, name ? name : "");
116 return name;
117}
118
119int
120getuid ()
121{
122 return 0;
123}
124
125int
126setuid (int uid)
127{
128 return 0;
129}
130
131struct passwd *
132getpwuid (int uid)
133{
134 return NULL;
135}
136
137char *
138getpass (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
184int
185fchown (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
177c0ea7 191 on network directories, so we handle that case here.
14f29224
GV
192 (Ulrich Leodolter, 1/11/95). */
193char *
194sys_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
200FILE *
201sys_fopen(const char * path, const char * mode)
202{
203 return fopen (path, mode);
204}
205
206int
207sys_chdir (const char * path)
208{
209 return _chdir (path);
210}