From 739a08b5b939cd96e7ab3fd65c13b2674cb59a42 Mon Sep 17 00:00:00 2001 From: Moritz Grimm Date: Fri, 27 Feb 2015 19:39:01 +0100 Subject: [PATCH] Untangle compatibility code * update to latest strlc*(3) and strtonum(3) functions * isolate local_basename() --- Makefile.am | 2 +- build-aux/Makefile.am | 2 +- compat/Makefile.am | 2 +- compat/compat.h | 32 ++++ {src => compat}/getopt.c | 283 ++++++++++++++++++++++++----- {src => compat}/strlcat.c | 57 +++--- compat/strlcpy.c | 52 ++++++ {src => compat}/strtonum.c | 30 +-- compat/sys/Makefile.am | 2 +- configure.ac | 4 +- doc/Makefile.am | 2 +- examples/Makefile.am | 2 +- m4/Makefile.am | 2 +- src/Makefile.am | 7 +- src/configfile.c | 3 +- src/ezstream.c | 7 +- src/ezstream.h | 24 +-- src/{compat.c => local_basename.c} | 11 +- src/local_basename.h | 8 + src/metadata.c | 5 +- src/strfctns.h | 23 --- src/strlcpy.c | 58 ------ win32/Makefile.am | 2 +- win32/shout/Makefile.am | 2 +- 24 files changed, 401 insertions(+), 221 deletions(-) create mode 100644 compat/compat.h rename {src => compat}/getopt.c (57%) rename {src => compat}/strlcat.c (51%) create mode 100644 compat/strlcpy.c rename {src => compat}/strtonum.c (76%) rename src/{compat.c => local_basename.c} (90%) create mode 100644 src/local_basename.h delete mode 100644 src/strfctns.h delete mode 100644 src/strlcpy.c diff --git a/Makefile.am b/Makefile.am index d142328..911f707 100644 --- a/Makefile.am +++ b/Makefile.am @@ -1,4 +1,4 @@ -AUTOMAKE_OPTIONS = 1.10 foreign +AUTOMAKE_OPTIONS = 1.10 foreign subdir-objects ACLOCAL_AMFLAGS = -I m4 SUBDIRS = build-aux compat doc examples m4 src win32 diff --git a/build-aux/Makefile.am b/build-aux/Makefile.am index 42aa17c..8908a13 100644 --- a/build-aux/Makefile.am +++ b/build-aux/Makefile.am @@ -1,4 +1,4 @@ -AUTOMAKE_OPTIONS = 1.10 foreign +AUTOMAKE_OPTIONS = 1.10 foreign subdir-objects EXTRA_DIST = config.rpath diff --git a/compat/Makefile.am b/compat/Makefile.am index be69ad1..eb8159a 100644 --- a/compat/Makefile.am +++ b/compat/Makefile.am @@ -1,4 +1,4 @@ -AUTOMAKE_OPTIONS = 1.10 foreign +AUTOMAKE_OPTIONS = 1.10 foreign subdir-objects SUBDIRS = sys diff --git a/compat/compat.h b/compat/compat.h new file mode 100644 index 0000000..87ad851 --- /dev/null +++ b/compat/compat.h @@ -0,0 +1,32 @@ +#ifndef __COMPAT_H__ +#define __COMPAT_H__ + +#ifdef HAVE_CONFIG_H +# include "config.h" +#endif /* HAVE_CONFIG_H */ + +#include + +#ifndef HAVE_GETOPT +extern int opterr; +extern int optind; +extern int optopt; +extern int optreset; +extern char *optarg; + +int getopt(int, char * const *, const char *); +#endif /* !HAVE_GETOPT */ + +#ifndef HAVE_STRLCAT +size_t strlcat(char *, const char *, size_t); +#endif /* !HAVE_STRLCAT */ + +#ifndef HAVE_STRLCPY +size_t strlcpy(char *, const char *, size_t); +#endif /* !HAVE_STRLCPY */ + +#ifndef HAVE_STRTONUM +long long strtonum(const char *, long long, long long, const char **); +#endif /* !HAVE_STROTONUM */ + +#endif /* __COMPAT_H__ */ diff --git a/src/getopt.c b/compat/getopt.c similarity index 57% rename from src/getopt.c rename to compat/getopt.c index b90c1b7..bed97f9 100644 --- a/src/getopt.c +++ b/compat/getopt.c @@ -1,4 +1,4 @@ -/* $OpenBSD: getopt_long.c,v 1.23 2007/10/31 12:34:57 chl Exp $ */ +/* $OpenBSD: getopt_long.c,v 1.26 2013/06/08 22:47:56 millert Exp $ */ /* $NetBSD: getopt_long.c,v 1.15 2002/01/31 22:43:40 tv Exp $ */ /* @@ -49,21 +49,19 @@ * POSSIBILITY OF SUCH DAMAGE. */ -#ifdef HAVE_CONFIG_H -# include "config.h" -#endif /* HAVE_CONFIG_H */ +#include +#include +#include +#include +#include -#include "ezstream.h" +#include "compat.h" -int local_getopt(int, char * const *, const char *); - -#ifndef HAVE_GETOPT - -int opterr = 1; /* if error message should be printed */ -int optind = 1; /* index into parent argv vector */ -int optopt = '?'; /* character checked for validity */ -int optreset; /* reset getopt */ -char *optarg; /* argument associated with option */ +int opterr = 1; /* if error message should be printed */ +int optind = 1; /* index into parent argv vector */ +int optopt = '?'; /* character checked for validity */ +int optreset; /* reset getopt */ +char *optarg; /* argument associated with option */ #define PRINT_ERROR ((opterr) && (*options != ':')) @@ -72,13 +70,16 @@ char *optarg; /* argument associated with option */ #define FLAG_LONGONLY 0x04 /* operate as getopt_long_only */ /* return values */ -#define BADCH (int)'?' -#define BADARG ((*options == ':') ? (int)':' : (int)'?') -#define INORDER (int)1 +#define BADCH (int)'?' +#define BADARG ((*options == ':') ? (int)':' : (int)'?') +#define INORDER (int)1 -#define EMSG "" +#define EMSG "" -static int getopt_internal(int, char * const *, const char *, int); +static int getopt_internal(int, char * const *, const char *, + const struct option *, int *, int); +static int parse_long_options(char * const *, const char *, + const struct option *, int *, int, int); static int gcd(int, int); static void permute_args(int, int, int, char * const *); @@ -89,8 +90,12 @@ static int nonopt_start = -1; /* first non option argument (for permute) */ static int nonopt_end = -1; /* first option after non options (for permute) */ /* Error messages */ -static const char recargchar[] = "option requires an argument -- %c\n"; -static const char illoptchar[] = "unknown option -- %c\n"; +static const char recargchar[] = "option requires an argument -- %c"; +static const char recargstring[] = "option requires an argument -- %s"; +static const char ambig[] = "ambiguous option -- %.*s"; +static const char noarg[] = "option doesn't take an argument -- %.*s"; +static const char illoptchar[] = "unknown option -- %c"; +static const char illoptstring[] = "unknown option -- %s"; /* * Compute the greatest common divisor of a and b. @@ -147,33 +152,147 @@ permute_args(int panonopt_start, int panonopt_end, int opt_end, } } +/* + * parse_long_options -- + * Parse long options in argc/argv argument vector. + * Returns -1 if short_too is set and the option does not match long_options. + */ +static int +parse_long_options(char * const *nargv, const char *options, + const struct option *long_options, int *idx, int short_too, int flags) +{ + char *current_argv, *has_equal; + size_t current_argv_len; + int i, match, exact_match, second_partial_match; + + current_argv = place; + match = -1; + exact_match = 0; + second_partial_match = 0; + + optind++; + + if ((has_equal = strchr(current_argv, '=')) != NULL) { + /* argument found (--option=arg) */ + current_argv_len = has_equal - current_argv; + has_equal++; + } else + current_argv_len = strlen(current_argv); + + for (i = 0; long_options[i].name; i++) { + /* find matching long option */ + if (strncmp(current_argv, long_options[i].name, + current_argv_len)) + continue; + + if (strlen(long_options[i].name) == current_argv_len) { + /* exact match */ + match = i; + exact_match = 1; + break; + } + /* + * If this is a known short option, don't allow + * a partial match of a single character. + */ + if (short_too && current_argv_len == 1) + continue; + + if (match == -1) /* first partial match */ + match = i; + else if ((flags & FLAG_LONGONLY) || + long_options[i].has_arg != long_options[match].has_arg || + long_options[i].flag != long_options[match].flag || + long_options[i].val != long_options[match].val) + second_partial_match = 1; + } + if (!exact_match && second_partial_match) { + /* ambiguous abbreviation */ + if (PRINT_ERROR) + warnx(ambig, (int)current_argv_len, current_argv); + optopt = 0; + return (BADCH); + } + if (match != -1) { /* option found */ + if (long_options[match].has_arg == no_argument + && has_equal) { + if (PRINT_ERROR) + warnx(noarg, (int)current_argv_len, + current_argv); + /* + * XXX: GNU sets optopt to val regardless of flag + */ + if (long_options[match].flag == NULL) + optopt = long_options[match].val; + else + optopt = 0; + return (BADARG); + } + if (long_options[match].has_arg == required_argument || + long_options[match].has_arg == optional_argument) { + if (has_equal) + optarg = has_equal; + else if (long_options[match].has_arg == + required_argument) { + /* + * optional argument doesn't use next nargv + */ + optarg = nargv[optind++]; + } + } + if ((long_options[match].has_arg == required_argument) + && (optarg == NULL)) { + /* + * Missing argument; leading ':' indicates no error + * should be generated. + */ + if (PRINT_ERROR) + warnx(recargstring, + current_argv); + /* + * XXX: GNU sets optopt to val regardless of flag + */ + if (long_options[match].flag == NULL) + optopt = long_options[match].val; + else + optopt = 0; + --optind; + return (BADARG); + } + } else { /* unknown option */ + if (short_too) { + --optind; + return (-1); + } + if (PRINT_ERROR) + warnx(illoptstring, current_argv); + optopt = 0; + return (BADCH); + } + if (idx) + *idx = match; + if (long_options[match].flag) { + *long_options[match].flag = long_options[match].val; + return (0); + } else + return (long_options[match].val); +} + /* * getopt_internal -- * Parse argc/argv argument vector. Called by user level routines. */ static int -getopt_internal(int nargc, char * const *nargv, const char *options, int flags) +getopt_internal(int nargc, char * const *nargv, const char *options, + const struct option *long_options, int *idx, int flags) { char *oli; /* option letter list index */ - int optchar; + int optchar, short_too; static int posixly_correct = -1; if (options == NULL) return (-1); - /* - * Disable GNU extensions if POSIXLY_CORRECT is set or options - * string begins with a '+'. - */ - if (posixly_correct == -1) - posixly_correct = (getenv("POSIXLY_CORRECT") != NULL); - if (posixly_correct || *options == '+') - flags &= ~FLAG_PERMUTE; - else if (*options == '-') - flags |= FLAG_ALLARGS; - if (*options == '+' || *options == '-') - options++; - /* * XXX Some GNU programs (like cvs) set optind to 0 instead of * XXX using optreset. Work around this braindamage. @@ -181,6 +300,19 @@ getopt_internal(int nargc, char * const *nargv, const char *options, int flags) if (optind == 0) optind = optreset = 1; + /* + * Disable GNU extensions if POSIXLY_CORRECT is set or options + * string begins with a '+'. + */ + if (posixly_correct == -1 || optreset) + posixly_correct = (getenv("POSIXLY_CORRECT") != NULL); + if (*options == '-') + flags |= FLAG_ALLARGS; + else if (posixly_correct || *options == '+') + flags &= ~FLAG_PERMUTE; + if (*options == '+' || *options == '-') + options++; + optarg = NULL; if (optreset) nonopt_start = nonopt_end = -1; @@ -260,6 +392,28 @@ start: } } + /* + * Check long options if: + * 1) we were passed some + * 2) the arg is not just "-" + * 3) either the arg starts with -- we are getopt_long_only() + */ + if (long_options != NULL && place != nargv[optind] && + (*place == '-' || (flags & FLAG_LONGONLY))) { + short_too = 0; + if (*place == '-') + place++; /* --foo long option */ + else if (*place != ':' && strchr(options, *place) != NULL) + short_too = 1; /* could be short option too */ + + optchar = parse_long_options(nargv, options, long_options, + idx, short_too, flags); + if (optchar != -1) { + place = EMSG; + return (optchar); + } + } + if ((optchar = (int)*place++) == (int)':' || (optchar == (int)'-' && *place != '\0') || (oli = strchr(options, optchar)) == NULL) { @@ -273,10 +427,27 @@ start: if (!*place) ++optind; if (PRINT_ERROR) - fprintf(stderr, illoptchar, optchar); + warnx(illoptchar, optchar); optopt = optchar; return (BADCH); } + if (long_options != NULL && optchar == 'W' && oli[1] == ';') { + /* -W long-option */ + if (*place) /* no space */ + /* NOTHING */; + else if (++optind >= nargc) { /* no arg */ + place = EMSG; + if (PRINT_ERROR) + warnx(recargchar, optchar); + optopt = optchar; + return (BADARG); + } else /* white space */ + place = nargv[optind]; + optchar = parse_long_options(nargv, options, long_options, + idx, 0, flags); + place = EMSG; + return (optchar); + } if (*++oli != ':') { /* doesn't take argument */ if (!*place) ++optind; @@ -288,7 +459,7 @@ start: if (++optind >= nargc) { /* no arg */ place = EMSG; if (PRINT_ERROR) - fprintf(stderr, recargchar, optchar); + warnx(recargchar, optchar); optopt = optchar; return (BADARG); } else @@ -301,8 +472,6 @@ start: return (optchar); } -#endif /* !HAVE_GETOPT */ - /* * getopt -- * Parse argc/argv argument vector. @@ -310,11 +479,8 @@ start: * [eventually this will replace the BSD getopt] */ int -local_getopt(int nargc, char * const *nargv, const char *options) +getopt(int nargc, char * const *nargv, const char *options) { -#ifdef HAVE_GETOPT - return (getopt(nargc, nargv, options)); -#else /* HAVE_GETOPT */ /* * We don't pass FLAG_PERMUTE to getopt_internal() since @@ -324,6 +490,31 @@ local_getopt(int nargc, char * const *nargv, const char *options) * before dropping privileges it makes sense to keep things * as simple (and bug-free) as possible. */ - return (getopt_internal(nargc, nargv, options, 0)); -#endif /* HAVE_GETOPT */ + return (getopt_internal(nargc, nargv, options, NULL, NULL, 0)); +} + +/* + * getopt_long -- + * Parse argc/argv argument vector. + */ +int +getopt_long(int nargc, char * const *nargv, const char *options, + const struct option *long_options, int *idx) +{ + + return (getopt_internal(nargc, nargv, options, long_options, idx, + FLAG_PERMUTE)); +} + +/* + * getopt_long_only -- + * Parse argc/argv argument vector. + */ +int +getopt_long_only(int nargc, char * const *nargv, const char *options, + const struct option *long_options, int *idx) +{ + + return (getopt_internal(nargc, nargv, options, long_options, idx, + FLAG_PERMUTE|FLAG_LONGONLY)); } diff --git a/src/strlcat.c b/compat/strlcat.c similarity index 51% rename from src/strlcat.c rename to compat/strlcat.c index 3307fb1..4d01766 100644 --- a/src/strlcat.c +++ b/compat/strlcat.c @@ -1,7 +1,7 @@ -/* $OpenBSD: strlcat.c,v 1.13 2005/08/08 08:05:37 espie Exp $ */ +/* $OpenBSD: strlcat.c,v 1.14 2015/01/15 03:54:12 millert Exp $ */ /* - * Copyright (c) 1998 Todd C. Miller + * Copyright (c) 1998, 2015 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 @@ -16,47 +16,42 @@ * 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 "strfctns.h" + +#include "compat.h" /* - * Appends src to string dst of size siz (unlike strncat, siz is the - * full size of dst, not space left). At most siz-1 characters - * will be copied. Always NUL terminates (unless siz <= strlen(dst)). - * Returns strlen(src) + MIN(siz, strlen(initial dst)). + * Appends src to string dst of size dsize (unlike strncat, dsize is the + * full size of dst, not space left). At most dsize-1 characters + * will be copied. Always NUL terminates (unless dsize <= strlen(dst)). + * Returns strlen(src) + MIN(dsize, strlen(initial dst)). * If retval >= siz, truncation occurred. */ size_t -local_strlcat(char *dst, const char *src, size_t siz) +strlcat(char *dst, const char *src, size_t dsize) { - char *d = dst; - const char *s = src; - size_t n = siz; + const char *odst = dst; + const char *osrc = src; + size_t n = dsize; size_t dlen; - /* Find the end of dst and adjust bytes left but don't go past end */ - while (n-- != 0 && *d != '\0') - d++; - dlen = d - dst; - n = siz - dlen; + /* Find the end of dst and adjust bytes left but don't go past end. */ + while (n-- != 0 && *dst != '\0') + dst++; + dlen = dst - odst; + n = dsize - dlen; - if (n == 0) - return(dlen + strlen(s)); - while (*s != '\0') { - if (n != 1) { - *d++ = *s; + if (n-- == 0) + return(dlen + strlen(src)); + while (*src != '\0') { + if (n != 0) { + *dst++ = *src; n--; } - s++; + src++; } - *d = '\0'; + *dst = '\0'; - return(dlen + (s - src)); /* count does not include NUL */ + return(dlen + (src - osrc)); /* count does not include NUL */ } diff --git a/compat/strlcpy.c b/compat/strlcpy.c new file mode 100644 index 0000000..ab420b6 --- /dev/null +++ b/compat/strlcpy.c @@ -0,0 +1,52 @@ +/* $OpenBSD: strlcpy.c,v 1.12 2015/01/15 03:54:12 millert Exp $ */ + +/* + * Copyright (c) 1998, 2015 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. + */ + +#include +#include + +#include "compat.h" + +/* + * Copy string src to buffer dst of size dsize. At most dsize-1 + * chars will be copied. Always NUL terminates (unless dsize == 0). + * Returns strlen(src); if retval >= dsize, truncation occurred. + */ +size_t +strlcpy(char *dst, const char *src, size_t dsize) +{ + const char *osrc = src; + size_t nleft = dsize; + + /* Copy as many bytes as will fit. */ + if (nleft != 0) { + while (--nleft != 0) { + if ((*dst++ = *src++) == '\0') + break; + } + } + + /* Not enough room in dst, add NUL and traverse rest of src. */ + if (nleft == 0) { + if (dsize != 0) + *dst = '\0'; /* NUL-terminate dst */ + while (*src++) + ; + } + + return(src - osrc - 1); /* count does not include NUL */ +} diff --git a/src/strtonum.c b/compat/strtonum.c similarity index 76% rename from src/strtonum.c rename to compat/strtonum.c index bf17979..fa03205 100644 --- a/src/strtonum.c +++ b/compat/strtonum.c @@ -1,4 +1,4 @@ -/* $OpenBSD: strtonum.c,v 1.6 2004/08/03 19:38:01 millert Exp $ */ +/* $OpenBSD: strtonum.c,v 1.7 2013/04/17 18:40:58 tedu Exp $ */ /* * Copyright (c) 2004 Ted Unangst and Todd Miller @@ -17,35 +17,23 @@ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ -#ifdef HAVE_CONFIG_H -# include "config.h" -#endif - -#include "ezstream.h" - #include #include #include -#include "strfctns.h" -#define INVALID 1 -#define TOOSMALL 2 -#define TOOLARGE 3 +#include "compat.h" -#if !defined(LLONG_MIN) || !defined(LLONG_MAX) -# undef LLONG_MIN -# undef LLONG_MAX -# define LLONG_MIN LONG_LONG_MIN -# define LLONG_MAX LONG_LONG_MAX -#endif +#define INVALID 1 +#define TOOSMALL 2 +#define TOOLARGE 3 long long -local_strtonum(const char *numstr, long long minval, long long maxval, +strtonum(const char *numstr, long long minval, long long maxval, const char **errstrp) { long long ll = 0; - char *ep; int error = 0; + char *ep; struct errval { const char *errstr; int err; @@ -58,9 +46,9 @@ local_strtonum(const char *numstr, long long minval, long long maxval, ev[0].err = errno; errno = 0; - if (minval > maxval) + if (minval > maxval) { error = INVALID; - else { + } else { ll = strtoll(numstr, &ep, 10); if (numstr == ep || *ep != '\0') error = INVALID; diff --git a/compat/sys/Makefile.am b/compat/sys/Makefile.am index 4bed8ed..9cafd1e 100644 --- a/compat/sys/Makefile.am +++ b/compat/sys/Makefile.am @@ -1,4 +1,4 @@ -AUTOMAKE_OPTIONS = 1.10 foreign +AUTOMAKE_OPTIONS = 1.10 foreign subdir-objects EXTRA_DIST = tree.h tree.3 diff --git a/configure.ac b/configure.ac index 4a28fed..1eff7c8 100644 --- a/configure.ac +++ b/configure.ac @@ -9,7 +9,7 @@ AC_CONFIG_SRCDIR([src/ezstream.c]) AC_CONFIG_AUX_DIR([build-aux]) AM_INIT_AUTOMAKE AC_CONFIG_HEADERS([src/config.h]) -AC_CONFIG_LIBOBJ_DIR([src]) +AC_CONFIG_LIBOBJ_DIR([compat]) AM_MAINTAINER_MODE AC_USE_SYSTEM_EXTENSIONS AC_PROG_CC_STDC @@ -211,7 +211,6 @@ dnl ####################### AC_CHECK_FUNCS([ \ arc4random \ basename \ - getopt \ gettimeofday \ nl_langinfo \ pclose \ @@ -232,6 +231,7 @@ AC_CHECK_FUNCS([ \ ]) AC_REPLACE_FUNCS([ \ + getopt \ strlcat \ strlcpy \ strtonum \ diff --git a/doc/Makefile.am b/doc/Makefile.am index c1f590a..266d43c 100644 --- a/doc/Makefile.am +++ b/doc/Makefile.am @@ -1,4 +1,4 @@ -AUTOMAKE_OPTIONS = 1.10 foreign +AUTOMAKE_OPTIONS = 1.10 foreign subdir-objects man_MANS = ezstream.1 ezstream-file.sh.1 diff --git a/examples/Makefile.am b/examples/Makefile.am index f733deb..4cc6809 100644 --- a/examples/Makefile.am +++ b/examples/Makefile.am @@ -1,4 +1,4 @@ -AUTOMAKE_OPTIONS = 1.10 foreign +AUTOMAKE_OPTIONS = 1.10 foreign subdir-objects examplesdir = @EXAMPLES_DIR@ dist_examples_DATA = \ diff --git a/m4/Makefile.am b/m4/Makefile.am index fe26dee..d8d03a6 100644 --- a/m4/Makefile.am +++ b/m4/Makefile.am @@ -1,4 +1,4 @@ -AUTOMAKE_OPTIONS = 1.10 foreign +AUTOMAKE_OPTIONS = 1.10 foreign subdir-objects CLEANFILES = core *.core *~ .*~ diff --git a/src/Makefile.am b/src/Makefile.am index 47cfe06..54daecf 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -1,13 +1,12 @@ -AUTOMAKE_OPTIONS = 1.10 foreign +AUTOMAKE_OPTIONS = 1.10 foreign subdir-objects bin_PROGRAMS = ezstream bin_SCRIPTS = ezstream-file.sh ezstream_SOURCES = \ - compat.c \ configfile.c \ ezstream.c \ - getopt.c \ + local_basename.c \ metadata.c \ playlist.c \ util.c \ @@ -21,9 +20,9 @@ AM_LDFLAGS = @EZ_LDFLAGS@ EXTRA_DIST = \ configfile.h \ ezstream.h \ + local_basename.h \ metadata.h \ playlist.h \ - strfctns.h \ util.h \ xalloc.h diff --git a/src/configfile.c b/src/configfile.c index 8bafdcc..75d4a9a 100644 --- a/src/configfile.c +++ b/src/configfile.c @@ -23,8 +23,9 @@ #include "ezstream.h" +#include "compat.h" + #include "configfile.h" -#include "strfctns.h" #include "util.h" #include "xalloc.h" diff --git a/src/ezstream.c b/src/ezstream.c index 329dbc8..969ee96 100644 --- a/src/ezstream.c +++ b/src/ezstream.c @@ -29,13 +29,16 @@ #include +#include "compat.h" + #include "configfile.h" #include "metadata.h" #include "playlist.h" -#include "strfctns.h" #include "util.h" #include "xalloc.h" +#include "local_basename.h" + #define STREAM_DONE 0 #define STREAM_CONT 1 #define STREAM_SKIP 2 @@ -1149,7 +1152,7 @@ main(int argc, char *argv[]) qFlag = 0; vFlag = 0; - while ((c = local_getopt(argc, argv, "c:hmnqsVv")) != -1) { + while ((c = getopt(argc, argv, "c:hmnqsVv")) != -1) { switch (c) { case 'c': if (configFile != NULL) { diff --git a/src/ezstream.h b/src/ezstream.h index 47b9693..3e6d192 100644 --- a/src/ezstream.h +++ b/src/ezstream.h @@ -14,8 +14,8 @@ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ -#ifndef __COMPAT_H__ -#define __COMPAT_H__ +#ifndef __EZSTREAM_H__ +#define __EZSTREAM_H__ #ifdef HAVE_CONFIG_H # include "config.h" @@ -98,22 +98,4 @@ # define sleep(a) Sleep((a) * 1000) #endif /* WIN32 */ -/* - * For compat.c and getopt.c: - */ - -extern int opterr; -extern int optind; -extern int optopt; -extern int optreset; -extern char *optarg; - -extern int - local_getopt(int, char * const *, const char *); - -extern const char *path_separators; - -extern char * - local_basename(const char *); - -#endif /* __COMPAT_H__ */ +#endif /* __EZSTREAM_H__ */ diff --git a/src/compat.c b/src/local_basename.c similarity index 90% rename from src/compat.c rename to src/local_basename.c index a26abc5..e27c82e 100644 --- a/src/compat.c +++ b/src/local_basename.c @@ -2,11 +2,18 @@ # include "config.h" #endif -#include "ezstream.h" +#ifdef HAVE_SYS_TYPES_H +# include +#endif /* HAVE_SYS_TYPES_H */ +#include #if defined(HAVE_LIBGEN_H) && !defined(__linux__) # include #endif /* HAVE_LIBGEN_H && !__linux__ */ +#include +#include + +#include "local_basename.h" #ifndef PATH_SEPARATORS # define PATH_SEPARATORS "/" @@ -34,7 +41,7 @@ is_separator(int c) /* * 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 $ + * $OpenBSD: src/lib/libc/gen/basename.c,v 1.15 2013/09/30 12:02:32 millert Exp $ */ /* * Copyright (c) 1997, 2004 Todd C. Miller diff --git a/src/local_basename.h b/src/local_basename.h new file mode 100644 index 0000000..3f0dd94 --- /dev/null +++ b/src/local_basename.h @@ -0,0 +1,8 @@ +#ifndef __LOCAL_BASENAME_H__ +#define __LOCAL_BASENAME_H__ + +extern const char *path_separators; + +char * local_basename(const char *); + +#endif /* __LOCAL_BASENAME_H__ */ diff --git a/src/metadata.c b/src/metadata.c index 2623607..f4a3ec7 100644 --- a/src/metadata.c +++ b/src/metadata.c @@ -20,6 +20,8 @@ #include "ezstream.h" +#include "compat.h" + #ifdef HAVE_TAGLIB # include #endif /* HAVE_TAGLIB */ @@ -29,10 +31,11 @@ #include #include "metadata.h" -#include "strfctns.h" #include "util.h" #include "xalloc.h" +#include "local_basename.h" + /* Usually defined in . */ #ifndef S_IEXEC # define S_IEXEC S_IXUSR diff --git a/src/strfctns.h b/src/strfctns.h deleted file mode 100644 index 83316fa..0000000 --- a/src/strfctns.h +++ /dev/null @@ -1,23 +0,0 @@ -#ifndef __STRLFCTNS_H__ -#define __STRLFCTNS_H__ - -#ifdef HAVE_CONFIG_H -# include "config.h" -#endif - -#ifndef HAVE_STRLCAT -# define strlcat local_strlcat -#endif -size_t local_strlcat(char *, const char *, size_t); - -#ifndef HAVE_STRLCPY -# define strlcpy local_strlcpy -#endif -size_t local_strlcpy(char *, const char *, size_t); - -#ifndef HAVE_STRTONUM -# define strtonum local_strtonum -#endif -long long local_strtonum(const char *, long long, long long, const char **); - -#endif /* __STRLFCTNS_H__ */ diff --git a/src/strlcpy.c b/src/strlcpy.c deleted file mode 100644 index e7b807b..0000000 --- a/src/strlcpy.c +++ /dev/null @@ -1,58 +0,0 @@ -/* $OpenBSD: strlcpy.c,v 1.11 2006/05/05 15:27:38 millert Exp $ */ - -/* - * Copyright (c) 1998 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. - */ - -#ifdef HAVE_CONFIG_H -# include "config.h" -#endif - -#ifdef HAVE_SYS_TYPES_H -# include -#endif -#include -#include "strfctns.h" - -/* - * Copy src to string dst of size siz. At most siz-1 characters - * will be copied. Always NUL terminates (unless siz == 0). - * Returns strlen(src); if retval >= siz, truncation occurred. - */ -size_t -local_strlcpy(char *dst, const char *src, size_t siz) -{ - char *d = dst; - const char *s = src; - size_t n = siz; - - /* Copy as many bytes as will fit */ - if (n != 0) { - while (--n != 0) { - if ((*d++ = *s++) == '\0') - break; - } - } - - /* Not enough room in dst, add NUL and traverse rest of src */ - if (n == 0) { - if (siz != 0) - *d = '\0'; /* NUL-terminate dst */ - while (*s++) - ; - } - - return(s - src - 1); /* count does not include NUL */ -} diff --git a/win32/Makefile.am b/win32/Makefile.am index 46e87c5..35f6653 100644 --- a/win32/Makefile.am +++ b/win32/Makefile.am @@ -1,4 +1,4 @@ -AUTOMAKE_OPTIONS = 1.10 foreign +AUTOMAKE_OPTIONS = 1.10 foreign subdir-objects SUBDIRS = shout diff --git a/win32/shout/Makefile.am b/win32/shout/Makefile.am index 70c10b2..28108ec 100644 --- a/win32/shout/Makefile.am +++ b/win32/shout/Makefile.am @@ -1,4 +1,4 @@ -AUTOMAKE_OPTIONS = 1.10 foreign +AUTOMAKE_OPTIONS = 1.10 foreign subdir-objects EXTRA_DIST = shout.h