#changed all email address to go through python.org
[python/dscho.git] / Python / getcwd.c
blob894993faa0820dcefc183f80fefb7e988a1750e2
1 /***********************************************************
2 Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
3 The Netherlands.
5 All Rights Reserved
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.
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.
23 ******************************************************************/
25 /* Two PD getcwd() implementations.
26 Author: Guido van Rossum, CWI Amsterdam, Jan 1991, <guido@cwi.nl>. */
28 #include <stdio.h>
29 #include <errno.h>
31 #ifdef HAVE_GETWD
33 /* Version for BSD systems -- use getwd() */
35 #ifdef HAVE_SYS_PARAM_H
36 #include <sys/param.h>
37 #endif
39 #ifndef MAXPATHLEN
40 #define MAXPATHLEN 1024
41 #endif
43 extern char *getwd();
45 char *
46 getcwd(buf, size)
47 char *buf;
48 int size;
50 char localbuf[MAXPATHLEN+1];
51 char *ret;
53 if (size <= 0) {
54 errno = EINVAL;
55 return NULL;
57 ret = getwd(localbuf);
58 if (ret != NULL && strlen(localbuf) >= size) {
59 errno = ERANGE;
60 return NULL;
62 if (ret == NULL) {
63 errno = EACCES; /* Most likely error */
64 return NULL;
66 strncpy(buf, localbuf, size);
67 return buf;
70 #else /* !HAVE_GETWD */
72 /* Version for really old UNIX systems -- use pipe from pwd */
74 #ifndef PWD_CMD
75 #define PWD_CMD "/bin/pwd"
76 #endif
78 char *
79 getcwd(buf, size)
80 char *buf;
81 int size;
83 FILE *fp;
84 char *p;
85 int sts;
86 if (size <= 0) {
87 errno = EINVAL;
88 return NULL;
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;
96 for (p = buf; *p != '\n'; p++) {
97 if (*p == '\0') {
98 errno = ERANGE;
99 return NULL;
102 *p = '\0';
103 return buf;
106 #endif /* !HAVE_GETWD */