blob: 05b58bcd13df6e2cb4ce75ac24a25f6bebbd10e3 [file] [log] [blame]
Guido van Rossumf70e43a1991-02-19 12:39:46 +00001/***********************************************************
Guido van Rossum1d5735e1994-08-30 08:27:36 +00002Copyright 1991, 1992, 1993, 1994 by Stichting Mathematisch Centrum,
Guido van Rossum9bfef441993-03-29 10:43:31 +00003Amsterdam, The Netherlands.
Guido van Rossumf70e43a1991-02-19 12:39:46 +00004
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#include <stdio.h>
29#include <errno.h>
Guido van Rossumde9775a1991-01-21 14:27:52 +000030
Guido van Rossum1d5735e1994-08-30 08:27:36 +000031#ifdef HAVE_GETWD
Guido van Rossum79273841991-02-19 12:28:18 +000032
Guido van Rossum1d5735e1994-08-30 08:27:36 +000033/* Version for BSD systems -- use getwd() */
Guido van Rossum79273841991-02-19 12:28:18 +000034
Guido van Rossum1d5735e1994-08-30 08:27:36 +000035#ifdef HAVE_SYS_PARAM_H
36#include <sys/param.h>
37#endif
38
39#ifndef MAXPATHLEN
40#define MAXPATHLEN 1024
41#endif
Guido van Rossum79273841991-02-19 12:28:18 +000042
Guido van Rossumde9775a1991-01-21 14:27:52 +000043extern char *getwd();
44
45char *
46getcwd(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 Rossum1d5735e1994-08-30 08:27:36 +000070#else /* !HAVE_GETWD */
Guido van Rossum79273841991-02-19 12:28:18 +000071
Guido van Rossum1d5735e1994-08-30 08:27:36 +000072/* Version for really old UNIX systems -- use pipe from pwd */
Guido van Rossum79273841991-02-19 12:28:18 +000073
Guido van Rossum1d5735e1994-08-30 08:27:36 +000074#ifndef PWD_CMD
Guido van Rossum79273841991-02-19 12:28:18 +000075#define PWD_CMD "/bin/pwd"
Guido van Rossum1d5735e1994-08-30 08:27:36 +000076#endif
Guido van Rossum79273841991-02-19 12:28:18 +000077
78char *
79getcwd(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 Rossum1d5735e1994-08-30 08:27:36 +0000106#endif /* !HAVE_GETWD */