diff --git a/Transmission.xcodeproj/project.pbxproj b/Transmission.xcodeproj/project.pbxproj index bea0b24d9..ea2025a3f 100644 --- a/Transmission.xcodeproj/project.pbxproj +++ b/Transmission.xcodeproj/project.pbxproj @@ -1475,6 +1475,7 @@ "-D__TRANSMISSION__", "-DHAVE_STRLCPY", "-DHAVE_STRLCAT", + "-DHAVE_LIBGEN", ); PRODUCT_NAME = transmission; }; diff --git a/configure b/configure index 2575995bf..e4395d2cc 100755 --- a/configure +++ b/configure @@ -180,6 +180,23 @@ EOF rm -f testconf* } +libgen_test() +{ + verbose libgen_test + cat > testconf.c << EOF +#include +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 diff --git a/libtransmission/basename.c b/libtransmission/basename.c new file mode 100644 index 000000000..21d4bceb7 --- /dev/null +++ b/libtransmission/basename.c @@ -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 + * + * 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 +#include +#include + +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 */ diff --git a/libtransmission/dirname.c b/libtransmission/dirname.c new file mode 100644 index 000000000..42a566b08 --- /dev/null +++ b/libtransmission/dirname.c @@ -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 + * + * 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 +#include +#include + +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 */ diff --git a/libtransmission/makemeta.c b/libtransmission/makemeta.c index 26efe9e7d..14ee3c92b 100644 --- a/libtransmission/makemeta.c +++ b/libtransmission/makemeta.c @@ -12,7 +12,6 @@ #include #include #include -#include #include #include /* FILE, snprintf, stderr */ diff --git a/libtransmission/trcompat.h b/libtransmission/trcompat.h index 1c92386de..8ca1deecc 100644 --- a/libtransmission/trcompat.h +++ b/libtransmission/trcompat.h @@ -36,4 +36,13 @@ size_t strlcat(char *dst, const char *src, size_t siz); #endif +#ifdef HAVE_LIBGEN +# include +#else +char * +dirname(const char *path); +char * +basename(const char *path); +#endif + #endif /* TRCOMPAT_H */ diff --git a/mk/lib.mk b/mk/lib.mk index 988d97f04..8fe9dc40a 100644 --- a/mk/lib.mk +++ b/mk/lib.mk @@ -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)