Fix missing semicolon
[python.git] / Python / getcwd.c
blob967d484b35acbbcb2a7624a23afb5f6c1635e287
2 /* Two PD getcwd() implementations.
3 Author: Guido van Rossum, CWI Amsterdam, Jan 1991, <guido@cwi.nl>. */
5 #include <stdio.h>
6 #include <errno.h>
8 #ifdef HAVE_GETWD
10 /* Version for BSD systems -- use getwd() */
12 #ifdef HAVE_SYS_PARAM_H
13 #include <sys/param.h>
14 #endif
16 #ifndef MAXPATHLEN
17 #if defined(PATH_MAX) && PATH_MAX > 1024
18 #define MAXPATHLEN PATH_MAX
19 #else
20 #define MAXPATHLEN 1024
21 #endif
22 #endif
24 extern char *getwd(char *);
26 char *
27 getcwd(char *buf, int size)
29 char localbuf[MAXPATHLEN+1];
30 char *ret;
32 if (size <= 0) {
33 errno = EINVAL;
34 return NULL;
36 ret = getwd(localbuf);
37 if (ret != NULL && strlen(localbuf) >= (size_t)size) {
38 errno = ERANGE;
39 return NULL;
41 if (ret == NULL) {
42 errno = EACCES; /* Most likely error */
43 return NULL;
45 strncpy(buf, localbuf, size);
46 return buf;
49 #else /* !HAVE_GETWD */
51 /* Version for really old UNIX systems -- use pipe from pwd */
53 #ifndef PWD_CMD
54 #define PWD_CMD "/bin/pwd"
55 #endif
57 char *
58 getcwd(char *buf, int size)
60 FILE *fp;
61 char *p;
62 int sts;
63 if (size <= 0) {
64 errno = EINVAL;
65 return NULL;
67 if ((fp = popen(PWD_CMD, "r")) == NULL)
68 return NULL;
69 if (fgets(buf, size, fp) == NULL || (sts = pclose(fp)) != 0) {
70 errno = EACCES; /* Most likely error */
71 return NULL;
73 for (p = buf; *p != '\n'; p++) {
74 if (*p == '\0') {
75 errno = ERANGE;
76 return NULL;
79 *p = '\0';
80 return buf;
83 #endif /* !HAVE_GETWD */