* Fix mingw32 build

This commit is contained in:
pancake 2011-09-14 12:37:26 +02:00
parent 2edfc3495e
commit 096b7eb406
21 changed files with 163 additions and 47 deletions

View File

@ -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);

View File

@ -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;
}

View File

@ -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

View File

@ -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)

View File

@ -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'`

View File

@ -2,9 +2,12 @@
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include "cdb.h"
#if USE_MMAN
#include <sys/mman.h>
#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;

View File

@ -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

View File

@ -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);
}

View File

@ -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]);

View File

@ -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);

View File

@ -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<j; i++, j--) {
char c = s[i];
@ -18,12 +18,12 @@ static void strrev(char *s, int len) {
}
}
static void ulltoa(ut64 n, char *s) {
static void __ulltoa(ut64 n, char *s) {
int i = 0;
do s[i++] = n % 10 + '0';
while ((n /= 10) > 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);
}

View File

@ -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

View File

@ -14,6 +14,7 @@
#include <stdlib.h>
#include <stdarg.h>
#include <sys/time.h>
#include <fcntl.h> /* for O_RDONLY */
// TODO: FS or R_SYS_DIR ??
#undef FS

View File

@ -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;
}
/*

View File

@ -33,6 +33,7 @@
* uncompress(method, old, n, newch) - uncompress old into new,
* using method, return sizeof new
*/
#if 0
#include "file.h"
#include <stdio.h>
#include <stdlib.h>
@ -42,9 +43,13 @@
#include <string.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/time.h>
#if __UNIX__
#include <sys/ioctl.h>
#include <sys/wait.h>
#include <sys/time.h>
#endif
#undef FIONREAD
#if defined(HAVE_ZLIB_H) && defined(HAVE_LIBZ)
#define BUILTIN_DECOMPRESS
@ -435,3 +440,4 @@ err:
return n;
}
}
#endif

View File

@ -43,7 +43,7 @@
#include <errno.h>
#include <fcntl.h> /* For open and flags */
#include <inttypes.h> // TODO: use utX
#include <regex.h>
#include <r_regex.h>
#include <sys/types.h>
/* Do this here and now, because struct stat gets re-defined on solaris */
#include <sys/stat.h>

View File

@ -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;

View File

@ -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;
}

View File

@ -48,7 +48,11 @@
#include <unistd.h> /* for read() */
#endif
#if __UNIX__
#include <netinet/in.h> /* 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)

View File

@ -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)

View File

@ -1,7 +1,6 @@
/* radare - LGPL - Copyright 2011 pancake<nopcode.org> */
#include <r_util.h>
#if __UNIX__
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
@ -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;