annotate src/nt.c @ 272:c5d627a313b1 r21-0b34

Import from CVS: tag r21-0b34
author cvs
date Mon, 13 Aug 2007 10:28:48 +0200
parents 727739f917cb
children ca9a9ec9c1c1
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
100
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1 /* Utility and Unix shadow routines for XEmacs on Windows NT.
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2 Copyright (C) 1994, 1995 Free Software Foundation, Inc.
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
3
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
4 This file is part of XEmacs.
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
5
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
6 XEmacs is free software; you can redistribute it and/or modify it
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
7 under the terms of the GNU General Public License as published by the
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
8 Free Software Foundation; either version 2, or (at your option) any
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
9 later version.
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
10
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
11 XEmacs is distributed in the hope that it will be useful, but WITHOUT
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
12 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
14 for more details.
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
15
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
16 You should have received a copy of the GNU General Public License
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
17 along with XEmacs; see the file COPYING. If not, write to the Free
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
18 Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
19 02111-1307, USA.
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
20
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
21
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
22 Geoff Voelker (voelker@cs.washington.edu) 7-29-94 */
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
23
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
24 /* Adapted for XEmacs by David Hobley <david@spook-le0.cia.com.au> */
209
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
25 /* Sync'ed with Emacs 19.34.6 by Marc Paquette <marcpa@cam.org> */
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
26
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
27 #include <stddef.h> /* for offsetof */
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
28 #include <string.h>
100
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
29 #include <stdlib.h>
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
30 #include <stdio.h>
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
31 #include <io.h>
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
32 #include <errno.h>
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
33 #include <fcntl.h>
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
34 #include <ctype.h>
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
35 #include <signal.h>
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
36
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
37 /* must include CRT headers *before* config.h */
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
38 #include "config.h"
223
2c611d1463a6 Import from CVS: tag r20-4b10
cvs
parents: 213
diff changeset
39 #include "systime.h"
2c611d1463a6 Import from CVS: tag r20-4b10
cvs
parents: 213
diff changeset
40 #include "syssignal.h"
239
41f2f0e326e9 Import from CVS: tag r20-5b18
cvs
parents: 223
diff changeset
41 #include "sysproc.h"
223
2c611d1463a6 Import from CVS: tag r20-4b10
cvs
parents: 213
diff changeset
42
100
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
43 #undef access
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
44 #undef chdir
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
45 #undef chmod
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
46 #undef creat
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
47 #undef ctime
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
48 #undef fopen
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
49 #undef link
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
50 #undef mkdir
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
51 #undef mktemp
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
52 #undef open
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
53 #undef rename
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
54 #undef rmdir
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
55 #undef unlink
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
56
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
57 #undef close
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
58 #undef dup
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
59 #undef dup2
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
60 #undef pipe
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
61 #undef read
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
62 #undef write
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
63 #undef closedir
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
64
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
65 #define getwd _getwd
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
66 #include "lisp.h"
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
67 #undef getwd
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
68
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
69 #include <pwd.h>
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
70
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
71 #include <windows.h>
223
2c611d1463a6 Import from CVS: tag r20-4b10
cvs
parents: 213
diff changeset
72 #include <mmsystem.h>
100
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
73
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
74 #ifdef HAVE_SOCKETS /* TCP connection support, if kernel can do it */
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
75 #include <sys/socket.h>
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
76 #undef socket
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
77 #undef bind
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
78 #undef connect
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
79 #undef htons
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
80 #undef ntohs
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
81 #undef inet_addr
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
82 #undef gethostname
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
83 #undef gethostbyname
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
84 #undef getservbyname
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
85 #endif
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
86
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
87 #include "nt.h"
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
88 #include <sys/dir.h>
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
89 #include "ntheap.h"
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
90
209
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
91
263
727739f917cb Import from CVS: tag r20-5b30
cvs
parents: 239
diff changeset
92 extern Lisp_Object Vmswindows_downcase_file_names;
727739f917cb Import from CVS: tag r20-5b30
cvs
parents: 239
diff changeset
93 #if 0
209
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
94 extern Lisp_Object Vwin32_generate_fake_inodes;
263
727739f917cb Import from CVS: tag r20-5b30
cvs
parents: 239
diff changeset
95 #endif
727739f917cb Import from CVS: tag r20-5b30
cvs
parents: 239
diff changeset
96 extern Lisp_Object Vmswindows_get_true_file_attributes;
209
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
97
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
98 static char startup_dir[ MAXPATHLEN ];
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
99
100
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
100 /* Get the current working directory. */
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
101 char *
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
102 getwd (char *dir)
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
103 {
209
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
104 #if 0
100
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
105 if (GetCurrentDirectory (MAXPATHLEN, dir) > 0)
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
106 return dir;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
107 return NULL;
209
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
108 #else
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
109 /* Emacs doesn't actually change directory itself, and we want to
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
110 force our real wd to be where emacs.exe is to avoid unnecessary
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
111 conflicts when trying to rename or delete directories. */
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
112 strcpy (dir, startup_dir);
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
113 return dir;
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
114 #endif
100
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
115 }
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
116
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
117 #ifndef HAVE_SOCKETS
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
118 /* Emulate gethostname. */
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
119 int
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
120 gethostname (char *buffer, int size)
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
121 {
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
122 /* NT only allows small host names, so the buffer is
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
123 certainly large enough. */
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
124 return !GetComputerName (buffer, &size);
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
125 }
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
126 #endif /* HAVE_SOCKETS */
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
127
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
128 /* Emulate getloadavg. */
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
129 int
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
130 getloadavg (double loadavg[], int nelem)
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
131 {
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
132 int i;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
133
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
134 /* A faithful emulation is going to have to be saved for a rainy day. */
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
135 for (i = 0; i < nelem; i++)
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
136 {
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
137 loadavg[i] = 0.0;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
138 }
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
139 return i;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
140 }
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
141
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
142 /* Emulate getpwuid, getpwnam and others. */
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
143
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
144 #define PASSWD_FIELD_SIZE 256
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
145
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
146 static char the_passwd_name[PASSWD_FIELD_SIZE];
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
147 static char the_passwd_passwd[PASSWD_FIELD_SIZE];
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
148 static char the_passwd_gecos[PASSWD_FIELD_SIZE];
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
149 static char the_passwd_dir[PASSWD_FIELD_SIZE];
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
150 static char the_passwd_shell[PASSWD_FIELD_SIZE];
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
151
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
152 static struct passwd the_passwd =
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
153 {
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
154 the_passwd_name,
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
155 the_passwd_passwd,
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
156 0,
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
157 0,
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
158 0,
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
159 the_passwd_gecos,
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
160 the_passwd_dir,
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
161 the_passwd_shell,
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
162 };
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
163
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
164 int
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
165 getuid ()
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
166 {
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
167 return the_passwd.pw_uid;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
168 }
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
169
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
170 int
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
171 geteuid ()
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
172 {
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
173 /* I could imagine arguing for checking to see whether the user is
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
174 in the Administrators group and returning a UID of 0 for that
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
175 case, but I don't know how wise that would be in the long run. */
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
176 return getuid ();
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
177 }
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
178
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
179 int
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
180 getgid ()
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
181 {
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
182 return the_passwd.pw_gid;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
183 }
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
184
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
185 int
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
186 getegid ()
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
187 {
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
188 return getgid ();
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
189 }
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
190
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
191 struct passwd *
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
192 getpwuid (int uid)
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
193 {
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
194 if (uid == the_passwd.pw_uid)
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
195 return &the_passwd;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
196 return NULL;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
197 }
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
198
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
199 struct passwd *
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
200 getpwnam (char *name)
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
201 {
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
202 struct passwd *pw;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
203
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
204 pw = getpwuid (getuid ());
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
205 if (!pw)
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
206 return pw;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
207
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
208 if (stricmp (name, pw->pw_name))
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
209 return NULL;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
210
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
211 return pw;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
212 }
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
213
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
214 void
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
215 init_user_info ()
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
216 {
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
217 /* Find the user's real name by opening the process token and
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
218 looking up the name associated with the user-sid in that token.
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
219
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
220 Use the relative portion of the identifier authority value from
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
221 the user-sid as the user id value (same for group id using the
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
222 primary group sid from the process token). */
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
223
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
224 char user_sid[256], name[256], domain[256];
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
225 DWORD length = sizeof (name), dlength = sizeof (domain), trash;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
226 HANDLE token = NULL;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
227 SID_NAME_USE user_type;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
228
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
229 if (OpenProcessToken (GetCurrentProcess (), TOKEN_QUERY, &token)
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
230 && GetTokenInformation (token, TokenUser,
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
231 (PVOID) user_sid, sizeof (user_sid), &trash)
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
232 && LookupAccountSid (NULL, *((PSID *) user_sid), name, &length,
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
233 domain, &dlength, &user_type))
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
234 {
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
235 strcpy (the_passwd.pw_name, name);
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
236 /* Determine a reasonable uid value. */
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
237 if (stricmp ("administrator", name) == 0)
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
238 {
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
239 the_passwd.pw_uid = 0;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
240 the_passwd.pw_gid = 0;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
241 }
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
242 else
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
243 {
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
244 SID_IDENTIFIER_AUTHORITY * pSIA;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
245
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
246 pSIA = GetSidIdentifierAuthority (*((PSID *) user_sid));
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
247 /* I believe the relative portion is the last 4 bytes (of 6)
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
248 with msb first. */
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
249 the_passwd.pw_uid = ((pSIA->Value[2] << 24) +
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
250 (pSIA->Value[3] << 16) +
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
251 (pSIA->Value[4] << 8) +
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
252 (pSIA->Value[5] << 0));
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
253 /* restrict to conventional uid range for normal users */
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
254 the_passwd.pw_uid = the_passwd.pw_uid % 60001;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
255
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
256 /* Get group id */
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
257 if (GetTokenInformation (token, TokenPrimaryGroup,
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
258 (PVOID) user_sid, sizeof (user_sid), &trash))
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
259 {
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
260 SID_IDENTIFIER_AUTHORITY * pSIA;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
261
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
262 pSIA = GetSidIdentifierAuthority (*((PSID *) user_sid));
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
263 the_passwd.pw_gid = ((pSIA->Value[2] << 24) +
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
264 (pSIA->Value[3] << 16) +
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
265 (pSIA->Value[4] << 8) +
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
266 (pSIA->Value[5] << 0));
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
267 /* I don't know if this is necessary, but for safety... */
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
268 the_passwd.pw_gid = the_passwd.pw_gid % 60001;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
269 }
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
270 else
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
271 the_passwd.pw_gid = the_passwd.pw_uid;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
272 }
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
273 }
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
274 /* If security calls are not supported (presumably because we
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
275 are running under Windows 95), fallback to this. */
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
276 else if (GetUserName (name, &length))
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
277 {
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
278 strcpy (the_passwd.pw_name, name);
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
279 if (stricmp ("administrator", name) == 0)
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
280 the_passwd.pw_uid = 0;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
281 else
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
282 the_passwd.pw_uid = 123;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
283 the_passwd.pw_gid = the_passwd.pw_uid;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
284 }
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
285 else
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
286 {
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
287 strcpy (the_passwd.pw_name, "unknown");
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
288 the_passwd.pw_uid = 123;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
289 the_passwd.pw_gid = 123;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
290 }
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
291
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
292 /* Ensure HOME and SHELL are defined. */
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
293 if (getenv ("HOME") == NULL)
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
294 putenv ("HOME=c:/");
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
295 if (getenv ("SHELL") == NULL)
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
296 putenv ((GetVersion () & 0x80000000) ? "SHELL=command" : "SHELL=cmd");
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
297
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
298 /* Set dir and shell from environment variables. */
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
299 strcpy (the_passwd.pw_dir, getenv ("HOME"));
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
300 strcpy (the_passwd.pw_shell, getenv ("SHELL"));
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
301
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
302 if (token)
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
303 CloseHandle (token);
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
304 }
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
305
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
306 /* Normalize filename by converting all path separators to
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
307 the specified separator. Also conditionally convert upper
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
308 case path name components to lower case. */
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
309
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
310 static void
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
311 normalize_filename (fp, path_sep)
203
850242ba4a81 Import from CVS: tag r20-3b28
cvs
parents: 100
diff changeset
312 REGISTER char *fp;
100
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
313 char path_sep;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
314 {
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
315 char sep;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
316 char *elem;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
317
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
318 /* Always lower-case drive letters a-z, even if the filesystem
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
319 preserves case in filenames.
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
320 This is so filenames can be compared by string comparison
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
321 functions that are case-sensitive. Even case-preserving filesystems
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
322 do not distinguish case in drive letters. */
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
323 if (fp[1] == ':' && *fp >= 'A' && *fp <= 'Z')
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
324 {
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
325 *fp += 'a' - 'A';
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
326 fp += 2;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
327 }
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
328
263
727739f917cb Import from CVS: tag r20-5b30
cvs
parents: 239
diff changeset
329 if (NILP (Vmswindows_downcase_file_names))
100
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
330 {
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
331 while (*fp)
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
332 {
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
333 if (*fp == '/' || *fp == '\\')
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
334 *fp = path_sep;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
335 fp++;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
336 }
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
337 return;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
338 }
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
339
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
340 sep = path_sep; /* convert to this path separator */
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
341 elem = fp; /* start of current path element */
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
342
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
343 do {
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
344 if (*fp >= 'a' && *fp <= 'z')
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
345 elem = 0; /* don't convert this element */
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
346
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
347 if (*fp == 0 || *fp == ':')
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
348 {
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
349 sep = *fp; /* restore current separator (or 0) */
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
350 *fp = '/'; /* after conversion of this element */
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
351 }
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
352
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
353 if (*fp == '/' || *fp == '\\')
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
354 {
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
355 if (elem && elem != fp)
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
356 {
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
357 *fp = 0; /* temporary end of string */
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
358 _strlwr (elem); /* while we convert to lower case */
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
359 }
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
360 *fp = sep; /* convert (or restore) path separator */
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
361 elem = fp + 1; /* next element starts after separator */
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
362 sep = path_sep;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
363 }
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
364 } while (*fp++);
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
365 }
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
366
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
367 /* Destructively turn backslashes into slashes. */
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
368 void
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
369 dostounix_filename (p)
203
850242ba4a81 Import from CVS: tag r20-3b28
cvs
parents: 100
diff changeset
370 REGISTER char *p;
100
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
371 {
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
372 normalize_filename (p, '/');
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
373 }
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
374
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
375 /* Destructively turn slashes into backslashes. */
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
376 void
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
377 unixtodos_filename (p)
203
850242ba4a81 Import from CVS: tag r20-3b28
cvs
parents: 100
diff changeset
378 REGISTER char *p;
100
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
379 {
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
380 normalize_filename (p, '\\');
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
381 }
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
382
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
383 /* Remove all CR's that are followed by a LF.
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
384 (From msdos.c...probably should figure out a way to share it,
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
385 although this code isn't going to ever change.) */
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
386 int
209
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
387 crlf_to_lf (n, buf, lf_count)
203
850242ba4a81 Import from CVS: tag r20-3b28
cvs
parents: 100
diff changeset
388 REGISTER int n;
850242ba4a81 Import from CVS: tag r20-3b28
cvs
parents: 100
diff changeset
389 REGISTER unsigned char *buf;
209
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
390 REGISTER unsigned *lf_count;
100
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
391 {
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
392 unsigned char *np = buf;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
393 unsigned char *startp = buf;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
394 unsigned char *endp = buf + n;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
395
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
396 if (n == 0)
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
397 return n;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
398 while (buf < endp - 1)
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
399 {
209
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
400 if (*buf == 0x0a)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
401 (*lf_count)++;
100
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
402 if (*buf == 0x0d)
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
403 {
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
404 if (*(++buf) != 0x0a)
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
405 *np++ = 0x0d;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
406 }
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
407 else
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
408 *np++ = *buf++;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
409 }
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
410 if (buf < endp)
209
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
411 {
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
412 if (*buf == 0x0a)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
413 (*lf_count)++;
100
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
414 *np++ = *buf++;
209
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
415 }
100
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
416 return np - startp;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
417 }
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
418
209
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
419 /* Parse the root part of file name, if present. Return length and
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
420 optionally store pointer to char after root. */
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
421 static int
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
422 parse_root (char * name, char ** pPath)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
423 {
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
424 char * start = name;
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
425
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
426 if (name == NULL)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
427 return 0;
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
428
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
429 /* find the root name of the volume if given */
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
430 if (isalpha (name[0]) && name[1] == ':')
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
431 {
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
432 /* skip past drive specifier */
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
433 name += 2;
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
434 if (IS_DIRECTORY_SEP (name[0]))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
435 name++;
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
436 }
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
437 else if (IS_DIRECTORY_SEP (name[0]) && IS_DIRECTORY_SEP (name[1]))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
438 {
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
439 int slashes = 2;
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
440 name += 2;
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
441 do
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
442 {
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
443 if (IS_DIRECTORY_SEP (*name) && --slashes == 0)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
444 break;
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
445 name++;
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
446 }
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
447 while ( *name );
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
448 if (IS_DIRECTORY_SEP (name[0]))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
449 name++;
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
450 }
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
451
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
452 if (pPath)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
453 *pPath = name;
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
454
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
455 return name - start;
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
456 }
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
457
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
458 /* Get long base name for name; name is assumed to be absolute. */
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
459 static int
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
460 get_long_basename (char * name, char * buf, int size)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
461 {
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
462 WIN32_FIND_DATA find_data;
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
463 HANDLE dir_handle;
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
464 int len = 0;
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
465 #ifdef PIGSFLY
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
466 char *p;
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
467
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
468 /* If the last component of NAME has a wildcard character,
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
469 return it as the basename. */
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
470 p = name + strlen (name);
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
471 while (*p != '\\' && *p != ':' && p > name) p--;
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
472 if (p > name) p++;
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
473 if (strchr (p, '*') || strchr (p, '?'))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
474 {
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
475 if ((len = strlen (p)) < size)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
476 memcpy (buf, p, len + 1);
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
477 else
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
478 len = 0;
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
479 return len;
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
480 }
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
481 #endif
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
482
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
483 dir_handle = FindFirstFile (name, &find_data);
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
484 if (dir_handle != INVALID_HANDLE_VALUE)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
485 {
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
486 if ((len = strlen (find_data.cFileName)) < size)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
487 memcpy (buf, find_data.cFileName, len + 1);
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
488 else
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
489 len = 0;
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
490 FindClose (dir_handle);
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
491 }
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
492 return len;
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
493 }
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
494
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
495 /* Get long name for file, if possible (assumed to be absolute). */
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
496 BOOL
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
497 win32_get_long_filename (char * name, char * buf, int size)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
498 {
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
499 char * o = buf;
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
500 char * p;
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
501 char * q;
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
502 char full[ MAX_PATH ];
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
503 int len;
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
504
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
505 len = strlen (name);
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
506 if (len >= MAX_PATH)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
507 return FALSE;
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
508
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
509 /* Use local copy for destructive modification. */
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
510 memcpy (full, name, len+1);
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
511 unixtodos_filename (full);
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
512
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
513 /* Copy root part verbatim. */
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
514 len = parse_root (full, &p);
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
515 memcpy (o, full, len);
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
516 o += len;
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
517 size -= len;
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
518
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
519 do
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
520 {
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
521 q = p;
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
522 p = strchr (q, '\\');
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
523 if (p) *p = '\0';
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
524 len = get_long_basename (full, o, size);
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
525 if (len > 0)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
526 {
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
527 o += len;
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
528 size -= len;
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
529 if (p != NULL)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
530 {
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
531 *p++ = '\\';
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
532 if (size < 2)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
533 return FALSE;
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
534 *o++ = '\\';
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
535 size--;
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
536 *o = '\0';
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
537 }
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
538 }
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
539 else
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
540 return FALSE;
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
541 }
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
542 while (p != NULL && *p);
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
543
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
544 return TRUE;
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
545 }
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
546
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
547
100
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
548 /* Routines that are no-ops on NT but are defined to get Emacs to compile. */
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
549
239
41f2f0e326e9 Import from CVS: tag r20-5b18
cvs
parents: 223
diff changeset
550 #if 0 /* #### We do not need those, do we? -kkm */
100
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
551 int
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
552 unrequest_sigio (void)
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
553 {
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
554 return 0;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
555 }
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
556
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
557 int
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
558 request_sigio (void)
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
559 {
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
560 return 0;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
561 }
239
41f2f0e326e9 Import from CVS: tag r20-5b18
cvs
parents: 223
diff changeset
562 #endif /* 0 */
100
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
563
213
78f53ef88e17 Import from CVS: tag r20-4b5
cvs
parents: 211
diff changeset
564 #define REG_ROOT "SOFTWARE\\GNU\\XEmacs"
100
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
565
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
566 LPBYTE
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
567 nt_get_resource (key, lpdwtype)
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
568 char *key;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
569 LPDWORD lpdwtype;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
570 {
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
571 LPBYTE lpvalue;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
572 HKEY hrootkey = NULL;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
573 DWORD cbData;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
574 BOOL ok = FALSE;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
575
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
576 /* Check both the current user and the local machine to see if
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
577 we have any resources. */
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
578
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
579 if (RegOpenKeyEx (HKEY_CURRENT_USER, REG_ROOT, 0, KEY_READ, &hrootkey) == ERROR_SUCCESS)
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
580 {
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
581 lpvalue = NULL;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
582
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
583 if (RegQueryValueEx (hrootkey, key, NULL, NULL, NULL, &cbData) == ERROR_SUCCESS
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
584 && (lpvalue = (LPBYTE) xmalloc (cbData)) != NULL
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
585 && RegQueryValueEx (hrootkey, key, NULL, lpdwtype, lpvalue, &cbData) == ERROR_SUCCESS)
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
586 {
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
587 return (lpvalue);
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
588 }
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
589
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
590 if (lpvalue) xfree (lpvalue);
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
591
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
592 RegCloseKey (hrootkey);
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
593 }
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
594
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
595 if (RegOpenKeyEx (HKEY_LOCAL_MACHINE, REG_ROOT, 0, KEY_READ, &hrootkey) == ERROR_SUCCESS)
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
596 {
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
597 lpvalue = NULL;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
598
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
599 if (RegQueryValueEx (hrootkey, key, NULL, NULL, NULL, &cbData) == ERROR_SUCCESS &&
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
600 (lpvalue = (LPBYTE) xmalloc (cbData)) != NULL &&
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
601 RegQueryValueEx (hrootkey, key, NULL, lpdwtype, lpvalue, &cbData) == ERROR_SUCCESS)
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
602 {
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
603 return (lpvalue);
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
604 }
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
605
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
606 if (lpvalue) xfree (lpvalue);
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
607
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
608 RegCloseKey (hrootkey);
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
609 }
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
610
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
611 return (NULL);
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
612 }
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
613
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
614 void
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
615 init_environment ()
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
616 {
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
617 /* Check for environment variables and use registry if they don't exist */
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
618 {
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
619 int i;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
620 LPBYTE lpval;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
621 DWORD dwType;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
622
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
623 static char * env_vars[] =
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
624 {
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
625 "HOME",
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
626 "PRELOAD_WINSOCK",
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
627 "emacs_dir",
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
628 "EMACSLOADPATH",
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
629 "SHELL",
209
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
630 "CMDPROXY",
100
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
631 "EMACSDATA",
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
632 "EMACSPATH",
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
633 "EMACSLOCKDIR",
209
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
634 /* We no longer set INFOPATH because Info-default-directory-list
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
635 is then ignored. We use a hook in winnt.el instead. */
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
636 /* "INFOPATH", */
100
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
637 "EMACSDOC",
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
638 "TERM",
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
639 };
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
640
272
c5d627a313b1 Import from CVS: tag r21-0b34
cvs
parents: 263
diff changeset
641 for (i = 0; i < countof (env_vars); i++)
100
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
642 {
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
643 if (!getenv (env_vars[i]) &&
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
644 (lpval = nt_get_resource (env_vars[i], &dwType)) != NULL)
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
645 {
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
646 if (dwType == REG_EXPAND_SZ)
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
647 {
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
648 char buf1[500], buf2[500];
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
649
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
650 ExpandEnvironmentStrings ((LPSTR) lpval, buf1, 500);
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
651 _snprintf (buf2, 499, "%s=%s", env_vars[i], buf1);
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
652 putenv (strdup (buf2));
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
653 }
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
654 else if (dwType == REG_SZ)
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
655 {
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
656 char buf[500];
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
657
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
658 _snprintf (buf, 499, "%s=%s", env_vars[i], lpval);
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
659 putenv (strdup (buf));
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
660 }
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
661
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
662 xfree (lpval);
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
663 }
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
664 }
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
665 }
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
666
209
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
667 /* Another special case: on NT, the PATH variable is actually named
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
668 "Path" although cmd.exe (perhaps NT itself) arranges for
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
669 environment variable lookup and setting to be case insensitive.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
670 However, Emacs assumes a fully case sensitive environment, so we
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
671 need to change "Path" to "PATH" to match the expectations of
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
672 various elisp packages. We do this by the sneaky method of
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
673 modifying the string in the C runtime environ entry.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
674
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
675 The same applies to COMSPEC. */
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
676 {
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
677 char ** envp;
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
678
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
679 for (envp = environ; *envp; envp++)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
680 if (_strnicmp (*envp, "PATH=", 5) == 0)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
681 memcpy (*envp, "PATH=", 5);
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
682 else if (_strnicmp (*envp, "COMSPEC=", 8) == 0)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
683 memcpy (*envp, "COMSPEC=", 8);
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
684 }
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
685
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
686 /* Remember the initial working directory for getwd, then make the
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
687 real wd be the location of emacs.exe to avoid conflicts when
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
688 renaming or deleting directories. (We also don't call chdir when
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
689 running subprocesses for the same reason.) */
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
690 if (!GetCurrentDirectory (MAXPATHLEN, startup_dir))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
691 abort ();
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
692
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
693 {
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
694 char *p;
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
695 char modname[MAX_PATH];
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
696
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
697 if (!GetModuleFileName (NULL, modname, MAX_PATH))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
698 abort ();
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
699 if ((p = strrchr (modname, '\\')) == NULL)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
700 abort ();
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
701 *p = 0;
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
702
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
703 SetCurrentDirectory (modname);
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
704 }
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
705
100
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
706 init_user_info ();
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
707 }
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
708
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
709 /* We don't have scripts to automatically determine the system configuration
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
710 for Emacs before it's compiled, and we don't want to have to make the
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
711 user enter it, so we define EMACS_CONFIGURATION to invoke this runtime
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
712 routine. */
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
713
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
714 static char configuration_buffer[32];
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
715
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
716 char *
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
717 get_emacs_configuration (void)
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
718 {
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
719 char *arch, *oem, *os;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
720
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
721 /* Determine the processor type. */
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
722 switch (get_processor_type ())
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
723 {
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
724
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
725 #ifdef PROCESSOR_INTEL_386
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
726 case PROCESSOR_INTEL_386:
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
727 case PROCESSOR_INTEL_486:
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
728 case PROCESSOR_INTEL_PENTIUM:
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
729 arch = "i386";
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
730 break;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
731 #endif
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
732
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
733 #ifdef PROCESSOR_INTEL_860
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
734 case PROCESSOR_INTEL_860:
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
735 arch = "i860";
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
736 break;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
737 #endif
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
738
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
739 #ifdef PROCESSOR_MIPS_R2000
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
740 case PROCESSOR_MIPS_R2000:
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
741 case PROCESSOR_MIPS_R3000:
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
742 case PROCESSOR_MIPS_R4000:
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
743 arch = "mips";
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
744 break;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
745 #endif
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
746
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
747 #ifdef PROCESSOR_ALPHA_21064
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
748 case PROCESSOR_ALPHA_21064:
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
749 arch = "alpha";
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
750 break;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
751 #endif
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
752
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
753 default:
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
754 arch = "unknown";
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
755 break;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
756 }
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
757
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
758 /* Let oem be "*" until we figure out how to decode the OEM field. */
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
759 oem = "*";
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
760
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
761 os = (GetVersion () & 0x80000000) ? "win95" : "nt";
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
762
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
763 sprintf (configuration_buffer, "%s-%s-%s%d.%d", arch, oem, os,
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
764 get_nt_major_version (), get_nt_minor_version ());
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
765 return configuration_buffer;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
766 }
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
767
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
768 #ifndef HAVE_X_WINDOWS
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
769 /* X11R6 on NT provides the single parameter version of this command. */
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
770
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
771 #include <sys/timeb.h>
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
772
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
773 /* Emulate gettimeofday (Ulrich Leodolter, 1/11/95). */
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
774 void
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
775 gettimeofday (struct timeval *tv, struct timezone *tz)
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
776 {
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
777 struct _timeb tb;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
778 _ftime (&tb);
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
779
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
780 tv->tv_sec = tb.time;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
781 tv->tv_usec = tb.millitm * 1000L;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
782 if (tz)
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
783 {
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
784 tz->tz_minuteswest = tb.timezone; /* minutes west of Greenwich */
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
785 tz->tz_dsttime = tb.dstflag; /* type of dst correction */
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
786 }
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
787 }
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
788
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
789 #endif /* HAVE_X_WINDOWS */
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
790
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
791 /* ------------------------------------------------------------------------- */
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
792 /* IO support and wrapper functions for Win32 API. */
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
793 /* ------------------------------------------------------------------------- */
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
794
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
795 /* Place a wrapper around the MSVC version of ctime. It returns NULL
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
796 on network directories, so we handle that case here.
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
797 (Ulrich Leodolter, 1/11/95). */
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
798 char *
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
799 sys_ctime (const time_t *t)
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
800 {
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
801 char *str = (char *) ctime (t);
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
802 return (str ? str : "Sun Jan 01 00:00:00 1970");
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
803 }
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
804
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
805 /* Emulate sleep...we could have done this with a define, but that
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
806 would necessitate including windows.h in the files that used it.
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
807 This is much easier. */
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
808
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
809 #ifndef HAVE_X_WINDOWS
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
810 void
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
811 sys_sleep (int seconds)
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
812 {
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
813 Sleep (seconds * 1000);
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
814 }
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
815 #endif
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
816
239
41f2f0e326e9 Import from CVS: tag r20-5b18
cvs
parents: 223
diff changeset
817 /* #### This is an evil dirty hack. We must get rid of it.
41f2f0e326e9 Import from CVS: tag r20-5b18
cvs
parents: 223
diff changeset
818 Word "munging" is not in XEmacs lexicon. - kkm */
41f2f0e326e9 Import from CVS: tag r20-5b18
cvs
parents: 223
diff changeset
819
100
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
820 /* Internal MSVC data and functions for low-level descriptor munging */
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
821 #if (_MSC_VER == 900)
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
822 extern char _osfile[];
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
823 #endif
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
824 extern int __cdecl _set_osfhnd (int fd, long h);
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
825 extern int __cdecl _free_osfhnd (int fd);
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
826
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
827 /* parallel array of private info on file handles */
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
828 filedesc fd_info [ MAXDESC ];
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
829
209
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
830 typedef struct volume_info_data {
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
831 struct volume_info_data * next;
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
832
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
833 /* time when info was obtained */
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
834 DWORD timestamp;
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
835
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
836 /* actual volume info */
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
837 char * root_dir;
100
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
838 DWORD serialnum;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
839 DWORD maxcomp;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
840 DWORD flags;
209
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
841 char * name;
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
842 char * type;
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
843 } volume_info_data;
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
844
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
845 /* Global referenced by various functions. */
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
846 static volume_info_data volume_info;
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
847
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
848 /* Vector to indicate which drives are local and fixed (for which cached
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
849 data never expires). */
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
850 static BOOL fixed_drives[26];
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
851
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
852 /* Consider cached volume information to be stale if older than 10s,
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
853 at least for non-local drives. Info for fixed drives is never stale. */
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
854 #define DRIVE_INDEX( c ) ( (c) <= 'Z' ? (c) - 'A' : (c) - 'a' )
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
855 #define VOLINFO_STILL_VALID( root_dir, info ) \
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
856 ( ( isalpha (root_dir[0]) && \
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
857 fixed_drives[ DRIVE_INDEX (root_dir[0]) ] ) \
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
858 || GetTickCount () - info->timestamp < 10000 )
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
859
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
860 /* Cache support functions. */
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
861
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
862 /* Simple linked list with linear search is sufficient. */
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
863 static volume_info_data *volume_cache = NULL;
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
864
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
865 static volume_info_data *
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
866 lookup_volume_info (char * root_dir)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
867 {
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
868 volume_info_data * info;
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
869
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
870 for (info = volume_cache; info; info = info->next)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
871 if (stricmp (info->root_dir, root_dir) == 0)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
872 break;
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
873 return info;
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
874 }
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
875
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
876 static void
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
877 add_volume_info (char * root_dir, volume_info_data * info)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
878 {
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
879 info->root_dir = xstrdup (root_dir);
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
880 info->next = volume_cache;
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
881 volume_cache = info;
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
882 }
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
883
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
884
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
885 /* Wrapper for GetVolumeInformation, which uses caching to avoid
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
886 performance penalty (~2ms on 486 for local drives, 7.5ms for local
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
887 cdrom drive, ~5-10ms or more for remote drives on LAN). */
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
888 volume_info_data *
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
889 GetCachedVolumeInformation (char * root_dir)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
890 {
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
891 volume_info_data * info;
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
892 char default_root[ MAX_PATH ];
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
893
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
894 /* NULL for root_dir means use root from current directory. */
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
895 if (root_dir == NULL)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
896 {
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
897 if (GetCurrentDirectory (MAX_PATH, default_root) == 0)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
898 return NULL;
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
899 parse_root (default_root, &root_dir);
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
900 *root_dir = 0;
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
901 root_dir = default_root;
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
902 }
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
903
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
904 /* Local fixed drives can be cached permanently. Removable drives
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
905 cannot be cached permanently, since the volume name and serial
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
906 number (if nothing else) can change. Remote drives should be
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
907 treated as if they are removable, since there is no sure way to
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
908 tell whether they are or not. Also, the UNC association of drive
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
909 letters mapped to remote volumes can be changed at any time (even
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
910 by other processes) without notice.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
911
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
912 As a compromise, so we can benefit from caching info for remote
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
913 volumes, we use a simple expiry mechanism to invalidate cache
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
914 entries that are more than ten seconds old. */
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
915
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
916 #if 0
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
917 /* No point doing this, because WNetGetConnection is even slower than
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
918 GetVolumeInformation, consistently taking ~50ms on a 486 (FWIW,
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
919 GetDriveType is about the only call of this type which does not
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
920 involve network access, and so is extremely quick). */
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
921
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
922 /* Map drive letter to UNC if remote. */
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
923 if ( isalpha( root_dir[0] ) && !fixed[ DRIVE_INDEX( root_dir[0] ) ] )
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
924 {
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
925 char remote_name[ 256 ];
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
926 char drive[3] = { root_dir[0], ':' };
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
927
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
928 if (WNetGetConnection (drive, remote_name, sizeof (remote_name))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
929 == NO_ERROR)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
930 /* do something */ ;
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
931 }
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
932 #endif
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
933
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
934 info = lookup_volume_info (root_dir);
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
935
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
936 if (info == NULL || ! VOLINFO_STILL_VALID (root_dir, info))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
937 {
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
938 char name[ 256 ];
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
939 DWORD serialnum;
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
940 DWORD maxcomp;
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
941 DWORD flags;
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
942 char type[ 256 ];
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
943
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
944 /* Info is not cached, or is stale. */
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
945 if (!GetVolumeInformation (root_dir,
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
946 name, sizeof (name),
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
947 &serialnum,
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
948 &maxcomp,
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
949 &flags,
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
950 type, sizeof (type)))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
951 return NULL;
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
952
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
953 /* Cache the volume information for future use, overwriting existing
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
954 entry if present. */
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
955 if (info == NULL)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
956 {
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
957 info = (volume_info_data *) xmalloc (sizeof (volume_info_data));
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
958 add_volume_info (root_dir, info);
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
959 }
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
960 else
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
961 {
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
962 free (info->name);
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
963 free (info->type);
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
964 }
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
965
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
966 info->name = xstrdup (name);
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
967 info->serialnum = serialnum;
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
968 info->maxcomp = maxcomp;
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
969 info->flags = flags;
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
970 info->type = xstrdup (type);
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
971 info->timestamp = GetTickCount ();
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
972 }
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
973
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
974 return info;
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
975 }
100
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
976
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
977 /* Get information on the volume where name is held; set path pointer to
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
978 start of pathname in name (past UNC header\volume header if present). */
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
979 int
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
980 get_volume_info (const char * name, const char ** pPath)
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
981 {
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
982 char temp[MAX_PATH];
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
983 char *rootname = NULL; /* default to current volume */
209
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
984 volume_info_data * info;
100
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
985
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
986 if (name == NULL)
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
987 return FALSE;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
988
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
989 /* find the root name of the volume if given */
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
990 if (isalpha (name[0]) && name[1] == ':')
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
991 {
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
992 rootname = temp;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
993 temp[0] = *name++;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
994 temp[1] = *name++;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
995 temp[2] = '\\';
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
996 temp[3] = 0;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
997 }
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
998 else if (IS_DIRECTORY_SEP (name[0]) && IS_DIRECTORY_SEP (name[1]))
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
999 {
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1000 char *str = temp;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1001 int slashes = 4;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1002 rootname = temp;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1003 do
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1004 {
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1005 if (IS_DIRECTORY_SEP (*name) && --slashes == 0)
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1006 break;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1007 *str++ = *name++;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1008 }
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1009 while ( *name );
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1010
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1011 *str++ = '\\';
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1012 *str = 0;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1013 }
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1014
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1015 if (pPath)
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1016 *pPath = name;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1017
209
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
1018 info = GetCachedVolumeInformation (rootname);
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
1019 if (info != NULL)
100
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1020 {
209
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
1021 /* Set global referenced by other functions. */
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
1022 volume_info = *info;
100
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1023 return TRUE;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1024 }
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1025 return FALSE;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1026 }
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1027
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1028 /* Determine if volume is FAT format (ie. only supports short 8.3
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1029 names); also set path pointer to start of pathname in name. */
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1030 int
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1031 is_fat_volume (const char * name, const char ** pPath)
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1032 {
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1033 if (get_volume_info (name, pPath))
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1034 return (volume_info.maxcomp == 12);
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1035 return FALSE;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1036 }
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1037
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1038 /* Map filename to a legal 8.3 name if necessary. */
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1039 const char *
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1040 map_win32_filename (const char * name, const char ** pPath)
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1041 {
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1042 static char shortname[MAX_PATH];
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1043 char * str = shortname;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1044 char c;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1045 char * path;
209
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
1046 const char * save_name = name;
100
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1047
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1048 if (is_fat_volume (name, &path)) /* truncate to 8.3 */
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1049 {
203
850242ba4a81 Import from CVS: tag r20-3b28
cvs
parents: 100
diff changeset
1050 REGISTER int left = 8; /* maximum number of chars in part */
850242ba4a81 Import from CVS: tag r20-3b28
cvs
parents: 100
diff changeset
1051 REGISTER int extn = 0; /* extension added? */
850242ba4a81 Import from CVS: tag r20-3b28
cvs
parents: 100
diff changeset
1052 REGISTER int dots = 2; /* maximum number of dots allowed */
100
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1053
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1054 while (name < path)
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1055 *str++ = *name++; /* skip past UNC header */
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1056
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1057 while ((c = *name++))
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1058 {
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1059 switch ( c )
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1060 {
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1061 case '\\':
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1062 case '/':
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1063 *str++ = '\\';
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1064 extn = 0; /* reset extension flags */
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1065 dots = 2; /* max 2 dots */
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1066 left = 8; /* max length 8 for main part */
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1067 break;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1068 case ':':
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1069 *str++ = ':';
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1070 extn = 0; /* reset extension flags */
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1071 dots = 2; /* max 2 dots */
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1072 left = 8; /* max length 8 for main part */
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1073 break;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1074 case '.':
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1075 if ( dots )
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1076 {
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1077 /* Convert path components of the form .xxx to _xxx,
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1078 but leave . and .. as they are. This allows .emacs
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1079 to be read as _emacs, for example. */
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1080
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1081 if (! *name ||
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1082 *name == '.' ||
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1083 IS_DIRECTORY_SEP (*name))
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1084 {
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1085 *str++ = '.';
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1086 dots--;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1087 }
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1088 else
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1089 {
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1090 *str++ = '_';
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1091 left--;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1092 dots = 0;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1093 }
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1094 }
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1095 else if ( !extn )
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1096 {
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1097 *str++ = '.';
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1098 extn = 1; /* we've got an extension */
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1099 left = 3; /* 3 chars in extension */
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1100 }
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1101 else
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1102 {
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1103 /* any embedded dots after the first are converted to _ */
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1104 *str++ = '_';
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1105 }
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1106 break;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1107 case '~':
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1108 case '#': /* don't lose these, they're important */
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1109 if ( ! left )
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1110 str[-1] = c; /* replace last character of part */
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1111 /* FALLTHRU */
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1112 default:
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1113 if ( left )
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1114 {
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1115 *str++ = tolower (c); /* map to lower case (looks nicer) */
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1116 left--;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1117 dots = 0; /* started a path component */
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1118 }
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1119 break;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1120 }
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1121 }
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1122 *str = '\0';
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1123 }
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1124 else
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1125 {
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1126 strcpy (shortname, name);
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1127 unixtodos_filename (shortname);
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1128 }
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1129
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1130 if (pPath)
209
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
1131 *pPath = shortname + (path - save_name);
100
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1132
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1133 return shortname;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1134 }
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1135
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1136
209
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
1137 /* Emulate the Unix directory procedures opendir, closedir,
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
1138 and readdir. We can't use the procedures supplied in sysdep.c,
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
1139 so we provide them here. */
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
1140
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
1141 struct direct dir_static; /* simulated directory contents */
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
1142 static HANDLE dir_find_handle = INVALID_HANDLE_VALUE;
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
1143 static int dir_is_fat;
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
1144 static char dir_pathname[MAXPATHLEN+1];
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
1145 static WIN32_FIND_DATA dir_find_data;
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
1146
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
1147 DIR *
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
1148 opendir (char *filename)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
1149 {
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
1150 DIR *dirp;
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
1151
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
1152 /* Opening is done by FindFirstFile. However, a read is inherent to
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
1153 this operation, so we defer the open until read time. */
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
1154
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
1155 if (!(dirp = (DIR *) xmalloc (sizeof (DIR))))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
1156 return NULL;
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
1157 if (dir_find_handle != INVALID_HANDLE_VALUE)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
1158 return NULL;
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
1159
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
1160 dirp->dd_fd = 0;
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
1161 dirp->dd_loc = 0;
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
1162 dirp->dd_size = 0;
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
1163
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
1164 strncpy (dir_pathname, map_win32_filename (filename, NULL), MAXPATHLEN);
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
1165 dir_pathname[MAXPATHLEN] = '\0';
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
1166 dir_is_fat = is_fat_volume (filename, NULL);
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
1167
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
1168 return dirp;
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
1169 }
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
1170
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
1171 void
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
1172 closedir (DIR *dirp)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
1173 {
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
1174 /* If we have a find-handle open, close it. */
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
1175 if (dir_find_handle != INVALID_HANDLE_VALUE)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
1176 {
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
1177 FindClose (dir_find_handle);
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
1178 dir_find_handle = INVALID_HANDLE_VALUE;
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
1179 }
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
1180 xfree ((char *) dirp);
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
1181 }
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
1182
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
1183 struct direct *
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
1184 readdir (DIR *dirp)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
1185 {
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
1186 /* If we aren't dir_finding, do a find-first, otherwise do a find-next. */
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
1187 if (dir_find_handle == INVALID_HANDLE_VALUE)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
1188 {
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
1189 char filename[MAXNAMLEN + 3];
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
1190 int ln;
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
1191
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
1192 strcpy (filename, dir_pathname);
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
1193 ln = strlen (filename) - 1;
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
1194 if (!IS_DIRECTORY_SEP (filename[ln]))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
1195 strcat (filename, "\\");
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
1196 strcat (filename, "*");
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
1197
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
1198 dir_find_handle = FindFirstFile (filename, &dir_find_data);
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
1199
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
1200 if (dir_find_handle == INVALID_HANDLE_VALUE)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
1201 return NULL;
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
1202 }
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
1203 else
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
1204 {
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
1205 if (!FindNextFile (dir_find_handle, &dir_find_data))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
1206 return NULL;
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
1207 }
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
1208
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
1209 /* Emacs never uses this value, so don't bother making it match
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
1210 value returned by stat(). */
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
1211 dir_static.d_ino = 1;
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
1212
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
1213 dir_static.d_reclen = sizeof (struct direct) - MAXNAMLEN + 3 +
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
1214 dir_static.d_namlen - dir_static.d_namlen % 4;
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
1215
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
1216 dir_static.d_namlen = strlen (dir_find_data.cFileName);
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
1217 strcpy (dir_static.d_name, dir_find_data.cFileName);
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
1218 if (dir_is_fat)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
1219 _strlwr (dir_static.d_name);
263
727739f917cb Import from CVS: tag r20-5b30
cvs
parents: 239
diff changeset
1220 else if (!NILP (Vmswindows_downcase_file_names))
209
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
1221 {
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
1222 REGISTER char *p;
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
1223 for (p = dir_static.d_name; *p; p++)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
1224 if (*p >= 'a' && *p <= 'z')
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
1225 break;
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
1226 if (!*p)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
1227 _strlwr (dir_static.d_name);
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
1228 }
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
1229
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
1230 return &dir_static;
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
1231 }
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
1232
100
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1233 /* Shadow some MSVC runtime functions to map requests for long filenames
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1234 to reasonable short names if necessary. This was originally added to
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1235 permit running Emacs on NT 3.1 on a FAT partition, which doesn't support
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1236 long file names. */
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1237
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1238 int
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1239 sys_access (const char * path, int mode)
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1240 {
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1241 return _access (map_win32_filename (path, NULL), mode);
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1242 }
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1243
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1244 int
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1245 sys_chdir (const char * path)
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1246 {
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1247 return _chdir (map_win32_filename (path, NULL));
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1248 }
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1249
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1250 int
272
c5d627a313b1 Import from CVS: tag r21-0b34
cvs
parents: 263
diff changeset
1251 sys_chmod (const char * path, mode_t mode)
100
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1252 {
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1253 return _chmod (map_win32_filename (path, NULL), mode);
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1254 }
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1255
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1256 int
272
c5d627a313b1 Import from CVS: tag r21-0b34
cvs
parents: 263
diff changeset
1257 sys_creat (const char * path, mode_t mode)
100
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1258 {
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1259 return _creat (map_win32_filename (path, NULL), mode);
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1260 }
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1261
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1262 FILE *
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1263 sys_fopen(const char * path, const char * mode)
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1264 {
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1265 int fd;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1266 int oflag;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1267 const char * mode_save = mode;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1268
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1269 /* Force all file handles to be non-inheritable. This is necessary to
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1270 ensure child processes don't unwittingly inherit handles that might
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1271 prevent future file access. */
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1272
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1273 if (mode[0] == 'r')
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1274 oflag = O_RDONLY;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1275 else if (mode[0] == 'w' || mode[0] == 'a')
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1276 oflag = O_WRONLY | O_CREAT | O_TRUNC;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1277 else
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1278 return NULL;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1279
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1280 /* Only do simplistic option parsing. */
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1281 while (*++mode)
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1282 if (mode[0] == '+')
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1283 {
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1284 oflag &= ~(O_RDONLY | O_WRONLY);
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1285 oflag |= O_RDWR;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1286 }
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1287 else if (mode[0] == 'b')
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1288 {
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1289 oflag &= ~O_TEXT;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1290 oflag |= O_BINARY;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1291 }
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1292 else if (mode[0] == 't')
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1293 {
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1294 oflag &= ~O_BINARY;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1295 oflag |= O_TEXT;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1296 }
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1297 else break;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1298
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1299 fd = _open (map_win32_filename (path, NULL), oflag | _O_NOINHERIT, 0644);
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1300 if (fd < 0)
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1301 return NULL;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1302
209
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
1303 return _fdopen (fd, mode_save);
100
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1304 }
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1305
209
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
1306 /* This only works on NTFS volumes, but is useful to have. */
100
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1307 int
209
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
1308 sys_link (const char * old, const char * new)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
1309 {
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
1310 HANDLE fileh;
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
1311 int result = -1;
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
1312 char oldname[MAX_PATH], newname[MAX_PATH];
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
1313
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
1314 if (old == NULL || new == NULL)
100
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1315 {
209
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
1316 errno = ENOENT;
100
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1317 return -1;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1318 }
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1319
209
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
1320 strcpy (oldname, map_win32_filename (old, NULL));
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
1321 strcpy (newname, map_win32_filename (new, NULL));
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
1322
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
1323 fileh = CreateFile (oldname, 0, 0, NULL, OPEN_EXISTING,
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
1324 FILE_FLAG_BACKUP_SEMANTICS, NULL);
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
1325 if (fileh != INVALID_HANDLE_VALUE)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
1326 {
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
1327 int wlen;
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
1328
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
1329 /* Confusingly, the "alternate" stream name field does not apply
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
1330 when restoring a hard link, and instead contains the actual
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
1331 stream data for the link (ie. the name of the link to create).
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
1332 The WIN32_STREAM_ID structure before the cStreamName field is
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
1333 the stream header, which is then immediately followed by the
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
1334 stream data. */
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
1335
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
1336 struct {
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
1337 WIN32_STREAM_ID wid;
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
1338 WCHAR wbuffer[MAX_PATH]; /* extra space for link name */
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
1339 } data;
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
1340
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
1341 wlen = MultiByteToWideChar (CP_ACP, MB_PRECOMPOSED, newname, -1,
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
1342 data.wid.cStreamName, MAX_PATH);
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
1343 if (wlen > 0)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
1344 {
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
1345 LPVOID context = NULL;
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
1346 DWORD wbytes = 0;
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
1347
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
1348 data.wid.dwStreamId = BACKUP_LINK;
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
1349 data.wid.dwStreamAttributes = 0;
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
1350 data.wid.Size.LowPart = wlen * sizeof(WCHAR);
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
1351 data.wid.Size.HighPart = 0;
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
1352 data.wid.dwStreamNameSize = 0;
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
1353
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
1354 if (BackupWrite (fileh, (LPBYTE)&data,
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
1355 offsetof (WIN32_STREAM_ID, cStreamName)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
1356 + data.wid.Size.LowPart,
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
1357 &wbytes, FALSE, FALSE, &context)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
1358 && BackupWrite (fileh, NULL, 0, &wbytes, TRUE, FALSE, &context))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
1359 {
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
1360 /* succeeded */
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
1361 result = 0;
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
1362 }
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
1363 else
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
1364 {
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
1365 /* Should try mapping GetLastError to errno; for now just
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
1366 indicate a general error (eg. links not supported). */
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
1367 errno = EINVAL; // perhaps EMLINK?
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
1368 }
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
1369 }
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
1370
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
1371 CloseHandle (fileh);
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
1372 }
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
1373 else
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
1374 errno = ENOENT;
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
1375
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
1376 return result;
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
1377 }
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
1378
100
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1379 int
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1380 sys_mkdir (const char * path)
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1381 {
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1382 return _mkdir (map_win32_filename (path, NULL));
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1383 }
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1384
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1385 /* Because of long name mapping issues, we need to implement this
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1386 ourselves. Also, MSVC's _mktemp returns NULL when it can't generate
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1387 a unique name, instead of setting the input template to an empty
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1388 string.
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1389
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1390 Standard algorithm seems to be use pid or tid with a letter on the
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1391 front (in place of the 6 X's) and cycle through the letters to find a
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1392 unique name. We extend that to allow any reasonable character as the
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1393 first of the 6 X's. */
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1394 char *
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1395 sys_mktemp (char * template)
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1396 {
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1397 char * p;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1398 int i;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1399 unsigned uid = GetCurrentThreadId ();
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1400 static char first_char[] = "abcdefghijklmnopqrstuvwyz0123456789!%-_@#";
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1401
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1402 if (template == NULL)
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1403 return NULL;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1404 p = template + strlen (template);
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1405 i = 5;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1406 /* replace up to the last 5 X's with uid in decimal */
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1407 while (--p >= template && p[0] == 'X' && --i >= 0)
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1408 {
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1409 p[0] = '0' + uid % 10;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1410 uid /= 10;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1411 }
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1412
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1413 if (i < 0 && p[0] == 'X')
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1414 {
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1415 i = 0;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1416 do
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1417 {
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1418 int save_errno = errno;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1419 p[0] = first_char[i];
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1420 if (sys_access (template, 0) < 0)
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1421 {
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1422 errno = save_errno;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1423 return template;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1424 }
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1425 }
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1426 while (++i < sizeof (first_char));
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1427 }
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1428
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1429 /* Template is badly formed or else we can't generate a unique name,
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1430 so return empty string */
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1431 template[0] = 0;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1432 return template;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1433 }
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1434
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1435 int
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1436 sys_open (const char * path, int oflag, int mode)
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1437 {
239
41f2f0e326e9 Import from CVS: tag r20-5b18
cvs
parents: 223
diff changeset
1438 int fd;
211
78478c60bfcd Import from CVS: tag r20-4b4
cvs
parents: 209
diff changeset
1439
100
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1440 /* Force all file handles to be non-inheritable. */
211
78478c60bfcd Import from CVS: tag r20-4b4
cvs
parents: 209
diff changeset
1441 fd = _open (map_win32_filename (path, NULL), oflag | _O_NOINHERIT, mode);
239
41f2f0e326e9 Import from CVS: tag r20-5b18
cvs
parents: 223
diff changeset
1442
41f2f0e326e9 Import from CVS: tag r20-5b18
cvs
parents: 223
diff changeset
1443 if (fd >= MAXDESC)
41f2f0e326e9 Import from CVS: tag r20-5b18
cvs
parents: 223
diff changeset
1444 {
41f2f0e326e9 Import from CVS: tag r20-5b18
cvs
parents: 223
diff changeset
1445 _close (fd);
41f2f0e326e9 Import from CVS: tag r20-5b18
cvs
parents: 223
diff changeset
1446 errno = EMFILE;
41f2f0e326e9 Import from CVS: tag r20-5b18
cvs
parents: 223
diff changeset
1447 return -1;
41f2f0e326e9 Import from CVS: tag r20-5b18
cvs
parents: 223
diff changeset
1448 }
41f2f0e326e9 Import from CVS: tag r20-5b18
cvs
parents: 223
diff changeset
1449
211
78478c60bfcd Import from CVS: tag r20-4b4
cvs
parents: 209
diff changeset
1450 if (fd >= 0)
78478c60bfcd Import from CVS: tag r20-4b4
cvs
parents: 209
diff changeset
1451 {
78478c60bfcd Import from CVS: tag r20-4b4
cvs
parents: 209
diff changeset
1452 fd_info[fd].cp = 0;
78478c60bfcd Import from CVS: tag r20-4b4
cvs
parents: 209
diff changeset
1453 }
78478c60bfcd Import from CVS: tag r20-4b4
cvs
parents: 209
diff changeset
1454 return (fd);
100
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1455 }
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1456
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1457 int
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1458 sys_rename (const char * oldname, const char * newname)
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1459 {
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1460 char temp[MAX_PATH];
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1461 DWORD attr;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1462
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1463 /* MoveFile on Win95 doesn't correctly change the short file name
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1464 alias in a number of circumstances (it is not easy to predict when
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1465 just by looking at oldname and newname, unfortunately). In these
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1466 cases, renaming through a temporary name avoids the problem.
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1467
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1468 A second problem on Win95 is that renaming through a temp name when
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1469 newname is uppercase fails (the final long name ends up in
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1470 lowercase, although the short alias might be uppercase) UNLESS the
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1471 long temp name is not 8.3.
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1472
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1473 So, on Win95 we always rename through a temp name, and we make sure
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1474 the temp name has a long extension to ensure correct renaming. */
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1475
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1476 strcpy (temp, map_win32_filename (oldname, NULL));
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1477
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1478 if (GetVersion () & 0x80000000)
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1479 {
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1480 char * p;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1481
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1482 if (p = strrchr (temp, '\\'))
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1483 p++;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1484 else
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1485 p = temp;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1486 /* Force temp name to require a manufactured 8.3 alias - this
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1487 seems to make the second rename work properly. */
209
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
1488 strcpy (p, "_rename_temp.XXXXXX");
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
1489 sys_mktemp (temp);
100
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1490 if (rename (map_win32_filename (oldname, NULL), temp) < 0)
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1491 return -1;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1492 }
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1493
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1494 /* Emulate Unix behaviour - newname is deleted if it already exists
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1495 (at least if it is a file; don't do this for directories).
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1496 However, don't do this if we are just changing the case of the file
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1497 name - we will end up deleting the file we are trying to rename! */
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1498 newname = map_win32_filename (newname, NULL);
209
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
1499
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
1500 /* TODO: Use GetInformationByHandle (on NT) to ensure newname and temp
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
1501 do not refer to the same file, eg. through share aliases. */
100
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1502 if (stricmp (newname, temp) != 0
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1503 && (attr = GetFileAttributes (newname)) != -1
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1504 && (attr & FILE_ATTRIBUTE_DIRECTORY) == 0)
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1505 {
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1506 _chmod (newname, 0666);
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1507 _unlink (newname);
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1508 }
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1509
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1510 return rename (temp, newname);
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1511 }
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1512
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1513 int
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1514 sys_rmdir (const char * path)
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1515 {
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1516 return _rmdir (map_win32_filename (path, NULL));
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1517 }
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1518
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1519 int
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1520 sys_unlink (const char * path)
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1521 {
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1522 return _unlink (map_win32_filename (path, NULL));
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1523 }
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1524
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1525 static FILETIME utc_base_ft;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1526 static long double utc_base;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1527 static int init = 0;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1528
263
727739f917cb Import from CVS: tag r20-5b30
cvs
parents: 239
diff changeset
1529 time_t
100
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1530 convert_time (FILETIME ft)
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1531 {
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1532 long double ret;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1533
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1534 if (!init)
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1535 {
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1536 /* Determine the delta between 1-Jan-1601 and 1-Jan-1970. */
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1537 SYSTEMTIME st;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1538
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1539 st.wYear = 1970;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1540 st.wMonth = 1;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1541 st.wDay = 1;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1542 st.wHour = 0;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1543 st.wMinute = 0;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1544 st.wSecond = 0;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1545 st.wMilliseconds = 0;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1546
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1547 SystemTimeToFileTime (&st, &utc_base_ft);
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1548 utc_base = (long double) utc_base_ft.dwHighDateTime
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1549 * 4096 * 1024 * 1024 + utc_base_ft.dwLowDateTime;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1550 init = 1;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1551 }
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1552
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1553 if (CompareFileTime (&ft, &utc_base_ft) < 0)
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1554 return 0;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1555
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1556 ret = (long double) ft.dwHighDateTime * 4096 * 1024 * 1024 + ft.dwLowDateTime;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1557 ret -= utc_base;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1558 return (time_t) (ret * 1e-7);
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1559 }
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1560
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1561 #if 0
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1562 /* in case we ever have need of this */
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1563 void
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1564 convert_from_time_t (time_t time, FILETIME * pft)
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1565 {
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1566 long double tmp;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1567
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1568 if (!init)
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1569 {
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1570 /* Determine the delta between 1-Jan-1601 and 1-Jan-1970. */
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1571 SYSTEMTIME st;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1572
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1573 st.wYear = 1970;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1574 st.wMonth = 1;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1575 st.wDay = 1;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1576 st.wHour = 0;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1577 st.wMinute = 0;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1578 st.wSecond = 0;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1579 st.wMilliseconds = 0;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1580
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1581 SystemTimeToFileTime (&st, &utc_base_ft);
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1582 utc_base = (long double) utc_base_ft.dwHighDateTime
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1583 * 4096 * 1024 * 1024 + utc_base_ft.dwLowDateTime;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1584 init = 1;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1585 }
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1586
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1587 /* time in 100ns units since 1-Jan-1601 */
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1588 tmp = (long double) time * 1e7 + utc_base;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1589 pft->dwHighDateTime = (DWORD) (tmp / (4096.0 * 1024 * 1024));
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1590 pft->dwLowDateTime = (DWORD) (tmp - pft->dwHighDateTime);
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1591 }
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1592 #endif
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1593
209
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
1594 #if 0
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
1595 /* No reason to keep this; faking inode values either by hashing or even
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
1596 using the file index from GetInformationByHandle, is not perfect and
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
1597 so by default Emacs doesn't use the inode values on Windows.
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
1598 Instead, we now determine file-truename correctly (except for
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
1599 possible drive aliasing etc). */
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
1600
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
1601 /* Modified version of "PJW" algorithm (see the "Dragon" compiler book). */
100
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1602 static unsigned
209
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
1603 hashval (const unsigned char * str)
100
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1604 {
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1605 unsigned h = 0;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1606 while (*str)
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1607 {
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1608 h = (h << 4) + *str++;
209
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
1609 h ^= (h >> 28);
100
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1610 }
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1611 return h;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1612 }
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1613
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1614 /* Return the hash value of the canonical pathname, excluding the
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1615 drive/UNC header, to get a hopefully unique inode number. */
209
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
1616 static DWORD
100
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1617 generate_inode_val (const char * name)
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1618 {
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1619 char fullname[ MAX_PATH ];
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1620 char * p;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1621 unsigned hash;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1622
209
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
1623 /* Get the truly canonical filename, if it exists. (Note: this
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
1624 doesn't resolve aliasing due to subst commands, or recognise hard
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
1625 links. */
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
1626 if (!win32_get_long_filename ((char *)name, fullname, MAX_PATH))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
1627 abort ();
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
1628
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
1629 parse_root (fullname, &p);
100
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1630 /* Normal Win32 filesystems are still case insensitive. */
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1631 _strlwr (p);
209
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
1632 return hashval (p);
100
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1633 }
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1634
209
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
1635 #endif
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
1636
100
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1637 /* MSVC stat function can't cope with UNC names and has other bugs, so
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1638 replace it with our own. This also allows us to calculate consistent
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1639 inode values without hacks in the main Emacs code. */
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1640 int
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1641 stat (const char * path, struct stat * buf)
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1642 {
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1643 char * name;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1644 WIN32_FIND_DATA wfd;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1645 HANDLE fh;
209
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
1646 DWORD fake_inode;
100
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1647 int permission;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1648 int len;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1649 int rootdir = FALSE;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1650
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1651 if (path == NULL || buf == NULL)
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1652 {
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1653 errno = EFAULT;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1654 return -1;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1655 }
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1656
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1657 name = (char *) map_win32_filename (path, &path);
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1658 /* must be valid filename, no wild cards */
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1659 if (strchr (name, '*') || strchr (name, '?'))
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1660 {
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1661 errno = ENOENT;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1662 return -1;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1663 }
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1664
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1665 /* Remove trailing directory separator, unless name is the root
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1666 directory of a drive or UNC volume in which case ensure there
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1667 is a trailing separator. */
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1668 len = strlen (name);
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1669 rootdir = (path >= name + len - 1
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1670 && (IS_DIRECTORY_SEP (*path) || *path == 0));
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1671 name = strcpy (alloca (len + 2), name);
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1672
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1673 if (rootdir)
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1674 {
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1675 if (!IS_DIRECTORY_SEP (name[len-1]))
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1676 strcat (name, "\\");
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1677 if (GetDriveType (name) < 2)
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1678 {
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1679 errno = ENOENT;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1680 return -1;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1681 }
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1682 memset (&wfd, 0, sizeof (wfd));
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1683 wfd.dwFileAttributes = FILE_ATTRIBUTE_DIRECTORY;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1684 wfd.ftCreationTime = utc_base_ft;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1685 wfd.ftLastAccessTime = utc_base_ft;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1686 wfd.ftLastWriteTime = utc_base_ft;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1687 strcpy (wfd.cFileName, name);
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1688 }
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1689 else
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1690 {
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1691 if (IS_DIRECTORY_SEP (name[len-1]))
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1692 name[len - 1] = 0;
209
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
1693
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
1694 /* (This is hacky, but helps when doing file completions on
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
1695 network drives.) Optimize by using information available from
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
1696 active readdir if possible. */
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
1697 if (dir_find_handle != INVALID_HANDLE_VALUE &&
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
1698 (len = strlen (dir_pathname)),
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
1699 strnicmp (name, dir_pathname, len) == 0 &&
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
1700 IS_DIRECTORY_SEP (name[len]) &&
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
1701 stricmp (name + len + 1, dir_static.d_name) == 0)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
1702 {
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
1703 /* This was the last entry returned by readdir. */
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
1704 wfd = dir_find_data;
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
1705 }
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
1706 else
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
1707 {
100
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1708 fh = FindFirstFile (name, &wfd);
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1709 if (fh == INVALID_HANDLE_VALUE)
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1710 {
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1711 errno = ENOENT;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1712 return -1;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1713 }
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1714 FindClose (fh);
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1715 }
209
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
1716 }
100
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1717
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1718 if (wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1719 {
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1720 buf->st_mode = _S_IFDIR;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1721 buf->st_nlink = 2; /* doesn't really matter */
209
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
1722 fake_inode = 0; /* this doesn't either I think */
100
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1723 }
263
727739f917cb Import from CVS: tag r20-5b30
cvs
parents: 239
diff changeset
1724 else if (!NILP (Vmswindows_get_true_file_attributes))
100
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1725 {
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1726 /* This is more accurate in terms of gettting the correct number
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1727 of links, but is quite slow (it is noticable when Emacs is
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1728 making a list of file name completions). */
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1729 BY_HANDLE_FILE_INFORMATION info;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1730
209
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
1731 /* No access rights required to get info. */
211
78478c60bfcd Import from CVS: tag r20-4b4
cvs
parents: 209
diff changeset
1732 fh = CreateFile (name, 0, FILE_SHARE_READ|FILE_SHARE_WRITE, NULL,
78478c60bfcd Import from CVS: tag r20-4b4
cvs
parents: 209
diff changeset
1733 OPEN_EXISTING, 0, NULL);
100
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1734
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1735 if (GetFileInformationByHandle (fh, &info))
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1736 {
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1737 switch (GetFileType (fh))
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1738 {
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1739 case FILE_TYPE_DISK:
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1740 buf->st_mode = _S_IFREG;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1741 break;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1742 case FILE_TYPE_PIPE:
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1743 buf->st_mode = _S_IFIFO;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1744 break;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1745 case FILE_TYPE_CHAR:
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1746 case FILE_TYPE_UNKNOWN:
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1747 default:
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1748 buf->st_mode = _S_IFCHR;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1749 }
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1750 buf->st_nlink = info.nNumberOfLinks;
209
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
1751 /* Might as well use file index to fake inode values, but this
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
1752 is not guaranteed to be unique unless we keep a handle open
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
1753 all the time (even then there are situations where it is
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
1754 not unique). Reputedly, there are at most 48 bits of info
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
1755 (on NTFS, presumably less on FAT). */
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
1756 fake_inode = info.nFileIndexLow ^ info.nFileIndexHigh;
100
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1757 CloseHandle (fh);
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1758 }
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1759 else
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1760 {
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1761 errno = EACCES;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1762 return -1;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1763 }
209
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
1764 }
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
1765 else
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
1766 {
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
1767 /* Don't bother to make this information more accurate. */
100
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1768 buf->st_mode = _S_IFREG;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1769 buf->st_nlink = 1;
209
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
1770 fake_inode = 0;
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
1771 }
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
1772
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
1773 #if 0
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
1774 /* Not sure if there is any point in this. */
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
1775 if (!NILP (Vwin32_generate_fake_inodes))
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
1776 fake_inode = generate_inode_val (name);
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
1777 else if (fake_inode == 0)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
1778 {
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
1779 /* For want of something better, try to make everything unique. */
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
1780 static DWORD gen_num = 0;
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
1781 fake_inode = ++gen_num;
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
1782 }
100
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1783 #endif
209
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
1784
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
1785 /* MSVC defines _ino_t to be short; other libc's might not. */
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
1786 if (sizeof (buf->st_ino) == 2)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
1787 buf->st_ino = fake_inode ^ (fake_inode >> 16);
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
1788 else
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
1789 buf->st_ino = fake_inode;
100
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1790
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1791 /* consider files to belong to current user */
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1792 buf->st_uid = the_passwd.pw_uid;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1793 buf->st_gid = the_passwd.pw_gid;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1794
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1795 /* volume_info is set indirectly by map_win32_filename */
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1796 buf->st_dev = volume_info.serialnum;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1797 buf->st_rdev = volume_info.serialnum;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1798
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1799
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1800 buf->st_size = wfd.nFileSizeLow;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1801
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1802 /* Convert timestamps to Unix format. */
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1803 buf->st_mtime = convert_time (wfd.ftLastWriteTime);
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1804 buf->st_atime = convert_time (wfd.ftLastAccessTime);
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1805 if (buf->st_atime == 0) buf->st_atime = buf->st_mtime;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1806 buf->st_ctime = convert_time (wfd.ftCreationTime);
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1807 if (buf->st_ctime == 0) buf->st_ctime = buf->st_mtime;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1808
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1809 /* determine rwx permissions */
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1810 if (wfd.dwFileAttributes & FILE_ATTRIBUTE_READONLY)
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1811 permission = _S_IREAD;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1812 else
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1813 permission = _S_IREAD | _S_IWRITE;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1814
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1815 if (wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1816 permission |= _S_IEXEC;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1817 else
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1818 {
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1819 char * p = strrchr (name, '.');
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1820 if (p != NULL &&
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1821 (stricmp (p, ".exe") == 0 ||
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1822 stricmp (p, ".com") == 0 ||
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1823 stricmp (p, ".bat") == 0 ||
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1824 stricmp (p, ".cmd") == 0))
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1825 permission |= _S_IEXEC;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1826 }
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1827
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1828 buf->st_mode |= permission | (permission >> 3) | (permission >> 6);
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1829
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1830 return 0;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1831 }
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1832
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1833 #ifdef HAVE_SOCKETS
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1834
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1835 /* Wrappers for winsock functions to map between our file descriptors
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1836 and winsock's handles; also set h_errno for convenience.
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1837
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1838 To allow Emacs to run on systems which don't have winsock support
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1839 installed, we dynamically link to winsock on startup if present, and
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1840 otherwise provide the minimum necessary functionality
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1841 (eg. gethostname). */
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1842
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1843 /* function pointers for relevant socket functions */
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1844 int (PASCAL *pfn_WSAStartup) (WORD wVersionRequired, LPWSADATA lpWSAData);
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1845 void (PASCAL *pfn_WSASetLastError) (int iError);
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1846 int (PASCAL *pfn_WSAGetLastError) (void);
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1847 int (PASCAL *pfn_socket) (int af, int type, int protocol);
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1848 int (PASCAL *pfn_bind) (SOCKET s, const struct sockaddr *addr, int namelen);
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1849 int (PASCAL *pfn_connect) (SOCKET s, const struct sockaddr *addr, int namelen);
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1850 int (PASCAL *pfn_ioctlsocket) (SOCKET s, long cmd, u_long *argp);
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1851 int (PASCAL *pfn_recv) (SOCKET s, char * buf, int len, int flags);
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1852 int (PASCAL *pfn_send) (SOCKET s, const char * buf, int len, int flags);
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1853 int (PASCAL *pfn_closesocket) (SOCKET s);
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1854 int (PASCAL *pfn_shutdown) (SOCKET s, int how);
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1855 int (PASCAL *pfn_WSACleanup) (void);
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1856
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1857 u_short (PASCAL *pfn_htons) (u_short hostshort);
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1858 u_short (PASCAL *pfn_ntohs) (u_short netshort);
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1859 unsigned long (PASCAL *pfn_inet_addr) (const char * cp);
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1860 int (PASCAL *pfn_gethostname) (char * name, int namelen);
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1861 struct hostent * (PASCAL *pfn_gethostbyname) (const char * name);
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1862 struct servent * (PASCAL *pfn_getservbyname) (const char * name, const char * proto);
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1863
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1864 /* SetHandleInformation is only needed to make sockets non-inheritable. */
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1865 BOOL (WINAPI *pfn_SetHandleInformation) (HANDLE object, DWORD mask, DWORD flags);
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1866 #ifndef HANDLE_FLAG_INHERIT
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1867 #define HANDLE_FLAG_INHERIT 1
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1868 #endif
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1869
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1870 HANDLE winsock_lib;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1871 static int winsock_inuse;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1872
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1873 BOOL
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1874 term_winsock (void)
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1875 {
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1876 if (winsock_lib != NULL && winsock_inuse == 0)
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1877 {
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1878 /* Not sure what would cause WSAENETDOWN, or even if it can happen
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1879 after WSAStartup returns successfully, but it seems reasonable
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1880 to allow unloading winsock anyway in that case. */
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1881 if (pfn_WSACleanup () == 0 ||
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1882 pfn_WSAGetLastError () == WSAENETDOWN)
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1883 {
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1884 if (FreeLibrary (winsock_lib))
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1885 winsock_lib = NULL;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1886 return TRUE;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1887 }
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1888 }
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1889 return FALSE;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1890 }
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1891
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1892 BOOL
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1893 init_winsock (int load_now)
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1894 {
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1895 WSADATA winsockData;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1896
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1897 if (winsock_lib != NULL)
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1898 return TRUE;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1899
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1900 pfn_SetHandleInformation = NULL;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1901 pfn_SetHandleInformation
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1902 = (void *) GetProcAddress (GetModuleHandle ("kernel32.dll"),
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1903 "SetHandleInformation");
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1904
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1905 winsock_lib = LoadLibrary ("wsock32.dll");
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1906
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1907 if (winsock_lib != NULL)
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1908 {
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1909 /* dynamically link to socket functions */
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1910
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1911 #define LOAD_PROC(fn) \
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1912 if ((pfn_##fn = (void *) GetProcAddress (winsock_lib, #fn)) == NULL) \
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1913 goto fail;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1914
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1915 LOAD_PROC( WSAStartup );
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1916 LOAD_PROC( WSASetLastError );
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1917 LOAD_PROC( WSAGetLastError );
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1918 LOAD_PROC( socket );
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1919 LOAD_PROC( bind );
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1920 LOAD_PROC( connect );
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1921 LOAD_PROC( ioctlsocket );
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1922 LOAD_PROC( recv );
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1923 LOAD_PROC( send );
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1924 LOAD_PROC( closesocket );
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1925 LOAD_PROC( shutdown );
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1926 LOAD_PROC( htons );
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1927 LOAD_PROC( ntohs );
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1928 LOAD_PROC( inet_addr );
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1929 LOAD_PROC( gethostname );
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1930 LOAD_PROC( gethostbyname );
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1931 LOAD_PROC( getservbyname );
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1932 LOAD_PROC( WSACleanup );
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1933
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1934 #undef LOAD_PROC
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1935
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1936 /* specify version 1.1 of winsock */
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1937 if (pfn_WSAStartup (0x101, &winsockData) == 0)
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1938 {
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1939 if (winsockData.wVersion != 0x101)
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1940 goto fail;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1941
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1942 if (!load_now)
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1943 {
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1944 /* Report that winsock exists and is usable, but leave
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1945 socket functions disabled. I am assuming that calling
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1946 WSAStartup does not require any network interaction,
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1947 and in particular does not cause or require a dial-up
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1948 connection to be established. */
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1949
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1950 pfn_WSACleanup ();
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1951 FreeLibrary (winsock_lib);
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1952 winsock_lib = NULL;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1953 }
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1954 winsock_inuse = 0;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1955 return TRUE;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1956 }
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1957
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1958 fail:
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1959 FreeLibrary (winsock_lib);
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1960 winsock_lib = NULL;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1961 }
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1962
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1963 return FALSE;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1964 }
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1965
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1966
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1967 int h_errno = 0;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1968
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1969 /* function to set h_errno for compatability; map winsock error codes to
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1970 normal system codes where they overlap (non-overlapping definitions
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1971 are already in <sys/socket.h> */
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1972 static void set_errno ()
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1973 {
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1974 if (winsock_lib == NULL)
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1975 h_errno = EINVAL;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1976 else
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1977 h_errno = pfn_WSAGetLastError ();
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1978
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1979 switch (h_errno)
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1980 {
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1981 case WSAEACCES: h_errno = EACCES; break;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1982 case WSAEBADF: h_errno = EBADF; break;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1983 case WSAEFAULT: h_errno = EFAULT; break;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1984 case WSAEINTR: h_errno = EINTR; break;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1985 case WSAEINVAL: h_errno = EINVAL; break;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1986 case WSAEMFILE: h_errno = EMFILE; break;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1987 case WSAENAMETOOLONG: h_errno = ENAMETOOLONG; break;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1988 case WSAENOTEMPTY: h_errno = ENOTEMPTY; break;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1989 }
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1990 errno = h_errno;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1991 }
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1992
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1993 static void check_errno ()
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1994 {
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1995 if (h_errno == 0 && winsock_lib != NULL)
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1996 pfn_WSASetLastError (0);
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1997 }
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1998
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
1999 /* [andrewi 3-May-96] I've had conflicting results using both methods,
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2000 but I believe the method of keeping the socket handle separate (and
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2001 insuring it is not inheritable) is the correct one. */
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2002
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2003 //#define SOCK_REPLACE_HANDLE
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2004
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2005 #ifdef SOCK_REPLACE_HANDLE
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2006 #define SOCK_HANDLE(fd) ((SOCKET) _get_osfhandle (fd))
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2007 #else
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2008 #define SOCK_HANDLE(fd) ((SOCKET) fd_info[fd].hnd)
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2009 #endif
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2010
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2011 int
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2012 sys_socket(int af, int type, int protocol)
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2013 {
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2014 int fd;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2015 long s;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2016 child_process * cp;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2017
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2018 if (winsock_lib == NULL)
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2019 {
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2020 h_errno = ENETDOWN;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2021 return INVALID_SOCKET;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2022 }
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2023
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2024 check_errno ();
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2025
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2026 /* call the real socket function */
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2027 s = (long) pfn_socket (af, type, protocol);
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2028
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2029 if (s != INVALID_SOCKET)
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2030 {
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2031 /* Although under NT 3.5 _open_osfhandle will accept a socket
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2032 handle, if opened with SO_OPENTYPE == SO_SYNCHRONOUS_NONALERT,
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2033 that does not work under NT 3.1. However, we can get the same
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2034 effect by using a backdoor function to replace an existing
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2035 descriptor handle with the one we want. */
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2036
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2037 /* allocate a file descriptor (with appropriate flags) */
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2038 fd = _open ("NUL:", _O_RDWR);
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2039 if (fd >= 0)
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2040 {
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2041 #ifdef SOCK_REPLACE_HANDLE
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2042 /* now replace handle to NUL with our socket handle */
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2043 CloseHandle ((HANDLE) _get_osfhandle (fd));
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2044 _free_osfhnd (fd);
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2045 _set_osfhnd (fd, s);
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2046 /* setmode (fd, _O_BINARY); */
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2047 #else
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2048 /* Make a non-inheritable copy of the socket handle. */
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2049 {
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2050 HANDLE parent;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2051 HANDLE new_s = INVALID_HANDLE_VALUE;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2052
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2053 parent = GetCurrentProcess ();
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2054
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2055 /* Apparently there is a bug in NT 3.51 with some service
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2056 packs, which prevents using DuplicateHandle to make a
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2057 socket handle non-inheritable (causes WSACleanup to
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2058 hang). The work-around is to use SetHandleInformation
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2059 instead if it is available and implemented. */
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2060 if (!pfn_SetHandleInformation
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2061 || !pfn_SetHandleInformation ((HANDLE) s,
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2062 HANDLE_FLAG_INHERIT,
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2063 HANDLE_FLAG_INHERIT))
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2064 {
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2065 DuplicateHandle (parent,
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2066 (HANDLE) s,
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2067 parent,
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2068 &new_s,
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2069 0,
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2070 FALSE,
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2071 DUPLICATE_SAME_ACCESS);
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2072 pfn_closesocket (s);
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2073 s = (SOCKET) new_s;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2074 }
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2075 fd_info[fd].hnd = (HANDLE) s;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2076 }
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2077 #endif
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2078
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2079 /* set our own internal flags */
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2080 fd_info[fd].flags = FILE_SOCKET | FILE_BINARY | FILE_READ | FILE_WRITE;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2081
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2082 cp = new_child ();
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2083 if (cp)
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2084 {
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2085 cp->fd = fd;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2086 cp->status = STATUS_READ_ACKNOWLEDGED;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2087
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2088 /* attach child_process to fd_info */
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2089 if (fd_info[ fd ].cp != NULL)
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2090 {
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2091 DebPrint (("sys_socket: fd_info[%d] apparently in use!\n", fd));
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2092 abort ();
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2093 }
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2094
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2095 fd_info[ fd ].cp = cp;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2096
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2097 /* success! */
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2098 winsock_inuse++; /* count open sockets */
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2099 return fd;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2100 }
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2101
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2102 /* clean up */
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2103 _close (fd);
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2104 }
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2105 pfn_closesocket (s);
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2106 h_errno = EMFILE;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2107 }
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2108 set_errno ();
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2109
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2110 return -1;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2111 }
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2112
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2113
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2114 int
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2115 sys_bind (int s, const struct sockaddr * addr, int namelen)
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2116 {
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2117 if (winsock_lib == NULL)
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2118 {
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2119 h_errno = ENOTSOCK;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2120 return SOCKET_ERROR;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2121 }
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2122
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2123 check_errno ();
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2124 if (fd_info[s].flags & FILE_SOCKET)
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2125 {
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2126 int rc = pfn_bind (SOCK_HANDLE (s), addr, namelen);
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2127 if (rc == SOCKET_ERROR)
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2128 set_errno ();
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2129 return rc;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2130 }
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2131 h_errno = ENOTSOCK;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2132 return SOCKET_ERROR;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2133 }
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2134
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2135
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2136 int
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2137 sys_connect (int s, const struct sockaddr * name, int namelen)
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2138 {
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2139 if (winsock_lib == NULL)
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2140 {
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2141 h_errno = ENOTSOCK;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2142 return SOCKET_ERROR;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2143 }
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2144
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2145 check_errno ();
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2146 if (fd_info[s].flags & FILE_SOCKET)
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2147 {
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2148 int rc = pfn_connect (SOCK_HANDLE (s), name, namelen);
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2149 if (rc == SOCKET_ERROR)
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2150 set_errno ();
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2151 return rc;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2152 }
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2153 h_errno = ENOTSOCK;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2154 return SOCKET_ERROR;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2155 }
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2156
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2157 u_short
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2158 sys_htons (u_short hostshort)
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2159 {
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2160 return (winsock_lib != NULL) ?
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2161 pfn_htons (hostshort) : hostshort;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2162 }
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2163
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2164 u_short
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2165 sys_ntohs (u_short netshort)
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2166 {
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2167 return (winsock_lib != NULL) ?
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2168 pfn_ntohs (netshort) : netshort;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2169 }
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2170
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2171 unsigned long
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2172 sys_inet_addr (const char * cp)
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2173 {
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2174 return (winsock_lib != NULL) ?
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2175 pfn_inet_addr (cp) : INADDR_NONE;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2176 }
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2177
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2178 int
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2179 sys_gethostname (char * name, int namelen)
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2180 {
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2181 if (winsock_lib != NULL)
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2182 return pfn_gethostname (name, namelen);
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2183
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2184 if (namelen > MAX_COMPUTERNAME_LENGTH)
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2185 return !GetComputerName (name, &namelen);
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2186
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2187 h_errno = EFAULT;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2188 return SOCKET_ERROR;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2189 }
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2190
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2191 struct hostent *
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2192 sys_gethostbyname(const char * name)
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2193 {
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2194 struct hostent * host;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2195
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2196 if (winsock_lib == NULL)
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2197 {
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2198 h_errno = ENETDOWN;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2199 return NULL;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2200 }
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2201
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2202 check_errno ();
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2203 host = pfn_gethostbyname (name);
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2204 if (!host)
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2205 set_errno ();
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2206 return host;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2207 }
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2208
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2209 struct servent *
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2210 sys_getservbyname(const char * name, const char * proto)
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2211 {
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2212 struct servent * serv;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2213
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2214 if (winsock_lib == NULL)
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2215 {
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2216 h_errno = ENETDOWN;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2217 return NULL;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2218 }
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2219
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2220 check_errno ();
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2221 serv = pfn_getservbyname (name, proto);
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2222 if (!serv)
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2223 set_errno ();
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2224 return serv;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2225 }
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2226
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2227 #endif /* HAVE_SOCKETS */
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2228
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2229
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2230 /* Shadow main io functions: we need to handle pipes and sockets more
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2231 intelligently, and implement non-blocking mode as well. */
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2232
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2233 int
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2234 sys_close (int fd)
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2235 {
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2236 int rc;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2237
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2238 if (fd < 0 || fd >= MAXDESC)
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2239 {
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2240 errno = EBADF;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2241 return -1;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2242 }
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2243
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2244 if (fd_info[fd].cp)
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2245 {
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2246 child_process * cp = fd_info[fd].cp;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2247
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2248 fd_info[fd].cp = NULL;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2249
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2250 if (CHILD_ACTIVE (cp))
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2251 {
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2252 /* if last descriptor to active child_process then cleanup */
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2253 int i;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2254 for (i = 0; i < MAXDESC; i++)
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2255 {
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2256 if (i == fd)
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2257 continue;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2258 if (fd_info[i].cp == cp)
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2259 break;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2260 }
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2261 if (i == MAXDESC)
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2262 {
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2263 #ifdef HAVE_SOCKETS
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2264 if (fd_info[fd].flags & FILE_SOCKET)
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2265 {
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2266 #ifndef SOCK_REPLACE_HANDLE
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2267 if (winsock_lib == NULL) abort ();
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2268
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2269 pfn_shutdown (SOCK_HANDLE (fd), 2);
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2270 rc = pfn_closesocket (SOCK_HANDLE (fd));
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2271 #endif
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2272 winsock_inuse--; /* count open sockets */
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2273 }
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2274 #endif
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2275 delete_child (cp);
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2276 }
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2277 }
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2278 }
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2279
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2280 /* Note that sockets do not need special treatment here (at least on
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2281 NT and Win95 using the standard tcp/ip stacks) - it appears that
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2282 closesocket is equivalent to CloseHandle, which is to be expected
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2283 because socket handles are fully fledged kernel handles. */
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2284 rc = _close (fd);
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2285
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2286 if (rc == 0)
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2287 fd_info[fd].flags = 0;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2288
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2289 return rc;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2290 }
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2291
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2292 int
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2293 sys_dup (int fd)
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2294 {
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2295 int new_fd;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2296
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2297 new_fd = _dup (fd);
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2298 if (new_fd >= 0)
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2299 {
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2300 /* duplicate our internal info as well */
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2301 fd_info[new_fd] = fd_info[fd];
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2302 }
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2303 return new_fd;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2304 }
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2305
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2306
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2307 int
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2308 sys_dup2 (int src, int dst)
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2309 {
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2310 int rc;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2311
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2312 if (dst < 0 || dst >= MAXDESC)
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2313 {
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2314 errno = EBADF;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2315 return -1;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2316 }
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2317
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2318 /* make sure we close the destination first if it's a pipe or socket */
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2319 if (src != dst && fd_info[dst].flags != 0)
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2320 sys_close (dst);
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2321
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2322 rc = _dup2 (src, dst);
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2323 if (rc == 0)
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2324 {
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2325 /* duplicate our internal info as well */
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2326 fd_info[dst] = fd_info[src];
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2327 }
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2328 return rc;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2329 }
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2330
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2331 /* From callproc.c */
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2332 extern Lisp_Object Vbinary_process_input;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2333 extern Lisp_Object Vbinary_process_output;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2334
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2335 /* Unix pipe() has only one arg */
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2336 int
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2337 sys_pipe (int * phandles)
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2338 {
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2339 int rc;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2340 unsigned flags;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2341 child_process * cp;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2342
209
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
2343 /* make pipe handles non-inheritable; when we spawn a child, we
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
2344 replace the relevant handle with an inheritable one. Also put
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
2345 pipes into binary mode; we will do text mode translation ourselves
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
2346 if required. */
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
2347 rc = _pipe (phandles, 0, _O_NOINHERIT | _O_BINARY);
100
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2348
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2349 if (rc == 0)
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2350 {
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2351 flags = FILE_PIPE | FILE_READ;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2352 if (!NILP (Vbinary_process_output))
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2353 flags |= FILE_BINARY;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2354 fd_info[phandles[0]].flags = flags;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2355
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2356 flags = FILE_PIPE | FILE_WRITE;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2357 if (!NILP (Vbinary_process_input))
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2358 flags |= FILE_BINARY;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2359 fd_info[phandles[1]].flags = flags;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2360 }
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2361
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2362 return rc;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2363 }
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2364
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2365 /* From ntproc.c */
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2366 extern Lisp_Object Vwin32_pipe_read_delay;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2367
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2368 /* Function to do blocking read of one byte, needed to implement
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2369 select. It is only allowed on sockets and pipes. */
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2370 int
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2371 _sys_read_ahead (int fd)
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2372 {
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2373 child_process * cp;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2374 int rc;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2375
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2376 if (fd < 0 || fd >= MAXDESC)
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2377 return STATUS_READ_ERROR;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2378
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2379 cp = fd_info[fd].cp;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2380
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2381 if (cp == NULL || cp->fd != fd || cp->status != STATUS_READ_READY)
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2382 return STATUS_READ_ERROR;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2383
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2384 if ((fd_info[fd].flags & (FILE_PIPE | FILE_SOCKET)) == 0
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2385 || (fd_info[fd].flags & FILE_READ) == 0)
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2386 {
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2387 DebPrint (("_sys_read_ahead: internal error: fd %d is not a pipe or socket!\n", fd));
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2388 abort ();
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2389 }
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2390
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2391 cp->status = STATUS_READ_IN_PROGRESS;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2392
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2393 if (fd_info[fd].flags & FILE_PIPE)
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2394 {
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2395 rc = _read (fd, &cp->chr, sizeof (char));
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2396
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2397 /* Give subprocess time to buffer some more output for us before
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2398 reporting that input is available; we need this because Win95
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2399 connects DOS programs to pipes by making the pipe appear to be
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2400 the normal console stdout - as a result most DOS programs will
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2401 write to stdout without buffering, ie. one character at a
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2402 time. Even some Win32 programs do this - "dir" in a command
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2403 shell on NT is very slow if we don't do this. */
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2404 if (rc > 0)
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2405 {
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2406 int wait = XINT (Vwin32_pipe_read_delay);
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2407
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2408 if (wait > 0)
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2409 Sleep (wait);
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2410 else if (wait < 0)
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2411 while (++wait <= 0)
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2412 /* Yield remainder of our time slice, effectively giving a
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2413 temporary priority boost to the child process. */
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2414 Sleep (0);
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2415 }
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2416 }
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2417 #ifdef HAVE_SOCKETS
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2418 else if (fd_info[fd].flags & FILE_SOCKET)
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2419 rc = pfn_recv (SOCK_HANDLE (fd), &cp->chr, sizeof (char), 0);
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2420 #endif
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2421
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2422 if (rc == sizeof (char))
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2423 cp->status = STATUS_READ_SUCCEEDED;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2424 else
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2425 cp->status = STATUS_READ_FAILED;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2426
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2427 return cp->status;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2428 }
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2429
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2430 int
272
c5d627a313b1 Import from CVS: tag r21-0b34
cvs
parents: 263
diff changeset
2431 sys_read (int fd, char * buffer, size_t count)
100
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2432 {
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2433 int nchars;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2434 int to_read;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2435 DWORD waiting;
209
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
2436 char * orig_buffer = buffer;
100
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2437
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2438 if (fd < 0 || fd >= MAXDESC)
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2439 {
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2440 errno = EBADF;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2441 return -1;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2442 }
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2443
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2444 if (fd_info[fd].flags & (FILE_PIPE | FILE_SOCKET))
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2445 {
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2446 child_process *cp = fd_info[fd].cp;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2447
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2448 if ((fd_info[fd].flags & FILE_READ) == 0)
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2449 {
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2450 errno = EBADF;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2451 return -1;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2452 }
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2453
209
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
2454 nchars = 0;
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
2455
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
2456 /* re-read CR carried over from last read */
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
2457 if (fd_info[fd].flags & FILE_LAST_CR)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
2458 {
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
2459 if (fd_info[fd].flags & FILE_BINARY) abort ();
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
2460 *buffer++ = 0x0d;
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
2461 count--;
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
2462 nchars++;
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
2463 fd_info[fd].flags &= ~FILE_LAST_CR;
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
2464 }
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
2465
100
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2466 /* presence of a child_process structure means we are operating in
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2467 non-blocking mode - otherwise we just call _read directly.
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2468 Note that the child_process structure might be missing because
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2469 reap_subprocess has been called; in this case the pipe is
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2470 already broken, so calling _read on it is okay. */
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2471 if (cp)
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2472 {
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2473 int current_status = cp->status;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2474
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2475 switch (current_status)
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2476 {
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2477 case STATUS_READ_FAILED:
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2478 case STATUS_READ_ERROR:
209
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
2479 /* report normal EOF if nothing in buffer */
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
2480 if (nchars <= 0)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
2481 fd_info[fd].flags |= FILE_AT_EOF;
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
2482 return nchars;
100
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2483
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2484 case STATUS_READ_READY:
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2485 case STATUS_READ_IN_PROGRESS:
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2486 DebPrint (("sys_read called when read is in progress\n"));
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2487 errno = EWOULDBLOCK;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2488 return -1;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2489
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2490 case STATUS_READ_SUCCEEDED:
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2491 /* consume read-ahead char */
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2492 *buffer++ = cp->chr;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2493 count--;
209
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
2494 nchars++;
100
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2495 cp->status = STATUS_READ_ACKNOWLEDGED;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2496 ResetEvent (cp->char_avail);
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2497
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2498 case STATUS_READ_ACKNOWLEDGED:
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2499 break;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2500
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2501 default:
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2502 DebPrint (("sys_read: bad status %d\n", current_status));
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2503 errno = EBADF;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2504 return -1;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2505 }
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2506
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2507 if (fd_info[fd].flags & FILE_PIPE)
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2508 {
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2509 PeekNamedPipe ((HANDLE) _get_osfhandle (fd), NULL, 0, NULL, &waiting, NULL);
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2510 to_read = min (waiting, (DWORD) count);
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2511
209
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
2512 if (to_read > 0)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
2513 nchars += _read (fd, buffer, to_read);
100
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2514 }
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2515 #ifdef HAVE_SOCKETS
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2516 else /* FILE_SOCKET */
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2517 {
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2518 if (winsock_lib == NULL) abort ();
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2519
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2520 /* do the equivalent of a non-blocking read */
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2521 pfn_ioctlsocket (SOCK_HANDLE (fd), FIONREAD, &waiting);
209
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
2522 if (waiting == 0 && nchars == 0)
100
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2523 {
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2524 h_errno = errno = EWOULDBLOCK;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2525 return -1;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2526 }
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2527
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2528 if (waiting)
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2529 {
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2530 /* always use binary mode for sockets */
209
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
2531 int res = pfn_recv (SOCK_HANDLE (fd), buffer, count, 0);
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
2532 if (res == SOCKET_ERROR)
100
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2533 {
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2534 DebPrint(("sys_read.recv failed with error %d on socket %ld\n",
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2535 pfn_WSAGetLastError (), SOCK_HANDLE (fd)));
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2536 set_errno ();
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2537 return -1;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2538 }
209
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
2539 nchars += res;
100
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2540 }
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2541 }
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2542 #endif
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2543 }
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2544 else
209
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
2545 {
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
2546 int nread = _read (fd, buffer, count);
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
2547 if (nread >= 0)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
2548 nchars += nread;
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
2549 else if (nchars == 0)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
2550 nchars = nread;
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
2551 }
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
2552
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
2553 if (nchars <= 0)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
2554 fd_info[fd].flags |= FILE_AT_EOF;
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
2555 /* Perform text mode translation if required. */
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
2556 else if ((fd_info[fd].flags & FILE_BINARY) == 0)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
2557 {
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
2558 unsigned lf_count = 0;
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
2559 nchars = crlf_to_lf (nchars, orig_buffer, &lf_count);
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
2560 /* If buffer contains only CR, return that. To be absolutely
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
2561 sure we should attempt to read the next char, but in
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
2562 practice a CR to be followed by LF would not appear by
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
2563 itself in the buffer. */
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
2564 if (nchars > 1 && orig_buffer[nchars - 1] == 0x0d)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
2565 {
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
2566 fd_info[fd].flags |= FILE_LAST_CR;
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
2567 nchars--;
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
2568 }
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
2569 }
100
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2570 }
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2571 else
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2572 nchars = _read (fd, buffer, count);
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2573
209
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
2574 return nchars;
100
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2575 }
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2576
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2577 /* For now, don't bother with a non-blocking mode */
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2578 int
272
c5d627a313b1 Import from CVS: tag r21-0b34
cvs
parents: 263
diff changeset
2579 sys_write (int fd, const void * buffer, size_t count)
100
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2580 {
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2581 int nchars;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2582
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2583 if (fd < 0 || fd >= MAXDESC)
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2584 {
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2585 errno = EBADF;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2586 return -1;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2587 }
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2588
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2589 if (fd_info[fd].flags & (FILE_PIPE | FILE_SOCKET))
209
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
2590 {
100
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2591 if ((fd_info[fd].flags & FILE_WRITE) == 0)
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2592 {
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2593 errno = EBADF;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2594 return -1;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2595 }
209
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
2596
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
2597 /* Perform text mode translation if required. */
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
2598 if ((fd_info[fd].flags & FILE_BINARY) == 0)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
2599 {
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
2600 char * tmpbuf = alloca (count * 2);
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
2601 unsigned char * src = (void *)buffer;
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
2602 unsigned char * dst = tmpbuf;
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
2603 int nbytes = count;
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
2604
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
2605 while (1)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
2606 {
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
2607 unsigned char *next;
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
2608 /* copy next line or remaining bytes */
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
2609 next = _memccpy (dst, src, '\n', nbytes);
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
2610 if (next)
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
2611 {
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
2612 /* copied one line ending with '\n' */
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
2613 int copied = next - dst;
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
2614 nbytes -= copied;
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
2615 src += copied;
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
2616 /* insert '\r' before '\n' */
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
2617 next[-1] = '\r';
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
2618 next[0] = '\n';
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
2619 dst = next + 1;
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
2620 count++;
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
2621 }
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
2622 else
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
2623 /* copied remaining partial line -> now finished */
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
2624 break;
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
2625 }
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
2626 buffer = tmpbuf;
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
2627 }
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
2628 }
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
2629
100
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2630 #ifdef HAVE_SOCKETS
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2631 if (fd_info[fd].flags & FILE_SOCKET)
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2632 {
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2633 if (winsock_lib == NULL) abort ();
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2634 nchars = pfn_send (SOCK_HANDLE (fd), buffer, count, 0);
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2635 if (nchars == SOCKET_ERROR)
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2636 {
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2637 DebPrint(("sys_read.send failed with error %d on socket %ld\n",
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2638 pfn_WSAGetLastError (), SOCK_HANDLE (fd)));
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2639 set_errno ();
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2640 }
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2641 }
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2642 else
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2643 #endif
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2644 nchars = _write (fd, buffer, count);
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2645
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2646 return nchars;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2647 }
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2648
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2649
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2650 void
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2651 term_ntproc ()
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2652 {
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2653 #ifdef HAVE_SOCKETS
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2654 /* shutdown the socket interface if necessary */
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2655 term_winsock ();
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2656 #endif
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2657 }
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2658
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2659 void
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2660 init_ntproc ()
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2661 {
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2662 #ifdef HAVE_SOCKETS
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2663 /* Initialise the socket interface now if available and requested by
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2664 the user by defining PRELOAD_WINSOCK; otherwise loading will be
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2665 delayed until open-network-stream is called (win32-has-winsock can
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2666 also be used to dynamically load or reload winsock).
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2667
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2668 Conveniently, init_environment is called before us, so
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2669 PRELOAD_WINSOCK can be set in the registry. */
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2670
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2671 /* Always initialize this correctly. */
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2672 winsock_lib = NULL;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2673
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2674 if (getenv ("PRELOAD_WINSOCK") != NULL)
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2675 init_winsock (TRUE);
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2676 #endif
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2677
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2678 /* Initial preparation for subprocess support: replace our standard
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2679 handles with non-inheritable versions. */
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2680 {
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2681 HANDLE parent;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2682 HANDLE stdin_save = INVALID_HANDLE_VALUE;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2683 HANDLE stdout_save = INVALID_HANDLE_VALUE;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2684 HANDLE stderr_save = INVALID_HANDLE_VALUE;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2685
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2686 parent = GetCurrentProcess ();
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2687
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2688 /* ignore errors when duplicating and closing; typically the
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2689 handles will be invalid when running as a gui program. */
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2690 DuplicateHandle (parent,
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2691 GetStdHandle (STD_INPUT_HANDLE),
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2692 parent,
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2693 &stdin_save,
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2694 0,
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2695 FALSE,
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2696 DUPLICATE_SAME_ACCESS);
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2697
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2698 DuplicateHandle (parent,
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2699 GetStdHandle (STD_OUTPUT_HANDLE),
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2700 parent,
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2701 &stdout_save,
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2702 0,
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2703 FALSE,
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2704 DUPLICATE_SAME_ACCESS);
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2705
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2706 DuplicateHandle (parent,
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2707 GetStdHandle (STD_ERROR_HANDLE),
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2708 parent,
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2709 &stderr_save,
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2710 0,
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2711 FALSE,
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2712 DUPLICATE_SAME_ACCESS);
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2713
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2714 fclose (stdin);
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2715 fclose (stdout);
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2716 fclose (stderr);
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2717
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2718 if (stdin_save != INVALID_HANDLE_VALUE)
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2719 _open_osfhandle ((long) stdin_save, O_TEXT);
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2720 else
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2721 _open ("nul", O_TEXT | O_NOINHERIT | O_RDONLY);
209
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
2722 _fdopen (0, "r");
100
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2723
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2724 if (stdout_save != INVALID_HANDLE_VALUE)
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2725 _open_osfhandle ((long) stdout_save, O_TEXT);
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2726 else
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2727 _open ("nul", O_TEXT | O_NOINHERIT | O_WRONLY);
209
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
2728 _fdopen (1, "w");
100
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2729
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2730 if (stderr_save != INVALID_HANDLE_VALUE)
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2731 _open_osfhandle ((long) stderr_save, O_TEXT);
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2732 else
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2733 _open ("nul", O_TEXT | O_NOINHERIT | O_WRONLY);
209
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
2734 _fdopen (2, "w");
100
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2735 }
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2736
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2737 /* unfortunately, atexit depends on implementation of malloc */
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2738 /* atexit (term_ntproc); */
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2739 signal (SIGABRT, term_ntproc);
209
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
2740
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
2741 /* determine which drives are fixed, for GetCachedVolumeInformation */
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
2742 {
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
2743 /* GetDriveType must have trailing backslash. */
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
2744 char drive[] = "A:\\";
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
2745
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
2746 /* Loop over all possible drive letters */
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
2747 while ( *drive <= 'Z' )
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
2748 {
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
2749 /* Record if this drive letter refers to a fixed drive. */
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
2750 fixed_drives[ DRIVE_INDEX (*drive) ] =
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
2751 (GetDriveType (drive) == DRIVE_FIXED);
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
2752
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
2753 (*drive)++;
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
2754 }
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
2755 }
100
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2756 }
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2757 #ifndef HAVE_TTY
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2758 Lisp_Object Vstdio_str;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2759
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2760 Lisp_Object
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2761 tty_semi_canonicalize_console_connection (Lisp_Object connection,
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2762 Error_behavior errb)
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2763 {
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2764 return Vstdio_str;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2765 }
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2766
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2767 Lisp_Object
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2768 tty_canonicalize_console_connection (Lisp_Object connection,
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2769 Error_behavior errb)
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2770 {
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2771 return Vstdio_str;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2772 }
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2773
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2774 Lisp_Object
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2775 tty_semi_canonicalize_device_connection (Lisp_Object connection,
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2776 Error_behavior errb)
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2777 {
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2778 return Vstdio_str;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2779 }
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2780
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2781 Lisp_Object
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2782 tty_canonicalize_device_connection (Lisp_Object connection,
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2783 Error_behavior errb)
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2784 {
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2785 return Vstdio_str;
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2786 }
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2787 #endif
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
2788
223
2c611d1463a6 Import from CVS: tag r20-4b10
cvs
parents: 213
diff changeset
2789 /*--------------------------------------------------------------------*/
2c611d1463a6 Import from CVS: tag r20-4b10
cvs
parents: 213
diff changeset
2790 /* Signal support */
2c611d1463a6 Import from CVS: tag r20-4b10
cvs
parents: 213
diff changeset
2791 /*--------------------------------------------------------------------*/
2c611d1463a6 Import from CVS: tag r20-4b10
cvs
parents: 213
diff changeset
2792
2c611d1463a6 Import from CVS: tag r20-4b10
cvs
parents: 213
diff changeset
2793 /* We need MS-defined signal and raise here */
2c611d1463a6 Import from CVS: tag r20-4b10
cvs
parents: 213
diff changeset
2794 #undef signal
2c611d1463a6 Import from CVS: tag r20-4b10
cvs
parents: 213
diff changeset
2795 #undef raise
2c611d1463a6 Import from CVS: tag r20-4b10
cvs
parents: 213
diff changeset
2796
2c611d1463a6 Import from CVS: tag r20-4b10
cvs
parents: 213
diff changeset
2797 #define sigmask(nsig) (1U << nsig)
2c611d1463a6 Import from CVS: tag r20-4b10
cvs
parents: 213
diff changeset
2798
2c611d1463a6 Import from CVS: tag r20-4b10
cvs
parents: 213
diff changeset
2799 /* We can support as many signals as fit into word */
2c611d1463a6 Import from CVS: tag r20-4b10
cvs
parents: 213
diff changeset
2800 #define SIG_MAX 32
2c611d1463a6 Import from CVS: tag r20-4b10
cvs
parents: 213
diff changeset
2801
2c611d1463a6 Import from CVS: tag r20-4b10
cvs
parents: 213
diff changeset
2802 /* Signal handlers. Initial value = 0 = SIG_DFL */
2c611d1463a6 Import from CVS: tag r20-4b10
cvs
parents: 213
diff changeset
2803 static void (__cdecl *signal_handlers[SIG_MAX])(int) = {0};
2c611d1463a6 Import from CVS: tag r20-4b10
cvs
parents: 213
diff changeset
2804
2c611d1463a6 Import from CVS: tag r20-4b10
cvs
parents: 213
diff changeset
2805 /* Signal block mask: bit set to 1 means blocked */
2c611d1463a6 Import from CVS: tag r20-4b10
cvs
parents: 213
diff changeset
2806 unsigned signal_block_mask = 0;
2c611d1463a6 Import from CVS: tag r20-4b10
cvs
parents: 213
diff changeset
2807
2c611d1463a6 Import from CVS: tag r20-4b10
cvs
parents: 213
diff changeset
2808 /* Signal pending mask: bit set to 1 means sig is pending */
2c611d1463a6 Import from CVS: tag r20-4b10
cvs
parents: 213
diff changeset
2809 unsigned signal_pending_mask = 0;
2c611d1463a6 Import from CVS: tag r20-4b10
cvs
parents: 213
diff changeset
2810
2c611d1463a6 Import from CVS: tag r20-4b10
cvs
parents: 213
diff changeset
2811 msw_sighandler msw_sigset (int nsig, msw_sighandler handler)
2c611d1463a6 Import from CVS: tag r20-4b10
cvs
parents: 213
diff changeset
2812 {
2c611d1463a6 Import from CVS: tag r20-4b10
cvs
parents: 213
diff changeset
2813 /* We delegate some signals to the system function */
2c611d1463a6 Import from CVS: tag r20-4b10
cvs
parents: 213
diff changeset
2814 if (nsig == SIGFPE || nsig == SIGABRT || nsig == SIGINT)
2c611d1463a6 Import from CVS: tag r20-4b10
cvs
parents: 213
diff changeset
2815 {
2c611d1463a6 Import from CVS: tag r20-4b10
cvs
parents: 213
diff changeset
2816 signal (nsig, handler);
2c611d1463a6 Import from CVS: tag r20-4b10
cvs
parents: 213
diff changeset
2817 return;
2c611d1463a6 Import from CVS: tag r20-4b10
cvs
parents: 213
diff changeset
2818 }
2c611d1463a6 Import from CVS: tag r20-4b10
cvs
parents: 213
diff changeset
2819
2c611d1463a6 Import from CVS: tag r20-4b10
cvs
parents: 213
diff changeset
2820 if (nsig < 0 || nsig > SIG_MAX)
2c611d1463a6 Import from CVS: tag r20-4b10
cvs
parents: 213
diff changeset
2821 {
2c611d1463a6 Import from CVS: tag r20-4b10
cvs
parents: 213
diff changeset
2822 errno = EINVAL;
2c611d1463a6 Import from CVS: tag r20-4b10
cvs
parents: 213
diff changeset
2823 return;
2c611d1463a6 Import from CVS: tag r20-4b10
cvs
parents: 213
diff changeset
2824 }
2c611d1463a6 Import from CVS: tag r20-4b10
cvs
parents: 213
diff changeset
2825
2c611d1463a6 Import from CVS: tag r20-4b10
cvs
parents: 213
diff changeset
2826 /* Store handler ptr */
2c611d1463a6 Import from CVS: tag r20-4b10
cvs
parents: 213
diff changeset
2827 signal_handlers[nsig] = handler;
2c611d1463a6 Import from CVS: tag r20-4b10
cvs
parents: 213
diff changeset
2828 }
2c611d1463a6 Import from CVS: tag r20-4b10
cvs
parents: 213
diff changeset
2829
2c611d1463a6 Import from CVS: tag r20-4b10
cvs
parents: 213
diff changeset
2830 int msw_sighold (int nsig)
2c611d1463a6 Import from CVS: tag r20-4b10
cvs
parents: 213
diff changeset
2831 {
2c611d1463a6 Import from CVS: tag r20-4b10
cvs
parents: 213
diff changeset
2832 if (nsig < 0 || nsig > SIG_MAX)
2c611d1463a6 Import from CVS: tag r20-4b10
cvs
parents: 213
diff changeset
2833 return errno = EINVAL;
2c611d1463a6 Import from CVS: tag r20-4b10
cvs
parents: 213
diff changeset
2834
2c611d1463a6 Import from CVS: tag r20-4b10
cvs
parents: 213
diff changeset
2835 signal_block_mask |= sigmask(nsig);
2c611d1463a6 Import from CVS: tag r20-4b10
cvs
parents: 213
diff changeset
2836 return 0;
2c611d1463a6 Import from CVS: tag r20-4b10
cvs
parents: 213
diff changeset
2837 }
2c611d1463a6 Import from CVS: tag r20-4b10
cvs
parents: 213
diff changeset
2838
2c611d1463a6 Import from CVS: tag r20-4b10
cvs
parents: 213
diff changeset
2839 int msw_sigrelse (int nsig)
2c611d1463a6 Import from CVS: tag r20-4b10
cvs
parents: 213
diff changeset
2840 {
2c611d1463a6 Import from CVS: tag r20-4b10
cvs
parents: 213
diff changeset
2841 if (nsig < 0 || nsig > SIG_MAX)
2c611d1463a6 Import from CVS: tag r20-4b10
cvs
parents: 213
diff changeset
2842 return errno = EINVAL;
2c611d1463a6 Import from CVS: tag r20-4b10
cvs
parents: 213
diff changeset
2843
2c611d1463a6 Import from CVS: tag r20-4b10
cvs
parents: 213
diff changeset
2844 signal_block_mask &= ~sigmask(nsig);
2c611d1463a6 Import from CVS: tag r20-4b10
cvs
parents: 213
diff changeset
2845
2c611d1463a6 Import from CVS: tag r20-4b10
cvs
parents: 213
diff changeset
2846 if (signal_pending_mask & sigmask(nsig))
2c611d1463a6 Import from CVS: tag r20-4b10
cvs
parents: 213
diff changeset
2847 msw_raise (nsig);
2c611d1463a6 Import from CVS: tag r20-4b10
cvs
parents: 213
diff changeset
2848
2c611d1463a6 Import from CVS: tag r20-4b10
cvs
parents: 213
diff changeset
2849 return 0;
2c611d1463a6 Import from CVS: tag r20-4b10
cvs
parents: 213
diff changeset
2850 }
2c611d1463a6 Import from CVS: tag r20-4b10
cvs
parents: 213
diff changeset
2851
2c611d1463a6 Import from CVS: tag r20-4b10
cvs
parents: 213
diff changeset
2852 int msw_sigpause (int nsig)
2c611d1463a6 Import from CVS: tag r20-4b10
cvs
parents: 213
diff changeset
2853 {
2c611d1463a6 Import from CVS: tag r20-4b10
cvs
parents: 213
diff changeset
2854 /* This is currently not called, because the only
2c611d1463a6 Import from CVS: tag r20-4b10
cvs
parents: 213
diff changeset
2855 call to sigpause inside XEmacs is with SIGCHLD
2c611d1463a6 Import from CVS: tag r20-4b10
cvs
parents: 213
diff changeset
2856 parameter. Just in case, we put an assert here,
2c611d1463a6 Import from CVS: tag r20-4b10
cvs
parents: 213
diff changeset
2857 so anyone who will add a call to sigpause will
2c611d1463a6 Import from CVS: tag r20-4b10
cvs
parents: 213
diff changeset
2858 be surprised (or surprise someone else...) */
2c611d1463a6 Import from CVS: tag r20-4b10
cvs
parents: 213
diff changeset
2859 assert (0);
2c611d1463a6 Import from CVS: tag r20-4b10
cvs
parents: 213
diff changeset
2860 return 0;
2c611d1463a6 Import from CVS: tag r20-4b10
cvs
parents: 213
diff changeset
2861 }
2c611d1463a6 Import from CVS: tag r20-4b10
cvs
parents: 213
diff changeset
2862
2c611d1463a6 Import from CVS: tag r20-4b10
cvs
parents: 213
diff changeset
2863 int msw_raise (int nsig)
2c611d1463a6 Import from CVS: tag r20-4b10
cvs
parents: 213
diff changeset
2864 {
2c611d1463a6 Import from CVS: tag r20-4b10
cvs
parents: 213
diff changeset
2865 /* We delegate some raises to the system routine */
2c611d1463a6 Import from CVS: tag r20-4b10
cvs
parents: 213
diff changeset
2866 if (nsig == SIGFPE || nsig == SIGABRT || nsig == SIGINT)
2c611d1463a6 Import from CVS: tag r20-4b10
cvs
parents: 213
diff changeset
2867 return raise (nsig);
2c611d1463a6 Import from CVS: tag r20-4b10
cvs
parents: 213
diff changeset
2868
2c611d1463a6 Import from CVS: tag r20-4b10
cvs
parents: 213
diff changeset
2869 if (nsig < 0 || nsig > SIG_MAX)
2c611d1463a6 Import from CVS: tag r20-4b10
cvs
parents: 213
diff changeset
2870 return errno = EINVAL;
2c611d1463a6 Import from CVS: tag r20-4b10
cvs
parents: 213
diff changeset
2871
2c611d1463a6 Import from CVS: tag r20-4b10
cvs
parents: 213
diff changeset
2872 /* If the signal is blocked, remember to issue later */
2c611d1463a6 Import from CVS: tag r20-4b10
cvs
parents: 213
diff changeset
2873 if (signal_block_mask & sigmask(nsig))
2c611d1463a6 Import from CVS: tag r20-4b10
cvs
parents: 213
diff changeset
2874 {
2c611d1463a6 Import from CVS: tag r20-4b10
cvs
parents: 213
diff changeset
2875 signal_pending_mask |= sigmask(nsig);
2c611d1463a6 Import from CVS: tag r20-4b10
cvs
parents: 213
diff changeset
2876 return 0;
2c611d1463a6 Import from CVS: tag r20-4b10
cvs
parents: 213
diff changeset
2877 }
2c611d1463a6 Import from CVS: tag r20-4b10
cvs
parents: 213
diff changeset
2878
2c611d1463a6 Import from CVS: tag r20-4b10
cvs
parents: 213
diff changeset
2879 if (signal_handlers[nsig] == SIG_IGN)
2c611d1463a6 Import from CVS: tag r20-4b10
cvs
parents: 213
diff changeset
2880 return 0;
2c611d1463a6 Import from CVS: tag r20-4b10
cvs
parents: 213
diff changeset
2881
2c611d1463a6 Import from CVS: tag r20-4b10
cvs
parents: 213
diff changeset
2882 if (signal_handlers[nsig] != SIG_DFL)
2c611d1463a6 Import from CVS: tag r20-4b10
cvs
parents: 213
diff changeset
2883 {
2c611d1463a6 Import from CVS: tag r20-4b10
cvs
parents: 213
diff changeset
2884 (*signal_handlers[nsig])(nsig);
2c611d1463a6 Import from CVS: tag r20-4b10
cvs
parents: 213
diff changeset
2885 return 0;
2c611d1463a6 Import from CVS: tag r20-4b10
cvs
parents: 213
diff changeset
2886 }
2c611d1463a6 Import from CVS: tag r20-4b10
cvs
parents: 213
diff changeset
2887
2c611d1463a6 Import from CVS: tag r20-4b10
cvs
parents: 213
diff changeset
2888 /* Default signal actions */
2c611d1463a6 Import from CVS: tag r20-4b10
cvs
parents: 213
diff changeset
2889 if (nsig == SIGALRM || nsig == SIGPROF)
2c611d1463a6 Import from CVS: tag r20-4b10
cvs
parents: 213
diff changeset
2890 exit (3);
2c611d1463a6 Import from CVS: tag r20-4b10
cvs
parents: 213
diff changeset
2891
2c611d1463a6 Import from CVS: tag r20-4b10
cvs
parents: 213
diff changeset
2892 /* Other signals are ignored by default */
2c611d1463a6 Import from CVS: tag r20-4b10
cvs
parents: 213
diff changeset
2893 }
2c611d1463a6 Import from CVS: tag r20-4b10
cvs
parents: 213
diff changeset
2894
2c611d1463a6 Import from CVS: tag r20-4b10
cvs
parents: 213
diff changeset
2895 /*--------------------------------------------------------------------*/
2c611d1463a6 Import from CVS: tag r20-4b10
cvs
parents: 213
diff changeset
2896 /* Async timers */
2c611d1463a6 Import from CVS: tag r20-4b10
cvs
parents: 213
diff changeset
2897 /*--------------------------------------------------------------------*/
2c611d1463a6 Import from CVS: tag r20-4b10
cvs
parents: 213
diff changeset
2898
2c611d1463a6 Import from CVS: tag r20-4b10
cvs
parents: 213
diff changeset
2899 /* We emulate two timers, one for SIGALRM, another for SIGPROF.
2c611d1463a6 Import from CVS: tag r20-4b10
cvs
parents: 213
diff changeset
2900
2c611d1463a6 Import from CVS: tag r20-4b10
cvs
parents: 213
diff changeset
2901 itimerproc() function has an implementation limitation: it does
2c611d1463a6 Import from CVS: tag r20-4b10
cvs
parents: 213
diff changeset
2902 not allow to set *both* interval and period. If an attempt is
2c611d1463a6 Import from CVS: tag r20-4b10
cvs
parents: 213
diff changeset
2903 made to set both, and then they are unequal, the function
2c611d1463a6 Import from CVS: tag r20-4b10
cvs
parents: 213
diff changeset
2904 asserts.
2c611d1463a6 Import from CVS: tag r20-4b10
cvs
parents: 213
diff changeset
2905
2c611d1463a6 Import from CVS: tag r20-4b10
cvs
parents: 213
diff changeset
2906 Minimum timer resolution on Win32 systems varies, and is greater
2c611d1463a6 Import from CVS: tag r20-4b10
cvs
parents: 213
diff changeset
2907 than or equal than 1 ms. The resolution is always wrapped not to
2c611d1463a6 Import from CVS: tag r20-4b10
cvs
parents: 213
diff changeset
2908 attempt to get below the system defined limit.
2c611d1463a6 Import from CVS: tag r20-4b10
cvs
parents: 213
diff changeset
2909 */
2c611d1463a6 Import from CVS: tag r20-4b10
cvs
parents: 213
diff changeset
2910
2c611d1463a6 Import from CVS: tag r20-4b10
cvs
parents: 213
diff changeset
2911 /* Timer precision, denominator of one fraction: for 100 ms
2c611d1463a6 Import from CVS: tag r20-4b10
cvs
parents: 213
diff changeset
2912 interval, request 10 ms precision
2c611d1463a6 Import from CVS: tag r20-4b10
cvs
parents: 213
diff changeset
2913 */
2c611d1463a6 Import from CVS: tag r20-4b10
cvs
parents: 213
diff changeset
2914 const int timer_prec = 10;
2c611d1463a6 Import from CVS: tag r20-4b10
cvs
parents: 213
diff changeset
2915
2c611d1463a6 Import from CVS: tag r20-4b10
cvs
parents: 213
diff changeset
2916 /* Last itimevals, as set by calls to setitimer */
2c611d1463a6 Import from CVS: tag r20-4b10
cvs
parents: 213
diff changeset
2917 static struct itimerval it_alarm;
2c611d1463a6 Import from CVS: tag r20-4b10
cvs
parents: 213
diff changeset
2918 static struct itimerval it_prof;
2c611d1463a6 Import from CVS: tag r20-4b10
cvs
parents: 213
diff changeset
2919
2c611d1463a6 Import from CVS: tag r20-4b10
cvs
parents: 213
diff changeset
2920 /* Timer IDs as returned by MM */
2c611d1463a6 Import from CVS: tag r20-4b10
cvs
parents: 213
diff changeset
2921 MMRESULT tid_alarm = 0;
2c611d1463a6 Import from CVS: tag r20-4b10
cvs
parents: 213
diff changeset
2922 MMRESULT tid_prof = 0;
2c611d1463a6 Import from CVS: tag r20-4b10
cvs
parents: 213
diff changeset
2923
2c611d1463a6 Import from CVS: tag r20-4b10
cvs
parents: 213
diff changeset
2924 static void CALLBACK timer_proc (UINT uID, UINT uMsg, DWORD dwUser,
2c611d1463a6 Import from CVS: tag r20-4b10
cvs
parents: 213
diff changeset
2925 DWORD dw1, DWORD dw2)
2c611d1463a6 Import from CVS: tag r20-4b10
cvs
parents: 213
diff changeset
2926 {
2c611d1463a6 Import from CVS: tag r20-4b10
cvs
parents: 213
diff changeset
2927 /* Just raise a signal indicated by dwUser parameter */
2c611d1463a6 Import from CVS: tag r20-4b10
cvs
parents: 213
diff changeset
2928 msw_raise (dwUser);
2c611d1463a6 Import from CVS: tag r20-4b10
cvs
parents: 213
diff changeset
2929 }
2c611d1463a6 Import from CVS: tag r20-4b10
cvs
parents: 213
diff changeset
2930
2c611d1463a6 Import from CVS: tag r20-4b10
cvs
parents: 213
diff changeset
2931 /* Divide time in ms specified by IT by DENOM. Return 1 ms
2c611d1463a6 Import from CVS: tag r20-4b10
cvs
parents: 213
diff changeset
2932 if division results in zero */
2c611d1463a6 Import from CVS: tag r20-4b10
cvs
parents: 213
diff changeset
2933 static UINT period (const struct itimerval* it, UINT denom)
2c611d1463a6 Import from CVS: tag r20-4b10
cvs
parents: 213
diff changeset
2934 {
2c611d1463a6 Import from CVS: tag r20-4b10
cvs
parents: 213
diff changeset
2935 static TIMECAPS time_caps;
2c611d1463a6 Import from CVS: tag r20-4b10
cvs
parents: 213
diff changeset
2936
2c611d1463a6 Import from CVS: tag r20-4b10
cvs
parents: 213
diff changeset
2937 UINT res;
2c611d1463a6 Import from CVS: tag r20-4b10
cvs
parents: 213
diff changeset
2938 const struct timeval* tv =
2c611d1463a6 Import from CVS: tag r20-4b10
cvs
parents: 213
diff changeset
2939 (it->it_value.tv_sec == 0 && it->it_value.tv_usec == 0)
2c611d1463a6 Import from CVS: tag r20-4b10
cvs
parents: 213
diff changeset
2940 ? &it->it_interval : &it->it_value;
2c611d1463a6 Import from CVS: tag r20-4b10
cvs
parents: 213
diff changeset
2941
2c611d1463a6 Import from CVS: tag r20-4b10
cvs
parents: 213
diff changeset
2942 /* Zero means stop timer */
2c611d1463a6 Import from CVS: tag r20-4b10
cvs
parents: 213
diff changeset
2943 if (tv->tv_sec == 0 && tv->tv_usec == 0)
2c611d1463a6 Import from CVS: tag r20-4b10
cvs
parents: 213
diff changeset
2944 return 0;
2c611d1463a6 Import from CVS: tag r20-4b10
cvs
parents: 213
diff changeset
2945
2c611d1463a6 Import from CVS: tag r20-4b10
cvs
parents: 213
diff changeset
2946 /* Conver to ms and divide by denom */
2c611d1463a6 Import from CVS: tag r20-4b10
cvs
parents: 213
diff changeset
2947 res = (tv->tv_sec * 1000 + (tv->tv_usec + 500) / 1000) / denom;
2c611d1463a6 Import from CVS: tag r20-4b10
cvs
parents: 213
diff changeset
2948
2c611d1463a6 Import from CVS: tag r20-4b10
cvs
parents: 213
diff changeset
2949 /* Converge to minimum timer resolution */
2c611d1463a6 Import from CVS: tag r20-4b10
cvs
parents: 213
diff changeset
2950 if (time_caps.wPeriodMin == 0)
2c611d1463a6 Import from CVS: tag r20-4b10
cvs
parents: 213
diff changeset
2951 timeGetDevCaps (&time_caps, sizeof(time_caps));
2c611d1463a6 Import from CVS: tag r20-4b10
cvs
parents: 213
diff changeset
2952
2c611d1463a6 Import from CVS: tag r20-4b10
cvs
parents: 213
diff changeset
2953 if (res < time_caps.wPeriodMin)
2c611d1463a6 Import from CVS: tag r20-4b10
cvs
parents: 213
diff changeset
2954 res = time_caps.wPeriodMin;
2c611d1463a6 Import from CVS: tag r20-4b10
cvs
parents: 213
diff changeset
2955
2c611d1463a6 Import from CVS: tag r20-4b10
cvs
parents: 213
diff changeset
2956 return res;
2c611d1463a6 Import from CVS: tag r20-4b10
cvs
parents: 213
diff changeset
2957 }
2c611d1463a6 Import from CVS: tag r20-4b10
cvs
parents: 213
diff changeset
2958
2c611d1463a6 Import from CVS: tag r20-4b10
cvs
parents: 213
diff changeset
2959 static int setitimer_helper (const struct itimerval* itnew,
2c611d1463a6 Import from CVS: tag r20-4b10
cvs
parents: 213
diff changeset
2960 struct itimerval* itold, struct itimerval* itcurrent,
2c611d1463a6 Import from CVS: tag r20-4b10
cvs
parents: 213
diff changeset
2961 MMRESULT* tid, DWORD sigkind)
2c611d1463a6 Import from CVS: tag r20-4b10
cvs
parents: 213
diff changeset
2962 {
2c611d1463a6 Import from CVS: tag r20-4b10
cvs
parents: 213
diff changeset
2963 UINT delay, resolution, event_type;
2c611d1463a6 Import from CVS: tag r20-4b10
cvs
parents: 213
diff changeset
2964
2c611d1463a6 Import from CVS: tag r20-4b10
cvs
parents: 213
diff changeset
2965 /* First stop the old timer */
2c611d1463a6 Import from CVS: tag r20-4b10
cvs
parents: 213
diff changeset
2966 if (*tid)
2c611d1463a6 Import from CVS: tag r20-4b10
cvs
parents: 213
diff changeset
2967 {
2c611d1463a6 Import from CVS: tag r20-4b10
cvs
parents: 213
diff changeset
2968 timeKillEvent (*tid);
2c611d1463a6 Import from CVS: tag r20-4b10
cvs
parents: 213
diff changeset
2969 timeEndPeriod (period (itcurrent, timer_prec));
2c611d1463a6 Import from CVS: tag r20-4b10
cvs
parents: 213
diff changeset
2970 *tid = 0;
2c611d1463a6 Import from CVS: tag r20-4b10
cvs
parents: 213
diff changeset
2971 }
2c611d1463a6 Import from CVS: tag r20-4b10
cvs
parents: 213
diff changeset
2972
2c611d1463a6 Import from CVS: tag r20-4b10
cvs
parents: 213
diff changeset
2973 /* Return old itimerval if requested */
2c611d1463a6 Import from CVS: tag r20-4b10
cvs
parents: 213
diff changeset
2974 if (itold)
2c611d1463a6 Import from CVS: tag r20-4b10
cvs
parents: 213
diff changeset
2975 *itold = *itcurrent;
2c611d1463a6 Import from CVS: tag r20-4b10
cvs
parents: 213
diff changeset
2976
2c611d1463a6 Import from CVS: tag r20-4b10
cvs
parents: 213
diff changeset
2977 *itcurrent = *itnew;
2c611d1463a6 Import from CVS: tag r20-4b10
cvs
parents: 213
diff changeset
2978
2c611d1463a6 Import from CVS: tag r20-4b10
cvs
parents: 213
diff changeset
2979 /* Determine if to start new timer */
2c611d1463a6 Import from CVS: tag r20-4b10
cvs
parents: 213
diff changeset
2980 delay = period (itnew, 1);
2c611d1463a6 Import from CVS: tag r20-4b10
cvs
parents: 213
diff changeset
2981 if (delay)
2c611d1463a6 Import from CVS: tag r20-4b10
cvs
parents: 213
diff changeset
2982 {
2c611d1463a6 Import from CVS: tag r20-4b10
cvs
parents: 213
diff changeset
2983 resolution = period (itnew, timer_prec);
2c611d1463a6 Import from CVS: tag r20-4b10
cvs
parents: 213
diff changeset
2984 event_type = (itnew->it_value.tv_sec == 0 && itnew->it_value.tv_usec == 0)
2c611d1463a6 Import from CVS: tag r20-4b10
cvs
parents: 213
diff changeset
2985 ? TIME_ONESHOT : TIME_PERIODIC;
2c611d1463a6 Import from CVS: tag r20-4b10
cvs
parents: 213
diff changeset
2986 timeBeginPeriod (resolution);
2c611d1463a6 Import from CVS: tag r20-4b10
cvs
parents: 213
diff changeset
2987 *tid = timeSetEvent (delay, resolution, timer_proc, sigkind, event_type);
2c611d1463a6 Import from CVS: tag r20-4b10
cvs
parents: 213
diff changeset
2988 }
2c611d1463a6 Import from CVS: tag r20-4b10
cvs
parents: 213
diff changeset
2989
2c611d1463a6 Import from CVS: tag r20-4b10
cvs
parents: 213
diff changeset
2990 return !delay || *tid;
2c611d1463a6 Import from CVS: tag r20-4b10
cvs
parents: 213
diff changeset
2991 }
2c611d1463a6 Import from CVS: tag r20-4b10
cvs
parents: 213
diff changeset
2992
2c611d1463a6 Import from CVS: tag r20-4b10
cvs
parents: 213
diff changeset
2993 int setitimer (int kind, const struct itimerval* itnew,
2c611d1463a6 Import from CVS: tag r20-4b10
cvs
parents: 213
diff changeset
2994 struct itimerval* itold)
2c611d1463a6 Import from CVS: tag r20-4b10
cvs
parents: 213
diff changeset
2995 {
2c611d1463a6 Import from CVS: tag r20-4b10
cvs
parents: 213
diff changeset
2996 /* In this version, both interval and value are allowed
2c611d1463a6 Import from CVS: tag r20-4b10
cvs
parents: 213
diff changeset
2997 only if they are equal. */
2c611d1463a6 Import from CVS: tag r20-4b10
cvs
parents: 213
diff changeset
2998 assert ((itnew->it_value.tv_sec == 0 && itnew->it_value.tv_usec == 0)
2c611d1463a6 Import from CVS: tag r20-4b10
cvs
parents: 213
diff changeset
2999 || (itnew->it_interval.tv_sec == 0 && itnew->it_interval.tv_usec == 0)
2c611d1463a6 Import from CVS: tag r20-4b10
cvs
parents: 213
diff changeset
3000 || (itnew->it_value.tv_sec == itnew->it_interval.tv_sec &&
2c611d1463a6 Import from CVS: tag r20-4b10
cvs
parents: 213
diff changeset
3001 itnew->it_value.tv_usec == itnew->it_interval.tv_usec));
2c611d1463a6 Import from CVS: tag r20-4b10
cvs
parents: 213
diff changeset
3002
2c611d1463a6 Import from CVS: tag r20-4b10
cvs
parents: 213
diff changeset
3003 if (kind == ITIMER_REAL)
2c611d1463a6 Import from CVS: tag r20-4b10
cvs
parents: 213
diff changeset
3004 return setitimer_helper (itnew, itold, &it_alarm, &tid_alarm, SIGALRM);
2c611d1463a6 Import from CVS: tag r20-4b10
cvs
parents: 213
diff changeset
3005 else if (kind == ITIMER_PROF)
2c611d1463a6 Import from CVS: tag r20-4b10
cvs
parents: 213
diff changeset
3006 return setitimer_helper (itnew, itold, &it_prof, &tid_prof, SIGPROF);
2c611d1463a6 Import from CVS: tag r20-4b10
cvs
parents: 213
diff changeset
3007 else
2c611d1463a6 Import from CVS: tag r20-4b10
cvs
parents: 213
diff changeset
3008 return errno = EINVAL;
2c611d1463a6 Import from CVS: tag r20-4b10
cvs
parents: 213
diff changeset
3009 }
2c611d1463a6 Import from CVS: tag r20-4b10
cvs
parents: 213
diff changeset
3010
100
4be1180a9e89 Import from CVS: tag r20-1b2
cvs
parents:
diff changeset
3011 /* end of nt.c */