Adds basename/dirname implementation for platforms that lack it (BeOS...)

This commit is contained in:
Eric Petit 2007-07-10 11:30:11 +00:00
parent 043ba9e1bf
commit 26b837ffc0
7 changed files with 168 additions and 2 deletions

View File

@ -1475,6 +1475,7 @@
"-D__TRANSMISSION__",
"-DHAVE_STRLCPY",
"-DHAVE_STRLCAT",
"-DHAVE_LIBGEN",
);
PRODUCT_NAME = transmission;
};

18
configure vendored
View File

@ -180,6 +180,23 @@ EOF
rm -f testconf*
}
libgen_test()
{
verbose libgen_test
cat > testconf.c << EOF
#include <libgen.h>
int main()
{
return 0;
}
EOF
if runcmd $CC -o testconf testconf.c
then
CFLAGS="$CFLAGS -DHAVE_LIBGEN"
fi
rm -f testconf*
}
gettext_test()
{
verbose gettext_test
@ -450,6 +467,7 @@ lrintf_test
#
strlcpy_test
strlcat_test
libgen_test
#
# Generate config.mk

View File

@ -0,0 +1,67 @@
/* $Id$ */
/* $OpenBSD: basename.c,v 1.14 2005/08/08 08:05:33 espie Exp $ */
/*
* 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
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#ifndef HAVE_LIBGEN
#include <errno.h>
#include <string.h>
#include <sys/param.h>
char *
basename(const char *path)
{
static char bname[MAXPATHLEN];
size_t len;
const char *endp, *startp;
/* Empty or NULL string gets treated as "." */
if (path == NULL || *path == '\0') {
bname[0] = '.';
bname[1] = '\0';
return (bname);
}
/* Strip any trailing slashes */
endp = path + strlen(path) - 1;
while (endp > path && *endp == '/')
endp--;
/* All slashes becomes "/" */
if (endp == path && *endp == '/') {
bname[0] = '/';
bname[1] = '\0';
return (bname);
}
/* Find the start of the base */
startp = endp;
while (startp > path && *(startp - 1) != '/')
startp--;
len = endp - startp + 1;
if (len >= sizeof(bname)) {
errno = ENAMETOOLONG;
return (NULL);
}
memcpy(bname, startp, len);
bname[len] = '\0';
return (bname);
}
#endif /* HAVE_LIBGEN */

71
libtransmission/dirname.c Normal file
View File

@ -0,0 +1,71 @@
/* $Id$ */
/* $OpenBSD: dirname.c,v 1.13 2005/08/08 08:05:33 espie Exp $ */
/*
* 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
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#ifndef HAVE_LIBGEN
#include <errno.h>
#include <string.h>
#include <sys/param.h>
char *
dirname(const char *path)
{
static char dname[MAXPATHLEN];
size_t len;
const char *endp;
/* Empty or NULL string gets treated as "." */
if (path == NULL || *path == '\0') {
dname[0] = '.';
dname[1] = '\0';
return (dname);
}
/* Strip any 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) {
dname[0] = *endp == '/' ? '/' : '.';
dname[1] = '\0';
return (dname);
} else {
/* Move forward past the separating slashes */
do {
endp--;
} while (endp > path && *endp == '/');
}
len = endp - path + 1;
if (len >= sizeof(dname)) {
errno = ENAMETOOLONG;
return (NULL);
}
memcpy(dname, path, len);
dname[len] = '\0';
return (dname);
}
#endif /* HAVE_LIBGEN */

View File

@ -12,7 +12,6 @@
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <libgen.h>
#include <dirent.h>
#include <stdio.h> /* FILE, snprintf, stderr */

View File

@ -36,4 +36,13 @@ size_t
strlcat(char *dst, const char *src, size_t siz);
#endif
#ifdef HAVE_LIBGEN
# include <libgen.h>
#else
char *
dirname(const char *path);
char *
basename(const char *path);
#endif
#endif /* TRCOMPAT_H */

View File

@ -6,7 +6,8 @@ include ../mk/common.mk
SRCS = bencode.c choking.c clients.c completion.c fastresume.c fdlimit.c \
http.c inout.c ipcparse.c list.c makemeta.c metainfo.c natpmp.c \
net.c peer.c platform.c ratecontrol.c sha1.c shared.c strlcat.c \
strlcpy.c torrent.c tracker.c transmission.c upnp.c utils.c xml.c
strlcpy.c torrent.c tracker.c transmission.c upnp.c utils.c xml.c \
basename.c dirname.c
OBJS = $(SRCS:%.c=%.o)