diff --git a/modules/src/system/open.c b/modules/src/system/open.c index eed31dc13..3ec515a77 100644 --- a/modules/src/system/open.c +++ b/modules/src/system/open.c @@ -6,6 +6,7 @@ #include #include +#include #include "system.h" extern File *_get_entry(); @@ -23,11 +24,11 @@ sys_open(path, flag, filep) return 0; switch (flag) { case OP_READ: - if ((fd = open(path, 0)) < 0) + if ((fd = open(path, O_RDONLY|O_BINARY)) < 0) return 0; break; case OP_APPEND: - if ((fd = open(path, 1)) < 0) { + if ((fd = open(path, O_WRONLY|O_BINARY)) < 0) { if (access(path, 0) == 0) return 0; } @@ -40,7 +41,7 @@ sys_open(path, flag, filep) } /* Fall through */ case OP_WRITE: - if ((fd = creat(path, 0666)) < 0) + if ((fd = open(path, O_CREAT|O_WRONLY|O_BINARY, 0666)) < 0) return 0; break; default: diff --git a/modules/src/system/read.c b/modules/src/system/read.c index f84c91de9..654ff406d 100644 --- a/modules/src/system/read.c +++ b/modules/src/system/read.c @@ -7,12 +7,24 @@ #include #include "system.h" -int -sys_read(fp, bufptr, bufsiz, pnbytes) - File *fp; - char *bufptr; - int bufsiz, *pnbytes; +int sys_read(File* fp, char* bufptr, int bufsiz, int* pnbytes) { - if (! fp) return 0; - return (*pnbytes = read(fp->o_fd, bufptr, bufsiz)) >= 0; + if (!fp) + return 0; + + *pnbytes = 0; + while (bufsiz != 0) + { + int len = read(fp->o_fd, bufptr, bufsiz); + if (len < 0) + return 0; + if (len == 0) + return *pnbytes != 0; + + *pnbytes += len; + bufptr += len; + bufsiz -= len; + } + + return 1; } diff --git a/modules/src/system/write.c b/modules/src/system/write.c index 7f87ecc28..af996867c 100644 --- a/modules/src/system/write.c +++ b/modules/src/system/write.c @@ -7,12 +7,21 @@ #include #include "system.h" -int -sys_write(fp, bufptr, nbytes) - File *fp; - char *bufptr; - int nbytes; +int sys_write(File* fp, char* bufptr, int nbytes) { - if (! fp) return 0; - return write(fp->o_fd, bufptr, nbytes) == nbytes; + if (!fp) + return 0; + + while (nbytes != 0) + { + int len = write(fp->o_fd, bufptr, nbytes); + if (len < 0) + return 0; + + bufptr += len; + nbytes -= len; + } + + return 1; } + diff --git a/util/ack/files.c b/util/ack/files.c index e5f00dac3..01a20c992 100644 --- a/util/ack/files.c +++ b/util/ack/files.c @@ -54,12 +54,22 @@ int setfiles(trf *phase) { } else { gr_init(&pathname) ; if ( !phase->t_keep && !t_flag ) { + /* Unix temporary directory. */ char* tmpdir = getenv("TMPDIR"); + /* Fallback: Windows, running under MSYS2. */ + if (!tmpdir) + tmpdir = getenv("temp"); + /* Fallback: Windows, running natively. */ + if (!tmpdir) + tmpdir = getenv("TEMP"); + /* Fallback: Hard-coded directory. */ if (!tmpdir) tmpdir = "/tmp"; gr_cat(&pathname, tmpdir); gr_cat(&pathname, "/Ack-XXXXXX"); - mkstemp(pathname.gr_string); + int fd = mkstemp(pathname.gr_string); + close(fd); + remove(pathname.gr_string); out.p_keep=NO ; } else { if ( !p_basename ) { diff --git a/util/ack/main.c b/util/ack/main.c index 269e6a571..b466d6025 100644 --- a/util/ack/main.c +++ b/util/ack/main.c @@ -56,11 +56,8 @@ int main(int argc, char** argv) } if (callname) { - if (machine) - { - fuerror("can not produce code for both %s and %s", callname, machine); - } - machine = callname; + if (!machine) + machine = callname; } if (!machine && !(machine = getenv("ACKM"))) { @@ -329,6 +326,8 @@ static void firstarg(char* argp) register char* name; name = strrchr(argp, '/'); + if (!name) + name = strrchr(argp, '\\'); if (name && *(name + 1)) { name++; diff --git a/util/ack/rmach.c b/util/ack/rmach.c index 1a4cd03a1..4d3e2b669 100644 --- a/util/ack/rmach.c +++ b/util/ack/rmach.c @@ -141,6 +141,7 @@ static void intrf(void) { new->t_stdin= YES ; } else if ( strcmp(ty_name,STD_OUT)==0 ) { + fprintf(stderr, "setting stdout for %s\n", new->t_name); if ( new->t_stdout ) twice=YES ; new->t_stdout= YES ; } else diff --git a/util/ack/run.c b/util/ack/run.c index 71eea0592..baafac5e7 100644 --- a/util/ack/run.c +++ b/util/ack/run.c @@ -23,7 +23,7 @@ static char rcs_id[] = "$Id$"; #define ARG_MORE 40 /* The size of args chunks to allocate */ -static int run_exec(trf*, const char*); +static int run_exec(trf*); static void x_arg(char*); static char** arglist; /* The first argument */ @@ -81,11 +81,11 @@ int runphase(trf* phase) x_arg(l_content(*elem)); } x_arg((char*)0); - result = run_exec(phase, prog); + result = run_exec(phase); return result; } -static int run_exec(trf* phase, const char* prog) +static int run_exec(trf* phase) { int status, child, waitchild; int oldstdin = -1; @@ -101,7 +101,7 @@ static int run_exec(trf* phase, const char* prog) oldstdin = dup(0); close(0); - if (open(in.p_path, 0) != 0) + if (open(in.p_path, O_RDONLY|O_BINARY) != 0) { error("cannot open %s", in.p_path); exit(1); @@ -111,13 +111,11 @@ static int run_exec(trf* phase, const char* prog) if (phase->t_stdout) { if (!out.p_path) - { fatal("no output file for %s", phase->t_name); - } oldstdout = dup(1); close(1); - if (creat(out.p_path, 0666) != 1) + if (open(out.p_path, O_CREAT|O_WRONLY|O_BINARY, 0666) != 1) { close(1); dup(2); @@ -126,7 +124,7 @@ static int run_exec(trf* phase, const char* prog) } } - status = sys_system(prog, arglist); + status = sys_system(phase->t_prog, (const char* const*) arglist); if (oldstdin != -1) { @@ -137,7 +135,7 @@ static int run_exec(trf* phase, const char* prog) if (oldstdout != -1) { - close(1); + int i = close(1); dup2(oldstdout, 1); close(oldstdout); } @@ -145,7 +143,7 @@ static int run_exec(trf* phase, const char* prog) if (status) { if (status & 0177) - error("%s died with signal %d", prog, status & 0177); + error("%s died with signal %d", phase->t_prog, status & 0177); /* The assumption is that processes voluntarely dying with a non-zero status already produced