* Added code for --include/--exclude. Does not work yet.

This commit is contained in:
Sam Hocevar 2006-12-15 07:53:09 +00:00 committed by sam
parent 1ebf62640b
commit 817f735d78
6 changed files with 47 additions and 21 deletions

View File

@ -26,6 +26,7 @@
#include <stdio.h>
#include <errno.h>
#include <stdarg.h>
#include <regex.h>
#include "libzzuf.h"
#include "debug.h"

View File

@ -25,6 +25,7 @@
#endif
#include <stdio.h>
#include <string.h>
#include <regex.h>
#include "libzzuf.h"
#include "debug.h"

View File

@ -29,6 +29,7 @@
#include <stdlib.h>
#include <fcntl.h>
#include <errno.h>
#include <regex.h>
#include <stdarg.h>
#include <dlfcn.h>
@ -41,6 +42,8 @@
int _zzuf_debug = 0;
int _zzuf_seed = 0;
float _zzuf_percent = 0.04f;
regex_t * _zzuf_include = NULL;
regex_t * _zzuf_exclude = NULL;
#define MAXFD 1024
struct zzuf files[MAXFD];
@ -70,6 +73,22 @@ void zzuf_init(void)
else if(_zzuf_percent > 1.0f)
_zzuf_percent = 1.0f;
tmp = getenv("ZZUF_INCLUDE");
if(tmp && *tmp)
{
_zzuf_include = malloc(sizeof(*_zzuf_include));
debug("zzuf_include = \"%s\"", tmp);
regcomp(_zzuf_include, tmp, 0);
}
tmp = getenv("ZZUF_EXCLUDE");
if(tmp && *tmp)
{
_zzuf_exclude = malloc(sizeof(*_zzuf_exclude));
debug("zzuf_exclude = \"%s\"", tmp);
regcomp(_zzuf_exclude, tmp, 0);
}
for(i = 0; i < MAXFD; i++)
files[i].managed = 0;
}

View File

@ -30,6 +30,8 @@ extern struct zzuf files[];
extern int _zzuf_debug;
extern int _zzuf_seed;
extern float _zzuf_percent;
extern regex_t * _zzuf_include;
extern regex_t * _zzuf_exclude;
/* Library initialisation shit */
extern void zzuf_init(void) __attribute__((constructor));

View File

@ -29,6 +29,7 @@
#include <stdlib.h>
#include <fcntl.h>
#include <errno.h>
#include <regex.h>
#include <stdarg.h>
#include <dlfcn.h>
@ -77,7 +78,9 @@ int zzuf_preload(void)
{ \
ret = ORIG(fn)(path, mode); \
debug(STR(fn) "(\"%s\", \"%s\") = %p", path, mode, ret); \
if(ret) \
if(ret \
&& (!_zzuf_include || !regexec(_zzuf_include, path, 0, NULL, 0)) \
&& (!_zzuf_exclude || regexec(_zzuf_exclude, path, 0, NULL, 0))) \
{ \
int fd = fileno(ret); \
files[fd].managed = 1; \
@ -127,13 +130,13 @@ size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream)
debug(STR(fn) "(\"%s\", %i) = %i", file, oflag, ret); \
} \
\
if(ret >= 0) \
if(ret >= 0 \
&& ((oflag & (O_RDONLY | O_RDWR | O_WRONLY)) != O_WRONLY) \
&& (!_zzuf_include || !regexec(_zzuf_include, file, 0, NULL, 0)) \
&& (!_zzuf_exclude || regexec(_zzuf_exclude, file, 0, NULL, 0))) \
{ \
if((oflag & (O_RDONLY | O_RDWR | O_WRONLY)) != O_WRONLY) \
{ \
files[ret].managed = 1; \
files[ret].pos = 0; \
} \
files[ret].managed = 1; \
files[ret].pos = 0; \
} \
} while(0)

View File

@ -41,10 +41,7 @@ static void usage(void);
int main(int argc, char *argv[])
{
char buf[BUFSIZ];
char **newargv;
long int seed = 0;
float percent = 0.04;
#if defined(HAVE_GETOPT_H)
for(;;)
@ -55,28 +52,36 @@ int main(int argc, char *argv[])
static struct option long_options[] =
{
/* Long option, needs arg, flag, short option */
{ "include", 1, NULL, 'i' },
{ "exclude", 1, NULL, 'e' },
{ "seed", 1, NULL, 's' },
{ "percent", 1, NULL, 'p' },
{ "help", 0, NULL, 'h' },
{ "version", 0, NULL, 'v' },
};
int c = getopt_long(argc, argv, "s:p:hv",
int c = getopt_long(argc, argv, "i:e:s:p:hv",
long_options, &option_index);
# else
# define MOREINFO "Try `%s -h' for more information.\n"
int c = getopt(argc, argv, "s:p:hv");
int c = getopt(argc, argv, "i:e:s:p:hv");
# endif
if(c == -1)
break;
switch(c)
{
case 'i': /* --include */
setenv("ZZUF_INCLUDE", optarg, 1);
break;
case 'e': /* --exclude */
setenv("ZZUF_EXCLUDE", optarg, 1);
break;
case 's': /* --seed */
seed = atol(optarg);
setenv("ZZUF_SEED", optarg, 1);
break;
case 'p': /* --percent */
percent = atof(optarg);
setenv("ZZUF_PERCENT", optarg, 1);
break;
case 'h': /* --help */
usage();
@ -109,12 +114,6 @@ int main(int argc, char *argv[])
/* Preload libzzuf.so */
set_ld_preload(argv[0]);
/* Set environment */
sprintf(buf, "%lu", (unsigned long int)seed);
setenv("ZZUF_SEED", buf, 1);
sprintf(buf, "%g", percent);
setenv("ZZUF_PERCENT", buf, 1);
/* Call our process */
execvp(newargv[0], newargv);
@ -149,7 +148,8 @@ static void version(void)
#if defined(HAVE_GETOPT_H)
static void usage(void)
{
printf("Usage: zzuf [ -vh ] [ -p percent ] [ -s seed ] PROG ARGS...\n");
printf("Usage: zzuf [ -vh ] [ -i regex ] [ -e regex ]\n");
printf(" [ -p percent ] [ -s seed ] PROG ARGS...\n");
# ifdef HAVE_GETOPT_LONG
printf(" -h, --help display this help and exit\n");
printf(" -v, --version output version information and exit\n");