blob: 967d484b35acbbcb2a7624a23afb5f6c1635e287 [file] [log] [blame]
Guido van Rossumf70e43a1991-02-19 12:39:46 +00001
Guido van Rossum79273841991-02-19 12:28:18 +00002/* Two PD getcwd() implementations.
3 Author: Guido van Rossum, CWI Amsterdam, Jan 1991, <guido@cwi.nl>. */
Guido van Rossumde9775a1991-01-21 14:27:52 +00004
Guido van Rossum79273841991-02-19 12:28:18 +00005#include <stdio.h>
6#include <errno.h>
Guido van Rossumde9775a1991-01-21 14:27:52 +00007
Guido van Rossum1d5735e1994-08-30 08:27:36 +00008#ifdef HAVE_GETWD
Guido van Rossum79273841991-02-19 12:28:18 +00009
Guido van Rossum1d5735e1994-08-30 08:27:36 +000010/* Version for BSD systems -- use getwd() */
Guido van Rossum79273841991-02-19 12:28:18 +000011
Guido van Rossum1d5735e1994-08-30 08:27:36 +000012#ifdef HAVE_SYS_PARAM_H
13#include <sys/param.h>
14#endif
15
16#ifndef MAXPATHLEN
Thomas Wouters477c8d52006-05-27 19:21:47 +000017#if defined(PATH_MAX) && PATH_MAX > 1024
18#define MAXPATHLEN PATH_MAX
19#else
Guido van Rossum1d5735e1994-08-30 08:27:36 +000020#define MAXPATHLEN 1024
21#endif
Thomas Wouters477c8d52006-05-27 19:21:47 +000022#endif
Guido van Rossum79273841991-02-19 12:28:18 +000023
Thomas Woutersf70ef4f2000-07-22 18:47:25 +000024extern char *getwd(char *);
Guido van Rossumde9775a1991-01-21 14:27:52 +000025
26char *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +000027getcwd(char *buf, int size)
Guido van Rossumde9775a1991-01-21 14:27:52 +000028{
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 Rossum582acec2000-06-28 22:07:35 +000037 if (ret != NULL && strlen(localbuf) >= (size_t)size) {
Guido van Rossumde9775a1991-01-21 14:27:52 +000038 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 Rossum1d5735e1994-08-30 08:27:36 +000049#else /* !HAVE_GETWD */
Guido van Rossum79273841991-02-19 12:28:18 +000050
Guido van Rossum1d5735e1994-08-30 08:27:36 +000051/* Version for really old UNIX systems -- use pipe from pwd */
Guido van Rossum79273841991-02-19 12:28:18 +000052
Guido van Rossum1d5735e1994-08-30 08:27:36 +000053#ifndef PWD_CMD
Guido van Rossum79273841991-02-19 12:28:18 +000054#define PWD_CMD "/bin/pwd"
Guido van Rossum1d5735e1994-08-30 08:27:36 +000055#endif
Guido van Rossum79273841991-02-19 12:28:18 +000056
57char *
Thomas Woutersf70ef4f2000-07-22 18:47:25 +000058getcwd(char *buf, int size)
Guido van Rossum79273841991-02-19 12:28:18 +000059{
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 Rossum1d5735e1994-08-30 08:27:36 +000083#endif /* !HAVE_GETWD */