pancake wrote:
> we need in libr/util/file.c a function named "r_file_mktemp". that
> should work on osx, w32 and other *nixes... theorically this is
> POSIX, but we all know how Windows follows the standards..so this
> needs to be wrapped.
Here it is. Calling convention is
int r_file_mkstemp ( const char * prefix, char **oname )
so that the output string name is passed back in oname. This means that
the caller doesn't need to know in advance how long the string must be
(the temp directory can be a few deep in w32), but does need to free()
it again afterwards.
Glyn
diff -r 01af0018db49 libr/asm/p/asm_x86_nasm.c
--- a/libr/asm/p/asm_x86_nasm.c Mon Mar 14 09:08:24 2011 +0100
+++ b/libr/asm/p/asm_x86_nasm.c Wed Mar 16 19:50:10 2011 +0000
@@ -6,42 +6,38 @@
#include <r_asm.h>
static int assemble(RAsm *a, RAsmOp *op, const char *buf) {
-#if __APPLE__
- char path_r_nasm[] = "/tmp/r_nasm-XXXXXX";
- int fd_r_nasm;
+ char *path_r_nasm_in, *path_r_nasm_out;
+ int fd_r_nasm_in, fd_r_nasm_out;
char asm_buf[R_ASM_BUFSIZE];
-#endif
- char cmd[R_ASM_BUFSIZE];
- ut8 *out;
int len = 0;
if (a->syntax != R_ASM_SYNTAX_INTEL) {
eprintf ("asm.x86.nasm does not support non-intel syntax\n");
return -1;
}
-#if __APPLE__
- fd_r_nasm = mkstemp (path_r_nasm);
- snprintf (asm_buf, sizeof (asm_buf),
- "BITS %i\nORG 0x%"PFMT64x"\n%s\n__", a->bits, a->pc,
buf);
- write (fd_r_nasm, asm_buf, sizeof (asm_buf));
- close (fd_r_nasm);
- snprintf (cmd, sizeof (cmd), "nasm %s -o /dev/stdout 2>/dev/null\n",
path_r_nasm);
-#else
- snprintf (cmd, sizeof (cmd),
- "nasm /dev/stdin -o /dev/stdout 2>/dev/null <<__\n"
- "BITS %i\nORG 0x%"PFMT64x"\n%s\n__", a->bits, a->pc,
buf);
-#endif
- out = (ut8 *)r_sys_cmd_str (cmd, "", &len);
-#if __APPLE__
- unlink (path_r_nasm);
-#endif
- if (out && memcmp (out, "/dev/stdin:", len>11?11:len)) {
- memcpy (op->buf, out, len<=R_ASM_BUFSIZE?len:R_ASM_BUFSIZE);
+ fd_r_nasm_in = r_file_mkstemp ( "r_nasm", &path_r_nasm_in );
+ fd_r_nasm_out = r_file_mkstemp ( "r_nasm", &path_r_nasm_out );
+
+ len = snprintf (asm_buf, sizeof (asm_buf),
+ "BITS %i\nORG 0x%"PFMT64x"\n%s", a->bits, a->pc, buf);
+ write (fd_r_nasm_in, asm_buf, len);
+
+ close (fd_r_nasm_in);
+
+ if ( !r_sys_cmdf ("nasm %s -o %s", path_r_nasm_in, path_r_nasm_out )) {
+ len = read (fd_r_nasm_out, op->buf, R_ASM_BUFSIZE);
} else {
eprintf ("Error running 'nasm'\n");
len = 0;
}
- if (out) free (out);
+
+ close (fd_r_nasm_out);
+
+ unlink (path_r_nasm_in);
+ unlink (path_r_nasm_out);
+ free (path_r_nasm_in);
+ free (path_r_nasm_out);
+
op->inst_len = len;
return len;
}
diff -r 01af0018db49 libr/include/r_anal.h
--- a/libr/include/r_anal.h Mon Mar 14 09:08:24 2011 +0100
+++ b/libr/include/r_anal.h Wed Mar 16 19:50:10 2011 +0000
@@ -331,7 +331,7 @@
R_ANAL_REF_TYPE_CODE = 'c', // code ref
R_ANAL_REF_TYPE_CALL = 'C', // code ref (call)
R_ANAL_REF_TYPE_DATA = 'd' // mem ref
-} RAnalRefType;
+};
typedef struct r_anal_ref_t {
int type;
diff -r 01af0018db49 libr/include/r_util.h
--- a/libr/include/r_util.h Mon Mar 14 09:08:24 2011 +0100
+++ b/libr/include/r_util.h Wed Mar 16 19:50:10 2011 +0000
@@ -293,6 +293,7 @@
R_API boolt r_file_rm(const char *file);
R_API boolt r_file_exist(const char *str);
R_API char *r_file_slurp_line(const char *file, int line, int context);
+R_API int r_file_mkstemp(const char *prefix, char **oname);
R_API ut64 r_sys_now();
R_API RList *r_sys_dir(const char *path);
diff -r 01af0018db49 libr/util/file.c
--- a/libr/util/file.c Mon Mar 14 09:08:24 2011 +0100
+++ b/libr/util/file.c Wed Mar 16 19:50:10 2011 +0000
@@ -8,8 +8,8 @@
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
+#include <fcntl.h>
#if __UNIX__
-#include <fcntl.h>
#include <sys/mman.h>
#endif
@@ -283,3 +283,31 @@
close (m->fd);
free (m);
}
+
+R_API int r_file_mkstemp (const char *prefix, char **oname) {
+ int h;
+ const char *path;
+ char *name = malloc( 1024 );
+
+#if __WINDOWS__
+ path = r_sys_getenv ("TEMP");
+ if ( GetTempFileName( path, prefix, 0, name ) ) {
+ h = open ( name, O_RDWR|O_EXCL );
+ } else {
+ h = -1;
+ }
+
+#else
+ path = r_sys_getenv ("TMPDIR");
+ if ( path==NULL )
+ path = "/tmp";
+
+ snprintf( name, 1024, "%s/%sXXXXXX", path, prefix );
+ h = mkstemp (name);
+#endif
+
+ if (oname && h!=-1) *oname = name;
+ else free (name);
+
+ return h;
+}
_______________________________________________
radare mailing list
[email protected]
http://lists.nopcode.org/listinfo.cgi/radare-nopcode.org