Get rid of the getopt reimplementation and depend on libcaca instead.

This commit is contained in:
Sam Hocevar
2010-10-07 22:52:54 +00:00
committed by sam
parent 4c504ac53e
commit ca2d1ada85
9 changed files with 97 additions and 271 deletions

View File

@@ -11,6 +11,18 @@ AM_PROG_CC_C_O
AC_PROG_CPP
AC_PROG_LIBTOOL
dnl AC_PROG_EGREP only exists in autoconf 2.54+, so we use AC_EGREP_CPP right
dnl now otherwise it might be set in an obscure if statement. Same thing for
dnl PKG_PROG_PKG_CONFIG which needs to be called first.
AC_EGREP_CPP(yes, foo)
PKG_PROG_PKG_CONFIG()
dnl Don't let pkg-config fuck our cross-compilation environment
m4_pattern_allow([^PKG_CONFIG_LIBDIR$])
if test "$build" != "$host" -a "${PKG_CONFIG_LIBDIR}" = ""; then
export PKG_CONFIG_LIBDIR=/dev/null
fi
AC_C_INLINE
AC_C_FUNC
@@ -23,6 +35,11 @@ esac
AC_MSG_RESULT($SONAME)
AC_DEFINE_UNQUOTED(SONAME, "$SONAME", [Define to the libzzuf full name])
PKG_CHECK_MODULES(CACA, caca >= 0.99.beta18,
[:],
[AC_MSG_RESULT(no)
AC_MSG_ERROR([you need libcaca version 0.99.beta18 or later])])
# Optimizations
AC_TRY_CFLAGS(-g -O2, CFLAGS="${CFLAGS} -g -O2")
# Code qui fait des warnings == code de porc == deux baffes dans ta gueule
@@ -45,7 +62,7 @@ esac
AC_SUBST(WINSOCK2_LIBS)
AC_SUBST(DLL_LDFLAGS)
AC_CHECK_HEADERS(windows.h winsock2.h io.h process.h unistd.h inttypes.h stdint.h getopt.h libc.h malloc.h dlfcn.h regex.h sys/cdefs.h sys/socket.h netinet/in.h arpa/inet.h sys/uio.h aio.h sys/mman.h sys/wait.h sys/resource.h sys/time.h endian.h mach/task.h)
AC_CHECK_HEADERS(windows.h winsock2.h io.h process.h unistd.h inttypes.h stdint.h libc.h malloc.h dlfcn.h regex.h sys/cdefs.h sys/socket.h netinet/in.h arpa/inet.h sys/uio.h aio.h sys/mman.h sys/wait.h sys/resource.h sys/time.h endian.h mach/task.h)
AC_CHECK_FUNCS(setenv waitpid setrlimit gettimeofday fork kill pipe _pipe)
AC_CHECK_FUNCS(dup dup2 ftello fseeko _IO_getc getline getdelim fgetln map_fd memalign posix_memalign aio_read accept bind connect socket readv pread recv recvfrom recvmsg mmap valloc sigaction getpagesize)
@@ -161,18 +178,6 @@ AC_TRY_COMPILE([],
[AC_MSG_RESULT(no)])
AC_DEFINE_UNQUOTED(ATTRIBUTE_PRINTF(x,y), $ac_v_attribute_printf, [Define to the __printf__ attribute if present])
ac_cv_have_getopt_long="no"
AC_CHECK_FUNCS(getopt_long,
[ac_cv_have_getopt_long="yes"],
[AC_CHECK_LIB(gnugetopt, getopt_long,
[ac_cv_have_getopt_long="yes"
GETOPT_LIBS="${GETOPT_LIBS} -lgnugetopt"])])
if test "$ac_cv_have_getopt_long" != "no"; then
AC_DEFINE(HAVE_GETOPT_LONG, 1, Define to 1 if you have the `getopt_long' function.)
fi
AM_CONDITIONAL(NEED_GETOPT_LONG, test "$ac_cv_have_getopt_long" = "no")
AC_SUBST(GETOPT_LIBS)
AC_CHECK_LIB(m, log, [MATH_LIBS="-lm"])
AC_SUBST(MATH_LIBS)
AC_CHECK_LIB(dl, dlopen, [DL_LIBS="-ldl"])

View File

@@ -49,8 +49,6 @@
/* #undef HAVE_GETC_UNLOCKED */
/* #undef HAVE_GETDELIM */
/* #undef HAVE_GETLINE */
/* #undef HAVE_GETOPT_H */
/* #undef HAVE_GETOPT_LONG */
#define HAVE_GETPAGESIZE 1
/* #undef HAVE_GETTIMEOFDAY */
/* #undef HAVE_GLIBC_FILE */

View File

@@ -93,17 +93,15 @@
</Link>
</ItemDefinitionGroup>
<ItemGroup>
<ClCompile Include="..\src\mygetopt.c" />
<ClCompile Include="..\src\zzat.c">
<ObjectFileName Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">$(IntDir)%(FileName)1.obj</ObjectFileName>
<ObjectFileName Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">$(IntDir)%(FileName)1.obj</ObjectFileName>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="..\src\mygetopt.h" />
<ClInclude Include="config.h" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
</ImportGroup>
</Project>
</Project>

View File

@@ -100,7 +100,6 @@
<ClInclude Include="..\src\common\ranges.h" />
<ClInclude Include="..\src\md5.h" />
<ClInclude Include="..\src\myfork.h" />
<ClInclude Include="..\src\mygetopt.h" />
<ClInclude Include="..\src\opts.h" />
<ClInclude Include="..\src\timer.h" />
<ClInclude Include="config.h" />
@@ -112,7 +111,6 @@
<ClCompile Include="..\src\common\ranges.c" />
<ClCompile Include="..\src\md5.c" />
<ClCompile Include="..\src\myfork.c" />
<ClCompile Include="..\src\mygetopt.c" />
<ClCompile Include="..\src\opts.c" />
<ClCompile Include="..\src\timer.c" />
<ClCompile Include="..\src\zzuf.c" />
@@ -120,4 +118,4 @@
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
</ImportGroup>
</Project>
</Project>

View File

@@ -23,21 +23,18 @@ COMMON = \
common/fd.c common/fd.h \
common/fuzz.c common/fuzz.h
if NEED_GETOPT_LONG
GETOPT = mygetopt.c mygetopt.h
endif
zzuf_SOURCES = $(ZZUF) $(COMMON) $(GETOPT)
zzuf_SOURCES = $(ZZUF) $(COMMON)
zzuf_CFLAGS = -DLIBDIR=\"$(libdir)/zzuf\" -I$(srcdir)/common
zzuf_LDFLAGS = $(MATH_LIBS) $(WINSOCK2_LIBS)
zzuf_LDFLAGS = $(CACA_LIBS) $(MATH_LIBS) $(WINSOCK2_LIBS)
zzuf_DEPENDENCIES = libzzuf.la
zzat_SOURCES = $(ZZAT) $(GETOPT)
zzat_SOURCES = $(ZZAT)
zzat_LDFLAGS = $(CACA_LIBS)
libzzuf_la_SOURCES = $(LIBZZUF) $(COMMON)
libzzuf_la_CFLAGS = -DLIBZZUF -I$(srcdir)/libzzuf -I$(srcdir)/common
libzzuf_la_LDFLAGS = -avoid-version -no-undefined $(DLL_LDFLAGS)
libzzuf_la_LIBADD = $(GETOPT_LIBS) $(DL_LIBS) $(MATH_LIBS) $(WINSOCK2_LIBS)
libzzuf_la_LIBADD = $(DL_LIBS) $(MATH_LIBS) $(WINSOCK2_LIBS)
echo-sources: ; echo $(SOURCES)

View File

@@ -1,120 +0,0 @@
/*
* zzuf - general purpose fuzzer
* Copyright (c) 2002-2010 Sam Hocevar <sam@hocevar.net>
* All Rights Reserved
*
* This program is free software. It comes without any warranty, to
* the extent permitted by applicable law. You can redistribute it
* and/or modify it under the terms of the Do What The Fuck You Want
* To Public License, Version 2, as published by Sam Hocevar. See
* http://sam.zoy.org/wtfpl/COPYING for more details.
*/
/*
* mygetopt.c: getopt_long reimplementation
*/
#include "config.h"
#if defined HAVE_STDINT_H
# include <stdint.h>
#elif defined HAVE_INTTYPES_H
# include <inttypes.h>
#endif
#include <stdio.h>
#include <string.h>
#include "mygetopt.h"
int myoptind = 1;
char *myoptarg = NULL;
/* XXX: this getopt_long implementation should not be trusted for other
* applications without any serious peer reviewing. It “just works” with
* zzuf but may fail miserably in other programs. */
int mygetopt(int argc, char * const _argv[], const char *optstring,
const struct myoption *longopts, int *longindex)
{
char **argv = (char **)(uintptr_t)_argv;
char *flag;
int i;
if(myoptind >= argc)
return -1;
flag = argv[myoptind];
if(flag[0] == '-' && flag[1] != '-')
{
char *tmp;
int ret = flag[1];
if(ret == '\0')
return -1;
tmp = strchr(optstring, ret);
if(!tmp || ret == ':')
return '?';
myoptind++;
if(tmp[1] == ':')
{
if(flag[2] != '\0')
myoptarg = flag + 2;
else
myoptarg = argv[myoptind++];
return ret;
}
if(flag[2] != '\0')
{
flag[1] = '-';
myoptind--;
argv[myoptind]++;
}
return ret;
}
if(flag[0] == '-' && flag[1] == '-')
{
if(flag[2] == '\0')
return -1;
for(i = 0; longopts[i].name; i++)
{
size_t l = strlen(longopts[i].name);
if(strncmp(flag + 2, longopts[i].name, l))
continue;
switch(flag[2 + l])
{
case '=':
if(!longopts[i].has_arg)
goto bad_opt;
if(longindex)
*longindex = i;
myoptind++;
myoptarg = flag + 2 + l + 1;
return longopts[i].val;
case '\0':
if(longindex)
*longindex = i;
myoptind++;
if(longopts[i].has_arg)
myoptarg = argv[myoptind++];
return longopts[i].val;
default:
break;
}
}
bad_opt:
fprintf(stderr, "%s: unrecognized option `%s'\n", argv[0], flag);
return '?';
}
return -1;
}

