summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorTodd C. Miller <millert@cvs.openbsd.org>2004-11-25 16:21:26 +0000
committerTodd C. Miller <millert@cvs.openbsd.org>2004-11-25 16:21:26 +0000
commit28e37fa2a659dc59d6d08a82f73f9063bc7c1330 (patch)
treee0d21031b3d0647d4fd4b4d5581f685be0713779 /lib
parentf7a08eb995954e1589ac898355fdd7c49742f0a8 (diff)
Don't use strlcpy() to copy just part of a string, it make the code too
confusing. While in here make the code more readable. Ok pat@
Diffstat (limited to 'lib')
-rw-r--r--lib/libc/gen/basename.c33
-rw-r--r--lib/libc/gen/dirname.c34
2 files changed, 39 insertions, 28 deletions
diff --git a/lib/libc/gen/basename.c b/lib/libc/gen/basename.c
index 1e8a9b50c20..deb0288c96c 100644
--- a/lib/libc/gen/basename.c
+++ b/lib/libc/gen/basename.c
@@ -1,7 +1,7 @@
-/* $OpenBSD: basename.c,v 1.11 2003/06/17 21:56:23 millert Exp $ */
+/* $OpenBSD: basename.c,v 1.12 2004/11/25 16:21:25 millert Exp $ */
/*
- * Copyright (c) 1997 Todd C. Miller <Todd.Miller@courtesan.com>
+ * Copyright (c) 1997, 2004 Todd C. Miller <Todd.Miller@courtesan.com>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
@@ -17,7 +17,7 @@
*/
#ifndef lint
-static char rcsid[] = "$OpenBSD: basename.c,v 1.11 2003/06/17 21:56:23 millert Exp $";
+static char rcsid[] = "$OpenBSD: basename.c,v 1.12 2004/11/25 16:21:25 millert Exp $";
#endif /* not lint */
#include <errno.h>
@@ -29,23 +29,26 @@ char *
basename(const char *path)
{
static char bname[MAXPATHLEN];
- register const char *endp, *startp;
+ size_t len;
+ const char *endp, *startp;
/* Empty or NULL string gets treated as "." */
if (path == NULL || *path == '\0') {
- (void)strlcpy(bname, ".", sizeof bname);
- return(bname);
+ bname[0] = '.';
+ bname[1] = '\0';
+ return (bname);
}
- /* Strip trailing slashes */
+ /* Strip any trailing slashes */
endp = path + strlen(path) - 1;
while (endp > path && *endp == '/')
endp--;
- /* All slashes become "/" */
+ /* All slashes becomes "/" */
if (endp == path && *endp == '/') {
- (void)strlcpy(bname, "/", sizeof bname);
- return(bname);
+ bname[0] = '/';
+ bname[1] = '\0';
+ return (bname);
}
/* Find the start of the base */
@@ -53,10 +56,12 @@ basename(const char *path)
while (startp > path && *(startp - 1) != '/')
startp--;
- if (endp - startp + 2 > sizeof(bname)) {
+ len = endp - startp + 1;
+ if (len >= sizeof(bname)) {
errno = ENAMETOOLONG;
- return(NULL);
+ return (NULL);
}
- strlcpy(bname, startp, endp - startp + 2);
- return(bname);
+ memcpy(bname, startp, len);
+ bname[len] = '\0';
+ return (bname);
}
diff --git a/lib/libc/gen/dirname.c b/lib/libc/gen/dirname.c
index f75a4715fb8..d05d8d0d1ba 100644
--- a/lib/libc/gen/dirname.c
+++ b/lib/libc/gen/dirname.c
@@ -1,7 +1,7 @@
-/* $OpenBSD: dirname.c,v 1.10 2003/06/17 21:56:23 millert Exp $ */
+/* $OpenBSD: dirname.c,v 1.11 2004/11/25 16:21:25 millert Exp $ */
/*
- * Copyright (c) 1997 Todd C. Miller <Todd.Miller@courtesan.com>
+ * Copyright (c) 1997, 2004 Todd C. Miller <Todd.Miller@courtesan.com>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
@@ -17,7 +17,7 @@
*/
#ifndef lint
-static char rcsid[] = "$OpenBSD: dirname.c,v 1.10 2003/06/17 21:56:23 millert Exp $";
+static char rcsid[] = "$OpenBSD: dirname.c,v 1.11 2004/11/25 16:21:25 millert Exp $";
#endif /* not lint */
#include <errno.h>
@@ -28,16 +28,18 @@ static char rcsid[] = "$OpenBSD: dirname.c,v 1.10 2003/06/17 21:56:23 millert Ex
char *
dirname(const char *path)
{
- static char bname[MAXPATHLEN];
- register const char *endp;
+ static char dname[MAXPATHLEN];
+ size_t len;
+ const char *endp;
/* Empty or NULL string gets treated as "." */
if (path == NULL || *path == '\0') {
- (void)strlcpy(bname, ".", sizeof bname);
- return(bname);
+ dname[0] = '.';
+ dname[1] = '\0';
+ return (dname);
}
- /* Strip trailing slashes */
+ /* Strip any trailing slashes */
endp = path + strlen(path) - 1;
while (endp > path && *endp == '/')
endp--;
@@ -48,18 +50,22 @@ dirname(const char *path)
/* Either the dir is "/" or there are no slashes */
if (endp == path) {
- (void)strlcpy(bname, *endp == '/' ? "/" : ".", sizeof bname);
- return(bname);
+ dname[0] = *endp == '/' ? '/' : '.';
+ dname[1] = '\0';
+ return (dname);
} else {
+ /* Move forward past the separating slashes */
do {
endp--;
} while (endp > path && *endp == '/');
}
- if (endp - path + 2 > sizeof(bname)) {
+ len = endp - path + 1;
+ if (len >= sizeof(dname)) {
errno = ENAMETOOLONG;
- return(NULL);
+ return (NULL);
}
- strlcpy(bname, path, endp - path + 2);
- return(bname);
+ memcpy(dname, path, len);
+ dname[len] = '\0';
+ return (dname);
}