diff options
author | obrien <obrien@FreeBSD.org> | 2011-10-06 06:01:12 +0000 |
---|---|---|
committer | obrien <obrien@FreeBSD.org> | 2011-10-06 06:01:12 +0000 |
commit | 75c49f9dd6a0ff710f7c791a485899c7a07af444 (patch) | |
tree | d25590ff6bfc3386fbca9494d26b8761e3d33410 /funcs.c | |
parent | 862d9405b857dba35f8f2eb6d0623b9a552d4353 (diff) | |
download | FreeBSD-src-75c49f9dd6a0ff710f7c791a485899c7a07af444.zip FreeBSD-src-75c49f9dd6a0ff710f7c791a485899c7a07af444.tar.gz |
Virgin import of Christos Zoulas's FILE 5.09.
Diffstat (limited to 'funcs.c')
-rw-r--r-- | funcs.c | 54 |
1 files changed, 44 insertions, 10 deletions
@@ -27,7 +27,7 @@ #include "file.h" #ifndef lint -FILE_RCSID("@(#)$File: funcs.c,v 1.53 2009/04/07 11:07:00 christos Exp $") +FILE_RCSID("@(#)$File: funcs.c,v 1.57 2011/05/11 01:02:41 christos Exp $") #endif /* lint */ #include "magic.h" @@ -95,7 +95,7 @@ file_printf(struct magic_set *ms, const char *fmt, ...) /*VARARGS*/ private void file_error_core(struct magic_set *ms, int error, const char *f, va_list va, - uint32_t lineno) + size_t lineno) { /* Only the first error is ok */ if (ms->event_flags & EVENT_HAD_ERR) @@ -103,7 +103,7 @@ file_error_core(struct magic_set *ms, int error, const char *f, va_list va, if (lineno != 0) { free(ms->o.buf); ms->o.buf = NULL; - file_printf(ms, "line %u: ", lineno); + file_printf(ms, "line %" SIZE_T_FORMAT "u: ", lineno); } file_vprintf(ms, f, va); if (error > 0) @@ -138,7 +138,8 @@ file_magerror(struct magic_set *ms, const char *f, ...) protected void file_oomem(struct magic_set *ms, size_t len) { - file_error(ms, errno, "cannot allocate %zu bytes", len); + file_error(ms, errno, "cannot allocate %" SIZE_T_FORMAT "u bytes", + len); } protected void @@ -155,8 +156,8 @@ file_badread(struct magic_set *ms) #ifndef COMPILE_ONLY protected int -file_buffer(struct magic_set *ms, int fd, const char *inname, const void *buf, - size_t nb) +file_buffer(struct magic_set *ms, int fd, const char *inname __attribute__ ((unused)), + const void *buf, size_t nb) { int m = 0, rv = 0, looks_text = 0; int mime = ms->flags & MAGIC_MIME; @@ -200,7 +201,7 @@ file_buffer(struct magic_set *ms, int fd, const char *inname, const void *buf, } } #endif - +#if HAVE_FORK /* try compression stuff */ if ((ms->flags & MAGIC_NO_CHECK_COMPRESS) == 0) if ((m = file_zmagic(ms, fd, inname, ubuf, nb)) != 0) { @@ -208,7 +209,7 @@ file_buffer(struct magic_set *ms, int fd, const char *inname, const void *buf, (void)fprintf(stderr, "zmagic %d\n", m); goto done; } - +#endif /* Check if we have a tar file */ if ((ms->flags & MAGIC_NO_CHECK_TAR) == 0) if ((m = file_is_tar(ms, ubuf, nb)) != 0) { @@ -391,9 +392,9 @@ file_getbuffer(struct magic_set *ms) } #endif - for (np = ms->o.pbuf, op = ms->o.buf; *op; op++) { + for (np = ms->o.pbuf, op = ms->o.buf; *op;) { if (isprint((unsigned char)*op)) { - *np++ = *op; + *np++ = *op++; } else { OCTALIFY(np, op); } @@ -424,3 +425,36 @@ file_check_mem(struct magic_set *ms, unsigned int level) #endif /* ENABLE_CONDITIONALS */ return 0; } + +protected size_t +file_printedlen(const struct magic_set *ms) +{ + return ms->o.buf == NULL ? 0 : strlen(ms->o.buf); +} + +protected int +file_replace(struct magic_set *ms, const char *pat, const char *rep) +{ + regex_t rx; + int rc; + + rc = regcomp(&rx, pat, REG_EXTENDED); + if (rc) { + char errmsg[512]; + (void)regerror(rc, &rx, errmsg, sizeof(errmsg)); + file_magerror(ms, "regex error %d, (%s)", rc, errmsg); + return -1; + } else { + regmatch_t rm; + int nm = 0; + while (regexec(&rx, ms->o.buf, 1, &rm, 0) == 0) { + ms->o.buf[rm.rm_so] = '\0'; + if (file_printf(ms, "%s%s", rep, + rm.rm_eo != 0 ? ms->o.buf + rm.rm_eo : "") == -1) + return -1; + nm++; + } + regfree(&rx); + return nm; + } +} |