Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 1 | /*********************************************************** |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 2 | Copyright 1991, 1992, 1993, 1994 by Stichting Mathematisch Centrum, |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 3 | Amsterdam, The Netherlands. |
Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 4 | |
| 5 | All Rights Reserved |
| 6 | |
| 7 | Permission to use, copy, modify, and distribute this software and its |
| 8 | documentation for any purpose and without fee is hereby granted, |
| 9 | provided that the above copyright notice appear in all copies and that |
| 10 | both that copyright notice and this permission notice appear in |
| 11 | supporting documentation, and that the names of Stichting Mathematisch |
| 12 | Centrum or CWI not be used in advertising or publicity pertaining to |
| 13 | distribution of the software without specific, written prior permission. |
| 14 | |
| 15 | STICHTING MATHEMATISCH CENTRUM DISCLAIMS ALL WARRANTIES WITH REGARD TO |
| 16 | THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND |
| 17 | FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH CENTRUM BE LIABLE |
| 18 | FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
| 19 | WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
| 20 | ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT |
| 21 | OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 22 | |
| 23 | ******************************************************************/ |
| 24 | |
Guido van Rossum | 7927384 | 1991-02-19 12:28:18 +0000 | [diff] [blame] | 25 | /* Two PD getcwd() implementations. |
| 26 | Author: Guido van Rossum, CWI Amsterdam, Jan 1991, <guido@cwi.nl>. */ |
Guido van Rossum | de9775a | 1991-01-21 14:27:52 +0000 | [diff] [blame] | 27 | |
Guido van Rossum | 7927384 | 1991-02-19 12:28:18 +0000 | [diff] [blame] | 28 | #include <stdio.h> |
| 29 | #include <errno.h> |
Guido van Rossum | de9775a | 1991-01-21 14:27:52 +0000 | [diff] [blame] | 30 | |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 31 | #ifdef HAVE_GETWD |
Guido van Rossum | 7927384 | 1991-02-19 12:28:18 +0000 | [diff] [blame] | 32 | |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 33 | /* Version for BSD systems -- use getwd() */ |
Guido van Rossum | 7927384 | 1991-02-19 12:28:18 +0000 | [diff] [blame] | 34 | |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 35 | #ifdef HAVE_SYS_PARAM_H |
| 36 | #include <sys/param.h> |
| 37 | #endif |
| 38 | |
| 39 | #ifndef MAXPATHLEN |
| 40 | #define MAXPATHLEN 1024 |
| 41 | #endif |
Guido van Rossum | 7927384 | 1991-02-19 12:28:18 +0000 | [diff] [blame] | 42 | |
Guido van Rossum | de9775a | 1991-01-21 14:27:52 +0000 | [diff] [blame] | 43 | extern char *getwd(); |
| 44 | |
| 45 | char * |
| 46 | getcwd(buf, size) |
| 47 | char *buf; |
| 48 | int size; |
| 49 | { |
| 50 | char localbuf[MAXPATHLEN+1]; |
| 51 | char *ret; |
| 52 | |
| 53 | if (size <= 0) { |
| 54 | errno = EINVAL; |
| 55 | return NULL; |
| 56 | } |
| 57 | ret = getwd(localbuf); |
| 58 | if (ret != NULL && strlen(localbuf) >= size) { |
| 59 | errno = ERANGE; |
| 60 | return NULL; |
| 61 | } |
| 62 | if (ret == NULL) { |
| 63 | errno = EACCES; /* Most likely error */ |
| 64 | return NULL; |
| 65 | } |
| 66 | strncpy(buf, localbuf, size); |
| 67 | return buf; |
| 68 | } |
| 69 | |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 70 | #else /* !HAVE_GETWD */ |
Guido van Rossum | 7927384 | 1991-02-19 12:28:18 +0000 | [diff] [blame] | 71 | |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 72 | /* Version for really old UNIX systems -- use pipe from pwd */ |
Guido van Rossum | 7927384 | 1991-02-19 12:28:18 +0000 | [diff] [blame] | 73 | |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 74 | #ifndef PWD_CMD |
Guido van Rossum | 7927384 | 1991-02-19 12:28:18 +0000 | [diff] [blame] | 75 | #define PWD_CMD "/bin/pwd" |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 76 | #endif |
Guido van Rossum | 7927384 | 1991-02-19 12:28:18 +0000 | [diff] [blame] | 77 | |
| 78 | char * |
| 79 | getcwd(buf, size) |
| 80 | char *buf; |
| 81 | int size; |
| 82 | { |
| 83 | FILE *fp; |
| 84 | char *p; |
| 85 | int sts; |
| 86 | if (size <= 0) { |
| 87 | errno = EINVAL; |
| 88 | return NULL; |
| 89 | } |
| 90 | if ((fp = popen(PWD_CMD, "r")) == NULL) |
| 91 | return NULL; |
| 92 | if (fgets(buf, size, fp) == NULL || (sts = pclose(fp)) != 0) { |
| 93 | errno = EACCES; /* Most likely error */ |
| 94 | return NULL; |
| 95 | } |
| 96 | for (p = buf; *p != '\n'; p++) { |
| 97 | if (*p == '\0') { |
| 98 | errno = ERANGE; |
| 99 | return NULL; |
| 100 | } |
| 101 | } |
| 102 | *p = '\0'; |
| 103 | return buf; |
| 104 | } |
| 105 | |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 106 | #endif /* !HAVE_GETWD */ |