Mercurial > cpython-lru_cache-weakref
annotate Python/getcwd.c @ 3930:52c19ee83a6b legacy-trunk
Guido's 1.4b2 fixes: Py_GetProgramName, Py_GetPrefix and Py_GetExecPrefix.
| author | Jack Jansen <jack.jansen@cwi.nl> |
|---|---|
| date | Fri, 02 Aug 1996 15:16:16 +0000 |
| parents | d0d0a69b5ca3 |
| children | 65d2d8e67da3 |
| rev | line source |
|---|---|
|
145
853b736a6a9e
Added copyright notice.
Guido van Rossum <guido@python.org>
parents:
144
diff
changeset
|
1 /*********************************************************** |
|
2062
d0d0a69b5ca3
Added 1995 to copyright message.
Guido van Rossum <guido@python.org>
parents:
1848
diff
changeset
|
2 Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam, |
|
d0d0a69b5ca3
Added 1995 to copyright message.
Guido van Rossum <guido@python.org>
parents:
1848
diff
changeset
|
3 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 #include <stdio.h> |
|
c70eefe48029
Added version that opens a pipe to /bin/pwd.
Guido van Rossum <guido@python.org>
parents:
116
diff
changeset
|
29 #include <errno.h> |
|
c70eefe48029
Added version that opens a pipe to /bin/pwd.
Guido van Rossum <guido@python.org>
parents:
116
diff
changeset
|
30 |
|
1848
b724599401ff
Merge back to main trunk
Guido van Rossum <guido@python.org>
parents:
1319
diff
changeset
|
31 #ifdef HAVE_GETWD |
|
b724599401ff
Merge back to main trunk
Guido van Rossum <guido@python.org>
parents:
1319
diff
changeset
|
32 |
|
b724599401ff
Merge back to main trunk
Guido van Rossum <guido@python.org>
parents:
1319
diff
changeset
|
33 /* Version for BSD systems -- use getwd() */ |
|
144
c70eefe48029
Added version that opens a pipe to /bin/pwd.
Guido van Rossum <guido@python.org>
parents:
116
diff
changeset
|
34 |
|
1848
b724599401ff
Merge back to main trunk
Guido van Rossum <guido@python.org>
parents:
1319
diff
changeset
|
35 #ifdef HAVE_SYS_PARAM_H |
|
b724599401ff
Merge back to main trunk
Guido van Rossum <guido@python.org>
parents:
1319
diff
changeset
|
36 #include <sys/param.h> |
|
b724599401ff
Merge back to main trunk
Guido van Rossum <guido@python.org>
parents:
1319
diff
changeset
|
37 #endif |
| 116 | 38 |
|
1848
b724599401ff
Merge back to main trunk
Guido van Rossum <guido@python.org>
parents:
1319
diff
changeset
|
39 #ifndef MAXPATHLEN |
|
b724599401ff
Merge back to main trunk
Guido van Rossum <guido@python.org>
parents:
1319
diff
changeset
|
40 #define MAXPATHLEN 1024 |
|
b724599401ff
Merge back to main trunk
Guido van Rossum <guido@python.org>
parents:
1319
diff
changeset
|
41 #endif |
| 116 | 42 |
| 43 extern char *getwd(); | |
| 44 | |
| 45 char * | |
| 46 getcwd(buf, size) | |
| 47 char *buf; | |
| 48 int size; | |
| 49 { | |
| 50 char localbuf[MAXPATHLEN+1]; | |
| 51 char *ret; | |
| 52 | |
| 53 if (size <= 0) { | |
| 54 errno = EINVAL; | |
| 55 return NULL; | |
| 56 } | |
| 57 ret = getwd(localbuf); | |
| 58 if (ret != NULL && strlen(localbuf) >= size) { | |
| 59 errno = ERANGE; | |
| 60 return NULL; | |
| 61 } | |
| 62 if (ret == NULL) { | |
| 63 errno = EACCES; /* Most likely error */ | |
| 64 return NULL; | |
| 65 } | |
| 66 strncpy(buf, localbuf, size); | |
| 67 return buf; | |
| 68 } | |
| 69 | |
|
1848
b724599401ff
Merge back to main trunk
Guido van Rossum <guido@python.org>
parents:
1319
diff
changeset
|
70 #else /* !HAVE_GETWD */ |
|
b724599401ff
Merge back to main trunk
Guido van Rossum <guido@python.org>
parents:
1319
diff
changeset
|
71 |
|
b724599401ff
Merge back to main trunk
Guido van Rossum <guido@python.org>
parents:
1319
diff
changeset
|
72 /* Version for really old UNIX systems -- use pipe from pwd */ |
|
144
c70eefe48029
Added version that opens a pipe to /bin/pwd.
Guido van Rossum <guido@python.org>
parents:
116
diff
changeset
|
73 |
|
1848
b724599401ff
Merge back to main trunk
Guido van Rossum <guido@python.org>
parents:
1319
diff
changeset
|
74 #ifndef PWD_CMD |
|
144
c70eefe48029
Added version that opens a pipe to /bin/pwd.
Guido van Rossum <guido@python.org>
parents:
116
diff
changeset
|
75 #define PWD_CMD "/bin/pwd" |
|
1848
b724599401ff
Merge back to main trunk
Guido van Rossum <guido@python.org>
parents:
1319
diff
changeset
|
76 #endif |
|
144
c70eefe48029
Added version that opens a pipe to /bin/pwd.
Guido van Rossum <guido@python.org>
parents:
116
diff
changeset
|
77 |
|
c70eefe48029
Added version that opens a pipe to /bin/pwd.
Guido van Rossum <guido@python.org>
parents:
116
diff
changeset
|
78 char * |
|
c70eefe48029
Added version that opens a pipe to /bin/pwd.
Guido van Rossum <guido@python.org>
parents:
116
diff
changeset
|
79 getcwd(buf, size) |
|
c70eefe48029
Added version that opens a pipe to /bin/pwd.
Guido van Rossum <guido@python.org>
parents:
116
diff
changeset
|
80 char *buf; |
|
c70eefe48029
Added version that opens a pipe to /bin/pwd.
Guido van Rossum <guido@python.org>
parents:
116
diff
changeset
|
81 int size; |
|
c70eefe48029
Added version that opens a pipe to /bin/pwd.
Guido van Rossum <guido@python.org>
parents:
116
diff
changeset
|
82 { |
|
c70eefe48029
Added version that opens a pipe to /bin/pwd.
Guido van Rossum <guido@python.org>
parents:
116
diff
changeset
|
83 FILE *fp; |
|
c70eefe48029
Added version that opens a pipe to /bin/pwd.
Guido van Rossum <guido@python.org>
parents:
116
diff
changeset
|
84 char *p; |
|
c70eefe48029
Added version that opens a pipe to /bin/pwd.
Guido van Rossum <guido@python.org>
parents:
116
diff
changeset
|
85 int sts; |
|
c70eefe48029
Added version that opens a pipe to /bin/pwd.
Guido van Rossum <guido@python.org>
parents:
116
diff
changeset
|
86 if (size <= 0) { |
|
c70eefe48029
Added version that opens a pipe to /bin/pwd.
Guido van Rossum <guido@python.org>
parents:
116
diff
changeset
|
87 errno = EINVAL; |
|
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 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
|
91 return NULL; |
|
c70eefe48029
Added version that opens a pipe to /bin/pwd.
Guido van Rossum <guido@python.org>
parents:
116
diff
changeset
|
92 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
|
93 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
|
94 return NULL; |
|
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 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
|
97 if (*p == '\0') { |
|
c70eefe48029
Added version that opens a pipe to /bin/pwd.
Guido van Rossum <guido@python.org>
parents:
116
diff
changeset
|
98 errno = ERANGE; |
|
c70eefe48029
Added version that opens a pipe to /bin/pwd.
Guido van Rossum <guido@python.org>
parents:
116
diff
changeset
|
99 return NULL; |
|
c70eefe48029
Added version that opens a pipe to /bin/pwd.
Guido van Rossum <guido@python.org>
parents:
116
diff
changeset
|
100 } |
|
c70eefe48029
Added version that opens a pipe to /bin/pwd.
Guido van Rossum <guido@python.org>
parents:
116
diff
changeset
|
101 } |
|
c70eefe48029
Added version that opens a pipe to /bin/pwd.
Guido van Rossum <guido@python.org>
parents:
116
diff
changeset
|
102 *p = '\0'; |
|
c70eefe48029
Added version that opens a pipe to /bin/pwd.
Guido van Rossum <guido@python.org>
parents:
116
diff
changeset
|
103 return buf; |
|
c70eefe48029
Added version that opens a pipe to /bin/pwd.
Guido van Rossum <guido@python.org>
parents:
116
diff
changeset
|
104 } |
|
c70eefe48029
Added version that opens a pipe to /bin/pwd.
Guido van Rossum <guido@python.org>
parents:
116
diff
changeset
|
105 |
|
1848
b724599401ff
Merge back to main trunk
Guido van Rossum <guido@python.org>
parents:
1319
diff
changeset
|
106 #endif /* !HAVE_GETWD */ |
