Source-Changes-HG archive
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]
[src/trunk]: src/lib/libc/gen - Remove macros used only by old getcwd() imple...
details: https://anonhg.NetBSD.org/src/rev/d8603940b3f3
branches: trunk
changeset: 573222:d8603940b3f3
user: enami <enami%NetBSD.org@localhost>
date: Sun Jan 23 01:00:51 2005 +0000
description:
- Remove macros used only by old getcwd() implementation.
- Fix memory leak on error. (I guess the loop isn't necessary but
it is different matter).
- Remove knowledge about old malloc implementation.
diffstat:
lib/libc/gen/getcwd.c | 71 +++++++++++++++++++-------------------------------
1 files changed, 27 insertions(+), 44 deletions(-)
diffs (104 lines):
diff -r b0bf764dcd38 -r d8603940b3f3 lib/libc/gen/getcwd.c
--- a/lib/libc/gen/getcwd.c Sun Jan 23 00:23:57 2005 +0000
+++ b/lib/libc/gen/getcwd.c Sun Jan 23 01:00:51 2005 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: getcwd.c,v 1.34 2005/01/06 23:43:32 simonb Exp $ */
+/* $NetBSD: getcwd.c,v 1.35 2005/01/23 01:00:51 enami Exp $ */
/*
* Copyright (c) 1989, 1991, 1993, 1995
@@ -37,7 +37,7 @@
#if 0
static char sccsid[] = "@(#)getcwd.c 8.5 (Berkeley) 2/7/95";
#else
-__RCSID("$NetBSD: getcwd.c,v 1.34 2005/01/06 23:43:32 simonb Exp $");
+__RCSID("$NetBSD: getcwd.c,v 1.35 2005/01/23 01:00:51 enami Exp $");
#endif
#endif /* LIBC_SCCS and not lint */
@@ -61,15 +61,6 @@
__weak_alias(realpath,_realpath)
#endif
-#define ISDOT(dp) \
- (dp->d_name[0] == '.' && (dp->d_name[1] == '\0' || \
- (dp->d_name[1] == '.' && dp->d_name[2] == '\0')))
-
-
-#if defined(__SVR4) || defined(__svr4__)
-#define d_fileno d_ino
-#endif
-
/*
* char *realpath(const char *path, char resolved_path[MAXPATHLEN]);
*
@@ -205,44 +196,36 @@
}
char *
-getcwd(pt, size)
- char *pt;
- size_t size;
+getcwd(char *pt, size_t size)
{
- size_t ptsize, bufsize;
- int len;
-
+ char *npt;
+
/*
- * If no buffer specified by the user, allocate one as necessary.
- * If a buffer is specified, the size has to be non-zero. The path
- * is built from the end of the buffer backwards.
+ * If a buffer is specified, the size has to be non-zero.
*/
- if (pt) {
- ptsize = 0;
- if (!size) {
+ if (pt != NULL) {
+ if (size == 0) {
+ /* __getcwd(pt, 0) results ERANGE. */
errno = EINVAL;
return (NULL);
}
- bufsize = size;
- } else {
- if ((pt = malloc(ptsize = 1024 - 4)) == NULL)
- return (NULL);
- bufsize = ptsize;
+ if (__getcwd(pt, size) >= 0)
+ return (pt);
+ return (NULL);
}
- for (;;) {
- len = __getcwd(pt, bufsize);
- if ((len < 0) && (size == 0) && (errno == ERANGE)) {
- if (ptsize > (MAXPATHLEN*4))
- return NULL;
- if ((pt = realloc(pt, ptsize *= 2)) == NULL)
- return NULL;
- bufsize = ptsize;
- continue;
- }
- break;
- }
- if (len < 0)
- return NULL;
- else
- return pt;
+
+ /*
+ * If no buffer specified by the user, allocate one as necessary.
+ */
+ size = 1024 >> 1;
+ do {
+ if ((npt = realloc(pt, size <<= 1)) == NULL)
+ break;
+ pt = npt;
+ if (__getcwd(pt, size) >= 0)
+ return (pt);
+ } while (size <= MAXPATHLEN * 4 && errno == ERANGE);
+
+ free(pt);
+ return (NULL);
}
Home |
Main Index |
Thread Index |
Old Index