annotate Python/getcwd.c @ 1590:ac2e5cc29949 legacy-trunk

Added tests for missing host to open_http and open_gopher
author Guido van Rossum <guido@python.org>
date Mon, 18 Apr 1994 09:39:56 +0000
parents cd397266dc1c
children b724599401ff
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
145
853b736a6a9e Added copyright notice.
Guido van Rossum <guido@python.org>
parents: 144
diff changeset
1 /***********************************************************
1257
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 795
diff changeset
2 Copyright 1991, 1992, 1993 by Stichting Mathematisch Centrum,
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 795
diff changeset
3 Amsterdam, The Netherlands.
145
853b736a6a9e Added copyright notice.
Guido van Rossum <guido@python.org>
parents: 144
diff changeset
4
853b736a6a9e Added copyright notice.
Guido van Rossum <guido@python.org>
parents: 144
diff changeset
5 All Rights Reserved
853b736a6a9e Added copyright notice.
Guido van Rossum <guido@python.org>
parents: 144
diff changeset
6
853b736a6a9e Added copyright notice.
Guido van Rossum <guido@python.org>
parents: 144
diff changeset
7 Permission to use, copy, modify, and distribute this software and its
853b736a6a9e Added copyright notice.
Guido van Rossum <guido@python.org>
parents: 144
diff changeset
8 documentation for any purpose and without fee is hereby granted,
853b736a6a9e Added copyright notice.
Guido van Rossum <guido@python.org>
parents: 144
diff changeset
9 provided that the above copyright notice appear in all copies and that
853b736a6a9e Added copyright notice.
Guido van Rossum <guido@python.org>
parents: 144
diff changeset
10 both that copyright notice and this permission notice appear in
853b736a6a9e Added copyright notice.
Guido van Rossum <guido@python.org>
parents: 144
diff changeset
11 supporting documentation, and that the names of Stichting Mathematisch
853b736a6a9e Added copyright notice.
Guido van Rossum <guido@python.org>
parents: 144
diff changeset
12 Centrum or CWI not be used in advertising or publicity pertaining to
853b736a6a9e Added copyright notice.
Guido van Rossum <guido@python.org>
parents: 144
diff changeset
13 distribution of the software without specific, written prior permission.
853b736a6a9e Added copyright notice.
Guido van Rossum <guido@python.org>
parents: 144
diff changeset
14
853b736a6a9e Added copyright notice.
Guido van Rossum <guido@python.org>
parents: 144
diff changeset
15 STICHTING MATHEMATISCH CENTRUM DISCLAIMS ALL WARRANTIES WITH REGARD TO
853b736a6a9e Added copyright notice.
Guido van Rossum <guido@python.org>
parents: 144
diff changeset
16 THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
853b736a6a9e Added copyright notice.
Guido van Rossum <guido@python.org>
parents: 144
diff changeset
17 FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH CENTRUM BE LIABLE
853b736a6a9e Added copyright notice.
Guido van Rossum <guido@python.org>
parents: 144
diff changeset
18 FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
853b736a6a9e Added copyright notice.
Guido van Rossum <guido@python.org>
parents: 144
diff changeset
19 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
853b736a6a9e Added copyright notice.
Guido van Rossum <guido@python.org>
parents: 144
diff changeset
20 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
853b736a6a9e Added copyright notice.
Guido van Rossum <guido@python.org>
parents: 144
diff changeset
21 OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
853b736a6a9e Added copyright notice.
Guido van Rossum <guido@python.org>
parents: 144
diff changeset
22
853b736a6a9e Added copyright notice.
Guido van Rossum <guido@python.org>
parents: 144
diff changeset
23 ******************************************************************/
853b736a6a9e Added copyright notice.
Guido van Rossum <guido@python.org>
parents: 144
diff changeset
24
144
c70eefe48029 Added version that opens a pipe to /bin/pwd.
Guido van Rossum <guido@python.org>
parents: 116
diff changeset
25 /* Two PD getcwd() implementations.
c70eefe48029 Added version that opens a pipe to /bin/pwd.
Guido van Rossum <guido@python.org>
parents: 116
diff changeset
26 Author: Guido van Rossum, CWI Amsterdam, Jan 1991, <guido@cwi.nl>. */
c70eefe48029 Added version that opens a pipe to /bin/pwd.
Guido van Rossum <guido@python.org>
parents: 116
diff changeset
27
c70eefe48029 Added version that opens a pipe to /bin/pwd.
Guido van Rossum <guido@python.org>
parents: 116
diff changeset
28 /* #define NO_GETWD /* Turn this on to popen pwd instead of calling getwd() */
c70eefe48029 Added version that opens a pipe to /bin/pwd.
Guido van Rossum <guido@python.org>
parents: 116
diff changeset
29
c70eefe48029 Added version that opens a pipe to /bin/pwd.
Guido van Rossum <guido@python.org>
parents: 116
diff changeset
30 #include <stdio.h>
c70eefe48029 Added version that opens a pipe to /bin/pwd.
Guido van Rossum <guido@python.org>
parents: 116
diff changeset
31 #include <errno.h>
c70eefe48029 Added version that opens a pipe to /bin/pwd.
Guido van Rossum <guido@python.org>
parents: 116
diff changeset
32
c70eefe48029 Added version that opens a pipe to /bin/pwd.
Guido van Rossum <guido@python.org>
parents: 116
diff changeset
33 #ifndef NO_GETWD
c70eefe48029 Added version that opens a pipe to /bin/pwd.
Guido van Rossum <guido@python.org>
parents: 116
diff changeset
34
c70eefe48029 Added version that opens a pipe to /bin/pwd.
Guido van Rossum <guido@python.org>
parents: 116
diff changeset
35 /* Default: Version for BSD systems -- use getwd() */
116
0a02ca9dcd6e Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
36
0a02ca9dcd6e Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
37 #include "sys/param.h"
0a02ca9dcd6e Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
38
0a02ca9dcd6e Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
39 extern char *getwd();
0a02ca9dcd6e Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
40
0a02ca9dcd6e Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
41 char *
0a02ca9dcd6e Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
42 getcwd(buf, size)
0a02ca9dcd6e Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
43 char *buf;
0a02ca9dcd6e Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
44 int size;
0a02ca9dcd6e Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
45 {
0a02ca9dcd6e Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
46 char localbuf[MAXPATHLEN+1];
0a02ca9dcd6e Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
47 char *ret;
0a02ca9dcd6e Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
48
0a02ca9dcd6e Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
49 if (size <= 0) {
0a02ca9dcd6e Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
50 errno = EINVAL;
0a02ca9dcd6e Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
51 return NULL;
0a02ca9dcd6e Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
52 }
0a02ca9dcd6e Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
53 ret = getwd(localbuf);
0a02ca9dcd6e Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
54 if (ret != NULL && strlen(localbuf) >= size) {
0a02ca9dcd6e Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
55 errno = ERANGE;
0a02ca9dcd6e Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
56 return NULL;
0a02ca9dcd6e Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
57 }
0a02ca9dcd6e Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
58 if (ret == NULL) {
0a02ca9dcd6e Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
59 errno = EACCES; /* Most likely error */
0a02ca9dcd6e Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
60 return NULL;
0a02ca9dcd6e Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
61 }
0a02ca9dcd6e Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
62 strncpy(buf, localbuf, size);
0a02ca9dcd6e Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
63 return buf;
0a02ca9dcd6e Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
64 }
0a02ca9dcd6e Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
65
144
c70eefe48029 Added version that opens a pipe to /bin/pwd.
Guido van Rossum <guido@python.org>
parents: 116
diff changeset
66 #else
c70eefe48029 Added version that opens a pipe to /bin/pwd.
Guido van Rossum <guido@python.org>
parents: 116
diff changeset
67
c70eefe48029 Added version that opens a pipe to /bin/pwd.
Guido van Rossum <guido@python.org>
parents: 116
diff changeset
68 /* NO_GETWD defined: Version for backward UNIXes -- popen /bin/pwd */
c70eefe48029 Added version that opens a pipe to /bin/pwd.
Guido van Rossum <guido@python.org>
parents: 116
diff changeset
69
c70eefe48029 Added version that opens a pipe to /bin/pwd.
Guido van Rossum <guido@python.org>
parents: 116
diff changeset
70 #define PWD_CMD "/bin/pwd"
c70eefe48029 Added version that opens a pipe to /bin/pwd.
Guido van Rossum <guido@python.org>
parents: 116
diff changeset
71
c70eefe48029 Added version that opens a pipe to /bin/pwd.
Guido van Rossum <guido@python.org>
parents: 116
diff changeset
72 char *
c70eefe48029 Added version that opens a pipe to /bin/pwd.
Guido van Rossum <guido@python.org>
parents: 116
diff changeset
73 getcwd(buf, size)
c70eefe48029 Added version that opens a pipe to /bin/pwd.
Guido van Rossum <guido@python.org>
parents: 116
diff changeset
74 char *buf;
c70eefe48029 Added version that opens a pipe to /bin/pwd.
Guido van Rossum <guido@python.org>
parents: 116
diff changeset
75 int size;
c70eefe48029 Added version that opens a pipe to /bin/pwd.
Guido van Rossum <guido@python.org>
parents: 116
diff changeset
76 {
c70eefe48029 Added version that opens a pipe to /bin/pwd.
Guido van Rossum <guido@python.org>
parents: 116
diff changeset
77 FILE *fp;
c70eefe48029 Added version that opens a pipe to /bin/pwd.
Guido van Rossum <guido@python.org>
parents: 116
diff changeset
78 char *p;
c70eefe48029 Added version that opens a pipe to /bin/pwd.
Guido van Rossum <guido@python.org>
parents: 116
diff changeset
79 int sts;
c70eefe48029 Added version that opens a pipe to /bin/pwd.
Guido van Rossum <guido@python.org>
parents: 116
diff changeset
80 if (size <= 0) {
c70eefe48029 Added version that opens a pipe to /bin/pwd.
Guido van Rossum <guido@python.org>
parents: 116
diff changeset
81 errno = EINVAL;
c70eefe48029 Added version that opens a pipe to /bin/pwd.
Guido van Rossum <guido@python.org>
parents: 116
diff changeset
82 return NULL;
c70eefe48029 Added version that opens a pipe to /bin/pwd.
Guido van Rossum <guido@python.org>
parents: 116
diff changeset
83 }
c70eefe48029 Added version that opens a pipe to /bin/pwd.
Guido van Rossum <guido@python.org>
parents: 116
diff changeset
84 if ((fp = popen(PWD_CMD, "r")) == NULL)
c70eefe48029 Added version that opens a pipe to /bin/pwd.
Guido van Rossum <guido@python.org>
parents: 116
diff changeset
85 return NULL;
c70eefe48029 Added version that opens a pipe to /bin/pwd.
Guido van Rossum <guido@python.org>
parents: 116
diff changeset
86 if (fgets(buf, size, fp) == NULL || (sts = pclose(fp)) != 0) {
c70eefe48029 Added version that opens a pipe to /bin/pwd.
Guido van Rossum <guido@python.org>
parents: 116
diff changeset
87 errno = EACCES; /* Most likely error */
c70eefe48029 Added version that opens a pipe to /bin/pwd.
Guido van Rossum <guido@python.org>
parents: 116
diff changeset
88 return NULL;
c70eefe48029 Added version that opens a pipe to /bin/pwd.
Guido van Rossum <guido@python.org>
parents: 116
diff changeset
89 }
c70eefe48029 Added version that opens a pipe to /bin/pwd.
Guido van Rossum <guido@python.org>
parents: 116
diff changeset
90 for (p = buf; *p != '\n'; p++) {
c70eefe48029 Added version that opens a pipe to /bin/pwd.
Guido van Rossum <guido@python.org>
parents: 116
diff changeset
91 if (*p == '\0') {
c70eefe48029 Added version that opens a pipe to /bin/pwd.
Guido van Rossum <guido@python.org>
parents: 116
diff changeset
92 errno = ERANGE;
c70eefe48029 Added version that opens a pipe to /bin/pwd.
Guido van Rossum <guido@python.org>
parents: 116
diff changeset
93 return NULL;
c70eefe48029 Added version that opens a pipe to /bin/pwd.
Guido van Rossum <guido@python.org>
parents: 116
diff changeset
94 }
c70eefe48029 Added version that opens a pipe to /bin/pwd.
Guido van Rossum <guido@python.org>
parents: 116
diff changeset
95 }
c70eefe48029 Added version that opens a pipe to /bin/pwd.
Guido van Rossum <guido@python.org>
parents: 116
diff changeset
96 *p = '\0';
c70eefe48029 Added version that opens a pipe to /bin/pwd.
Guido van Rossum <guido@python.org>
parents: 116
diff changeset
97 return buf;
c70eefe48029 Added version that opens a pipe to /bin/pwd.
Guido van Rossum <guido@python.org>
parents: 116
diff changeset
98 }
c70eefe48029 Added version that opens a pipe to /bin/pwd.
Guido van Rossum <guido@python.org>
parents: 116
diff changeset
99
c70eefe48029 Added version that opens a pipe to /bin/pwd.
Guido van Rossum <guido@python.org>
parents: 116
diff changeset
100 #endif