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 |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 17 | #if defined(PATH_MAX) && PATH_MAX > 1024 |
| 18 | #define MAXPATHLEN PATH_MAX |
| 19 | #else |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 20 | #define MAXPATHLEN 1024 |
| 21 | #endif |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 22 | #endif |
Guido van Rossum | 7927384 | 1991-02-19 12:28:18 +0000 | [diff] [blame] | 23 | |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 24 | extern char *getwd(char *); |
Guido van Rossum | de9775a | 1991-01-21 14:27:52 +0000 | [diff] [blame] | 25 | |
| 26 | char * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 27 | getcwd(char *buf, int size) |
Guido van Rossum | de9775a | 1991-01-21 14:27:52 +0000 | [diff] [blame] | 28 | { |
| 29 | char localbuf[MAXPATHLEN+1]; |
| 30 | char *ret; |
| 31 | |
| 32 | if (size <= 0) { |
| 33 | errno = EINVAL; |
| 34 | return NULL; |
| 35 | } |
| 36 | ret = getwd(localbuf); |
Guido van Rossum | 582acec | 2000-06-28 22:07:35 +0000 | [diff] [blame] | 37 | if (ret != NULL && strlen(localbuf) >= (size_t)size) { |
Guido van Rossum | de9775a | 1991-01-21 14:27:52 +0000 | [diff] [blame] | 38 | errno = ERANGE; |
| 39 | return NULL; |
| 40 | } |
| 41 | if (ret == NULL) { |
| 42 | errno = EACCES; /* Most likely error */ |
| 43 | return NULL; |
| 44 | } |
| 45 | strncpy(buf, localbuf, size); |
| 46 | return buf; |
| 47 | } |
| 48 | |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 49 | #else /* !HAVE_GETWD */ |
Guido van Rossum | 7927384 | 1991-02-19 12:28:18 +0000 | [diff] [blame] | 50 | |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 51 | /* Version for really old UNIX systems -- use pipe from pwd */ |
Guido van Rossum | 7927384 | 1991-02-19 12:28:18 +0000 | [diff] [blame] | 52 | |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 53 | #ifndef PWD_CMD |
Guido van Rossum | 7927384 | 1991-02-19 12:28:18 +0000 | [diff] [blame] | 54 | #define PWD_CMD "/bin/pwd" |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 55 | #endif |
Guido van Rossum | 7927384 | 1991-02-19 12:28:18 +0000 | [diff] [blame] | 56 | |
| 57 | char * |
Thomas Wouters | f70ef4f | 2000-07-22 18:47:25 +0000 | [diff] [blame] | 58 | getcwd(char *buf, int size) |
Guido van Rossum | 7927384 | 1991-02-19 12:28:18 +0000 | [diff] [blame] | 59 | { |
| 60 | FILE *fp; |
| 61 | char *p; |
| 62 | int sts; |
| 63 | if (size <= 0) { |
| 64 | errno = EINVAL; |
| 65 | return NULL; |
| 66 | } |
| 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; |
| 72 | } |
| 73 | for (p = buf; *p != '\n'; p++) { |
| 74 | if (*p == '\0') { |
| 75 | errno = ERANGE; |
| 76 | return NULL; |
| 77 | } |
| 78 | } |
| 79 | *p = '\0'; |
| 80 | return buf; |
| 81 | } |
| 82 | |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 83 | #endif /* !HAVE_GETWD */ |