View File

@@ -1,29 +0,0 @@
/*
* zzuf - general purpose fuzzer
* Copyright (c) 2002-2010 Sam Hocevar <sam@hocevar.net>
* All Rights Reserved
*
* This program is free software. It comes without any warranty, to
* the extent permitted by applicable law. You can redistribute it
* and/or modify it under the terms of the Do What The Fuck You Want
* To Public License, Version 2, as published by Sam Hocevar. See
* http://sam.zoy.org/wtfpl/COPYING for more details.
*/
/*
* mygetopt.h: getopt_long reimplementation
*/
struct myoption
{
const char *name;
int has_arg;
int *flag;
int val;
};
extern int myoptind;
extern char *myoptarg;
int mygetopt(int, char * const[], const char *, const struct myoption *, int *);

View File

@@ -50,18 +50,7 @@ typedef int ssize_t;
#include <stdio.h>
#include <string.h>
#if !defined HAVE_GETOPT_LONG
# include "mygetopt.h"
#elif defined HAVE_GETOPT_H
# include <getopt.h>
#endif
#if defined HAVE_GETOPT_LONG
# define mygetopt getopt_long
# define myoptind optind
# define myoptarg optarg
# define myoption option
#endif
#include <caca.h>
static int run(char const *sequence, char const *file);
static void output(char const *buf, size_t len);
@@ -99,7 +88,7 @@ int main(int argc, char *argv[])
#define OPTSTR "+AbdeEnr:stTvx:lhV"
#define MOREINFO "Try `%s --help' for more information.\n"
int option_index = 0;
static struct myoption long_options[] =
static struct caca_option long_options[] =
{
{ "show-all", 0, NULL, 'A' },
{ "number-nonblank", 0, NULL, 'b' },
@@ -116,7 +105,7 @@ int main(int argc, char *argv[])
{ "version", 0, NULL, 'V' },
{ NULL, 0, NULL, 0 }
};
int c = mygetopt(argc, argv, OPTSTR, long_options, &option_index);
int c = caca_getopt(argc, argv, OPTSTR, long_options, &option_index);
if (c == -1)
break;
@@ -142,7 +131,7 @@ int main(int argc, char *argv[])
number_lines = 1;
break;
case 'r': /* --repeat */
repeat = atoi(myoptarg);
repeat = atoi(caca_optarg);
break;
case 's': /* --squeeze-blank */
squeeze_lines = 1;
@@ -157,9 +146,9 @@ int main(int argc, char *argv[])
escape_tabs = 1;
break;
case 'x': /* --execute */
if (myoptarg[0] == '=')
myoptarg++;
sequence = myoptarg;
if (caca_optarg[0] == '=')
caca_optarg++;
sequence = caca_optarg;
break;
case 'l': /* --list */
syntax();
@@ -177,14 +166,14 @@ int main(int argc, char *argv[])
}
}
if (myoptind >= argc)
if (caca_optind >= argc)
{
fprintf(stderr, "E: zzat: too few arguments\n");
return EXIT_FAILURE;
}
while (repeat-- > 0)
for (i = myoptind; i < argc; i++)
for (i = caca_optind; i < argc; i++)
{
int ret = run(sequence, argv[i]);
if (ret)

View File

@@ -23,11 +23,6 @@
#elif defined HAVE_INTTYPES_H
# include <inttypes.h>
#endif
#if !defined HAVE_GETOPT_LONG
# include "mygetopt.h"
#elif defined HAVE_GETOPT_H
# include <getopt.h>
#endif
#include <stdio.h>
#include <stdlib.h>
#if defined HAVE_UNISTD_H
@@ -55,6 +50,8 @@
# include <sys/resource.h> /* for RLIMIT_AS */
#endif
#include <caca.h>
#include "common.h"
#include "opts.h"
#include "random.h"
@@ -64,13 +61,6 @@
#include "md5.h"
#include "timer.h"
#if defined HAVE_GETOPT_LONG
# define mygetopt getopt_long
# define myoptind optind
# define myoptarg optarg
# define myoption option
#endif
#if !defined SIGKILL
# define SIGKILL 9
#endif
@@ -156,7 +146,7 @@ int main(int argc, char *argv[])
"a:Ab:B:C:dD:e:f:F:ij:l:mnO:p:P:qr:R:s:St:U:vxhV"
#define MOREINFO "Try `%s --help' for more information.\n"
int option_index = 0;
static struct myoption long_options[] =
static struct caca_option long_options[] =
{
/* Long option, needs arg, flag, short option */
{ "allow", 1, NULL, 'a' },
@@ -199,7 +189,7 @@ int main(int argc, char *argv[])
{ "version", 0, NULL, 'V' },
{ NULL, 0, NULL, 0 }
};
int c = mygetopt(argc, argv, OPTSTR, long_options, &option_index);
int c = caca_getopt(argc, argv, OPTSTR, long_options, &option_index);
if(c == -1)
break;
@@ -207,18 +197,18 @@ int main(int argc, char *argv[])
switch(c)
{
case 'a': /* --allow */
opts->allow = myoptarg;
opts->allow = caca_optarg;
break;
case 'A': /* --autoinc */
setenv("ZZUF_AUTOINC", "1", 1);
break;
case 'b': /* --bytes */
opts->bytes = myoptarg;
opts->bytes = caca_optarg;
break;
case 'B': /* --max-bytes */
if(myoptarg[0] == '=')
myoptarg++;
opts->maxbytes = atoi(myoptarg);
if(caca_optarg[0] == '=')
caca_optarg++;
opts->maxbytes = atoi(caca_optarg);
break;
#if defined HAVE_REGEX_H
case 'c': /* --cmdline */
@@ -226,9 +216,9 @@ int main(int argc, char *argv[])
break;
#endif
case 'C': /* --max-crashes */
if(myoptarg[0] == '=')
myoptarg++;
opts->maxcrashes = atoi(myoptarg);
if(caca_optarg[0] == '=')
caca_optarg++;
opts->maxcrashes = atoi(caca_optarg);
if(opts->maxcrashes <= 0)
opts->maxcrashes = 0;
break;
@@ -236,24 +226,24 @@ int main(int argc, char *argv[])
debug++;
break;
case 'D': /* --delay */
if(myoptarg[0] == '=')
myoptarg++;
opts->delay = (int64_t)(atof(myoptarg) * 1000000.0);
if(caca_optarg[0] == '=')
caca_optarg++;
opts->delay = (int64_t)(atof(caca_optarg) * 1000000.0);
break;
#if defined HAVE_REGEX_H
case 'E': /* --exclude */
exclude = merge_regex(exclude, myoptarg);
exclude = merge_regex(exclude, caca_optarg);
if(!exclude)
{
fprintf(stderr, "%s: invalid regex -- `%s'\n",
argv[0], myoptarg);
argv[0], caca_optarg);
_zz_opts_fini(opts);
return EXIT_FAILURE;
}
break;
#endif
case 'f': /* --fuzzing */
opts->fuzzing = myoptarg;
opts->fuzzing = caca_optarg;
break;
case 'F':
fprintf(stderr, "%s: `-F' is deprecated, use `-j'\n", argv[0]);
@@ -264,32 +254,32 @@ int main(int argc, char *argv[])
break;
#if defined HAVE_REGEX_H
case 'I': /* --include */
include = merge_regex(include, myoptarg);
include = merge_regex(include, caca_optarg);
if(!include)
{
fprintf(stderr, "%s: invalid regex -- `%s'\n",
argv[0], myoptarg);
argv[0], caca_optarg);
_zz_opts_fini(opts);
return EXIT_FAILURE;
}
break;
#endif
case 'j': /* --jobs */
if(myoptarg[0] == '=')
myoptarg++;
opts->maxchild = atoi(myoptarg) > 1 ? atoi(myoptarg) : 1;
if(caca_optarg[0] == '=')
caca_optarg++;
opts->maxchild = atoi(caca_optarg) > 1 ? atoi(caca_optarg) : 1;
break;
case 'l': /* --list */
opts->list = myoptarg;
opts->list = caca_optarg;
break;
case 'm': /* --md5 */
opts->md5 = 1;
break;
#if defined HAVE_SETRLIMIT && defined ZZUF_RLIMIT_MEM
case 'M': /* --max-memory */
if(myoptarg[0] == '=')
myoptarg++;
opts->maxmem = atoi(myoptarg);
if(caca_optarg[0] == '=')
caca_optarg++;
opts->maxmem = atoi(caca_optarg);
break;
#endif
case 'n': /* --network */
@@ -297,44 +287,44 @@ int main(int argc, char *argv[])
network = 1;
break;
case 'O': /* --opmode */
if(myoptarg[0] == '=')
myoptarg++;
if (!strcmp(myoptarg, "preload"))
if(caca_optarg[0] == '=')
caca_optarg++;
if (!strcmp(caca_optarg, "preload"))
opts->opmode = OPMODE_PRELOAD;
else if (!strcmp(myoptarg, "copy"))
else if (!strcmp(caca_optarg, "copy"))
opts->opmode = OPMODE_COPY;
else
{
fprintf(stderr, "%s: invalid operating mode -- `%s'\n",
argv[0], myoptarg);
argv[0], caca_optarg);
_zz_opts_fini(opts);
return EXIT_FAILURE;
}
break;
case 'p': /* --ports */
opts->ports = myoptarg;
opts->ports = caca_optarg;
break;
case 'P': /* --protect */
opts->protect = myoptarg;
opts->protect = caca_optarg;
break;
case 'q': /* --quiet */
opts->quiet = 1;
break;
case 'r': /* --ratio */
if(myoptarg[0] == '=')
myoptarg++;
tmp = strchr(myoptarg, ':');
opts->minratio = atof(myoptarg);
if(caca_optarg[0] == '=')
caca_optarg++;
tmp = strchr(caca_optarg, ':');
opts->minratio = atof(caca_optarg);
opts->maxratio = tmp ? atof(tmp + 1) : opts->minratio;
break;
case 'R': /* --refuse */
opts->refuse = myoptarg;
opts->refuse = caca_optarg;
break;
case 's': /* --seed */
if(myoptarg[0] == '=')
myoptarg++;
tmp = strchr(myoptarg, ':');
opts->seed = atol(myoptarg);
if(caca_optarg[0] == '=')
caca_optarg++;
tmp = strchr(caca_optarg, ':');
opts->seed = atol(caca_optarg);
opts->endseed = tmp ? tmp[1] ? (uint32_t)atol(tmp + 1)
: (uint32_t)-1L
: opts->seed + 1;
@@ -343,21 +333,21 @@ int main(int argc, char *argv[])
setenv("ZZUF_SIGNAL", "1", 1);
break;
case 't': /* --max-time */
if(myoptarg[0] == '=')
myoptarg++;
opts->maxtime = (int64_t)atoi(myoptarg) * 1000000;
if(caca_optarg[0] == '=')
caca_optarg++;
opts->maxtime = (int64_t)atoi(caca_optarg) * 1000000;
break;
#if defined HAVE_SETRLIMIT && defined ZZUF_RLIMIT_CPU
case 'T': /* --max-cputime */
if(myoptarg[0] == '=')
myoptarg++;
opts->maxcpu = (int)(atof(myoptarg) + 0.5);
if(caca_optarg[0] == '=')
caca_optarg++;
opts->maxcpu = (int)(atof(caca_optarg) + 0.5);
break;
#endif
case 'U': /* --max-usertime */
if(myoptarg[0] == '=')
myoptarg++;
opts->maxusertime = (int64_t)(atof(myoptarg) * 1000000.0);
if(caca_optarg[0] == '=')
caca_optarg++;
opts->maxusertime = (int64_t)(atof(caca_optarg) * 1000000.0);
break;
case 'x': /* --check-exit */
opts->checkexit = 1;
@@ -419,7 +409,7 @@ int main(int argc, char *argv[])
/*
* Mode 1: asked to read from the standard input
*/
if(myoptind >= argc)
if(caca_optind >= argc)
{
if(opts->verbose)
{
@@ -448,7 +438,7 @@ int main(int argc, char *argv[])
{
int dashdash = 0;
for(i = myoptind + 1; i < argc; i++)
for(i = caca_optind + 1; i < argc; i++)
{
if(dashdash)
include = merge_file(include, argv[i]);
@@ -503,9 +493,9 @@ int main(int argc, char *argv[])
opts->oldargv = argv;
for(i = 0; i < opts->maxchild; i++)
{
int len = argc - myoptind;
int len = argc - caca_optind;
opts->child[i].newargv = malloc((len + 1) * sizeof(char *));
memcpy(opts->child[i].newargv, argv + myoptind,
memcpy(opts->child[i].newargv, argv + caca_optind,
len * sizeof(char *));
opts->child[i].newargv[len] = (char *)NULL;
}
@@ -704,7 +694,7 @@ static void spawn_children(struct opts *opts)
if (!tmpdir || !*tmpdir)
tmpdir = "/tmp";
for (j = myoptind + 1; j < opts->oldargc; j++)
for (j = caca_optind + 1; j < opts->oldargc; j++)
{
fpin = fopen(opts->oldargv[j], "r");
if (!fpin)
@@ -722,7 +712,7 @@ static void spawn_children(struct opts *opts)
continue;
}
opts->child[i].newargv[j - myoptind] = strdup(tmpname);
opts->child[i].newargv[j - caca_optind] = strdup(tmpname);
_zz_register(k);
while(!feof(fpin))
@@ -897,13 +887,13 @@ static void clean_children(struct opts *opts)
if (opts->opmode == OPMODE_COPY)
{
for (j = myoptind + 1; j < opts->oldargc; j++)
for (j = caca_optind + 1; j < opts->oldargc; j++)
{
if (opts->child[i].newargv[j - myoptind] != opts->oldargv[j])
if (opts->child[i].newargv[j - caca_optind] != opts->oldargv[j])
{
unlink(opts->child[i].newargv[j - myoptind]);
free(opts->child[i].newargv[j - myoptind]);
opts->child[i].newargv[j - myoptind] = opts->oldargv[j];
unlink(opts->child[i].newargv[j - caca_optind]);
free(opts->child[i].newargv[j - caca_optind]);
opts->child[i].newargv[j - caca_optind] = opts->oldargv[j];
}
}
}