From 096b7eb406f2a477c1ef2bfd060ae37deeb033e1 Mon Sep 17 00:00:00 2001 From: pancake Date: Wed, 14 Sep 2011 12:37:26 +0200 Subject: [PATCH] * Fix mingw32 build --- binr/ragg2/ragg2.c | 2 ++ libr/asm/p/asm_ppc.c | 11 +++++++-- libr/db/Makefile | 4 +++- libr/db/sdb/config.mk | 2 +- libr/db/sdb/src/Makefile | 13 ++++++----- libr/db/sdb/src/cdb.c | 14 +++++++++++- libr/db/sdb/src/cdb.h | 8 +++++++ libr/db/sdb/src/lock.c | 5 +++++ libr/db/sdb/src/main.c | 8 +++++-- libr/db/sdb/src/sdb.c | 11 +++++++-- libr/db/sdb/src/sdbn.c | 8 +++---- libr/include/r_regex.h | 1 + libr/include/r_types.h | 1 + libr/magic/apprentice.c | 24 +++++++++++++------- libr/magic/compress.c | 8 ++++++- libr/magic/file.h | 2 +- libr/magic/fsmagic.c | 6 ++++- libr/magic/funcs.c | 17 +++++++++++--- libr/magic/magic.c | 15 +++++++++++-- libr/magic/softmagic.c | 48 ++++++++++++++++++++++++++++++---------- libr/util/chmod.c | 2 +- 21 files changed, 163 insertions(+), 47 deletions(-) diff --git a/binr/ragg2/ragg2.c b/binr/ragg2/ragg2.c index 2674230010..133a967e5b 100644 --- a/binr/ragg2/ragg2.c +++ b/binr/ragg2/ragg2.c @@ -58,8 +58,10 @@ int main(int argc, char **argv) { { int fd = open (optarg, O_RDWR|O_CREAT, 0644); if (fd != -1) { +#if __UNIX__ if (*format != 'r') fchmod (fd, 0755); +#endif close (1); dup2 (fd, 1); } else eprintf ("Cannot open '%s'\n", optarg); diff --git a/libr/asm/p/asm_ppc.c b/libr/asm/p/asm_ppc.c index f9200e486b..dcb72901a2 100644 --- a/libr/asm/p/asm_ppc.c +++ b/libr/asm/p/asm_ppc.c @@ -38,15 +38,22 @@ static void print_address(bfd_vma address, struct disassemble_info *info) { } static int buf_fprintf(void *stream, const char *format, ...) { + int flen, glen; va_list ap; char *tmp; if (buf_global == NULL) return 0; va_start (ap, format); - tmp = alloca (strlen (format)+strlen (buf_global)+2); - sprintf (tmp, "%s%s", buf_global, format); + flen = strlen (format); + glen = strlen (buf_global); + tmp = malloc (flen + glen + 2); + memcpy (tmp, buf_global, glen); + memcpy (tmp+glen, format, flen); + tmp[flen+glen] = 0; +// XXX: overflow here? vsprintf (buf_global, tmp, ap); va_end (ap); + free (tmp); return 0; } diff --git a/libr/db/Makefile b/libr/db/Makefile index fcf3ec87d7..b4eac32962 100644 --- a/libr/db/Makefile +++ b/libr/db/Makefile @@ -17,7 +17,9 @@ sdbclean: sdb/src/libsdb.a: #cd sdb/src ; ${MAKE} CFLAGS=-fPIC libsdb.a - cd sdb/src ; ${MAKE} CFLAGS="${CFLAGS}" LDFLAGS="${LDFLAGS}" libsdb.a + cd sdb/src ; ${MAKE} \ + RANLIB="${RANLIB}" \ + CC="${CC}" CFLAGS="${CFLAGS}" LDFLAGS="${LDFLAGS}" libsdb.a .PHONY: sdb-sync SYNCFILES=sdb.hg/README sdb.hg/config.mk sdb.hg/src diff --git a/libr/db/sdb/config.mk b/libr/db/sdb/config.mk index ca7739fb27..6822b76655 100644 --- a/libr/db/sdb/config.mk +++ b/libr/db/sdb/config.mk @@ -7,6 +7,6 @@ CFLAGS+=-DVERSION=\"${VERSION}\" CFLAGS+=-Wall #CFLAGS+=-O3 -CFLAGS+=-ggdb -g -Wall -O0 +#CFLAGS+=-ggdb -g -Wall -O0 HAVE_VALA=$(shell valac --version) diff --git a/libr/db/sdb/src/Makefile b/libr/db/sdb/src/Makefile index 8c70621443..8b040c8d18 100644 --- a/libr/db/sdb/src/Makefile +++ b/libr/db/sdb/src/Makefile @@ -1,20 +1,23 @@ include ../config.mk #CFLAGS+=-DOLDFMT=1 -CFLAGS+=-g OBJ=alloc.o cdb.o buffer.o cdb_make.o ls.o ht.o sdb.o sdbn.o lock.o # This is hacky SOEXT=dylib #SOEXT=so ARCH=$(shell uname -m) +RANLIB?=ranlib +EXEXT= + +BIN=sdb${EXEXT} .PHONY: all libsdb.${SOEXT} clean wcl syms -all: libsdb.a sdb +all: libsdb.a ${BIN} libsdb.a: ${OBJ} @#ar qf libasb.a ${OBJ} ar -r libsdb.a ${OBJ} - ranlib libsdb.a + ${RANLIB} libsdb.a libsdb.${SOEXT}: ${CC} -o $@ $(subst .o,.c,${OBJ}) -fPIC -shared -arch ${ARCH} @@ -42,8 +45,8 @@ syms: main.c: -sdb: libsdb.a main.c - ${CC} ${CFLAGS} -o sdb main.c libsdb.a +${BIN}: libsdb.a main.c + ${CC} ${CFLAGS} -o ${BIN} *.c wcl: wc -l `echo ${OBJ}|sed -e 's,\.o,.c,g'` diff --git a/libr/db/sdb/src/cdb.c b/libr/db/sdb/src/cdb.c index 62847ddfbc..b15a9541de 100644 --- a/libr/db/sdb/src/cdb.c +++ b/libr/db/sdb/src/cdb.c @@ -2,9 +2,12 @@ #include #include -#include #include "cdb.h" +#if USE_MMAN +#include +#endif + int getkvlen(int fd, ut32 *klen, ut32 *vlen) { char buf[4]; if (read (fd, buf, 4) != 4) @@ -28,7 +31,11 @@ ut32 cdb_hash(const char *buf, ut32 len) { void cdb_free(struct cdb *c) { if (!c->map) return; +#if USE_MMAN munmap (c->map, c->size); +#else + free (c->map); +#endif c->map = NULL; } @@ -42,7 +49,12 @@ void cdb_init(struct cdb *c, int fd) { cdb_findstart (c); c->fd = fd; if (!fstat (fd, &st) && st.st_size != UT32_MAX) { +#if USE_MMAN char *x = mmap (0, st.st_size, PROT_READ, MAP_SHARED, fd, 0); +#else + char *x = malloc (st.st_size); + read (fd, x, st.st_size); // TODO: handle return value +#endif if (x + 1) { c->size = st.st_size; c->map = x; diff --git a/libr/db/sdb/src/cdb.h b/libr/db/sdb/src/cdb.h index d051c66472..00f18e69a1 100644 --- a/libr/db/sdb/src/cdb.h +++ b/libr/db/sdb/src/cdb.h @@ -8,6 +8,14 @@ #define KVLSZ 4 +#if __WIN32__ || __CYGWIN__ || MINGW32 +#define ULLFMT "I64" +#define USE_MMAN 0 +#else +#define ULLFMT "ll" +#define USE_MMAN 1 +#endif + // GTFO! int getkvlen(int fd, ut32 *klen, ut32 *vlen); #define CDB_HASHSTART 5381 diff --git a/libr/db/sdb/src/lock.c b/libr/db/sdb/src/lock.c index bb97c0c8c9..1f2386a23a 100644 --- a/libr/db/sdb/src/lock.c +++ b/libr/db/sdb/src/lock.c @@ -27,6 +27,11 @@ int sdb_lock(const char *s) { return 1; } +void sdb_lock_wait(const char *s) { + while (!sdb_lock (s)) + usleep (100); // hack +} + void sdb_unlock(const char *s) { unlink (s); } diff --git a/libr/db/sdb/src/main.c b/libr/db/sdb/src/main.c index 68df2f2f4a..9d6a8c33b6 100644 --- a/libr/db/sdb/src/main.c +++ b/libr/db/sdb/src/main.c @@ -17,6 +17,7 @@ static void terminate(int sig) { exit (0); } +#if USE_MMAN static void syncronize(int sig) { // TODO: must be in sdb_sync() or wat? Sdb *n; @@ -25,6 +26,7 @@ static void syncronize(int sig) { sdb_free (s); s = n; } +#endif static int sdb_dump (const char *db) { char k[SDB_KEYSIZE]; @@ -74,12 +76,12 @@ static void runline (Sdb *s, const char *cmd) { case '+': // inc n = sdb_inc (s, cmd, 1); save = 1; - printf ("%lld\n", n); + printf ("%"ULLFMT"d\n", n); break; case '-': // dec n = sdb_inc (s, cmd, -1); save = 1; - printf ("%lld\n", n); + printf ("%"ULLFMT"d\n", n); break; default: if ((eq = strchr (cmd, '='))) { @@ -115,8 +117,10 @@ int main(int argc, char **argv) { if (argc == 2) return sdb_dump (argv[1]); +#if USE_MMAN signal (SIGINT, terminate); signal (SIGHUP, syncronize); +#endif if (!strcmp (argv[2], "=")) { createdb (argv[1]); diff --git a/libr/db/sdb/src/sdb.c b/libr/db/sdb/src/sdb.c index fb7590409a..4819cfedc3 100644 --- a/libr/db/sdb/src/sdb.c +++ b/libr/db/sdb/src/sdb.c @@ -90,7 +90,7 @@ char *sdb_get (Sdb* s, const char *key) { } int sdb_delete (Sdb* s, const char *key) { - return sdb_set (s, key, ""); + return key? sdb_set (s, key, ""): 0; } int sdb_exists (Sdb* s, const char *key) { @@ -131,7 +131,10 @@ void sdb_kv_free (struct sdb_kv *kv) { int sdb_set (Sdb* s, const char *key, const char *val) { SdbKv *kv; SdbHashEntry *e; - ut32 hash = cdb_hashstr (key); + ut32 hash; + if (!key || !val) + return 0; + hash = cdb_hashstr (key); cdb_findstart (&s->db); e = ht_search (s->ht, hash); if (e) { @@ -147,6 +150,8 @@ int sdb_set (Sdb* s, const char *key, const char *val) { // TODO: refactoring hard int sdb_add (struct cdb_make *c, const char *key, const char *data) { + if (!key || !data) + return 0; return cdb_make_add (c, key, strlen (key), data, strlen (data)); } @@ -197,7 +202,9 @@ int sdb_sync (Sdb* s) { } // printf ("db '%s' created\n", f); cdb_make_finish (&c); +#if USE_MMAN fsync (fd); +#endif close (fd); rename (ftmp, f); free (ftmp); diff --git a/libr/db/sdb/src/sdbn.c b/libr/db/sdb/src/sdbn.c index 48314a913c..850bd465b4 100644 --- a/libr/db/sdb/src/sdbn.c +++ b/libr/db/sdb/src/sdbn.c @@ -9,7 +9,7 @@ int sdb_nexists (Sdb *s, const char *key) { return c>='0' && c<='9'; } -static void strrev(char *s, int len) { +static void __strrev(char *s, int len) { int i, j = len -1; for (i=0; i 0); s[i] = '\0'; - strrev (s, i); + __strrev (s, i); } ut64 sdb_getn(Sdb *s, const char *key) { @@ -39,7 +39,7 @@ ut64 sdb_getn(Sdb *s, const char *key) { void sdb_setn(Sdb *s, const char *key, ut64 v) { char b[128]; - ulltoa (v, b); + __ulltoa (v, b); sdb_set (s, key, b); } diff --git a/libr/include/r_regex.h b/libr/include/r_regex.h index 7647c71318..bd5aa37dad 100644 --- a/libr/include/r_regex.h +++ b/libr/include/r_regex.h @@ -16,6 +16,7 @@ typedef struct r_regmatch_t { off_t rm_eo; /* end of match */ } RRegexMatch; +typedef int regoff_t; /* regcomp() flags */ #define R_REGEX_BASIC 0000 diff --git a/libr/include/r_types.h b/libr/include/r_types.h index 03fb43e2c9..acc38cd88f 100644 --- a/libr/include/r_types.h +++ b/libr/include/r_types.h @@ -14,6 +14,7 @@ #include #include #include +#include /* for O_RDONLY */ // TODO: FS or R_SYS_DIR ?? #undef FS diff --git a/libr/magic/apprentice.c b/libr/magic/apprentice.c index 3bd4196c31..d7fc04ef1a 100644 --- a/libr/magic/apprentice.c +++ b/libr/magic/apprentice.c @@ -92,7 +92,7 @@ static void bs1(struct r_magic *); static ut16 swap2(ut16); static ut32 swap4(ut32); static ut64 swap8(ut64); -static void mkdbname(const char *, char **, int); +static char *mkdbname(const char *, int); static int apprentice_map(RMagic *, struct r_magic **, ut32 *, const char *); static int apprentice_compile(RMagic *, struct r_magic **, ut32 *, const char *); static int check_format_type(const char *, int); @@ -1656,7 +1656,7 @@ static int apprentice_map(RMagic *ms, struct r_magic **magicp, ut32 *nmagicp, co char *dbname = NULL; void *mm = NULL; - mkdbname (fn, &dbname, 0); + dbname = mkdbname (fn, 0); if (dbname == NULL) goto error2; @@ -1754,7 +1754,7 @@ static int apprentice_compile(RMagic *ms, struct r_magic **magicp, ut32 *nmagicp char *dbname; int rv = -1; - mkdbname(fn, &dbname, 1); + dbname = mkdbname(fn, 1); if (dbname == NULL) goto out; @@ -1792,18 +1792,26 @@ static const char ext[] = ".mgc"; /* * make a dbname */ -static void mkdbname(const char *fn, char **buf, int strip) { +static char *mkdbname(const char *fn, int strip) { + char *buf; + int fnlen, extlen; if (strip) { const char *p; if ((p = strrchr(fn, '/')) != NULL) fn = ++p; } + fnlen = strlen (fn); + extlen = strlen (ext); + buf = malloc (fnlen + extlen); + memcpy (buf, fn, fnlen); + memcpy (buf+fnlen, ext, extlen); + buf[fnlen+extlen] = 0; - (void)asprintf(buf, "%s%s", fn, ext); - if (*buf && strlen(*buf) > MAXPATHLEN) { - free(*buf); - *buf = NULL; + if (buf && strlen (buf) > MAXPATHLEN) { + free(buf); + return NULL; } + return buf; } /* diff --git a/libr/magic/compress.c b/libr/magic/compress.c index c98fb06bdd..050a574625 100644 --- a/libr/magic/compress.c +++ b/libr/magic/compress.c @@ -33,6 +33,7 @@ * uncompress(method, old, n, newch) - uncompress old into new, * using method, return sizeof new */ +#if 0 #include "file.h" #include #include @@ -42,9 +43,13 @@ #include #include #include +#include +#if __UNIX__ #include #include -#include +#endif + +#undef FIONREAD #if defined(HAVE_ZLIB_H) && defined(HAVE_LIBZ) #define BUILTIN_DECOMPRESS @@ -435,3 +440,4 @@ err: return n; } } +#endif diff --git a/libr/magic/file.h b/libr/magic/file.h index 1854d8c159..988bc75ea0 100644 --- a/libr/magic/file.h +++ b/libr/magic/file.h @@ -43,7 +43,7 @@ #include #include /* For open and flags */ #include // TODO: use utX -#include +#include #include /* Do this here and now, because struct stat gets re-defined on solaris */ #include diff --git a/libr/magic/fsmagic.c b/libr/magic/fsmagic.c index 64e9b0ef95..c5530efc04 100644 --- a/libr/magic/fsmagic.c +++ b/libr/magic/fsmagic.c @@ -58,9 +58,13 @@ #undef HAVE_MAJOR static int bad_link(RMagic *ms, int err, char *buf) { - char *errfmt = (err == ELOOP)? +#ifdef ELOOP + const char *errfmt = (err == ELOOP)? "symbolic link in a loop": "broken symbolic link to `%s'"; +#else + const char *errfmt = "broken symbolic link to `%s'"; +#endif if (ms->flags & R_MAGIC_ERROR) { file_error (ms, err, errfmt, buf); return -1; diff --git a/libr/magic/funcs.c b/libr/magic/funcs.c index ac46c92289..adc390b25c 100644 --- a/libr/magic/funcs.c +++ b/libr/magic/funcs.c @@ -54,17 +54,26 @@ int file_printf(RMagic *ms, const char *fmt, ...) { int file_vprintf(RMagic *ms, const char *fmt, va_list ap) { va_list ap2; int len; + char cbuf[4096]; char *buf, *newstr; + int buflen;// = strlen (buf); va_copy (ap2, ap); - len = vasprintf (&buf, fmt, ap2); + len = vsnprintf (cbuf, sizeof (cbuf), fmt, ap2); va_end (ap2); if (len < 0) goto out; + cbuf[len] = 0; + buf = strdup (cbuf); + buflen = len; if (ms->o.buf != NULL) { - len = asprintf (&newstr, "%s%s", ms->o.buf, buf); - free(buf); + int obuflen = strlen (ms->o.buf); + len = obuflen+buflen+1; + newstr = malloc (len); + memcpy (newstr, ms->o.buf, obuflen); + memcpy (newstr+obuflen, buf, buflen); + free (buf); if (len < 0) goto out; free (ms->o.buf); @@ -147,6 +156,7 @@ int file_buffer(RMagic *ms, int fd, const char *inname, const void *buf, size_t return 1; } +#if 0 /* try compression stuff */ if ((ms->flags & R_MAGIC_NO_CHECK_COMPRESS) != 0 || (m = file_zmagic(ms, fd, inname, buf, nb)) == 0) { @@ -169,6 +179,7 @@ int file_buffer(RMagic *ms, int fd, const char *inname, const void *buf, size_t } } } +#endif return m; } diff --git a/libr/magic/magic.c b/libr/magic/magic.c index 7fb1f43ef6..ff59ab0132 100644 --- a/libr/magic/magic.c +++ b/libr/magic/magic.c @@ -48,7 +48,11 @@ #include /* for read() */ #endif +#if __UNIX__ #include /* for byte swapping */ +#else +#undef O_NONBLOCK +#endif #include "patchlevel.h" @@ -126,7 +130,9 @@ static const char * file_or_fd(RMagic *ms, const char *inname, int fd) { int flags = O_RDONLY|O_BINARY; if (stat (inname, &sb) == 0 && S_ISFIFO (sb.st_mode)) { +#if O_NONBLOCK flags |= O_NONBLOCK; +#endif ispipe = 1; } errno = 0; @@ -148,11 +154,13 @@ static const char * file_or_fd(RMagic *ms, const char *inname, int fd) { /* * try looking at the first HOWMANY bytes */ +#ifdef O_NONBLOCK if (ispipe) { ssize_t r = 0; - while ((r = sread(fd, (void *)&buf[nbytes], - (size_t)(HOWMANY - nbytes), 1)) > 0) { + //while ((r = sread(fd, (void *)&buf[nbytes], + while ((r = read(fd, (void *)&buf[nbytes], + (size_t)(HOWMANY - nbytes))) > 0) { nbytes += r; if (r < PIPE_BUF) break; } @@ -166,11 +174,14 @@ static const char * file_or_fd(RMagic *ms, const char *inname, int fd) { } } else { +#endif if ((nbytes = read(fd, (char *)buf, HOWMANY)) == -1) { file_error(ms, errno, "cannot read `%s'", inname); goto done; } +#ifdef O_NONBLOCK } +#endif (void)memset(buf + nbytes, 0, SLOP); /* NUL terminate */ if (file_buffer(ms, fd, inname, buf, (size_t)nbytes) == -1) diff --git a/libr/magic/softmagic.c b/libr/magic/softmagic.c index d2b3efc79d..02358094f3 100644 --- a/libr/magic/softmagic.c +++ b/libr/magic/softmagic.c @@ -303,9 +303,13 @@ static st32 mprint(RMagic *ms, struct r_magic *m) { case -1: return -1; case 1: - if (asprintf(&buf, "%c", (ut8)v) < 0) + buf = malloc (2); + if (snprintf (buf, 2, "%c", (ut8)v)<0) { return -1; - if (file_printf(ms, R_MAGIC_DESC, buf) == -1) + free (buf); + } + if (file_printf (ms, R_MAGIC_DESC, buf) == -1) + free (buf); return -1; break; default: @@ -318,15 +322,20 @@ static st32 mprint(RMagic *ms, struct r_magic *m) { case FILE_SHORT: case FILE_BESHORT: case FILE_LESHORT: - v = file_signextend(ms, m, (ut64)p->h); - switch (check_fmt(ms, m)) { + v = file_signextend (ms, m, (ut64)p->h); + switch (check_fmt (ms, m)) { case -1: return -1; case 1: - if (asprintf(&buf, "%hu", (unsigned short)v) < 0) + buf = malloc (32); + if (snprintf (buf, 32, "%hu", (unsigned short)v) < 0) { + free (buf); return -1; - if (file_printf(ms, R_MAGIC_DESC, buf) == -1) + } + if (file_printf(ms, R_MAGIC_DESC, buf) == -1) { + free (buf); return -1; + } break; default: if (file_printf(ms, R_MAGIC_DESC, (unsigned short) v) == -1) @@ -344,10 +353,15 @@ static st32 mprint(RMagic *ms, struct r_magic *m) { case -1: return -1; case 1: - if (asprintf(&buf, "%u", (ut32)v) < 0) + buf = malloc (32); + if (snprintf (buf, 32, "%u", (ut32)v) < 0) { + free (buf); return -1; - if (file_printf(ms, R_MAGIC_DESC, buf) == -1) + } + if (file_printf(ms, R_MAGIC_DESC, buf) == -1) { + free (buf); return -1; + } break; default: if (file_printf(ms, R_MAGIC_DESC, (ut32) v) == -1) @@ -424,10 +438,15 @@ static st32 mprint(RMagic *ms, struct r_magic *m) { case -1: return -1; case 1: - if (asprintf(&buf, "%g", vf) < 0) + buf = malloc (32); + if (snprintf (buf, 32, "%g", vf) < 0) { + free (buf); return -1; - if (file_printf(ms, R_MAGIC_DESC, buf) == -1) + } + if (file_printf (ms, R_MAGIC_DESC, buf) == -1) { + free (buf); return -1; + } break; default: if (file_printf(ms, R_MAGIC_DESC, vf) == -1) @@ -444,10 +463,15 @@ static st32 mprint(RMagic *ms, struct r_magic *m) { case -1: return -1; case 1: - if (asprintf(&buf, "%g", vd) < 0) + buf = malloc (32); + if (snprintf (buf, 32, "%g", vd) < 0) { + free (buf); return -1; - if (file_printf(ms, R_MAGIC_DESC, buf) == -1) + } + if (file_printf (ms, R_MAGIC_DESC, buf) == -1) { + free (buf); return -1; + } break; default: if (file_printf(ms, R_MAGIC_DESC, vd) == -1) diff --git a/libr/util/chmod.c b/libr/util/chmod.c index affce2451b..0777d66bc9 100644 --- a/libr/util/chmod.c +++ b/libr/util/chmod.c @@ -1,7 +1,6 @@ /* radare - LGPL - Copyright 2011 pancake */ #include -#if __UNIX__ #include #include #include @@ -27,6 +26,7 @@ R_API int r_file_chmod (const char *file, const char *mod, int recursive) { #endif } +#if __UNIX__ /* copied from sbase/chmod.c (suckless.org) */ int chmodr(const char *path, int rflag) { struct stat st;