From ca41d8cac834e0685c855a97c5a1d968a639e570 Mon Sep 17 00:00:00 2001 From: moritz Date: Wed, 7 Mar 2007 21:01:16 +0000 Subject: [PATCH] More code reorganization, move compatibility and utility functions out of ezstream.c. Use a basename() function for Windows that behaves identical to a modern Unix' basename(). git-svn-id: https://svn.xiph.org/trunk/ezstream@12673 0101bb08-14d6-0310-b084-bc0e0c8e3800 --- src/Makefile.am | 2 +- src/compat.c | 74 +++++++++++++++++++++++++++++++++++++++++++ src/compat.h | 46 +++++++++++++++++++++++---- src/configfile.c | 5 +-- src/ezstream.c | 42 ------------------------ src/playlist.c | 10 +----- src/util.c | 14 +++++++- src/util.h | 1 + win32/ezstream.vcproj | 3 ++ 9 files changed, 133 insertions(+), 64 deletions(-) create mode 100644 src/compat.c diff --git a/src/Makefile.am b/src/Makefile.am index 82c574e..ceaf4ee 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -2,7 +2,7 @@ AUTOMAKE_OPTIONS = 1.9 foreign bin_PROGRAMS = ezstream -ezstream_SOURCES = ezstream.c configfile.c playlist.c util.c +ezstream_SOURCES = ezstream.c compat.c configfile.c playlist.c util.c ezstream_LDADD = @LIBOBJS@ @XIPH_LIBS@ AM_CFLAGS = @XIPH_CFLAGS@ diff --git a/src/compat.c b/src/compat.c new file mode 100644 index 0000000..f86abba --- /dev/null +++ b/src/compat.c @@ -0,0 +1,74 @@ +/* + * Copyright (c) 2007 Moritz Grimm + * + * 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. + */ + +#ifdef HAVE_CONFIG_H +# include "config.h" +#endif + +#ifdef HAVE_SYS_TYPES_H +# include +#endif +#include +#include +#include + +#include "compat.h" + +/* + * Modified basename() implementation from OpenBSD, based on: + * $OpenBSD: basename.c,v 1.14 2005/08/08 08:05:33 espie Exp $ + * Copyright (c) 1997, 2004 Todd C. Miller + */ +char * +local_basename(const char *path) +{ + static char bname[PATH_MAX]; + size_t len; + const char *startp, *endp; + + 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 == PATH_SEPARATOR) + endp--; + + /* All slashes become "\" */ + if (endp == path && *endp == PATH_SEPARATOR) { + bname[0] = PATH_SEPARATOR; + bname[1] = '\0'; + return (bname); + } + + /* Find the start of the base */ + startp = endp; + while (startp > path && *(startp - 1) != PATH_SEPARATOR) + startp--; + + len = endp - startp + 1; + if (len >= sizeof(bname)) { + errno = ENAMETOOLONG; + return (NULL); + } + memcpy(bname, startp, len); + bname[len] = '\0'; + + return (bname); +} diff --git a/src/compat.h b/src/compat.h index a1fe0e1..5ae1d67 100644 --- a/src/compat.h +++ b/src/compat.h @@ -1,3 +1,19 @@ +/* + * Copyright (c) 2007 Moritz Grimm + * + * 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 __COMPAT_H__ #define __COMPAT_H__ @@ -13,8 +29,16 @@ # endif /* WIN32 */ #endif /* !PATH_SEPARATOR */ -#ifdef WIN32 +#ifndef PATH_MAX +# define PATH_MAX 256 +#endif /* !PATH_MAX */ +/* Sometimes defined through . */ +#ifndef SIZE_T_MAX +# define SIZE_T_MAX UINT_MAX +#endif /* !SIZE_T_MAX */ + +#ifdef WIN32 # define _PATH_DEVNULL "nul" # define pclose _pclose @@ -31,12 +55,20 @@ # define S_IXGRP 0 # define S_IXOTH 0 -#else - -# ifndef S_IEXEC -# define S_IEXEC S_IXUSR -# endif - +# define basename local_basename +# define sleep(a) Sleep((a) * 1000) #endif /* WIN32 */ +/* Usually defined in . */ +#ifndef S_IEXEC +# define S_IEXEC S_IXUSR +#endif /* !S_IEXEC */ + +/* For Solaris, possibly others (usually defined in .) */ +#ifndef _PATH_DEVNULL +# define _PATH_DEVNULL "/dev/null" +#endif /* !_PATH_DEVNULL */ + +char * local_basename(const char *); + #endif /* __COMPAT_H__ */ diff --git a/src/configfile.c b/src/configfile.c index 7c2cb56..03beb3e 100644 --- a/src/configfile.c +++ b/src/configfile.c @@ -26,14 +26,11 @@ #include #include +#include "compat.h" #include "configfile.h" #include "strfctns.h" #include "util.h" -#ifndef PATH_MAX -# define PATH_MAX 256 -#endif - extern char *__progname; static EZCONFIG ezConfig; diff --git a/src/ezstream.c b/src/ezstream.c index 25c4b11..9845437 100644 --- a/src/ezstream.c +++ b/src/ezstream.c @@ -62,15 +62,6 @@ #include "strfctns.h" #include "util.h" -#ifndef PATH_MAX -# define PATH_MAX 256 -#endif - -/* For Solaris, possibly others (usually defined in .) */ -#ifndef _PATH_DEVNULL -# define _PATH_DEVNULL "/dev/null" -#endif /* _PATH_DEVNULL */ - #define STREAM_DONE 0 #define STREAM_CONT 1 #define STREAM_SKIP 2 @@ -112,10 +103,6 @@ typedef struct tag_ID3Tag { char genre; } ID3Tag; -#ifdef WIN32 -char * basename(const char *); -#endif -int strrcmp(const char *, const char *); int urlParse(const char *, char **, int *, char **); void replaceString(const char *, char *, size_t, const char *, const char *); void setMetadata(shout_t *, const char *); @@ -150,31 +137,6 @@ sig_handler(int sig) } #endif /* HAVE_SIGNALS */ -#ifdef WIN32 -char * -basename(const char *fileName) -{ - char *pLast = strrchr(fileName, PATH_SEPARATOR); - - if (pLast != NULL) - return (pLast + 1); - - return (NULL); -} -#endif /* WIN32 */ - -int -strrcmp(const char *s, const char *sub) -{ - size_t slen = strlen(s); - size_t sublen = strlen(sub); - - if (sublen > slen) - return (1); - - return (memcmp(s + slen - sublen, sub, sublen)); -} - int urlParse(const char *url, char **hostname, int *port, char **mountname) { @@ -614,11 +576,7 @@ reconnectServer(shout_t *shout, int closeConn) printf("%s: Waiting 5s for %s to come back ...\n", __progname, pezConfig->URL); -#ifdef WIN32 - Sleep(5000); -#else sleep(5); -#endif }; printf("%s: Giving up\n", __progname); diff --git a/src/playlist.c b/src/playlist.c index c1f4096..7434307 100644 --- a/src/playlist.c +++ b/src/playlist.c @@ -35,15 +35,7 @@ #include "playlist.h" #include "util.h" -#ifndef SIZE_T_MAX -# define SIZE_T_MAX UINT_MAX -#endif - -#ifndef PATH_MAX -# define PATH_MAX 256 -#endif - -extern char *__progname; +extern char *__progname; struct playlist { char *filename; diff --git a/src/util.c b/src/util.c index aca273c..06e85f8 100644 --- a/src/util.c +++ b/src/util.c @@ -33,7 +33,7 @@ # define SIZE_T_MAX UINT_MAX #endif -extern char *__progname; +extern char *__progname; void * xmalloc(size_t size) @@ -137,3 +137,15 @@ xstrdup(const char *str) memcpy(nstr, str, len); return (nstr); } + +int +strrcmp(const char *s, const char *sub) +{ + size_t slen = strlen(s); + size_t sublen = strlen(sub); + + if (sublen > slen) + return (1); + + return (memcmp(s + slen - sublen, sub, sublen)); +} diff --git a/src/util.h b/src/util.h index 25b1298..105553b 100644 --- a/src/util.h +++ b/src/util.h @@ -22,5 +22,6 @@ void * xcalloc(size_t /* nmemb */, size_t /* size */); void * xrealloc(void *, size_t /* nmemb */, size_t /* size */); void xfree(void *); char * xstrdup(const char *); +int strrcmp(const char *, const char *); #endif /* __UTIL_H__ */ diff --git a/win32/ezstream.vcproj b/win32/ezstream.vcproj index de5e036..9f35eab 100644 --- a/win32/ezstream.vcproj +++ b/win32/ezstream.vcproj @@ -153,6 +153,9 @@ + +