blob: 4bedbd1f759f86aa9390569953ae27c8758b4b12 [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{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000029 char localbuf[MAXPATHLEN+1];
30 char *ret;
31
32 if (size <= 0) {
33 errno = EINVAL;
34 return NULL;
35 }
36 ret = getwd(localbuf);
37 if (ret != NULL && strlen(localbuf) >= (size_t)size) {
38 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;
Guido van Rossumde9775a1991-01-21 14:27:52 +000047}
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{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000060 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;
Guido van Rossum79273841991-02-19 12:28:18 +000081}
82
Guido van Rossum1d5735e1994-08-30 08:27:36 +000083#endif /* !HAVE_GETWD */