annotate Python/getcwd.c @ 58689:514b56efd89a 3.1

Merged revisions 77962 via svnmerge from svn+ssh://pythondev@svn.python.org/python/branches/py3k ................ r77962 | r.david.murray | 2010-02-04 11:41:57 -0500 (Thu, 04 Feb 2010) | 17 lines Merged revisions 77943,77959-77960 via svnmerge from svn+ssh://pythondev@svn.python.org/python/trunk ........ r77943 | r.david.murray | 2010-02-03 08:36:23 -0500 (Wed, 03 Feb 2010) | 2 lines Explicitly mention the default value for formatdate's usegmt parameter. ........ r77959 | nick.coghlan | 2010-02-04 07:43:58 -0500 (Thu, 04 Feb 2010) | 1 line Issue 6760: Clarify args handling for subprocess.Popen. Patch by Chris Rebert ........ r77960 | r.david.murray | 2010-02-04 11:33:31 -0500 (Thu, 04 Feb 2010) | 2 lines Add Chris Rebert to ACKS for issue 6760 Popen doc improvements. ........ ................
author R. David Murray <rdmurray@bitdance.com>
date Thu, 04 Feb 2010 16:44:31 +0000
parents 29ae5e0fc348
children e38335d4df91 5cea27275af1
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
145
853b736a6a9e Added copyright notice.
Guido van Rossum <guido@python.org>
parents: 144
diff changeset
1
144
c70eefe48029 Added version that opens a pipe to /bin/pwd.
Guido van Rossum <guido@python.org>
parents: 116
diff changeset
2 /* Two PD getcwd() implementations.
c70eefe48029 Added version that opens a pipe to /bin/pwd.
Guido van Rossum <guido@python.org>
parents: 116
diff changeset
3 Author: Guido van Rossum, CWI Amsterdam, Jan 1991, <guido@cwi.nl>. */
c70eefe48029 Added version that opens a pipe to /bin/pwd.
Guido van Rossum <guido@python.org>
parents: 116
diff changeset
4
c70eefe48029 Added version that opens a pipe to /bin/pwd.
Guido van Rossum <guido@python.org>
parents: 116
diff changeset
5 #include <stdio.h>
c70eefe48029 Added version that opens a pipe to /bin/pwd.
Guido van Rossum <guido@python.org>
parents: 116
diff changeset
6 #include <errno.h>
c70eefe48029 Added version that opens a pipe to /bin/pwd.
Guido van Rossum <guido@python.org>
parents: 116
diff changeset
7
1848
b724599401ff Merge back to main trunk
Guido van Rossum <guido@python.org>
parents: 1319
diff changeset
8 #ifdef HAVE_GETWD
b724599401ff Merge back to main trunk
Guido van Rossum <guido@python.org>
parents: 1319
diff changeset
9
b724599401ff Merge back to main trunk
Guido van Rossum <guido@python.org>
parents: 1319
diff changeset
10 /* Version for BSD systems -- use getwd() */
144
c70eefe48029 Added version that opens a pipe to /bin/pwd.
Guido van Rossum <guido@python.org>
parents: 116
diff changeset
11
1848
b724599401ff Merge back to main trunk
Guido van Rossum <guido@python.org>
parents: 1319
diff changeset
12 #ifdef HAVE_SYS_PARAM_H
b724599401ff Merge back to main trunk
Guido van Rossum <guido@python.org>
parents: 1319
diff changeset
13 #include <sys/param.h>
b724599401ff Merge back to main trunk
Guido van Rossum <guido@python.org>
parents: 1319
diff changeset
14 #endif
116
0a02ca9dcd6e Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
15
1848
b724599401ff Merge back to main trunk
Guido van Rossum <guido@python.org>
parents: 1319
diff changeset
16 #ifndef MAXPATHLEN
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 14375
diff changeset
17 #if defined(PATH_MAX) && PATH_MAX > 1024
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 14375
diff changeset
18 #define MAXPATHLEN PATH_MAX
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 14375
diff changeset
19 #else
1848
b724599401ff Merge back to main trunk
Guido van Rossum <guido@python.org>
parents: 1319
diff changeset
20 #define MAXPATHLEN 1024
b724599401ff Merge back to main trunk
Guido van Rossum <guido@python.org>
parents: 1319
diff changeset
21 #endif
38453
29ae5e0fc348 Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents: 14375
diff changeset
22 #endif
116
0a02ca9dcd6e Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
23
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13256
diff changeset
24 extern char *getwd(char *);
116
0a02ca9dcd6e Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
25
0a02ca9dcd6e Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
26 char *
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13256
diff changeset
27 getcwd(char *buf, int size)
116
0a02ca9dcd6e Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
28 {
0a02ca9dcd6e Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
29 char localbuf[MAXPATHLEN+1];
0a02ca9dcd6e Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
30 char *ret;
0a02ca9dcd6e Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
31
0a02ca9dcd6e Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
32 if (size <= 0) {
0a02ca9dcd6e Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
33 errno = EINVAL;
0a02ca9dcd6e Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
34 return NULL;
0a02ca9dcd6e Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
35 }
0a02ca9dcd6e Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
36 ret = getwd(localbuf);
13096
8c8de5b736dc Trent Mick's Win64 changes: size_t vs. int or long; also some overflow
Guido van Rossum <guido@python.org>
parents: 4503
diff changeset
37 if (ret != NULL && strlen(localbuf) >= (size_t)size) {
116
0a02ca9dcd6e Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
38 errno = ERANGE;
0a02ca9dcd6e Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
39 return NULL;
0a02ca9dcd6e Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
40 }
0a02ca9dcd6e Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
41 if (ret == NULL) {
0a02ca9dcd6e Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
42 errno = EACCES; /* Most likely error */
0a02ca9dcd6e Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
43 return NULL;
0a02ca9dcd6e Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
44 }
0a02ca9dcd6e Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
45 strncpy(buf, localbuf, size);
0a02ca9dcd6e Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
46 return buf;
0a02ca9dcd6e Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
47 }
0a02ca9dcd6e Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
48
1848
b724599401ff Merge back to main trunk
Guido van Rossum <guido@python.org>
parents: 1319
diff changeset
49 #else /* !HAVE_GETWD */
b724599401ff Merge back to main trunk
Guido van Rossum <guido@python.org>
parents: 1319
diff changeset
50
b724599401ff Merge back to main trunk
Guido van Rossum <guido@python.org>
parents: 1319
diff changeset
51 /* Version for really old UNIX systems -- use pipe from pwd */
144
c70eefe48029 Added version that opens a pipe to /bin/pwd.
Guido van Rossum <guido@python.org>
parents: 116
diff changeset
52
1848
b724599401ff Merge back to main trunk
Guido van Rossum <guido@python.org>
parents: 1319
diff changeset
53 #ifndef PWD_CMD
144
c70eefe48029 Added version that opens a pipe to /bin/pwd.
Guido van Rossum <guido@python.org>
parents: 116
diff changeset
54 #define PWD_CMD "/bin/pwd"
1848
b724599401ff Merge back to main trunk
Guido van Rossum <guido@python.org>
parents: 1319
diff changeset
55 #endif
144
c70eefe48029 Added version that opens a pipe to /bin/pwd.
Guido van Rossum <guido@python.org>
parents: 116
diff changeset
56
c70eefe48029 Added version that opens a pipe to /bin/pwd.
Guido van Rossum <guido@python.org>
parents: 116
diff changeset
57 char *
13658
f79434ee41fa Mass ANSIfication of function definitions. Doesn't cover all 'extern'
Thomas Wouters <thomas@python.org>
parents: 13256
diff changeset
58 getcwd(char *buf, int size)
144
c70eefe48029 Added version that opens a pipe to /bin/pwd.
Guido van Rossum <guido@python.org>
parents: 116
diff changeset
59 {
c70eefe48029 Added version that opens a pipe to /bin/pwd.
Guido van Rossum <guido@python.org>
parents: 116
diff changeset
60 FILE *fp;
c70eefe48029 Added version that opens a pipe to /bin/pwd.
Guido van Rossum <guido@python.org>
parents: 116
diff changeset
61 char *p;
c70eefe48029 Added version that opens a pipe to /bin/pwd.
Guido van Rossum <guido@python.org>
parents: 116
diff changeset
62 int sts;
c70eefe48029 Added version that opens a pipe to /bin/pwd.
Guido van Rossum <guido@python.org>
parents: 116
diff changeset
63 if (size <= 0) {
c70eefe48029 Added version that opens a pipe to /bin/pwd.
Guido van Rossum <guido@python.org>
parents: 116
diff changeset
64 errno = EINVAL;
c70eefe48029 Added version that opens a pipe to /bin/pwd.
Guido van Rossum <guido@python.org>
parents: 116
diff changeset
65 return NULL;
c70eefe48029 Added version that opens a pipe to /bin/pwd.
Guido van Rossum <guido@python.org>
parents: 116
diff changeset
66 }
c70eefe48029 Added version that opens a pipe to /bin/pwd.
Guido van Rossum <guido@python.org>
parents: 116
diff changeset
67 if ((fp = popen(PWD_CMD, "r")) == NULL)
c70eefe48029 Added version that opens a pipe to /bin/pwd.
Guido van Rossum <guido@python.org>
parents: 116
diff changeset
68 return NULL;
c70eefe48029 Added version that opens a pipe to /bin/pwd.
Guido van Rossum <guido@python.org>
parents: 116
diff changeset
69 if (fgets(buf, size, fp) == NULL || (sts = pclose(fp)) != 0) {
c70eefe48029 Added version that opens a pipe to /bin/pwd.
Guido van Rossum <guido@python.org>
parents: 116
diff changeset
70 errno = EACCES; /* Most likely error */
c70eefe48029 Added version that opens a pipe to /bin/pwd.
Guido van Rossum <guido@python.org>
parents: 116
diff changeset
71 return NULL;
c70eefe48029 Added version that opens a pipe to /bin/pwd.
Guido van Rossum <guido@python.org>
parents: 116
diff changeset
72 }
c70eefe48029 Added version that opens a pipe to /bin/pwd.
Guido van Rossum <guido@python.org>
parents: 116
diff changeset
73 for (p = buf; *p != '\n'; p++) {
c70eefe48029 Added version that opens a pipe to /bin/pwd.
Guido van Rossum <guido@python.org>
parents: 116
diff changeset
74 if (*p == '\0') {
c70eefe48029 Added version that opens a pipe to /bin/pwd.
Guido van Rossum <guido@python.org>
parents: 116
diff changeset
75 errno = ERANGE;
c70eefe48029 Added version that opens a pipe to /bin/pwd.
Guido van Rossum <guido@python.org>
parents: 116
diff changeset
76 return NULL;
c70eefe48029 Added version that opens a pipe to /bin/pwd.
Guido van Rossum <guido@python.org>
parents: 116
diff changeset
77 }
c70eefe48029 Added version that opens a pipe to /bin/pwd.
Guido van Rossum <guido@python.org>
parents: 116
diff changeset
78 }
c70eefe48029 Added version that opens a pipe to /bin/pwd.
Guido van Rossum <guido@python.org>
parents: 116
diff changeset
79 *p = '\0';
c70eefe48029 Added version that opens a pipe to /bin/pwd.
Guido van Rossum <guido@python.org>
parents: 116
diff changeset
80 return buf;
c70eefe48029 Added version that opens a pipe to /bin/pwd.
Guido van Rossum <guido@python.org>
parents: 116
diff changeset
81 }
c70eefe48029 Added version that opens a pipe to /bin/pwd.
Guido van Rossum <guido@python.org>
parents: 116
diff changeset
82
1848
b724599401ff Merge back to main trunk
Guido van Rossum <guido@python.org>
parents: 1319
diff changeset
83 #endif /* !HAVE_GETWD */