Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 1 | |
Guido van Rossum | 7927384 | 1991-02-19 12:28:18 +0000 | [diff] [blame] | 2 | /* Two PD getcwd() implementations. |
| 3 | Author: Guido van Rossum, CWI Amsterdam, Jan 1991, <guido@cwi.nl>. */ |
Guido van Rossum | de9775a | 1991-01-21 14:27:52 +0000 | [diff] [blame] | 4 | |
Guido van Rossum | 7927384 | 1991-02-19 12:28:18 +0000 | [diff] [blame] | 5 | #include <stdio.h> |
| 6 | #include <errno.h> |
Guido van Rossum | de9775a | 1991-01-21 14:27:52 +0000 | [diff] [blame] | 7 | |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 8 | #ifdef HAVE_GETWD |
Guido van Rossum | 7927384 | 1991-02-19 12:28:18 +0000 | [diff] [blame] | 9 | |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 10 | /* Version for BSD systems -- use getwd() */ |
Guido van Rossum | 7927384 | 1991-02-19 12:28:18 +0000 | [diff] [blame] | 11 | |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 12 | #ifdef HAVE_SYS_PARAM_H |
| 13 | #include <sys/param.h> |
| 14 | #endif |
| 15 | |
| 16 | #ifndef MAXPATHLEN |
| 17 | #define MAXPATHLEN 1024 |
| 18 | #endif |
Guido van Rossum | 7927384 | 1991-02-19 12:28:18 +0000 | [diff] [blame] | 19 | |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 20 | extern char *getwd(char *); |
Guido van Rossum | de9775a | 1991-01-21 14:27:52 +0000 | [diff] [blame] | 21 | |
| 22 | char * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 23 | getcwd(char *buf, int size) |
Guido van Rossum | de9775a | 1991-01-21 14:27:52 +0000 | [diff] [blame] | 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); |
Guido van Rossum | 582acec | 2000-06-28 22:07:35 +0000 | [diff] [blame] | 33 | if (ret != NULL && strlen(localbuf) >= (size_t)size) { |
Guido van Rossum | de9775a | 1991-01-21 14:27:52 +0000 | [diff] [blame] | 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 | |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 45 | #else /* !HAVE_GETWD */ |
Guido van Rossum | 7927384 | 1991-02-19 12:28:18 +0000 | [diff] [blame] | 46 | |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 47 | /* Version for really old UNIX systems -- use pipe from pwd */ |
Guido van Rossum | 7927384 | 1991-02-19 12:28:18 +0000 | [diff] [blame] | 48 | |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 49 | #ifndef PWD_CMD |
Guido van Rossum | 7927384 | 1991-02-19 12:28:18 +0000 | [diff] [blame] | 50 | #define PWD_CMD "/bin/pwd" |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 51 | #endif |
Guido van Rossum | 7927384 | 1991-02-19 12:28:18 +0000 | [diff] [blame] | 52 | |
| 53 | char * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 54 | getcwd(char *buf, int size) |
Guido van Rossum | 7927384 | 1991-02-19 12:28:18 +0000 | [diff] [blame] | 55 | { |
| 56 | FILE *fp; |
| 57 | char *p; |
| 58 | int sts; |
| 59 | if (size <= 0) { |
| 60 | errno = EINVAL; |
| 61 | return NULL; |
| 62 | } |
| 63 | if ((fp = popen(PWD_CMD, "r")) == NULL) |
| 64 | return NULL; |
| 65 | if (fgets(buf, size, fp) == NULL || (sts = pclose(fp)) != 0) { |
| 66 | errno = EACCES; /* Most likely error */ |
| 67 | return NULL; |
| 68 | } |
| 69 | for (p = buf; *p != '\n'; p++) { |
| 70 | if (*p == '\0') { |
| 71 | errno = ERANGE; |
| 72 | return NULL; |
| 73 | } |
| 74 | } |
| 75 | *p = '\0'; |
| 76 | return buf; |
| 77 | } |
| 78 | |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 79 | #endif /* !HAVE_GETWD */ |