diff --git a/configure.ac b/configure.ac index 892087c..67ddc17 100644 --- a/configure.ac +++ b/configure.ac @@ -25,7 +25,19 @@ AC_LIBTOOL_CXX AC_C_CONST AC_C_INLINE -AC_CHECK_HEADERS(stdio.h stdarg.h inttypes.h endian.h stdint.h) +AC_CHECK_HEADERS(stdio.h stdarg.h inttypes.h endian.h stdint.h getopt.h) + +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) # Optimizations CFLAGS="${CFLAGS} -g -O2 -fno-strength-reduce -fomit-frame-pointer" diff --git a/genethumb/Makefile.am b/genethumb/Makefile.am index 6eb16d9..2f6ac04 100644 --- a/genethumb/Makefile.am +++ b/genethumb/Makefile.am @@ -2,8 +2,12 @@ AM_CPPFLAGS = -I$(top_srcdir) -I$(top_srcdir)/pipi +if NEED_GETOPT_LONG +GETOPT = mygetopt.c mygetopt.h +endif + bin_PROGRAMS = genethumb -genethumb_SOURCES = genethumb.c -genethumb_LDADD = ../pipi/libpipi.la +genethumb_SOURCES = genethumb.c $(GETOPT) +genethumb_LDADD = ../pipi/libpipi.la $(GETOPT_LIBS) diff --git a/genethumb/genethumb.c b/genethumb/genethumb.c index 6d08a27..52cac66 100644 --- a/genethumb/genethumb.c +++ b/genethumb/genethumb.c @@ -1,17 +1,73 @@ #include "config.h" #include "common.h" +#if !defined HAVE_GETOPT_LONG +# include "mygetopt.h" +#elif defined HAVE_GETOPT_H +# include +#endif + +#include +#include +#include + #include -int main(void) +#if defined HAVE_GETOPT_LONG +# define mygetopt getopt_long +# define myoptind optind +# define myoptarg optarg +# define myoption option +#endif + +#define MOREINFO "Try `%s --help' for more information.\n" + +int main(int argc, char *argv[]) { - pipi_image_t *i, *j; + char *srcname = NULL, *dstname = NULL; + pipi_image_t *src, *dst; + + int i, w = 0, h = 0; + + for(;;) + { + int option_index = 0; + static struct myoption long_options[] = + { + { "geometry", 1, NULL, 'g' }, + }; + int c = mygetopt(argc, argv, "g:", long_options, &option_index); + + if(c == -1) + break; + + switch(c) + { + case 'g': + w = atoi(myoptarg); + if(strchr(myoptarg, 'x')) + h = atoi(strchr(myoptarg, 'x') + 1); + break; + default: + fprintf(stderr, "%s: invalid option -- %c\n", argv[0], c); + printf(MOREINFO, argv[0]); + return EXIT_FAILURE; + } + } + + for(i = myoptind; i < argc; i++) + { + if(!srcname) + srcname = argv[i]; + else + dstname = argv[i]; + } - i = pipi_load("irc.png"); - j = pipi_resize(i, 180, 180); - pipi_save(j, "irc.bmp"); - pipi_free(i); - pipi_free(j); + src = pipi_load(srcname); + dst = pipi_resize(src, w, h); + pipi_save(dst, dstname); + pipi_free(src); + pipi_free(dst); return 0; } diff --git a/genethumb/mygetopt.c b/genethumb/mygetopt.c new file mode 100644 index 0000000..aaad15e --- /dev/null +++ b/genethumb/mygetopt.c @@ -0,0 +1,129 @@ +/* + * zzuf - general purpose fuzzer + * Copyright (c) 2002, 2007 Sam Hocevar + * All Rights Reserved + * + * $Id: mygetopt.c 271 2007-02-02 11:58:06Z sam $ + * + * 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 +#elif defined HAVE_INTTYPES_H +# include +#endif + +/* DOS / Kernel driver */ +#ifndef __intptr_t_defined +#ifndef _UINTPTR_T +typedef unsigned int uintptr_t; +#endif +#endif + +#include +#include + +#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 = tmp + 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; +} + diff --git a/genethumb/mygetopt.h b/genethumb/mygetopt.h new file mode 100644 index 0000000..2cf62e5 --- /dev/null +++ b/genethumb/mygetopt.h @@ -0,0 +1,31 @@ +/* + * zzuf - general purpose fuzzer + * Copyright (c) 2002, 2007 Sam Hocevar + * All Rights Reserved + * + * $Id: mygetopt.h 268 2007-02-01 22:33:07Z sam $ + * + * 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 *); +