* Factored regex matching stuff.
This commit is contained in:
@@ -26,7 +26,6 @@
|
||||
#include <stdio.h>
|
||||
#include <errno.h>
|
||||
#include <stdarg.h>
|
||||
#include <regex.h>
|
||||
|
||||
#include "debug.h"
|
||||
|
||||
|
||||
@@ -25,7 +25,6 @@
|
||||
#endif
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <regex.h>
|
||||
|
||||
#include "libzzuf.h"
|
||||
#include "debug.h"
|
||||
|
||||
+26
-9
@@ -42,8 +42,14 @@ int _zz_ready = 0;
|
||||
int _zz_hasdebug = 0;
|
||||
int _zz_seed = 0;
|
||||
float _zz_ratio = 0.004f;
|
||||
regex_t * _zz_include = NULL;
|
||||
regex_t * _zz_exclude = NULL;
|
||||
|
||||
/* Local variables */
|
||||
static regex_t * re_include = NULL;
|
||||
static regex_t * re_exclude = NULL;
|
||||
|
||||
/* Local prototypes */
|
||||
static void _zz_fd_init(void);
|
||||
static void _zz_fd_fini(void);
|
||||
|
||||
/* Library initialisation shit */
|
||||
void _zz_init(void)
|
||||
@@ -69,15 +75,15 @@ void _zz_init(void)
|
||||
tmp = getenv("ZZUF_INCLUDE");
|
||||
if(tmp && *tmp)
|
||||
{
|
||||
_zz_include = malloc(sizeof(*_zz_include));
|
||||
regcomp(_zz_include, tmp, 0);
|
||||
re_include = malloc(sizeof(*re_include));
|
||||
regcomp(re_include, tmp, 0);
|
||||
}
|
||||
|
||||
tmp = getenv("ZZUF_EXCLUDE");
|
||||
if(tmp && *tmp)
|
||||
{
|
||||
_zz_exclude = malloc(sizeof(*_zz_exclude));
|
||||
regcomp(_zz_exclude, tmp, 0);
|
||||
re_exclude = malloc(sizeof(*re_exclude));
|
||||
regcomp(re_exclude, tmp, 0);
|
||||
}
|
||||
|
||||
_zz_fd_init();
|
||||
@@ -113,7 +119,7 @@ static struct files
|
||||
static int *fds;
|
||||
static int maxfd, nfiles;
|
||||
|
||||
void _zz_fd_init(void)
|
||||
static void _zz_fd_init(void)
|
||||
{
|
||||
files = NULL;
|
||||
nfiles = 0;
|
||||
@@ -124,7 +130,7 @@ void _zz_fd_init(void)
|
||||
fds[maxfd] = -1;
|
||||
}
|
||||
|
||||
void _zz_fd_fini(void)
|
||||
static void _zz_fd_fini(void)
|
||||
{
|
||||
int i;
|
||||
|
||||
@@ -141,7 +147,18 @@ void _zz_fd_fini(void)
|
||||
free(fds);
|
||||
}
|
||||
|
||||
int _zz_ismanaged(int fd)
|
||||
int _zz_mustwatch(char const *file)
|
||||
{
|
||||
if(re_include && regexec(re_include, file, 0, NULL, 0) == REG_NOMATCH)
|
||||
return 0; /* not included: ignore */
|
||||
|
||||
if(re_exclude && regexec(re_exclude, file, 0, NULL, 0) != REG_NOMATCH)
|
||||
return 0; /* excluded: ignore */
|
||||
|
||||
return 1; /* default */
|
||||
}
|
||||
|
||||
int _zz_iswatched(int fd)
|
||||
{
|
||||
if(fd < 0 || fd >= maxfd || fds[fd] == -1)
|
||||
return 0;
|
||||
|
||||
+2
-5
@@ -33,17 +33,14 @@ extern int _zz_ready;
|
||||
extern int _zz_hasdebug;
|
||||
extern int _zz_seed;
|
||||
extern float _zz_ratio;
|
||||
extern regex_t * _zz_include;
|
||||
extern regex_t * _zz_exclude;
|
||||
|
||||
/* Library initialisation shit */
|
||||
extern void _zz_init(void) __attribute__((constructor));
|
||||
extern void _zz_fini(void) __attribute__((destructor));
|
||||
|
||||
/* File descriptor handling */
|
||||
extern void _zz_fd_init(void);
|
||||
extern void _zz_fd_fini(void);
|
||||
extern int _zz_ismanaged(int);
|
||||
extern int _zz_mustwatch(char const *);
|
||||
extern int _zz_iswatched(int);
|
||||
extern void _zz_register(int);
|
||||
extern void _zz_unregister(int);
|
||||
extern long int _zz_getpos(int);
|
||||
|
||||
+10
-19
@@ -29,7 +29,6 @@
|
||||
# include <inttypes.h>
|
||||
#endif
|
||||
#include <stdlib.h>
|
||||
#include <regex.h>
|
||||
#include <dlfcn.h>
|
||||
|
||||
#include <sys/types.h>
|
||||
@@ -81,23 +80,15 @@ void _zz_load_fd(void)
|
||||
if(!_zz_ready) \
|
||||
return ret; \
|
||||
if(ret >= 0 \
|
||||
&& ((oflag & (O_RDONLY | O_RDWR | O_WRONLY)) != O_WRONLY)) \
|
||||
&& ((oflag & (O_RDONLY | O_RDWR | O_WRONLY)) != O_WRONLY) \
|
||||
&& _zz_mustwatch(file)) \
|
||||
{ \
|
||||
if(_zz_include && \
|
||||
regexec(_zz_include, file, 0, NULL, 0) == REG_NOMATCH) \
|
||||
/* not included: ignore */ ; \
|
||||
else if(_zz_exclude && \
|
||||
regexec(_zz_exclude, file, 0, NULL, 0) != REG_NOMATCH) \
|
||||
/* excluded: ignore */ ; \
|
||||
if(oflag & O_CREAT) \
|
||||
debug(STR(fn) "(\"%s\", %i, %i) = %i", \
|
||||
file, oflag, mode, ret); \
|
||||
else \
|
||||
{ \
|
||||
if(oflag & O_CREAT) \
|
||||
debug(STR(fn) "(\"%s\", %i, %i) = %i", \
|
||||
file, oflag, mode, ret); \
|
||||
else \
|
||||
debug(STR(fn) "(\"%s\", %i) = %i", file, oflag, ret); \
|
||||
_zz_register(ret); \
|
||||
} \
|
||||
debug(STR(fn) "(\"%s\", %i) = %i", file, oflag, ret); \
|
||||
_zz_register(ret); \
|
||||
} \
|
||||
} while(0)
|
||||
|
||||
@@ -118,7 +109,7 @@ ssize_t read(int fd, void *buf, size_t count)
|
||||
if(!_zz_ready)
|
||||
LOADSYM(read);
|
||||
ret = read_orig(fd, buf, count);
|
||||
if(!_zz_ready || !_zz_ismanaged(fd))
|
||||
if(!_zz_ready || !_zz_iswatched(fd))
|
||||
return ret;
|
||||
|
||||
debug("read(%i, %p, %li) = %i", fd, buf, (long int)count, ret);
|
||||
@@ -140,7 +131,7 @@ ssize_t read(int fd, void *buf, size_t count)
|
||||
if(!_zz_ready) \
|
||||
LOADSYM(fn); \
|
||||
ret = ORIG(fn)(fd, offset, whence); \
|
||||
if(!_zz_ready || !_zz_ismanaged(fd)) \
|
||||
if(!_zz_ready || !_zz_iswatched(fd)) \
|
||||
return ret; \
|
||||
debug(STR(fn)"(%i, %lli, %i) = %lli", \
|
||||
fd, (long long int)offset, whence, (long long int)ret); \
|
||||
@@ -169,7 +160,7 @@ int close(int fd)
|
||||
if(!_zz_ready)
|
||||
LOADSYM(close);
|
||||
ret = close_orig(fd);
|
||||
if(!_zz_ready || !_zz_ismanaged(fd))
|
||||
if(!_zz_ready || !_zz_iswatched(fd))
|
||||
return ret;
|
||||
|
||||
debug("close(%i) = %i", fd, ret);
|
||||
|
||||
+11
-21
@@ -26,7 +26,6 @@
|
||||
# include <inttypes.h>
|
||||
#endif
|
||||
#include <stdlib.h>
|
||||
#include <regex.h>
|
||||
#include <dlfcn.h>
|
||||
|
||||
#include <stdio.h>
|
||||
@@ -82,20 +81,11 @@ void _zz_load_stream(void)
|
||||
return ORIG(fn)(path, mode); \
|
||||
} \
|
||||
ret = ORIG(fn)(path, mode); \
|
||||
if(ret) \
|
||||
if(ret && _zz_mustwatch(path)) \
|
||||
{ \
|
||||
if(_zz_include && \
|
||||
regexec(_zz_include, path, 0, NULL, 0) == REG_NOMATCH) \
|
||||
/* not included: ignore */ ; \
|
||||
else if(_zz_exclude && \
|
||||
regexec(_zz_exclude, path, 0, NULL, 0) != REG_NOMATCH) \
|
||||
/* excluded: ignore */ ; \
|
||||
else \
|
||||
{ \
|
||||
int fd = fileno(ret); \
|
||||
_zz_register(fd); \
|
||||
debug(STR(fn) "(\"%s\", \"%s\") = %p", path, mode, ret); \
|
||||
} \
|
||||
int fd = fileno(ret); \
|
||||
_zz_register(fd); \
|
||||
debug(STR(fn) "(\"%s\", \"%s\") = %p", path, mode, ret); \
|
||||
} \
|
||||
} while(0)
|
||||
|
||||
@@ -116,7 +106,7 @@ int fseek(FILE *stream, long offset, int whence)
|
||||
if(!_zz_ready)
|
||||
LOADSYM(fseek);
|
||||
fd = fileno(stream);
|
||||
if(!_zz_ready || !_zz_ismanaged(fd))
|
||||
if(!_zz_ready || !_zz_iswatched(fd))
|
||||
return fseek_orig(stream, offset, whence);
|
||||
|
||||
ret = fseek_orig(stream, offset, whence);
|
||||
@@ -148,7 +138,7 @@ size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream)
|
||||
if(!_zz_ready)
|
||||
LOADSYM(fread);
|
||||
fd = fileno(stream);
|
||||
if(!_zz_ready || !_zz_ismanaged(fd))
|
||||
if(!_zz_ready || !_zz_iswatched(fd))
|
||||
return fread_orig(ptr, size, nmemb, stream);
|
||||
|
||||
pos = ftell(stream);
|
||||
@@ -172,7 +162,7 @@ size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream)
|
||||
if(!_zz_ready) \
|
||||
LOADSYM(fn); \
|
||||
fd = fileno(stream); \
|
||||
if(!_zz_ready || !_zz_ismanaged(fd)) \
|
||||
if(!_zz_ready || !_zz_iswatched(fd)) \
|
||||
return ORIG(fn)(stream); \
|
||||
ret = ORIG(fn)(stream); \
|
||||
if(ret != EOF) \
|
||||
@@ -203,7 +193,7 @@ char *fgets(char *s, int size, FILE *stream)
|
||||
if(!_zz_ready)
|
||||
LOADSYM(fgets);
|
||||
fd = fileno(stream);
|
||||
if(!_zz_ready || !_zz_ismanaged(fd))
|
||||
if(!_zz_ready || !_zz_iswatched(fd))
|
||||
return fgets_orig(s, size, stream);
|
||||
|
||||
if(size <= 0)
|
||||
@@ -246,7 +236,7 @@ int ungetc(int c, FILE *stream)
|
||||
if(!_zz_ready)
|
||||
LOADSYM(ungetc);
|
||||
fd = fileno(stream);
|
||||
if(!_zz_ready || !_zz_ismanaged(fd))
|
||||
if(!_zz_ready || !_zz_iswatched(fd))
|
||||
return ungetc_orig(c, stream);
|
||||
|
||||
_zz_addpos(fd, -1);
|
||||
@@ -267,7 +257,7 @@ int fclose(FILE *fp)
|
||||
if(!_zz_ready)
|
||||
LOADSYM(fclose);
|
||||
fd = fileno(fp);
|
||||
if(!_zz_ready || !_zz_ismanaged(fd))
|
||||
if(!_zz_ready || !_zz_iswatched(fd))
|
||||
return fclose_orig(fp);
|
||||
|
||||
ret = fclose_orig(fp);
|
||||
@@ -285,7 +275,7 @@ int fclose(FILE *fp)
|
||||
if(!_zz_ready) \
|
||||
LOADSYM(fn); \
|
||||
fd = fileno(stream); \
|
||||
if(!_zz_ready || !_zz_ismanaged(fd)) \
|
||||
if(!_zz_ready || !_zz_iswatched(fd)) \
|
||||
return getdelim_orig(lineptr, n, delim, stream); \
|
||||
line = *lineptr; \
|
||||
size = line ? *n : 0; \
|
||||
|
||||
Reference in New Issue
Block a user