source: zzuf/trunk/src/zzuf.c @ 1475

Last change on this file since 1475 was 1475, checked in by Sam Hocevar, 16 years ago
  • Better error reporting if command failed.
  • Property svn:keywords set to Id
File size: 4.3 KB
Line 
1/*
2 *  zzuf - general purpose fuzzer
3 *  Copyright (c) 2006 Sam Hocevar <sam@zoy.org>
4 *                All Rights Reserved
5 *
6 *  $Id: zzuf.c 1475 2006-12-15 09:01:46Z sam $
7 *
8 *  This program is free software. It comes without any warranty, to
9 *  the extent permitted by applicable law. You can redistribute it
10 *  and/or modify it under the terms of the Do What The Fuck You Want
11 *  To Public License, Version 2, as published by Sam Hocevar. See
12 *  http://sam.zoy.org/wtfpl/COPYING for more details.
13 */
14
15/*
16 *  main.c: main program
17 */
18
19#include "config.h"
20
21#if defined HAVE_STDINT_H
22#   include <stdint.h>
23#elif defined HAVE_INTTYPES_H
24#   include <inttypes.h>
25#endif
26#if defined(HAVE_GETOPT_H)
27#   include <getopt.h>
28#endif
29#include <stdio.h>
30#include <stdlib.h>
31#include <unistd.h>
32#include <string.h>
33
34#include "random.h"
35
36static void set_ld_preload(char const *);
37static void version(void);
38#if defined(HAVE_GETOPT_H)
39static void usage(void);
40#endif
41
42int main(int argc, char *argv[])
43{
44    char **newargv;
45
46#if defined(HAVE_GETOPT_H)
47    for(;;)
48    {
49#   ifdef HAVE_GETOPT_LONG
50#       define MOREINFO "Try `%s --help' for more information.\n"
51        int option_index = 0;
52        static struct option long_options[] =
53            {
54                /* Long option, needs arg, flag, short option */
55                { "include", 1, NULL, 'i' },
56                { "exclude", 1, NULL, 'e' },
57                { "seed", 1, NULL, 's' },
58                { "percent", 1, NULL, 'p' },
59                { "debug", 1, NULL, 'd' },
60                { "help", 0, NULL, 'h' },
61                { "version", 0, NULL, 'v' },
62            };
63
64        int c = getopt_long(argc, argv, "i:e:s:p:dhv",
65                            long_options, &option_index);
66#   else
67#       define MOREINFO "Try `%s -h' for more information.\n"
68        int c = getopt(argc, argv, "i:e:s:p:dhv");
69#   endif
70        if(c == -1)
71            break;
72
73        switch(c)
74        {
75        case 'i': /* --include */
76            setenv("ZZUF_INCLUDE", optarg, 1);
77            break;
78        case 'e': /* --exclude */
79            setenv("ZZUF_EXCLUDE", optarg, 1);
80            break;
81        case 's': /* --seed */
82            setenv("ZZUF_SEED", optarg, 1);
83            break;
84        case 'p': /* --percent */
85            setenv("ZZUF_PERCENT", optarg, 1);
86            break;
87        case 'd': /* --debug */
88            setenv("ZZUF_DEBUG", "1", 1);
89            break;
90        case 'h': /* --help */
91            usage();
92            return 0;
93        case 'v': /* --version */
94            version();
95            return 0;
96        default:
97            printf("%s: invalid option -- %c\n", argv[0], c);
98            printf(MOREINFO, argv[0]);
99            return 1;
100        }
101    }
102#else
103#   define MOREINFO "Usage: %s message...\n"
104    int optind = 1;
105#endif
106
107    if(optind >= argc)
108    {
109        usage();
110        return -1;
111    }
112
113    /* Create new argv */
114    newargv = malloc((argc - optind + 1) * sizeof(char *));
115    memcpy(newargv, argv + optind, (argc - optind) * sizeof(char *));
116    newargv[argc - optind] = (char *)NULL;
117
118    /* Preload libzzuf.so */
119    set_ld_preload(argv[0]);
120
121    /* Call our process */
122    if(execvp(newargv[0], newargv))
123    {
124        perror(newargv[0]);
125        return -1;
126    }
127
128    return 0;   
129}
130
131static void set_ld_preload(char const *progpath)
132{
133    char *libpath, *tmp;
134    int len = strlen(progpath);
135
136    libpath = malloc(len + strlen("/.libs/libzzuf.so") + 1);
137    strcpy(libpath, progpath);
138    tmp = strrchr(libpath, '/');
139    strcpy(tmp ? tmp + 1 : libpath, ".libs/libzzuf.so");
140    if(access(libpath, R_OK) == 0)
141    {
142        setenv("LD_PRELOAD", libpath, 1);
143        return;
144    }
145    free(libpath);
146
147    /* FIXME: use real path */
148    setenv("LD_PRELOAD", "/usr/lib/zzuf/libzzuf.so", 1);
149}
150
151static void version(void)
152{
153    printf("zzuf %s by Sam Hocevar <sam@zoy.org>\n", VERSION);
154}
155
156#if defined(HAVE_GETOPT_H)
157static void usage(void)
158{
159    printf("Usage: zzuf [ -vdh ] [ -i regex ] [ -e regex ]\n");
160    printf("                     [ -p percent ] [ -s seed ] PROGRAM ARGS...\n");
161#   ifdef HAVE_GETOPT_LONG
162    printf("  -h, --help          display this help and exit\n");
163    printf("  -v, --version       output version information and exit\n");
164#   else
165    printf("  -h        display this help and exit\n");
166    printf("  -v        output version information and exit\n");
167#   endif
168}
169#endif
170
Note: See TracBrowser for help on using the repository browser.