New versions of openbsd_{basename,dirname}.c
Hi,
Dynamic allocation versions of openbsd_basename.c and openbsd_dirname.c
are attached together with old functions together with some test strings
in test_base+dirname.c. Comments are appreciated. Another part of the
patch for libtar.
Thanks!
SOURCES = test_base+dirname.c \
openbsd_basename.c openbsd_basename_new.c \
openbsd_dirname.c openbsd_dirname_new.c
OBJECTS = $(SOURCES:.c=.o)
TARGET = test_base+dirname
RUN = test
CC = gcc
CFLAGS = -g -c -O2 -Wall -Werror -Dlint
LDFLAGS = -g
all: $(SOURCES) $(TARGET)
$(TARGET): $(OBJECTS)
$(CC) $(LDFLAGS) $(OBJECTS) -o $@ -lbsd
$(RUN): $(TARGET)
echo Testing base+dirname
./$(TARGET); echo $$?
.c.o:
$(CC) $(CFLAGS) $< -o $@
clean:
-rm *.o *~ $(TARGET)
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
/* POSIX version (default) */
# include <libgen.h>
typedef struct {
char *test;
char *result;
} test_t;
const test_t tests1[] = {
{ "/usr/local/foo", "foo" },
{ "/usr/local/foo/", "foo" },
{ NULL, NULL }
};
const test_t tests2[] = {
{ "foobar", "." },
{ "/usr/local/foo", "/usr/local" },
{ "/usr/local/foo/", "/usr/local" },
{ "/", "/" },
{ "//", "/" },
{ "", "." },
{ NULL, NULL }
};
char *openbsd_basename(const char *path);
char *openbsd_basename_new(const char *path);
char *openbsd_dirname(const char *path);
char *openbsd_dirname_new(const char *path);
int main() {
char test1[1024], *result;
int i, ret1, ret2;
/* basename tests */
fprintf(stdout,"\nopenbsd_basename:\n");
for (i = 0; tests1[i].test; i++) {
strcpy(test1, tests1[i].test);
result = openbsd_basename(test1);
fprintf(stdout,"\nTest: #%d\n",i);
fprintf(stdout,"result test[%d].result: |%s| |%s|\n", i, result, tests1[i].result);
fprintf(stdout,"test1 tests[%d].test= = |%s| |%s|\n", i, test1, tests1[i].test);
ret1 = strcmp(result, tests1[i].result);
ret2 = strcmp(test1, tests1[i].test);
if (ret1) {
printf("basename test failed\n");
}
if (ret2) {
printf("test1 modified by openbsd_basename\n");
}
}
fprintf(stdout,"\nopenbsd_basename_new:\n");
for (i = 0; tests1[i].test; i++) {
strcpy(test1, tests1[i].test);
result = openbsd_basename_new(test1);
fprintf(stdout,"\nTest: #%d\n",i);
fprintf(stdout,"result test[%d].result: |%s| |%s|\n", i, result, tests1[i].result);
fprintf(stdout,"test1 tests1[%d].test: |%s| |%s|\n", i, test1, tests1[i].test);
ret1 = strcmp(result, tests1[i].result);
ret2 = strcmp(test1, tests1[i].test);
if (ret1) {
printf("basename test failed\n");
}
if (ret2) {
printf("test1 modified by openbsd_basename_new\n");
}
}
/* dirname tests */
fprintf(stdout,"\nopenbsd_dirname:\n");
for (i = 0; tests2[i].test; i++) {
strcpy(test1, tests2[i].test);
result = openbsd_dirname(test1);
fprintf(stdout,"\nTest: #%d\n",i);
fprintf(stdout,"result test[%d].result: |%s| |%s|\n", i, result, tests2[i].result);
fprintf(stdout,"test1 tests[%d].test= = |%s| |%s|\n", i, test1, tests2[i].test);
ret1 = strcmp(result, tests2[i].result);
ret2 = strcmp(test1, tests2[i].test);
if (ret1) {
printf("dirname test failed\n");
}
if (ret2) {
printf("test1 modified by openbsd_dirname\n");
}
}
fprintf(stdout,"\nopenbsd_dirname_new:\n");
for (i = 0; tests2[i].test; i++) {
strcpy(test1, tests2[i].test);
result = openbsd_dirname_new(test1);
fprintf(stdout,"\nTest: #%d\n",i);
fprintf(stdout,"result test[%d].result: |%s| |%s|\n", i, result, tests2[i].result);
fprintf(stdout,"test1 tests2[%d].test: |%s| |%s|\n", i, test1, tests2[i].test);
ret1 = strcmp(result, tests2[i].result);
ret2 = strcmp(test1, tests2[i].test);
if (ret1) {
printf("dirname test failed\n");
}
if (ret2) {
printf("test1 modified by openbsd_dirname_new\n");
}
}
exit(0);
}
/* $OpenBSD: basename.c,v 1.4 1999/05/30 17:10:30 espie Exp $ */
/*
* Copyright (c) 1997 Todd C. Miller <Todd.Miller@courtesan.com>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
* AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
* THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef lint
static char rcsid[] = "$OpenBSD: basename.c,v 1.4 1999/05/30 17:10:30 espie Exp $";
#endif /* not lint */
#include <errno.h>
#include <string.h>
#include <sys/param.h>
char *
openbsd_basename(path)
const char *path;
{
static char bname[MAXPATHLEN];
register const char *endp, *startp;
/* Empty or NULL string gets treated as "." */
if (path == NULL || *path == '\0') {
(void)strcpy(bname, ".");
return(bname);
}
/* Strip trailing slashes */
endp = path + strlen(path) - 1;
while (endp > path && *endp == '/')
endp--;
/* All slashes becomes "/" */
if (endp == path && *endp == '/') {
(void)strcpy(bname, "/");
return(bname);
}
/* Find the start of the base */
startp = endp;
while (startp > path && *(startp - 1) != '/')
startp--;
if (endp - startp + 1 > sizeof(bname)) {
errno = ENAMETOOLONG;
return(NULL);
}
(void)strncpy(bname, startp, endp - startp + 1);
bname[endp - startp + 1] = '\0';
return(bname);
}
/* $OpenBSD: basename.c,v 1.4 1999/05/30 17:10:30 espie Exp $ */
/*
* Copyright (c) 1997 Todd C. Miller <Todd.Miller@courtesan.com>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
* AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
* THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef lint
static char rcsid[] = "$OpenBSD: basename.c,v 1.4 1999/05/30 17:10:30 espie Exp $";
#endif /* not lint */
#include <errno.h>
#include <string.h>
#include <sys/param.h>
char *
openbsd_basename_new(path)
const char *path;
{
static char *bname = NULL;
register const char *endp, *startp;
bname = strdup(path);
/* Empty or NULL string gets treated as "." */
if (path == NULL || *path == '\0') {
(void)strcpy(bname, ".");
return(bname);
}
/* Strip trailing slashes */
endp = path + strlen(path) - 1;
while (endp > path && *endp == '/')
endp--;
/* All slashes becomes "/" */
if (endp == path && *endp == '/') {
(void)strcpy(bname, "/");
return(bname);
}
/* Find the start of the base */
startp = endp;
while (startp > path && *(startp - 1) != '/')
startp--;
if (endp - startp + 1 > sizeof(bname)) {
errno = ENAMETOOLONG;
return(NULL);
}
(void)strncpy(bname, startp, endp - startp + 1);
bname[endp - startp + 1] = '\0';
return(bname);
}
/* $OpenBSD: dirname.c,v 1.4 1999/05/30 17:10:30 espie Exp $ */
/*
* Copyright (c) 1997 Todd C. Miller <Todd.Miller@courtesan.com>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
* AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
* THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef lint
static char rcsid[] = "$OpenBSD: dirname.c,v 1.4 1999/05/30 17:10:30 espie Exp $";
#endif /* not lint */
#include <errno.h>
#include <string.h>
#include <sys/param.h>
char *
openbsd_dirname(path)
const char *path;
{
static char bname[MAXPATHLEN];
register const char *endp;
/* Empty or NULL string gets treated as "." */
if (path == NULL || *path == '\0') {
(void)strcpy(bname, ".");
return(bname);
}
/* Strip trailing slashes */
endp = path + strlen(path) - 1;
while (endp > path && *endp == '/')
endp--;
/* Find the start of the dir */
while (endp > path && *endp != '/')
endp--;
/* Either the dir is "/" or there are no slashes */
if (endp == path) {
(void)strcpy(bname, *endp == '/' ? "/" : ".");
return(bname);
} else {
do {
endp--;
} while (endp > path && *endp == '/');
}
if (endp - path + 1 > sizeof(bname)) {
errno = ENAMETOOLONG;
return(NULL);
}
(void)strncpy(bname, path, endp - path + 1);
bname[endp - path + 1] = '\0';
return(bname);
}
/* $OpenBSD: dirname.c,v 1.4 1999/05/30 17:10:30 espie Exp $ */
/*
* Copyright (c) 1997 Todd C. Miller <Todd.Miller@courtesan.com>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
* AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
* THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef lint
static char rcsid[] = "$OpenBSD: dirname.c,v 1.4 1999/05/30 17:10:30 espie Exp $";
#endif /* not lint */
#include <errno.h>
#include <string.h>
#include <sys/param.h>
#include <stdio.h>
#include <stdlib.h>
char *
openbsd_dirname_new(path)
const char *path;
{
static char *bname = NULL;
register const char *endp;
/* Empty or NULL string gets treated as "." */
if (path == NULL || *path == '\0') {
bname = malloc(2);
(void)strcpy(bname, ".");
return(bname);
}
bname = strdup(path);
/* Strip trailing slashes */
endp = path + strlen(path) - 1;
while (endp > path && *endp == '/')
endp--;
/* Find the start of the dir */
while (endp > path && *endp != '/')
endp--;
/* Either the dir is "/" or there are no slashes */
if (endp == path) {
(void)strcpy(bname, *endp == '/' ? "/" : ".");
return(bname);
} else {
do {
endp--;
} while (endp > path && *endp == '/');
}
if (endp - path + 1 > strlen(bname)) {
errno = ENAMETOOLONG;
return(NULL);
}
(void)strncpy(bname, path, endp - path + 1);
bname[endp - path + 1] = '\0';
return(bname);
}
Reply to: