Mercurial > cpython-lru_cache-weakref
annotate Python/getcwd.c @ 20737:00a581bdedeb legacy-trunk
When overriding __str__ or __repr__, set the tp_print slot to NULL.
| author | Guido van Rossum <guido@python.org> |
|---|---|
| date | Mon, 29 Oct 2001 14:33:44 +0000 |
| parents | 6f6f965c5e2d |
| children | b535c191a44b |
| rev | line source |
|---|---|
|
145
853b736a6a9e
Added copyright notice.
Guido van Rossum <guido@python.org>
parents:
144
diff
changeset
|
1 |
|
144
c70eefe48029
Added version that opens a pipe to /bin/pwd.
Guido van Rossum <guido@python.org>
parents:
116
diff
changeset
|
2 /* Two PD getcwd() implementations. |
|
c70eefe48029
Added version that opens a pipe to /bin/pwd.
Guido van Rossum <guido@python.org>
parents:
116
diff
changeset
|
3 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
|
4 |
|
c70eefe48029
Added version that opens a pipe to /bin/pwd.
Guido van Rossum <guido@python.org>
parents:
116
diff
changeset
|
5 #include <stdio.h> |
|
c70eefe48029
Added version that opens a pipe to /bin/pwd.
Guido van Rossum <guido@python.org>
parents:
116
diff
changeset
|
6 #include <errno.h> |
|
c70eefe48029
Added version that opens a pipe to /bin/pwd.
Guido van Rossum <guido@python.org>
parents:
116
diff
changeset
|
7 |
|
1848
b724599401ff
Merge back to main trunk
Guido van Rossum <guido@python.org>
parents:
1319
diff
changeset
|
8 #ifdef HAVE_GETWD |
|
b724599401ff
Merge back to main trunk
Guido van Rossum <guido@python.org>
parents:
1319
diff
changeset
|
9 |
|
b724599401ff
Merge back to main trunk
Guido van Rossum <guido@python.org>
parents:
1319
diff
changeset
|
10 /* 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
|
11 |
|
1848
b724599401ff
Merge back to main trunk
Guido van Rossum <guido@python.org>
parents:
1319
diff
changeset
|
12 #ifdef HAVE_SYS_PARAM_H |
|
b724599401ff
Merge back to main trunk
Guido van Rossum <guido@python.org>
parents:
1319
diff
changeset
|
13 #include <sys/param.h> |
|
b724599401ff
Merge back to main trunk
Guido van Rossum <guido@python.org>
parents:
1319
diff
changeset
|
14 #endif |
| 116 | 15 |
|
1848
b724599401ff
Merge back to main trunk
Guido van Rossum <guido@python.org>
parents:
1319
diff
changeset
|
16 #ifndef MAXPATHLEN |
|
b724599401ff
Merge back to main trunk
Guido van Rossum <guido@python.org>
parents:
1319
diff
changeset
|
17 #define MAXPATHLEN 1024 |
|
b724599401ff
Merge back to main trunk
Guido van Rossum <guido@python.org>
parents:
1319
diff
changeset
|
18 #endif |
| 116 | 19 |
|
13658
f79434ee41fa
Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents:
13256
diff
changeset
|
20 extern char *getwd(char *); |
| 116 | 21 |
| 22 char * | |
|
13658
f79434ee41fa
Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents:
13256
diff
changeset
|
23 getcwd(char *buf, int size) |
| 116 | 24 { |
| 25 char localbuf[MAXPATHLEN+1]; | |
| 26 char *ret; | |
| 27 | |
| 28 if (size <= 0) { | |
| 29 errno = EINVAL; | |
| 30 return NULL; | |
| 31 } | |
| 32 ret = getwd(localbuf); | |
|
13096
8c8de5b736dc
Trent Mick's Win64 changes: size_t vs. int or long; also some overflow
Guido van Rossum <guido@python.org>
parents:
4503
diff
changeset
|
33 if (ret != NULL && strlen(localbuf) >= (size_t)size) { |
| 116 | 34 errno = ERANGE; |
| 35 return NULL; | |
| 36 } | |
| 37 if (ret == NULL) { | |
| 38 errno = EACCES; /* Most likely error */ | |
| 39 return NULL; | |
| 40 } | |
| 41 strncpy(buf, localbuf, size); | |
| 42 return buf; | |
| 43 } | |
| 44 | |
|
1848
b724599401ff
Merge back to main trunk
Guido van Rossum <guido@python.org>
parents:
1319
diff
changeset
|
45 #else /* !HAVE_GETWD */ |
|
b724599401ff
Merge back to main trunk
Guido van Rossum <guido@python.org>
parents:
1319
diff
changeset
|
46 |
|
b724599401ff
Merge back to main trunk
Guido van Rossum <guido@python.org>
parents:
1319
diff
changeset
|
47 /* 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
|
48 |
|
1848
b724599401ff
Merge back to main trunk
Guido van Rossum <guido@python.org>
parents:
1319
diff
changeset
|
49 #ifndef PWD_CMD |
|
144
c70eefe48029
Added version that opens a pipe to /bin/pwd.
Guido van Rossum <guido@python.org>
parents:
116
diff
changeset
|
50 #define PWD_CMD "/bin/pwd" |
|
1848
b724599401ff
Merge back to main trunk
Guido van Rossum <guido@python.org>
parents:
1319
diff
changeset
|
51 #endif |
|
144
c70eefe48029
Added version that opens a pipe to /bin/pwd.
Guido van Rossum <guido@python.org>
parents:
116
diff
changeset
|
52 |
|
c70eefe48029
Added version that opens a pipe to /bin/pwd.
Guido van Rossum <guido@python.org>
parents:
116
diff
changeset
|
53 char * |
|
13658
f79434ee41fa
Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents:
13256
diff
changeset
|
54 getcwd(char *buf, int size) |
|
144
c70eefe48029
Added version that opens a pipe to /bin/pwd.
Guido van Rossum <guido@python.org>
parents:
116
diff
changeset
|
55 { |
|
c70eefe48029
Added version that opens a pipe to /bin/pwd.
Guido van Rossum <guido@python.org>
parents:
116
diff
changeset
|
56 FILE *fp; |
|
c70eefe48029
Added version that opens a pipe to /bin/pwd.
Guido van Rossum <guido@python.org>
parents:
116
diff
changeset
|
57 char *p; |
|
c70eefe48029
Added version that opens a pipe to /bin/pwd.
Guido van Rossum <guido@python.org>
parents:
116
diff
changeset
|
58 int sts; |
|
c70eefe48029
Added version that opens a pipe to /bin/pwd.
Guido van Rossum <guido@python.org>
parents:
116
diff
changeset
|
59 if (size <= 0) { |
|
c70eefe48029
Added version that opens a pipe to /bin/pwd.
Guido van Rossum <guido@python.org>
parents:
116
diff
changeset
|
60 errno = EINVAL; |
|
c70eefe48029
Added version that opens a pipe to /bin/pwd.
Guido van Rossum <guido@python.org>
parents:
116
diff
changeset
|
61 return NULL; |
|
c70eefe48029
Added version that opens a pipe to /bin/pwd.
Guido van Rossum <guido@python.org>
parents:
116
diff
changeset
|
62 } |
|
c70eefe48029
Added version that opens a pipe to /bin/pwd.
Guido van Rossum <guido@python.org>
parents:
116
diff
changeset
|
63 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
|
64 return NULL; |
|
c70eefe48029
Added version that opens a pipe to /bin/pwd.
Guido van Rossum <guido@python.org>
parents:
116
diff
changeset
|
65 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
|
66 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
|
67 return NULL; |
|
c70eefe48029
Added version that opens a pipe to /bin/pwd.
Guido van Rossum <guido@python.org>
parents:
116
diff
changeset
|
68 } |
|
c70eefe48029
Added version that opens a pipe to /bin/pwd.
Guido van Rossum <guido@python.org>
parents:
116
diff
changeset
|
69 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
|
70 if (*p == '\0') { |
|
c70eefe48029
Added version that opens a pipe to /bin/pwd.
Guido van Rossum <guido@python.org>
parents:
116
diff
changeset
|
71 errno = ERANGE; |
|
c70eefe48029
Added version that opens a pipe to /bin/pwd.
Guido van Rossum <guido@python.org>
parents:
116
diff
changeset
|
72 return NULL; |
|
c70eefe48029
Added version that opens a pipe to /bin/pwd.
Guido van Rossum <guido@python.org>
parents:
116
diff
changeset
|
73 } |
|
c70eefe48029
Added version that opens a pipe to /bin/pwd.
Guido van Rossum <guido@python.org>
parents:
116
diff
changeset
|
74 } |
|
c70eefe48029
Added version that opens a pipe to /bin/pwd.
Guido van Rossum <guido@python.org>
parents:
116
diff
changeset
|
75 *p = '\0'; |
|
c70eefe48029
Added version that opens a pipe to /bin/pwd.
Guido van Rossum <guido@python.org>
parents:
116
diff
changeset
|
76 return buf; |
|
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 |
|
1848
b724599401ff
Merge back to main trunk
Guido van Rossum <guido@python.org>
parents:
1319
diff
changeset
|
79 #endif /* !HAVE_GETWD */ |
