annotate Python/getcwd.c @ 61270:6fb6fa802221 legacy-trunk

Remove an unneeded variable and fix a little whitespace. Found using Clang's static analyzer.
author Brett Cannon <bcannon@gmail.com>
date Wed, 05 May 2010 20:26:58 +0000
parents 2d8fa74f9263
children fd25030223b0
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
37980
b535c191a44b Define MAXPATHLEN to be at least PATH_MAX, if that's defined. Python uses
Thomas Wouters <thomas@python.org>
parents: 14375
diff changeset
17 #if defined(PATH_MAX) && PATH_MAX > 1024
b535c191a44b Define MAXPATHLEN to be at least PATH_MAX, if that's defined. Python uses
Thomas Wouters <thomas@python.org>
parents: 14375
diff changeset
18 #define MAXPATHLEN PATH_MAX
b535c191a44b Define MAXPATHLEN to be at least PATH_MAX, if that's defined. Python uses
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
37980
b535c191a44b Define MAXPATHLEN to be at least PATH_MAX, if that's defined. Python uses
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;
61270
6fb6fa802221 Remove an unneeded variable and fix a little whitespace.
Brett Cannon <bcannon@gmail.com>
parents: 61268
diff changeset
31
116
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 if (size <= 0) {
c70eefe48029 Added version that opens a pipe to /bin/pwd.
Guido van Rossum <guido@python.org>
parents: 116
diff changeset
63 errno = EINVAL;
c70eefe48029 Added version that opens a pipe to /bin/pwd.
Guido van Rossum <guido@python.org>
parents: 116
diff changeset
64 return NULL;
c70eefe48029 Added version that opens a pipe to /bin/pwd.
Guido van Rossum <guido@python.org>
parents: 116
diff changeset
65 }
c70eefe48029 Added version that opens a pipe to /bin/pwd.
Guido van Rossum <guido@python.org>
parents: 116
diff changeset
66 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
67 return NULL;
61270
6fb6fa802221 Remove an unneeded variable and fix a little whitespace.
Brett Cannon <bcannon@gmail.com>
parents: 61268
diff changeset
68 if (fgets(buf, size, fp) == NULL || pclose(fp) != 0) {
144
c70eefe48029 Added version that opens a pipe to /bin/pwd.
Guido van Rossum <guido@python.org>
parents: 116
diff changeset
69 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
70 return NULL;
c70eefe48029 Added version that opens a pipe to /bin/pwd.
Guido van Rossum <guido@python.org>
parents: 116
diff changeset
71 }
c70eefe48029 Added version that opens a pipe to /bin/pwd.
Guido van Rossum <guido@python.org>
parents: 116
diff changeset
72 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
73 if (*p == '\0') {
c70eefe48029 Added version that opens a pipe to /bin/pwd.
Guido van Rossum <guido@python.org>
parents: 116
diff changeset
74 errno = ERANGE;
c70eefe48029 Added version that opens a pipe to /bin/pwd.
Guido van Rossum <guido@python.org>
parents: 116
diff changeset
75 return NULL;
c70eefe48029 Added version that opens a pipe to /bin/pwd.
Guido van Rossum <guido@python.org>
parents: 116
diff changeset
76 }
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 *p = '\0';
c70eefe48029 Added version that opens a pipe to /bin/pwd.
Guido van Rossum <guido@python.org>
parents: 116
diff changeset
79 return buf;
c70eefe48029 Added version that opens a pipe to /bin/pwd.
Guido van Rossum <guido@python.org>
parents: 116
diff changeset
80 }
c70eefe48029 Added version that opens a pipe to /bin/pwd.
Guido van Rossum <guido@python.org>
parents: 116
diff changeset
81
1848
b724599401ff Merge back to main trunk
Guido van Rossum <guido@python.org>
parents: 1319
diff changeset
82 #endif /* !HAVE_GETWD */