Skip to content

Commit 7927384

Browse files
committed
Added version that opens a pipe to /bin/pwd.
1 parent 426035c commit 7927384

File tree

1 file changed

+47
-4
lines changed

1 file changed

+47
-4
lines changed

Python/getcwd.c

Lines changed: 47 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,19 @@
1-
/* Quick hack to get posix.getcwd() working for pure BSD 4.3 */
1+
/* Two PD getcwd() implementations.
2+
Author: Guido van Rossum, CWI Amsterdam, Jan 1991, <guido@cwi.nl>. */
23

3-
#include "sys/param.h"
4-
#include "errno.h"
4+
/* #define NO_GETWD /* Turn this on to popen pwd instead of calling getwd() */
5+
6+
#include <stdio.h>
7+
#include <errno.h>
58

69
extern int errno;
710

11+
#ifndef NO_GETWD
12+
13+
/* Default: Version for BSD systems -- use getwd() */
14+
15+
#include "sys/param.h"
16+
817
extern char *getwd();
918

1019
char *
@@ -32,4 +41,38 @@ getcwd(buf, size)
3241
return buf;
3342
}
3443

35-
/* PS: for really old systems you must popen /bin/pwd ... */
44+
#else
45+
46+
/* NO_GETWD defined: Version for backward UNIXes -- popen /bin/pwd */
47+
48+
#define PWD_CMD "/bin/pwd"
49+
50+
char *
51+
getcwd(buf, size)
52+
char *buf;
53+
int size;
54+
{
55+
FILE *fp;
56+
char *p;
57+
int sts;
58+
if (size <= 0) {
59+
errno = EINVAL;
60+
return NULL;
61+
}
62+
if ((fp = popen(PWD_CMD, "r")) == NULL)
63+
return NULL;
64+
if (fgets(buf, size, fp) == NULL || (sts = pclose(fp)) != 0) {
65+
errno = EACCES; /* Most likely error */
66+
return NULL;
67+
}
68+
for (p = buf; *p != '\n'; p++) {
69+
if (*p == '\0') {
70+
errno = ERANGE;
71+
return NULL;
72+
}
73+
}
74+
*p = '\0';
75+
return buf;
76+
}
77+
78+
#endif

0 commit comments

Comments
 (0)