-/* $OpenBSD: dirname.c,v 1.4 1999/05/30 17:10:30 espie Exp $ */
+/* $OpenBSD: dirname.c,v 1.7 2002/05/24 21:22:37 deraadt Exp $ */
/*
* Copyright (c) 1997 Todd C. Miller <Todd.Miller@courtesan.com>
#ifndef HAVE_DIRNAME
#if defined(LIBC_SCCS) && !defined(lint)
-static char rcsid[] = "$OpenBSD: dirname.c,v 1.4 1999/05/30 17:10:30 espie Exp $";
+static char rcsid[] = "$OpenBSD: dirname.c,v 1.7 2002/05/24 21:22:37 deraadt Exp $";
#endif /* LIBC_SCCS and not lint */
#include <errno.h>
/* Empty or NULL string gets treated as "." */
if (path == NULL || *path == '\0') {
- (void)strcpy(bname, ".");
+ (void)strlcpy(bname, ".", sizeof bname);
return(bname);
}
/* Either the dir is "/" or there are no slashes */
if (endp == path) {
- (void)strcpy(bname, *endp == '/' ? "/" : ".");
+ (void)strlcpy(bname, *endp == '/' ? "/" : ".", sizeof bname);
return(bname);
} else {
do {
} while (endp > path && *endp == '/');
}
- if (endp - path + 1 > sizeof(bname)) {
+ if (endp - path + 2 > sizeof(bname)) {
errno = ENAMETOOLONG;
return(NULL);
}
- (void)strncpy(bname, path, endp - path + 1);
- bname[endp - path + 1] = '\0';
+ strlcpy(bname, path, endp - path + 2);
return(bname);
}
#endif