blob: 88861c66e09a5a3a59ac6799149d14f22f2caccd [file] [log] [blame]
Guido van Rossumf70e43a1991-02-19 12:39:46 +00001/***********************************************************
Guido van Rossum775f4da1993-01-09 17:18:52 +00002Copyright 1991, 1992, 1993 by Stichting Mathematisch Centrum,
3Amsterdam, The Netherlands.
Guido van Rossumf70e43a1991-02-19 12:39:46 +00004
5 All Rights Reserved
6
7Permission to use, copy, modify, and distribute this software and its
8documentation for any purpose and without fee is hereby granted,
9provided that the above copyright notice appear in all copies and that
10both that copyright notice and this permission notice appear in
11supporting documentation, and that the names of Stichting Mathematisch
12Centrum or CWI not be used in advertising or publicity pertaining to
13distribution of the software without specific, written prior permission.
14
15STICHTING MATHEMATISCH CENTRUM DISCLAIMS ALL WARRANTIES WITH REGARD TO
16THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
17FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH CENTRUM BE LIABLE
18FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
19WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
20ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
21OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
22
23******************************************************************/
24
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000025/* POSIX module implementation */
26
Guido van Rossum1ff6cb41991-04-08 20:59:13 +000027#ifdef AMOEBA
28#define NO_LSTAT
29#define SYSV
30#endif
31
Guido van Rossum0ee42cd1991-04-08 21:01:03 +000032#ifdef MSDOS
33#define NO_LSTAT
Guido van Rossumc39de5f1992-02-05 11:15:54 +000034#define NO_UNAME
Guido van Rossum0ee42cd1991-04-08 21:01:03 +000035#endif
36
Guido van Rossumc2670a01992-09-13 20:07:29 +000037#ifdef __sgi
38#define DO_PG
39#endif
40
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000041#include <signal.h>
42#include <string.h>
43#include <setjmp.h>
44#include <sys/types.h>
45#include <sys/stat.h>
Guido van Rossum0ee42cd1991-04-08 21:01:03 +000046
Guido van Rossum22db57e1992-04-05 14:25:30 +000047#ifdef DO_TIMES
48#include <sys/times.h>
49#include <sys/param.h>
50#include <errno.h>
51#endif
52
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000053#ifdef SYSV
Guido van Rossum0ee42cd1991-04-08 21:01:03 +000054
Guido van Rossum1ff6cb41991-04-08 20:59:13 +000055#define UTIME_STRUCT
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000056#include <dirent.h>
57#define direct dirent
Guido van Rossum1ff6cb41991-04-08 20:59:13 +000058#ifdef i386
59#define mode_t int
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000060#endif
Guido van Rossum0ee42cd1991-04-08 21:01:03 +000061
Guido van Rossum1ff6cb41991-04-08 20:59:13 +000062#else /* !SYSV */
Guido van Rossum0ee42cd1991-04-08 21:01:03 +000063
64#ifndef MSDOS
Guido van Rossum1ff6cb41991-04-08 20:59:13 +000065#include <sys/dir.h>
Guido van Rossum0ee42cd1991-04-08 21:01:03 +000066#endif
67
Guido van Rossum1ff6cb41991-04-08 20:59:13 +000068#endif /* !SYSV */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000069
Guido van Rossum50e61dc1992-03-27 17:22:31 +000070#ifndef NO_UNISTD
Guido van Rossum22db57e1992-04-05 14:25:30 +000071#include <unistd.h> /* Take this out and hope the best if it doesn't exist */
Guido van Rossum50e61dc1992-03-27 17:22:31 +000072#endif
73
Guido van Rossum3f5da241990-12-20 15:06:42 +000074#include "allobjects.h"
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000075#include "modsupport.h"
Guido van Rossumff4949e1992-08-05 19:58:53 +000076#include "ceval.h"
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000077
Guido van Rossum0b0db8e1993-01-21 16:07:51 +000078#ifdef _SEQUENT_
79#include <unistd.h>
80#else /* _SEQUENT_ */
Guido van Rossuma2b7f401993-01-04 09:09:59 +000081/* XXX Aren't these always declared in unistd.h? */
Guido van Rossum7f77e2d1990-10-30 13:34:38 +000082extern char *strerror PROTO((int));
Guido van Rossuma2b7f401993-01-04 09:09:59 +000083extern int chmod PROTO((const char *, mode_t));
84extern char *getcwd PROTO((char *, int)); /* XXX or size_t? */
85extern int mkdir PROTO((const char *, mode_t));
86extern int chdir PROTO((const char *));
87extern int link PROTO((const char *, const char *));
88extern int rename PROTO((const char *, const char *));
89extern int rmdir PROTO((const char *));
90extern int stat PROTO((const char *, struct stat *));
91extern int unlink PROTO((const char *));
92extern int pclose PROTO((FILE *));
Guido van Rossum0b0db8e1993-01-21 16:07:51 +000093#endif /* _SEQUENT_ */
Guido van Rossuma2b7f401993-01-04 09:09:59 +000094#ifdef NO_LSTAT
95#define lstat stat
96#else
97extern int lstat PROTO((const char *, struct stat *));
98extern int symlink PROTO((const char *, const char *));
99#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000100
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000101
102/* Return a dictionary corresponding to the POSIX environment table */
103
104extern char **environ;
105
106static object *
107convertenviron()
108{
109 object *d;
110 char **e;
111 d = newdictobject();
112 if (d == NULL)
113 return NULL;
114 if (environ == NULL)
115 return d;
116 /* XXX This part ignores errors */
117 for (e = environ; *e != NULL; e++) {
118 object *v;
119 char *p = strchr(*e, '=');
120 if (p == NULL)
121 continue;
122 v = newstringobject(p+1);
123 if (v == NULL)
124 continue;
125 *p = '\0';
126 (void) dictinsert(d, *e, v);
127 *p = '=';
128 DECREF(v);
129 }
130 return d;
131}
132
133
134static object *PosixError; /* Exception posix.error */
135
136/* Set a POSIX-specific error from errno, and return NULL */
137
Guido van Rossum687dd131993-05-17 08:34:16 +0000138static object * posix_error() { return err_errno(PosixError);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000139}
140
141
142/* POSIX generic methods */
143
144static object *
145posix_1str(args, func)
146 object *args;
147 int (*func) FPROTO((const char *));
148{
Guido van Rossumef0a00e1992-01-27 16:51:30 +0000149 char *path1;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000150 int res;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000151 if (!getstrarg(args, &path1))
152 return NULL;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000153 BGN_SAVE
154 res = (*func)(path1);
155 END_SAVE
156 if (res < 0)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000157 return posix_error();
158 INCREF(None);
159 return None;
160}
161
162static object *
163posix_2str(args, func)
164 object *args;
165 int (*func) FPROTO((const char *, const char *));
166{
Guido van Rossumef0a00e1992-01-27 16:51:30 +0000167 char *path1, *path2;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000168 int res;
Guido van Rossum234f9421993-06-17 12:35:49 +0000169 if (!getargs(args, "(ss)", &path1, &path2))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000170 return NULL;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000171 BGN_SAVE
172 res = (*func)(path1, path2);
173 END_SAVE
174 if (res < 0)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000175 return posix_error();
176 INCREF(None);
177 return None;
178}
179
180static object *
181posix_strint(args, func)
182 object *args;
183 int (*func) FPROTO((const char *, int));
184{
Guido van Rossumef0a00e1992-01-27 16:51:30 +0000185 char *path;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000186 int i;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000187 int res;
Guido van Rossum234f9421993-06-17 12:35:49 +0000188 if (!getargs(args, "(si)", &path, &i))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000189 return NULL;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000190 BGN_SAVE
191 res = (*func)(path, i);
192 END_SAVE
193 if (res < 0)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000194 return posix_error();
195 INCREF(None);
196 return None;
197}
198
199static object *
200posix_do_stat(self, args, statfunc)
201 object *self;
202 object *args;
203 int (*statfunc) FPROTO((const char *, struct stat *));
204{
205 struct stat st;
Guido van Rossumef0a00e1992-01-27 16:51:30 +0000206 char *path;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000207 int res;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000208 if (!getstrarg(args, &path))
209 return NULL;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000210 BGN_SAVE
211 res = (*statfunc)(path, &st);
212 END_SAVE
213 if (res != 0)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000214 return posix_error();
Guido van Rossum687dd131993-05-17 08:34:16 +0000215 return mkvalue("(llllllllll)",
Guido van Rossume5372401993-03-16 12:15:04 +0000216 (long)st.st_mode,
217 (long)st.st_ino,
218 (long)st.st_dev,
219 (long)st.st_nlink,
220 (long)st.st_uid,
221 (long)st.st_gid,
222 (long)st.st_size,
223 (long)st.st_atime,
224 (long)st.st_mtime,
225 (long)st.st_ctime);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000226}
227
228
229/* POSIX methods */
230
231static object *
232posix_chdir(self, args)
233 object *self;
234 object *args;
235{
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000236 return posix_1str(args, chdir);
237}
238
239static object *
240posix_chmod(self, args)
241 object *self;
242 object *args;
243{
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000244 return posix_strint(args, chmod);
245}
246
247static object *
248posix_getcwd(self, args)
249 object *self;
250 object *args;
251{
252 char buf[1026];
Guido van Rossumff4949e1992-08-05 19:58:53 +0000253 char *res;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000254 if (!getnoarg(args))
255 return NULL;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000256 BGN_SAVE
257 res = getcwd(buf, sizeof buf);
258 END_SAVE
259 if (res == NULL)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000260 return posix_error();
261 return newstringobject(buf);
262}
263
Guido van Rossum0ee42cd1991-04-08 21:01:03 +0000264#ifndef MSDOS
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000265static object *
266posix_link(self, args)
267 object *self;
268 object *args;
269{
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000270 return posix_2str(args, link);
271}
Guido van Rossum0ee42cd1991-04-08 21:01:03 +0000272#endif /* !MSDOS */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000273
274static object *
275posix_listdir(self, args)
276 object *self;
277 object *args;
278{
Guido van Rossumef0a00e1992-01-27 16:51:30 +0000279 char *name;
280 object *d, *v;
Guido van Rossum0ee42cd1991-04-08 21:01:03 +0000281
282#ifdef MSDOS
283 struct ffblk ep;
284 int rv;
285 if (!getstrarg(args, &name))
286 return NULL;
287
Guido van Rossumef0a00e1992-01-27 16:51:30 +0000288 if (findfirst(name, &ep, 0) == -1)
Guido van Rossum0ee42cd1991-04-08 21:01:03 +0000289 return posix_error();
290 if ((d = newlistobject(0)) == NULL)
291 return NULL;
292 do {
293 v = newstringobject(ep.ff_name);
294 if (v == NULL) {
295 DECREF(d);
296 d = NULL;
297 break;
298 }
299 if (addlistitem(d, v) != 0) {
300 DECREF(v);
301 DECREF(d);
302 d = NULL;
303 break;
304 }
305 DECREF(v);
306 } while ((rv = findnext(&ep)) == 0);
307#else /* !MSDOS */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000308 DIR *dirp;
309 struct direct *ep;
310 if (!getstrarg(args, &name))
311 return NULL;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000312 BGN_SAVE
313 if ((dirp = opendir(name)) == NULL) {
314 RET_SAVE
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000315 return posix_error();
Guido van Rossumff4949e1992-08-05 19:58:53 +0000316 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000317 if ((d = newlistobject(0)) == NULL) {
318 closedir(dirp);
Guido van Rossumff4949e1992-08-05 19:58:53 +0000319 RET_SAVE
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000320 return NULL;
321 }
322 while ((ep = readdir(dirp)) != NULL) {
323 v = newstringobject(ep->d_name);
324 if (v == NULL) {
325 DECREF(d);
326 d = NULL;
327 break;
328 }
329 if (addlistitem(d, v) != 0) {
330 DECREF(v);
331 DECREF(d);
332 d = NULL;
333 break;
334 }
335 DECREF(v);
336 }
337 closedir(dirp);
Guido van Rossumff4949e1992-08-05 19:58:53 +0000338 END_SAVE
Guido van Rossum0ee42cd1991-04-08 21:01:03 +0000339#endif /* !MSDOS */
340
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000341 return d;
342}
343
344static object *
345posix_mkdir(self, args)
346 object *self;
347 object *args;
348{
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000349 return posix_strint(args, mkdir);
350}
351
Guido van Rossum775f4da1993-01-09 17:18:52 +0000352#ifndef MSDOS
353static object *
354posix_nice(self, args)
355 object *self;
356 object *args;
357{
358 int increment, value;
359
360 if (!getargs(args, "i", &increment))
361 return NULL;
362 value = nice(increment);
363 if (value == -1)
364 return posix_error();
365 return newintobject((long) value);
366}
367#endif
368
Guido van Rossum0b0db8e1993-01-21 16:07:51 +0000369#if i386 && ! _SEQUENT_
Guido van Rossum1ff6cb41991-04-08 20:59:13 +0000370int
371rename(from, to)
372 char *from;
373 char *to;
374{
375 int status;
376 /* XXX Shouldn't this unlink the destination first? */
377 status = link(from, to);
378 if (status != 0)
379 return status;
380 return unlink(from);
381}
Guido van Rossum0b0db8e1993-01-21 16:07:51 +0000382#endif /* i386 && ! _SEQUENT_ */
Guido van Rossum1ff6cb41991-04-08 20:59:13 +0000383
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000384static object *
385posix_rename(self, args)
386 object *self;
387 object *args;
388{
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000389 return posix_2str(args, rename);
390}
391
392static object *
393posix_rmdir(self, args)
394 object *self;
395 object *args;
396{
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000397 return posix_1str(args, rmdir);
398}
399
400static object *
401posix_stat(self, args)
402 object *self;
403 object *args;
404{
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000405 return posix_do_stat(self, args, stat);
406}
407
408static object *
409posix_system(self, args)
410 object *self;
411 object *args;
412{
Guido van Rossumef0a00e1992-01-27 16:51:30 +0000413 char *command;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000414 long sts;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000415 if (!getstrarg(args, &command))
416 return NULL;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000417 BGN_SAVE
Guido van Rossumef0a00e1992-01-27 16:51:30 +0000418 sts = system(command);
Guido van Rossumff4949e1992-08-05 19:58:53 +0000419 END_SAVE
420 return newintobject(sts);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000421}
422
Guido van Rossum0ee42cd1991-04-08 21:01:03 +0000423#ifndef MSDOS
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000424static object *
425posix_umask(self, args)
426 object *self;
427 object *args;
428{
429 int i;
430 if (!getintarg(args, &i))
431 return NULL;
432 i = umask(i);
433 if (i < 0)
434 return posix_error();
435 return newintobject((long)i);
436}
Guido van Rossum0ee42cd1991-04-08 21:01:03 +0000437#endif /* !MSDOS */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000438
439static object *
440posix_unlink(self, args)
441 object *self;
442 object *args;
443{
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000444 return posix_1str(args, unlink);
445}
446
Guido van Rossumc39de5f1992-02-05 11:15:54 +0000447#ifndef NO_UNAME
448#include <sys/utsname.h>
449
Guido van Rossuma2b7f401993-01-04 09:09:59 +0000450extern int uname PROTO((struct utsname *));
451
Guido van Rossumc39de5f1992-02-05 11:15:54 +0000452static object *
453posix_uname(self, args)
454 object *self;
455 object *args;
456{
Guido van Rossumc39de5f1992-02-05 11:15:54 +0000457 struct utsname u;
458 object *v;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000459 int res;
Guido van Rossum50e61dc1992-03-27 17:22:31 +0000460 if (!getnoarg(args))
461 return NULL;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000462 BGN_SAVE
463 res = uname(&u);
464 END_SAVE
465 if (res < 0)
Guido van Rossumc39de5f1992-02-05 11:15:54 +0000466 return posix_error();
Guido van Rossume5372401993-03-16 12:15:04 +0000467 return mkvalue("(sssss)",
468 u.sysname,
469 u.nodename,
470 u.release,
471 u.version,
472 u.machine);
Guido van Rossumc39de5f1992-02-05 11:15:54 +0000473}
474#endif /* NO_UNAME */
475
Guido van Rossum1ff6cb41991-04-08 20:59:13 +0000476#ifdef UTIME_STRUCT
477#include <utime.h>
478#endif
479
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000480static object *
Guido van Rossum1ff6cb41991-04-08 20:59:13 +0000481posix_utime(self, args)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000482 object *self;
483 object *args;
484{
Guido van Rossumef0a00e1992-01-27 16:51:30 +0000485 char *path;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000486 int res;
Guido van Rossum1ff6cb41991-04-08 20:59:13 +0000487
488#ifdef UTIME_STRUCT
489 struct utimbuf buf;
490#define ATIME buf.actime
491#define MTIME buf.modtime
492#define UTIME_ARG &buf
493
494#else
495 time_t buf[2];
496#define ATIME buf[0]
497#define MTIME buf[1]
498#define UTIME_ARG buf
499#endif
500
Guido van Rossumef0a00e1992-01-27 16:51:30 +0000501 if (!getargs(args, "(s(ll))", &path, &ATIME, &MTIME))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000502 return NULL;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000503 BGN_SAVE
504 res = utime(path, UTIME_ARG);
505 END_SAVE
506 if (res < 0)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000507 return posix_error();
508 INCREF(None);
509 return None;
Guido van Rossum1ff6cb41991-04-08 20:59:13 +0000510#undef UTIME_ARG
511#undef ATIME
512#undef MTIME
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000513}
514
Guido van Rossum85e3b011991-06-03 12:42:10 +0000515
516#ifndef MSDOS
517
Guido van Rossum3b066191991-06-04 19:40:25 +0000518/* Process operations */
Guido van Rossum85e3b011991-06-03 12:42:10 +0000519
520static object *
521posix__exit(self, args)
522 object *self;
523 object *args;
524{
525 int sts;
526 if (!getintarg(args, &sts))
527 return NULL;
528 _exit(sts);
529 /* NOTREACHED */
530}
531
532/* XXX To do: exece, execp */
533
534static object *
535posix_exec(self, args)
536 object *self;
537 object *args;
538{
Guido van Rossumef0a00e1992-01-27 16:51:30 +0000539 char *path;
540 object *argv;
Guido van Rossum85e3b011991-06-03 12:42:10 +0000541 char **argvlist;
542 int i, argc;
543 object *(*getitem) PROTO((object *, int));
544
545 /* exec has two arguments: (path, argv), where
546 argv is a list or tuple of strings. */
547
Guido van Rossumef0a00e1992-01-27 16:51:30 +0000548 if (!getargs(args, "(sO)", &path, &argv))
Guido van Rossum85e3b011991-06-03 12:42:10 +0000549 return NULL;
Guido van Rossum85e3b011991-06-03 12:42:10 +0000550 if (is_listobject(argv)) {
551 argc = getlistsize(argv);
552 getitem = getlistitem;
553 }
554 else if (is_tupleobject(argv)) {
555 argc = gettuplesize(argv);
556 getitem = gettupleitem;
557 }
Guido van Rossumef0a00e1992-01-27 16:51:30 +0000558 else {
559 badarg:
560 err_badarg();
561 return NULL;
562 }
Guido van Rossum85e3b011991-06-03 12:42:10 +0000563
564 argvlist = NEW(char *, argc+1);
565 if (argvlist == NULL)
566 return NULL;
567 for (i = 0; i < argc; i++) {
Guido van Rossumef0a00e1992-01-27 16:51:30 +0000568 if (!getstrarg((*getitem)(argv, i), &argvlist[i])) {
Guido van Rossum85e3b011991-06-03 12:42:10 +0000569 DEL(argvlist);
570 goto badarg;
571 }
Guido van Rossum85e3b011991-06-03 12:42:10 +0000572 }
573 argvlist[argc] = NULL;
574
Guido van Rossumef0a00e1992-01-27 16:51:30 +0000575 execv(path, argvlist);
Guido van Rossum85e3b011991-06-03 12:42:10 +0000576
577 /* If we get here it's definitely an error */
578
579 DEL(argvlist);
580 return posix_error();
581}
582
583static object *
584posix_fork(self, args)
585 object *self;
586 object *args;
587{
588 int pid;
Guido van Rossum50e61dc1992-03-27 17:22:31 +0000589 if (!getnoarg(args))
590 return NULL;
Guido van Rossum85e3b011991-06-03 12:42:10 +0000591 pid = fork();
592 if (pid == -1)
593 return posix_error();
594 return newintobject((long)pid);
595}
596
597static object *
Guido van Rossum46003ff1992-05-15 11:05:24 +0000598posix_getegid(self, args)
599 object *self;
600 object *args;
601{
602 if (!getnoarg(args))
603 return NULL;
604 return newintobject((long)getegid());
605}
606
607static object *
608posix_geteuid(self, args)
609 object *self;
610 object *args;
611{
612 if (!getnoarg(args))
613 return NULL;
614 return newintobject((long)geteuid());
615}
616
617static object *
618posix_getgid(self, args)
619 object *self;
620 object *args;
621{
622 if (!getnoarg(args))
623 return NULL;
624 return newintobject((long)getgid());
625}
626
627static object *
Guido van Rossum85e3b011991-06-03 12:42:10 +0000628posix_getpid(self, args)
629 object *self;
630 object *args;
631{
Guido van Rossum04814471991-06-04 20:23:49 +0000632 if (!getnoarg(args))
Guido van Rossum85e3b011991-06-03 12:42:10 +0000633 return NULL;
634 return newintobject((long)getpid());
635}
636
637static object *
Guido van Rossum04814471991-06-04 20:23:49 +0000638posix_getpgrp(self, args)
639 object *self;
640 object *args;
641{
642 if (!getnoarg(args))
643 return NULL;
Guido van Rossum50e61dc1992-03-27 17:22:31 +0000644#ifdef SYSV
645 return newintobject((long)getpgrp());
646#else
Guido van Rossum971443b1991-06-07 13:59:29 +0000647 return newintobject((long)getpgrp(0));
Guido van Rossum50e61dc1992-03-27 17:22:31 +0000648#endif
Guido van Rossum04814471991-06-04 20:23:49 +0000649}
650
651static object *
Guido van Rossumc2670a01992-09-13 20:07:29 +0000652posix_setpgrp(self, args)
653 object *self;
654 object *args;
655{
656 if (!getnoarg(args))
657 return NULL;
658#ifdef SYSV
659 if (setpgrp() < 0)
660#else
661 if (setpgrp(0, 0) < 0)
662#endif
Guido van Rossum687dd131993-05-17 08:34:16 +0000663 return posix_error();
Guido van Rossumc2670a01992-09-13 20:07:29 +0000664 INCREF(None);
665 return None;
666}
667
668static object *
Guido van Rossum85e3b011991-06-03 12:42:10 +0000669posix_getppid(self, args)
670 object *self;
671 object *args;
672{
Guido van Rossum04814471991-06-04 20:23:49 +0000673 if (!getnoarg(args))
Guido van Rossum85e3b011991-06-03 12:42:10 +0000674 return NULL;
675 return newintobject((long)getppid());
676}
677
678static object *
Guido van Rossum46003ff1992-05-15 11:05:24 +0000679posix_getuid(self, args)
680 object *self;
681 object *args;
682{
683 if (!getnoarg(args))
684 return NULL;
685 return newintobject((long)getuid());
686}
687
688static object *
Guido van Rossum85e3b011991-06-03 12:42:10 +0000689posix_kill(self, args)
690 object *self;
691 object *args;
692{
693 int pid, sig;
Guido van Rossum234f9421993-06-17 12:35:49 +0000694 if (!getargs(args, "(ii)", &pid, &sig))
Guido van Rossum85e3b011991-06-03 12:42:10 +0000695 return NULL;
696 if (kill(pid, sig) == -1)
697 return posix_error();
698 INCREF(None);
699 return None;
700}
701
702static object *
Guido van Rossum3b066191991-06-04 19:40:25 +0000703posix_popen(self, args)
704 object *self;
705 object *args;
706{
Guido van Rossumef0a00e1992-01-27 16:51:30 +0000707 char *name, *mode;
Guido van Rossum3b066191991-06-04 19:40:25 +0000708 FILE *fp;
Guido van Rossumef0a00e1992-01-27 16:51:30 +0000709 if (!getargs(args, "(ss)", &name, &mode))
Guido van Rossum3b066191991-06-04 19:40:25 +0000710 return NULL;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000711 BGN_SAVE
Guido van Rossumef0a00e1992-01-27 16:51:30 +0000712 fp = popen(name, mode);
Guido van Rossumff4949e1992-08-05 19:58:53 +0000713 END_SAVE
Guido van Rossum3b066191991-06-04 19:40:25 +0000714 if (fp == NULL)
715 return posix_error();
Guido van Rossume0d452d1991-07-27 21:41:01 +0000716 /* From now on, ignore SIGPIPE and let the error checking
717 do the work. */
718 (void) signal(SIGPIPE, SIG_IGN);
Guido van Rossumef0a00e1992-01-27 16:51:30 +0000719 return newopenfileobject(fp, name, mode, pclose);
Guido van Rossum3b066191991-06-04 19:40:25 +0000720}
721
722static object *
Guido van Rossum21803b81992-08-09 12:55:27 +0000723posix_waitpid(self, args)
Guido van Rossum85e3b011991-06-03 12:42:10 +0000724 object *self;
725 object *args;
726{
Guido van Rossum85e3b011991-06-03 12:42:10 +0000727#ifdef NO_WAITPID
Guido van Rossum21803b81992-08-09 12:55:27 +0000728 err_setstr(PosixError,
729 "posix.waitpid() not supported on this system");
730 return NULL;
Guido van Rossum85e3b011991-06-03 12:42:10 +0000731#else
Guido van Rossum21803b81992-08-09 12:55:27 +0000732 int pid, options, sts;
733 if (!getargs(args, "(ii)", &pid, &options))
734 return NULL;
735 BGN_SAVE
736 pid = waitpid(pid, &sts, options);
737 END_SAVE
Guido van Rossum85e3b011991-06-03 12:42:10 +0000738 if (pid == -1)
739 return posix_error();
Guido van Rossum21803b81992-08-09 12:55:27 +0000740 else
741 return mkvalue("ii", pid, sts);
742#endif
743}
744
745static object *
746posix_wait(self, args)
747 object *self;
748 object *args;
749{
750 int pid, sts;
751 if (args != NULL)
752 return posix_waitpid(self, args); /* BW compat */
753 BGN_SAVE
754 pid = wait(&sts);
755 END_SAVE
756 if (pid == -1)
757 return posix_error();
758 else
759 return mkvalue("ii", pid, sts);
Guido van Rossum85e3b011991-06-03 12:42:10 +0000760}
761
762#endif /* MSDOS */
763
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000764static object *
765posix_lstat(self, args)
766 object *self;
767 object *args;
768{
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000769 return posix_do_stat(self, args, lstat);
770}
771
772static object *
773posix_readlink(self, args)
774 object *self;
775 object *args;
776{
Guido van Rossumc39de5f1992-02-05 11:15:54 +0000777#ifdef NO_LSTAT
778 err_setstr(PosixError, "readlink not implemented on this system");
779 return NULL;
780#else
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000781 char buf[1024]; /* XXX Should use MAXPATHLEN */
Guido van Rossumef0a00e1992-01-27 16:51:30 +0000782 char *path;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000783 int n;
784 if (!getstrarg(args, &path))
785 return NULL;
Guido van Rossumff4949e1992-08-05 19:58:53 +0000786 BGN_SAVE
Guido van Rossum50e61dc1992-03-27 17:22:31 +0000787 n = readlink(path, buf, (int) sizeof buf);
Guido van Rossumff4949e1992-08-05 19:58:53 +0000788 END_SAVE
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000789 if (n < 0)
790 return posix_error();
791 return newsizedstringobject(buf, n);
Guido van Rossumc39de5f1992-02-05 11:15:54 +0000792#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000793}
794
795static object *
796posix_symlink(self, args)
797 object *self;
798 object *args;
799{
Guido van Rossumc39de5f1992-02-05 11:15:54 +0000800#ifdef NO_LSTAT
801 err_setstr(PosixError, "symlink not implemented on this system");
802 return NULL;
803#else
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000804 return posix_2str(args, symlink);
Guido van Rossumc39de5f1992-02-05 11:15:54 +0000805#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000806}
807
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000808
Guido van Rossum22db57e1992-04-05 14:25:30 +0000809#ifdef DO_TIMES
810
811static object *
812posix_times(self, args)
813 object *self;
814 object *args;
815{
816 struct tms t;
817 clock_t c;
Guido van Rossum22db57e1992-04-05 14:25:30 +0000818 if (!getnoarg(args))
819 return NULL;
820 errno = 0;
821 c = times(&t);
Guido van Rossum687dd131993-05-17 08:34:16 +0000822 if (c == (clock_t) -1)
823 return posix_error();
Guido van Rossum0b0db8e1993-01-21 16:07:51 +0000824 return mkvalue("dddd",
825 (double)t.tms_utime / HZ,
826 (double)t.tms_stime / HZ,
827 (double)t.tms_cutime / HZ,
828 (double)t.tms_cstime / HZ);
Guido van Rossum22db57e1992-04-05 14:25:30 +0000829}
830
Guido van Rossumc2670a01992-09-13 20:07:29 +0000831#endif /* DO_TIMES */
832
833#ifdef DO_PG
834
835static object *
836posix_setsid(self, args)
837 object *self;
838 object *args;
839{
840 if (!getnoarg(args))
841 return NULL;
Guido van Rossum687dd131993-05-17 08:34:16 +0000842 if (setsid() < 0)
843 return posix_error();
Guido van Rossumc2670a01992-09-13 20:07:29 +0000844 INCREF(None);
845 return None;
846}
847
848static object *
849posix_setpgid(self, args)
850 object *self;
851 object *args;
852{
853 int pid, pgrp;
854 if (!getargs(args, "(ii)", &pid, &pgrp))
855 return NULL;
Guido van Rossum687dd131993-05-17 08:34:16 +0000856 if (setpgid(pid, pgrp) < 0)
857 return posix_error();
Guido van Rossumc2670a01992-09-13 20:07:29 +0000858 INCREF(None);
859 return None;
860}
861
Guido van Rossum7066dd71992-09-17 17:54:56 +0000862static object *
863posix_tcgetpgrp(self, args)
864 object *self;
865 object *args;
866{
867 int fd, pgid;
868 if (!getargs(args, "i", &fd))
869 return NULL;
870 pgid = tcgetpgrp(fd);
Guido van Rossum687dd131993-05-17 08:34:16 +0000871 if (pgid < 0)
872 return posix_error();
Guido van Rossum7066dd71992-09-17 17:54:56 +0000873 return newintobject((long)pgid);
874}
875
876static object *
877posix_tcsetpgrp(self, args)
878 object *self;
879 object *args;
880{
881 int fd, pgid;
882 if (!getargs(args, "(ii)", &fd, &pgid))
883 return NULL;
Guido van Rossum687dd131993-05-17 08:34:16 +0000884 if (tcsetpgrp(fd, pgid) < 0)
885 return posix_error();
Guido van Rossum7066dd71992-09-17 17:54:56 +0000886 INCREF(None);
887 return None;
888}
889
Guido van Rossumc2670a01992-09-13 20:07:29 +0000890#endif /* DO_PG */
Guido van Rossum22db57e1992-04-05 14:25:30 +0000891
Guido van Rossum687dd131993-05-17 08:34:16 +0000892/* Functions acting on file descriptors */
893
Guido van Rossum234f9421993-06-17 12:35:49 +0000894static object *
Guido van Rossum687dd131993-05-17 08:34:16 +0000895posix_open(self, args)
896 object *self;
897 object *args;
898{
899 char *file;
900 int flag;
901 int mode = 0777;
902 int fd;
903 if (!getargs(args, "(si)", &file, &flag)) {
904 err_clear();
905 if (!getargs(args, "(sii)", &file, &flag, &mode))
906 return NULL;
907 }
908 BGN_SAVE
909 fd = open(file, flag, mode);
910 END_SAVE
911 if (fd < 0)
912 return posix_error();
913 return newintobject((long)fd);
914}
915
Guido van Rossum234f9421993-06-17 12:35:49 +0000916static object *
Guido van Rossum687dd131993-05-17 08:34:16 +0000917posix_close(self, args)
918 object *self;
919 object *args;
920{
921 int fd, res;
922 if (!getargs(args, "i", &fd))
923 return NULL;
924 BGN_SAVE
925 res = close(fd);
926 END_SAVE
927 if (res < 0)
928 return posix_error();
929 INCREF(None);
930 return None;
931}
932
Guido van Rossum234f9421993-06-17 12:35:49 +0000933static object *
Guido van Rossum687dd131993-05-17 08:34:16 +0000934posix_dup(self, args)
935 object *self;
936 object *args;
937{
938 int fd;
939 if (!getargs(args, "i", &fd))
940 return NULL;
941 BGN_SAVE
942 fd = dup(fd);
943 END_SAVE
944 if (fd < 0)
945 return posix_error();
946 return newintobject((long)fd);
947}
948
Guido van Rossum234f9421993-06-17 12:35:49 +0000949static object *
Guido van Rossum687dd131993-05-17 08:34:16 +0000950posix_dup2(self, args)
951 object *self;
952 object *args;
953{
954 int fd, fd2, res;
955 if (!getargs(args, "(ii)", &fd, &fd2))
956 return NULL;
957 BGN_SAVE
958 res = dup2(fd, fd2);
959 END_SAVE
960 if (res < 0)
961 return posix_error();
962 INCREF(None);
963 return None;
964}
965
Guido van Rossum234f9421993-06-17 12:35:49 +0000966static object *
Guido van Rossum687dd131993-05-17 08:34:16 +0000967posix_lseek(self, args)
968 object *self;
969 object *args;
970{
971 int fd, how;
972 long pos, res;
973 if (!getargs(args, "(ili)", &fd, &pos, &how))
974 return NULL;
975#ifdef SEEK_SET
976 /* Turn 0, 1, 2 into SEEK_{SET,CUR,END} */
977 switch (how) {
978 case 0: how = SEEK_SET; break;
979 case 1: how = SEEK_CUR; break;
980 case 2: how = SEEK_END; break;
981 }
982#endif
983 BGN_SAVE
984 res = lseek(fd, pos, how);
985 END_SAVE
986 if (res < 0)
987 return posix_error();
988 return newintobject(res);
989}
990
Guido van Rossum234f9421993-06-17 12:35:49 +0000991static object *
Guido van Rossum687dd131993-05-17 08:34:16 +0000992posix_read(self, args)
993 object *self;
994 object *args;
995{
996 int fd, size;
997 object *buffer;
998 if (!getargs(args, "(ii)", &fd, &size))
999 return NULL;
1000 buffer = newsizedstringobject((char *)NULL, size);
1001 if (buffer == NULL)
1002 return NULL;
1003 BGN_SAVE
1004 size = read(fd, getstringvalue(buffer), size);
1005 END_SAVE
1006 if (size < 0) {
1007 DECREF(buffer);
1008 return posix_error();
1009 }
1010 resizestring(&buffer, size);
1011 return buffer;
1012}
1013
Guido van Rossum234f9421993-06-17 12:35:49 +00001014static object *
Guido van Rossum687dd131993-05-17 08:34:16 +00001015posix_write(self, args)
1016 object *self;
1017 object *args;
1018{
1019 int fd, size;
1020 char *buffer;
1021 if (!getargs(args, "(is#)", &fd, &buffer, &size))
1022 return NULL;
1023 BGN_SAVE
1024 size = write(fd, buffer, size);
1025 END_SAVE
1026 if (size < 0)
1027 return posix_error();
1028 return newintobject((long)size);
1029}
1030
Guido van Rossum234f9421993-06-17 12:35:49 +00001031static object *
Guido van Rossum687dd131993-05-17 08:34:16 +00001032posix_fstat(self, args)
1033 object *self;
1034 object *args;
1035{
1036 int fd;
1037 struct stat st;
1038 int res;
1039 if (!getargs(args, "i", &fd))
1040 return NULL;
1041 BGN_SAVE
1042 res = fstat(fd, &st);
1043 END_SAVE
1044 if (res != 0)
1045 return posix_error();
1046 return mkvalue("(llllllllll)",
1047 (long)st.st_mode,
1048 (long)st.st_ino,
1049 (long)st.st_dev,
1050 (long)st.st_nlink,
1051 (long)st.st_uid,
1052 (long)st.st_gid,
1053 (long)st.st_size,
1054 (long)st.st_atime,
1055 (long)st.st_mtime,
1056 (long)st.st_ctime);
1057}
1058
1059static object *
1060posix_fdopen(self, args)
1061 object *self;
1062 object *args;
1063{
1064 extern int fclose PROTO((FILE *));
1065 int fd;
1066 char *mode;
1067 FILE *fp;
1068 if (!getargs(args, "(is)", &fd, &mode))
1069 return NULL;
1070 BGN_SAVE
1071 fp = fdopen(fd, mode);
1072 END_SAVE
1073 if (fp == NULL)
1074 return posix_error();
1075 /* From now on, ignore SIGPIPE and let the error checking
1076 do the work. */
1077 (void) signal(SIGPIPE, SIG_IGN);
1078 return newopenfileobject(fp, "(fdopen)", mode, fclose);
1079}
1080
1081#ifndef MSDOS
Guido van Rossum234f9421993-06-17 12:35:49 +00001082static object *
Guido van Rossum687dd131993-05-17 08:34:16 +00001083posix_pipe(self, args)
1084 object *self;
1085 object *args;
1086{
1087 int fds[2];
1088 int res;
1089 if (!getargs(args, ""))
1090 return NULL;
1091 BGN_SAVE
1092 res = pipe(fds);
1093 END_SAVE
1094 if (res != 0)
1095 return posix_error();
1096 return mkvalue("(ii)", fds[0], fds[1]);
1097}
1098#endif /* MSDOS */
Guido van Rossum22db57e1992-04-05 14:25:30 +00001099
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001100static struct methodlist posix_methods[] = {
1101 {"chdir", posix_chdir},
1102 {"chmod", posix_chmod},
1103 {"getcwd", posix_getcwd},
Guido van Rossum0ee42cd1991-04-08 21:01:03 +00001104#ifndef MSDOS
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001105 {"link", posix_link},
Guido van Rossum0ee42cd1991-04-08 21:01:03 +00001106#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001107 {"listdir", posix_listdir},
Guido van Rossumc39de5f1992-02-05 11:15:54 +00001108 {"lstat", posix_lstat},
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001109 {"mkdir", posix_mkdir},
Guido van Rossum775f4da1993-01-09 17:18:52 +00001110#ifndef MSDOS
1111 {"nice", posix_nice},
1112#endif
Guido van Rossumc39de5f1992-02-05 11:15:54 +00001113 {"readlink", posix_readlink},
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001114 {"rename", posix_rename},
1115 {"rmdir", posix_rmdir},
1116 {"stat", posix_stat},
Guido van Rossumc39de5f1992-02-05 11:15:54 +00001117 {"symlink", posix_symlink},
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001118 {"system", posix_system},
Guido van Rossum0ee42cd1991-04-08 21:01:03 +00001119#ifndef MSDOS
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001120 {"umask", posix_umask},
Guido van Rossum0ee42cd1991-04-08 21:01:03 +00001121#endif
Guido van Rossumc39de5f1992-02-05 11:15:54 +00001122#ifndef NO_UNAME
1123 {"uname", posix_uname},
1124#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001125 {"unlink", posix_unlink},
Guido van Rossum1ff6cb41991-04-08 20:59:13 +00001126 {"utime", posix_utime},
Guido van Rossum22db57e1992-04-05 14:25:30 +00001127#ifdef DO_TIMES
1128 {"times", posix_times},
1129#endif
Guido van Rossumc39de5f1992-02-05 11:15:54 +00001130
Guido van Rossum85e3b011991-06-03 12:42:10 +00001131#ifndef MSDOS
1132 {"_exit", posix__exit},
1133 {"exec", posix_exec},
1134 {"fork", posix_fork},
Guido van Rossum46003ff1992-05-15 11:05:24 +00001135 {"getegid", posix_getegid},
1136 {"geteuid", posix_geteuid},
1137 {"getgid", posix_getgid},
Guido van Rossum85e3b011991-06-03 12:42:10 +00001138 {"getpid", posix_getpid},
Guido van Rossum04814471991-06-04 20:23:49 +00001139 {"getpgrp", posix_getpgrp},
Guido van Rossum85e3b011991-06-03 12:42:10 +00001140 {"getppid", posix_getppid},
Guido van Rossum46003ff1992-05-15 11:05:24 +00001141 {"getuid", posix_getuid},
Guido van Rossum85e3b011991-06-03 12:42:10 +00001142 {"kill", posix_kill},
Guido van Rossum3b066191991-06-04 19:40:25 +00001143 {"popen", posix_popen},
Guido van Rossumc2670a01992-09-13 20:07:29 +00001144 {"setpgrp", posix_setpgrp},
Guido van Rossum85e3b011991-06-03 12:42:10 +00001145 {"wait", posix_wait},
Guido van Rossum21803b81992-08-09 12:55:27 +00001146 {"waitpid", posix_waitpid},
Guido van Rossum85e3b011991-06-03 12:42:10 +00001147#endif
Guido van Rossumc39de5f1992-02-05 11:15:54 +00001148
Guido van Rossumc2670a01992-09-13 20:07:29 +00001149#ifdef DO_PG
1150 {"setsid", posix_setsid},
1151 {"setpgid", posix_setpgid},
Guido van Rossum7066dd71992-09-17 17:54:56 +00001152 {"tcgetpgrp", posix_tcgetpgrp},
1153 {"tcsetpgrp", posix_tcsetpgrp},
Guido van Rossumc2670a01992-09-13 20:07:29 +00001154#endif
1155
Guido van Rossum687dd131993-05-17 08:34:16 +00001156 {"open", posix_open},
1157 {"close", posix_close},
1158 {"dup", posix_dup},
1159 {"dup2", posix_dup2},
1160 {"lseek", posix_lseek},
1161 {"read", posix_read},
1162 {"write", posix_write},
1163 {"fstat", posix_fstat},
1164 {"fdopen", posix_fdopen},
1165#ifndef MSDOS
1166 {"pipe", posix_pipe},
1167#endif
1168
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001169 {NULL, NULL} /* Sentinel */
1170};
1171
1172
1173void
1174initposix()
1175{
1176 object *m, *d, *v;
1177
1178 m = initmodule("posix", posix_methods);
1179 d = getmoduledict(m);
1180
1181 /* Initialize posix.environ dictionary */
1182 v = convertenviron();
1183 if (v == NULL || dictinsert(d, "environ", v) != 0)
1184 fatal("can't define posix.environ");
1185 DECREF(v);
1186
1187 /* Initialize posix.error exception */
1188 PosixError = newstringobject("posix.error");
1189 if (PosixError == NULL || dictinsert(d, "error", PosixError) != 0)
1190 fatal("can't define posix.error");
1191}
Guido van Rossum0ee42cd1991-04-08 21:01:03 +00001192
Guido van Rossum3b066191991-06-04 19:40:25 +00001193
1194/* Function used elsewhere to get a file's modification time */
1195
1196long
1197getmtime(path)
1198 char *path;
1199{
1200 struct stat st;
1201 if (stat(path, &st) != 0)
1202 return -1;
1203 else
1204 return st.st_mtime;
1205}
1206
1207
Guido van Rossum0ee42cd1991-04-08 21:01:03 +00001208#ifdef MSDOS
1209
1210/* A small "compatibility library" for TurboC under MS-DOS */
1211
1212#include <sir.h>
1213#include <io.h>
1214#include <dos.h>
1215#include <fcntl.h>
1216
1217int
1218chmod(path, mode)
1219 char *path;
1220 int mode;
1221{
1222 return _chmod(path, 1, mode);
1223}
1224
1225int
1226utime(path, times)
1227 char *path;
1228 time_t times[2];
1229{
1230 struct date dt;
1231 struct time tm;
1232 struct ftime dft;
1233 int fh;
1234 unixtodos(tv[0].tv_sec,&dt,&tm);
1235 dft.ft_tsec = tm.ti_sec; dft.ft_min = tm.ti_min;
1236 dft.ft_hour = tm.ti_hour; dft.ft_day = dt.da_day;
1237 dft.ft_month = dt.da_mon;
1238 dft.ft_year = (dt.da_year - 1980); /* this is for TC library */
1239
Guido van Rossumef0a00e1992-01-27 16:51:30 +00001240 if ((fh = open(path,O_RDWR)) < 0)
Guido van Rossum0ee42cd1991-04-08 21:01:03 +00001241 return posix_error(); /* can't open file to set time */
1242 if (setftime(fh,&dft) < 0)
1243 {
1244 close(fh);
1245 return posix_error();
1246 }
1247 close(fh); /* close the temp handle */
1248}
1249
1250#endif /* MSDOS */