From c0b09af703198aaf005be124e1be69f7cb30bf36 Mon Sep 17 00:00:00 2001 From: moritz Date: Wed, 18 Mar 2009 09:10:28 +0000 Subject: [PATCH] Work towards cleaner portability goo, and remove some ununsed code while there. git-svn-id: https://svn.xiph.org/trunk/ezstream@15794 0101bb08-14d6-0310-b084-bc0e0c8e3800 --- configure.in | 1 + src/Makefile.am | 2 +- src/compat.c | 78 +++++++++++++++++++++++++----------- src/configfile.c | 6 +-- src/ezstream.c | 31 ++------------ src/{compat.h => ezstream.h} | 67 +++++++++++++++++-------------- src/getopt.c | 10 +---- src/metadata.c | 23 ++++------- src/playlist.c | 19 +++------ src/util.c | 14 +------ src/xalloc.c | 78 +----------------------------------- src/xalloc.h | 7 +--- win32/compat.h | 10 +++-- 13 files changed, 124 insertions(+), 222 deletions(-) rename src/{compat.h => ezstream.h} (70%) diff --git a/configure.in b/configure.in index 28e89d3..e499b2d 100644 --- a/configure.in +++ b/configure.in @@ -227,6 +227,7 @@ dnl ####################### AC_CHECK_FUNCS([ \ arc4random \ + basename \ gettimeofday \ getopt \ nl_langinfo \ diff --git a/src/Makefile.am b/src/Makefile.am index a00187c..582aa37 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -19,8 +19,8 @@ AM_CPPFLAGS = @EZ_CPPFLAGS@ AM_LDFLAGS = @EZ_LDFLAGS@ EXTRA_DIST = \ - compat.h \ configfile.h \ + ezstream.h \ metadata.h \ playlist.h \ strfctns.h \ diff --git a/src/compat.c b/src/compat.c index 5c7c466..78442a4 100644 --- a/src/compat.c +++ b/src/compat.c @@ -1,4 +1,47 @@ /* $Id$ */ + +#ifdef HAVE_CONFIG_H +# include "config.h" +#endif + +#include "ezstream.h" + +#if defined(HAVE_LIBGEN_H) && !defined(__linux__) +# include +#endif /* HAVE_LIBGEN_H && !__linux__ */ + +#ifndef PATH_MAX +# define PATH_MAX 256 +#endif /* !PATH_MAX */ + +#ifndef PATH_SEPARATORS +# define PATH_SEPARATORS "/" +#endif /* !PATH_SEPARATORS */ + +char * local_basename(const char *); + +static const char *path_separators = PATH_SEPARATORS; + +static inline int + is_separator(int); + +static inline int +is_separator(int c) +{ + const char *cp; + + for (cp = path_separators; '\0' != *cp; cp++) { + if (*cp == c) + return (1); + } + + return (0); +} + +/* + * Modified basename() implementation from OpenBSD, based on: + * $OpenBSD: src/lib/libc/gen/basename.c,v 1.14 2005/08/08 08:05:33 espie Exp $ + */ /* * Copyright (c) 1997, 2004 Todd C. Miller * @@ -14,27 +57,12 @@ * 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: src/lib/libc/gen/basename.c,v 1.14 2005/08/08 08:05:33 espie Exp $ - */ char * local_basename(const char *path) { +#ifdef HAVE_BASENAME + return (basename(path)); +#else /* HAVE_BASENAME */ static char bname[PATH_MAX]; size_t len; const char *startp, *endp; @@ -45,13 +73,16 @@ local_basename(const char *path) return (bname); } - /* Strip any trailing slashes */ + /* Strip any trailing path separators */ endp = path + strlen(path) - 1; - while (endp > path && *endp == PATH_SEPARATOR) + while (endp > path && is_separator(*endp)) endp--; - /* All slashes become "\" */ - if (endp == path && *endp == PATH_SEPARATOR) { + /* + * All path separators become a single one; pick the first in the + * list as the default. + */ + if (endp == path && is_separator(*endp)) { bname[0] = PATH_SEPARATOR; bname[1] = '\0'; return (bname); @@ -59,7 +90,7 @@ local_basename(const char *path) /* Find the start of the base */ startp = endp; - while (startp > path && *(startp - 1) != PATH_SEPARATOR) + while (startp > path && !is_separator(*(startp - 1))) startp--; len = endp - startp + 1; @@ -71,4 +102,5 @@ local_basename(const char *path) bname[len] = '\0'; return (bname); +#endif /* HAVE_BASENAME */ } diff --git a/src/configfile.c b/src/configfile.c index 1b02aed..ec3f86c 100644 --- a/src/configfile.c +++ b/src/configfile.c @@ -22,12 +22,8 @@ # include "config.h" #endif -#include -#include -#include -#include +#include "ezstream.h" -#include "compat.h" #include "configfile.h" #include "strfctns.h" #include "util.h" diff --git a/src/ezstream.c b/src/ezstream.c index 41492a8..e15f254 100644 --- a/src/ezstream.c +++ b/src/ezstream.c @@ -22,37 +22,14 @@ # include "config.h" #endif -#ifdef HAVE_SYS_TYPES_H -# include -#endif -#ifdef HAVE_SYS_STAT_H -# include -#endif -#ifdef HAVE_SYS_TIME_H -# include -#endif -#include -#include -#include -#ifdef HAVE_LIBGEN_H -# include -#endif -#include -#ifdef HAVE_PATHS_H -# include -#endif +#include "ezstream.h" + #ifdef HAVE_SIGNAL_H # include #endif -#include -#include -#include -#ifdef HAVE_UNISTD_H -# include -#endif + #include -#include "compat.h" #include "configfile.h" #include "metadata.h" #include "playlist.h" @@ -833,7 +810,7 @@ sendStream(shout_t *shout, FILE *filepstream, const char *fileName, if (pezConfig->fileNameIsProgram) { char *tmp = xstrdup(pezConfig->fileName); printf(" [%s]", - basename(tmp)); + local_basename(tmp)); xfree(tmp); } else printf(" [%4lu/%-4lu]", diff --git a/src/compat.h b/src/ezstream.h similarity index 70% rename from src/compat.h rename to src/ezstream.h index 339e7cd..c7bf3e4 100644 --- a/src/compat.h +++ b/src/ezstream.h @@ -22,28 +22,44 @@ # include "config.h" #endif -#ifndef PATH_SEPARATOR +#ifdef HAVE_SYS_TYPES_H +# include +#endif /* HAVE_SYS_TYPES_H */ +#ifdef HAVE_SYS_TIME_H +# include +#else /* HAVE_SYS_TIME_H */ +# include +#endif /* HAVE_SYS_TIME_H */ +#ifdef HAVE_SYS_STAT_H +# include +#endif /* HAVE_SYS_STAT_H */ + +#include +#include +#include +#include +#ifdef HAVE_PATHS_H +# include +#endif /* HAVE_PATHS_H */ +#include +#include +#include +#include +#ifdef HAVE_UNISTD_H +# include +#endif + +#ifndef _PATH_DEVNULL # ifdef WIN32 -# define PATH_SEPARATOR '\\' -# else -# define PATH_SEPARATOR '/' +# define _PATH_DEVNULL "nul" +# else /* WIN32 */ +# define _PATH_DEVNULL "/dev/null" # endif /* WIN32 */ -#endif /* !PATH_SEPARATOR */ - -#ifndef PATH_MAX -# define PATH_MAX 256 -#endif /* !PATH_MAX */ - -/* Sometimes defined through . */ -#ifndef SIZE_T_MAX -# define SIZE_T_MAX ((size_t)-1) -#endif /* !SIZE_T_MAX */ +#endif /* !_PATH_DEVNULL */ #ifdef WIN32 # include -# define _PATH_DEVNULL "nul" - # define pclose _pclose # define popen _popen # define snprintf _snprintf @@ -60,20 +76,9 @@ # define S_IXGRP 0 # define S_IXOTH 0 -# 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 */ - #ifndef HAVE_STRUCT_TIMEVAL struct timeval { long tv_sec; @@ -81,6 +86,10 @@ struct timeval { }; #endif +/* + * For compat.c and getopt.c: + */ + extern int opterr; extern int optind; extern int optopt; @@ -89,7 +98,7 @@ extern char *optarg; extern int local_getopt(int, char * const *, const char *); - -char * local_basename(const char *); +extern char * + local_basename(const char *); #endif /* __COMPAT_H__ */ diff --git a/src/getopt.c b/src/getopt.c index 8734c29..cca8dfd 100644 --- a/src/getopt.c +++ b/src/getopt.c @@ -54,15 +54,7 @@ # include "config.h" #endif /* HAVE_CONFIG_H */ -#include -#include -#include -#include -#ifdef HAVE_UNISTD_H -# include -#endif /* HAVE_UNISTD_H */ - -#include "compat.h" +#include "ezstream.h" int local_getopt(int, char * const *, const char *); diff --git a/src/metadata.c b/src/metadata.c index 6693b28..0d05628 100644 --- a/src/metadata.c +++ b/src/metadata.c @@ -19,20 +19,7 @@ # include "config.h" #endif -#ifdef HAVE_SYS_TYPES_H -# include -#endif -#ifdef HAVE_SYS_STAT_H -# include -#endif -#include -#include -#ifdef HAVE_LIBGEN_H -# include -#endif -#include -#include -#include +#include "ezstream.h" #ifdef HAVE_TAGLIB # include @@ -42,12 +29,16 @@ #endif /* HAVE_VORBISFILE */ #include -#include "compat.h" #include "metadata.h" #include "strfctns.h" #include "util.h" #include "xalloc.h" +/* Usually defined in . */ +#ifndef S_IEXEC +# define S_IEXEC S_IXUSR +#endif /* !S_IEXEC */ + extern char *__progname; extern int vFlag; @@ -314,7 +305,7 @@ metadata_get_name(const char *file) abort(); } - if ((p1 = basename(filename)) == NULL) { + if ((p1 = local_basename(filename)) == NULL) { printf("%s: Internal error: basename() failed with '%s'\n", __progname, filename); exit(1); diff --git a/src/playlist.c b/src/playlist.c index 6a8451c..3ac461c 100644 --- a/src/playlist.c +++ b/src/playlist.c @@ -19,23 +19,16 @@ # include "config.h" #endif -#ifdef HAVE_SYS_TYPES_H -# include -#endif -#ifdef HAVE_SYS_STAT_H -# include -#endif -#include -#include -#include -#include -#include -#include +#include "ezstream.h" -#include "compat.h" #include "playlist.h" #include "xalloc.h" +/* Usually defined in . */ +#ifndef S_IEXEC +# define S_IEXEC S_IXUSR +#endif /* !S_IEXEC */ + extern char *__progname; struct playlist { diff --git a/src/util.c b/src/util.c index e64fe2a..dec3c3d 100644 --- a/src/util.c +++ b/src/util.c @@ -27,32 +27,20 @@ # include "config.h" #endif -#ifdef HAVE_SYS_TYPES_H -# include -#endif -#ifdef HAVE_SYS_TIME_H -# include -#else -# include -#endif +#include "ezstream.h" -#include -#include #ifdef HAVE_LANGINFO_H # include #endif #ifdef HAVE_LOCALE_H # include #endif -#include -#include #ifdef HAVE_ICONV # include #endif #include -#include "compat.h" #include "util.h" #include "configfile.h" #include "xalloc.h" diff --git a/src/xalloc.c b/src/xalloc.c index f8d6c0b..7723200 100644 --- a/src/xalloc.c +++ b/src/xalloc.c @@ -19,15 +19,7 @@ # include "config.h" #endif -#ifdef HAVE_SYS_TYPES_H -# include -#endif -#include -#include -#include -#include -#include -#include +#include "ezstream.h" #include "xalloc.h" @@ -43,27 +35,6 @@ # undef XALLOC_SILENT #endif /* XALLOC_DEBUG && XALLOC_SILENT */ -#ifdef THREAD_SAFE -# include -static pthread_mutex_t xalloc_mutex; -static pthread_mutex_t strerror_mutex; -# define XALLOC_LOCK(mtx) do { \ - int error; \ - if ((error = pthread_mutex_lock(&mtx)) != 0) \ - _xalloc_error(error, "XALLOC: Internal error in %s:%u: pthread_mutex_lock()", \ - __FILE__, __LINE__); \ -} while (0) -# define XALLOC_UNLOCK(mtx) do { \ - int error; \ - if ((error = pthread_mutex_unlock(&mtx)) != 0) \ - _xalloc_error(error, "XALLOC: Internal error in %s:%u: pthread_mutex_unlock()", \ - __FILE__, __LINE__); \ -} while (0) -#else -# define XALLOC_LOCK(mtx) ((void)0) -# define XALLOC_UNLOCK(mtx) ((void)0) -#endif /* THREAD_SAFE */ - #ifdef XALLOC_DEBUG # include @@ -170,13 +141,8 @@ _xalloc_error(int errnum, const char *fmt, ...) va_start(ap, fmt); #ifndef XALLOC_SILENT vfprintf(debug_output, fmt, ap); - if (errnum > 0) { - if (xalloc_initialized) - XALLOC_LOCK(strerror_mutex); + if (errnum > 0) fprintf(debug_output, ": %s\n", strerror(errnum)); - if (xalloc_initialized) - XALLOC_UNLOCK(strerror_mutex); - } fflush(debug_output); #endif /* !XALLOC_SILENT */ va_end(ap); @@ -292,10 +258,6 @@ _xalloc_vasprintf(char **str_p, const char *fmt, va_list ap, size_t *strsiz) void xalloc_initialize_debug(unsigned int level, FILE *output) { -#ifdef THREAD_SAFE - int err; -#endif /* THREAD_SAFE */ - if (xalloc_initialized) _xalloc_fatal("XALLOC: xalloc_initialize(): Xalloc library already initialized\n"); @@ -315,13 +277,6 @@ xalloc_initialize_debug(unsigned int level, FILE *output) xalloc_peak = 0; xalloc_freed = 0; -#ifdef THREAD_SAFE - if ((err = pthread_mutex_init(&strerror_mutex, NULL)) != 0) - _xalloc_error(err, "XALLOC: xalloc_initialize(): Initializing xalloc_mutex"); - if ((err = pthread_mutex_init(&xalloc_mutex, NULL)) != 0) - _xalloc_error(err, "XALLOC: xalloc_initialize(): Initializing strerror_mutex"); -#endif /* THREAD_SAFE */ - xalloc_initialized = 1; } @@ -339,12 +294,10 @@ xalloc_set_functions(void *(*malloc_func)(size_t), realloc_func == NULL) _xalloc_fatal("XALLOC: xalloc_set_functions(): Bad argument(s)\n"); - XALLOC_LOCK(xalloc_mutex); real_malloc = malloc_func; real_calloc = calloc_func; real_realloc = realloc_func; real_free = free_func; - XALLOC_UNLOCK(xalloc_mutex); } void @@ -358,8 +311,6 @@ xalloc_shutdown(void) struct memory *mem, *mem_next; size_t leaked_bytes = 0; - XALLOC_LOCK(xalloc_mutex); - for (mem = RB_MIN(memory_tree, &memory_tree_head); mem != NULL; mem = mem_next) { @@ -395,20 +346,9 @@ xalloc_shutdown(void) (unsigned long)xalloc_peak, (unsigned long)xalloc_freed, (unsigned long)xalloc_total); - - XALLOC_UNLOCK(xalloc_mutex); } #endif /* XALLOC_DEBUG */ -#ifdef THREAD_SAFE - if (pthread_mutex_destroy(&xalloc_mutex) != 0) - _xalloc_fatal("XALLOC: Internal error: xalloc_shutdown(): xalloc_mutex %p cannot be destroyed\n", - xalloc_mutex); - if (pthread_mutex_destroy(&strerror_mutex) != 0) - _xalloc_fatal("XALLOC: Internal error: xalloc_shutdown(): strerror_mutex %p cannot be destroyed\n", - strerror_mutex); -#endif /* THREAD_SAFE */ - xalloc_initialized = 0; } @@ -442,7 +382,6 @@ xmalloc_c(size_t size, const char *file, unsigned int line) else mem->allocated_by = unknown_file; mem->allocated_in_line = line; - XALLOC_LOCK(xalloc_mutex); mem->id = ++xalloc_next_id; if ((mem_exists = RB_INSERT(memory_tree, &memory_tree_head, mem)) != NULL) { /* Freed pointer is being reused: */ @@ -457,7 +396,6 @@ xmalloc_c(size_t size, const char *file, unsigned int line) xalloc_total += size; if (xalloc_allocated > xalloc_peak) xalloc_peak = xalloc_allocated; - XALLOC_UNLOCK(xalloc_mutex); } #endif /* XALLOC_DEBUG */ @@ -499,7 +437,6 @@ xcalloc_c(size_t nmemb, size_t size, int may_fail, else mem->allocated_by = unknown_file; mem->allocated_in_line = line; - XALLOC_LOCK(xalloc_mutex); mem->id = ++xalloc_next_id; if ((mem_exists = RB_INSERT(memory_tree, &memory_tree_head, mem)) != NULL) { /* Freed pointer is being reused: */ @@ -514,7 +451,6 @@ xcalloc_c(size_t nmemb, size_t size, int may_fail, xalloc_total += nmemb * size; if (xalloc_allocated > xalloc_peak) xalloc_peak = xalloc_allocated; - XALLOC_UNLOCK(xalloc_mutex); } #endif /* XALLOC_DEBUG */ @@ -549,9 +485,7 @@ xrealloc_c(void *ptr, size_t nmemb, size_t size, if ((mem = real_calloc(1, sizeof(struct memory))) == NULL) _xalloc_error(errno, "XALLOC: Internal error"); mem->ptr = ret; - XALLOC_LOCK(xalloc_mutex); mem->id = ++xalloc_next_id; - XALLOC_UNLOCK(xalloc_mutex); if (file) mem->allocated_by = file; else @@ -563,7 +497,6 @@ xrealloc_c(void *ptr, size_t nmemb, size_t size, #ifdef XALLOC_DEBUG struct memory find_mem; - XALLOC_LOCK(xalloc_mutex); if (debug_level > 0) { find_mem.ptr = ptr; if ((mem = RB_FIND(memory_tree, &memory_tree_head, &find_mem)) == NULL) @@ -572,7 +505,6 @@ xrealloc_c(void *ptr, size_t nmemb, size_t size, line, ptr); RB_REMOVE(memory_tree, &memory_tree_head, mem); } - XALLOC_UNLOCK(xalloc_mutex); #endif /* XALLOC_DEBUG */ ret = real_realloc(ptr, nsiz); #ifdef XALLOC_DEBUG @@ -597,7 +529,6 @@ xrealloc_c(void *ptr, size_t nmemb, size_t size, struct memory *mem_exists; ssize_t diff = (ssize_t)(nsiz - mem->size); - XALLOC_LOCK(xalloc_mutex); xalloc_allocated += diff; if (diff < 0) xalloc_freed += -diff; @@ -615,7 +546,6 @@ xrealloc_c(void *ptr, size_t nmemb, size_t size, _memory_free(&mem_exists); RB_INSERT(memory_tree, &memory_tree_head, mem); } - XALLOC_UNLOCK(xalloc_mutex); } #endif /* XALLOC_DEBUG */ @@ -659,7 +589,6 @@ xfree_c(void **ptr_p, const char *file, unsigned int line) if (debug_level > 0) { struct memory *mem = NULL, find_mem; - XALLOC_LOCK(xalloc_mutex); find_mem.ptr = *ptr_p; if ((mem = RB_FIND(memory_tree, &memory_tree_head, &find_mem)) == NULL) _xalloc_fatal("XALLOC: xfree(): %s:%u: Junk pointer %p not accounted for\n", @@ -696,7 +625,6 @@ xfree_c(void **ptr_p, const char *file, unsigned int line) RB_REMOVE(memory_tree, &memory_tree_head, mem); _memory_free(&mem); } - XALLOC_UNLOCK(xalloc_mutex); } #endif /* XALLOC_DEBUG */ @@ -744,7 +672,6 @@ xasprintf_c(const char *file, unsigned int line, else mem->allocated_by = unknown_file; mem->allocated_in_line = line; - XALLOC_LOCK(xalloc_mutex); mem->id = ++xalloc_next_id; if ((mem_exists = RB_INSERT(memory_tree, &memory_tree_head, mem)) != NULL) { /* Freed pointer is being reused: */ @@ -759,7 +686,6 @@ xasprintf_c(const char *file, unsigned int line, xalloc_total += strsiz; if (xalloc_allocated > xalloc_peak) xalloc_peak = xalloc_allocated; - XALLOC_UNLOCK(xalloc_mutex); } # endif /* XALLOC_DEBUG */ diff --git a/src/xalloc.h b/src/xalloc.h index 3c814b4..3a96c83 100644 --- a/src/xalloc.h +++ b/src/xalloc.h @@ -37,8 +37,7 @@ * over time!) * * Define XALLOC_SILENT to suppress all messages, which makes libxalloc - * abort() and exit() silently. This has no effect when THREAD_DEBUG is - * defined. + * abort() and exit() silently. * * Define XALLOC_WITH_XASPRINTF to expose the xasprintf() interface. Doing * so will require libxalloc to be compiled with a compiler that supports C99 @@ -55,10 +54,6 @@ /* The default output stream for messages: */ #define XALLOC_DEFAULT_OUTPUT stderr -#if (defined(_REENTRANT) || defined(_POSIX_THREADS)) && !defined(THREAD_SAFE) -# define THREAD_SAFE 1 -#endif - /* * Library initialization and shutdown. diff --git a/win32/compat.h b/win32/compat.h index a16e408..427b49a 100644 --- a/win32/compat.h +++ b/win32/compat.h @@ -12,8 +12,11 @@ #define popen _popen #define pclose _pclose #define snprintf _snprintf -#define strncasecmp strnicmp #define stat _stat +#define strncasecmp strnicmp +#ifndef __GNUC__ +# define strtoll _strtoi64 +#endif /* !__GNUC__ */ #define sleep(a) Sleep((a) * 1000) @@ -25,11 +28,10 @@ #define S_IWOTH 0 #define S_IXGRP 0 #define S_IXOTH 0 -#define PATH_SEPARATOR '\\' -#define _PATH_DEVNULL "nul" +#define PATH_SEPARATORS "\\/" #ifndef ssize_t -# define ssize_t long +# define ssize_t long #endif /* !ssize_t */ #endif /* __WIN32_COMPAT_H__ */ \ No newline at end of file