Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/netbsd-1-6]: src/lib/libc/gen Pull up revision 1.5 (requested by thorpej...



details:   https://anonhg.NetBSD.org/src/rev/3165cd5eee43
branches:  netbsd-1-6
changeset: 529692:3165cd5eee43
user:      he <he%NetBSD.org@localhost>
date:      Sat Dec 07 22:19:04 2002 +0000

description:
Pull up revision 1.5 (requested by thorpej in ticket #918):
  Change basename(3) and dirname(3) to return a pointer to
  static storage instead of modifying their arguments.  Fixes
  PR#18647.

diffstat:

 lib/libc/gen/basename.c |  38 +++++++++++++++++++++++++-------------
 1 files changed, 25 insertions(+), 13 deletions(-)

diffs (75 lines):

diff -r 74ad2b2ce22b -r 3165cd5eee43 lib/libc/gen/basename.c
--- a/lib/libc/gen/basename.c   Sat Dec 07 22:18:46 2002 +0000
+++ b/lib/libc/gen/basename.c   Sat Dec 07 22:19:04 2002 +0000
@@ -1,11 +1,11 @@
-/*     $NetBSD: basename.c,v 1.4 2002/01/31 22:43:37 tv Exp $  */
+/*     $NetBSD: basename.c,v 1.4.2.1 2002/12/07 22:19:04 he Exp $      */
 
 /*-
- * Copyright (c) 1997 The NetBSD Foundation, Inc.
+ * Copyright (c) 1997, 2002 The NetBSD Foundation, Inc.
  * All rights reserved.
  *
  * This code is derived from software contributed to The NetBSD Foundation
- * by Klaus Klein.
+ * by Klaus Klein and Jason R. Thorpe.
  *
  * Redistribution and use in source and binary forms, with or without
  * modification, are permitted provided that the following conditions
@@ -38,11 +38,12 @@
 
 #include <sys/cdefs.h>
 #if defined(LIBC_SCCS) && !defined(lint)
-__RCSID("$NetBSD: basename.c,v 1.4 2002/01/31 22:43:37 tv Exp $");
+__RCSID("$NetBSD: basename.c,v 1.4.2.1 2002/12/07 22:19:04 he Exp $");
 #endif /* !LIBC_SCCS && !lint */
 
 #include "namespace.h"
 #include <libgen.h>
+#include <limits.h>
 #include <string.h>
 
 #ifdef __weak_alias
@@ -55,7 +56,9 @@
        char *path;
 {
        static char singledot[] = ".";
-       char *p;
+       static char result[PATH_MAX];
+       char *p, *lastp;
+       size_t len;
 
        /*
         * If `path' is a null pointer or points to an empty string,
@@ -65,14 +68,23 @@
                return (singledot);
 
        /* Strip trailing slashes, if any. */
-       p = path + strlen(path) - 1;
-       while (*p == '/' && p != path)
-               *p-- = '\0';
+       lastp = path + strlen(path) - 1;
+       while (lastp != path && *lastp == '/')
+               lastp--;
+
+       /* Now find the beginning of this (final) component. */
+       p = lastp;
+       while (p != path && *(p - 1) != '/')
+               p--;
 
-       /* Return pointer to the final pathname component. */
-       if (((p = strrchr(path, '/')) == NULL) || (*(p + 1) == '\0'))
-               return (path);
-       else
-               return (p + 1);
+       /* ...and copy the result into the result buffer. */
+       len = (lastp - p) + 1 /* last char */;
+       if (len > (PATH_MAX - 1))
+               len = PATH_MAX - 1;
+
+       memcpy(result, p, len);
+       result[len] = '\0';
+
+       return (result);
 }
 #endif



Home | Main Index | Thread Index | Old Index