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