ntlib.c 7.8 KB

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