Make work, mostly, on Windows --- temporary files are now created in the

right place, creat() isn't used because it doesn't work, partial file
read/writes work, etc.
This commit is contained in:
David Given 2022-07-17 01:58:16 +02:00
parent 1654700314
commit cb37502a23
7 changed files with 63 additions and 33 deletions

View file

@ -6,6 +6,7 @@
#include <fcntl.h> #include <fcntl.h>
#include <unistd.h> #include <unistd.h>
#include <errno.h>
#include "system.h" #include "system.h"
extern File *_get_entry(); extern File *_get_entry();
@ -23,11 +24,11 @@ sys_open(path, flag, filep)
return 0; return 0;
switch (flag) { switch (flag) {
case OP_READ: case OP_READ:
if ((fd = open(path, 0)) < 0) if ((fd = open(path, O_RDONLY|O_BINARY)) < 0)
return 0; return 0;
break; break;
case OP_APPEND: case OP_APPEND:
if ((fd = open(path, 1)) < 0) { if ((fd = open(path, O_WRONLY|O_BINARY)) < 0) {
if (access(path, 0) == 0) if (access(path, 0) == 0)
return 0; return 0;
} }
@ -40,7 +41,7 @@ sys_open(path, flag, filep)
} }
/* Fall through */ /* Fall through */
case OP_WRITE: case OP_WRITE:
if ((fd = creat(path, 0666)) < 0) if ((fd = open(path, O_CREAT|O_WRONLY|O_BINARY, 0666)) < 0)
return 0; return 0;
break; break;
default: default:

View file

@ -7,12 +7,24 @@
#include <unistd.h> #include <unistd.h>
#include "system.h" #include "system.h"
int int sys_read(File* fp, char* bufptr, int bufsiz, int* pnbytes)
sys_read(fp, bufptr, bufsiz, pnbytes)
File *fp;
char *bufptr;
int bufsiz, *pnbytes;
{ {
if (! fp) return 0; if (!fp)
return (*pnbytes = read(fp->o_fd, bufptr, bufsiz)) >= 0; 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;
} }

View file

@ -7,12 +7,21 @@
#include <unistd.h> #include <unistd.h>
#include "system.h" #include "system.h"
int int sys_write(File* fp, char* bufptr, int nbytes)
sys_write(fp, bufptr, nbytes)
File *fp;
char *bufptr;
int nbytes;
{ {
if (! fp) return 0; if (!fp)
return write(fp->o_fd, bufptr, nbytes) == nbytes; return 0;
while (nbytes != 0)
{
int len = write(fp->o_fd, bufptr, nbytes);
if (len < 0)
return 0;
bufptr += len;
nbytes -= len;
}
return 1;
} }

View file

@ -54,12 +54,22 @@ int setfiles(trf *phase) {
} else { } else {
gr_init(&pathname) ; gr_init(&pathname) ;
if ( !phase->t_keep && !t_flag ) { if ( !phase->t_keep && !t_flag ) {
/* Unix temporary directory. */
char* tmpdir = getenv("TMPDIR"); 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) if (!tmpdir)
tmpdir = "/tmp"; tmpdir = "/tmp";
gr_cat(&pathname, tmpdir); gr_cat(&pathname, tmpdir);
gr_cat(&pathname, "/Ack-XXXXXX"); 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 ; out.p_keep=NO ;
} else { } else {
if ( !p_basename ) { if ( !p_basename ) {

View file

@ -56,10 +56,7 @@ int main(int argc, char** argv)
} }
if (callname) if (callname)
{ {
if (machine) if (!machine)
{
fuerror("can not produce code for both %s and %s", callname, machine);
}
machine = callname; machine = callname;
} }
if (!machine && !(machine = getenv("ACKM"))) if (!machine && !(machine = getenv("ACKM")))
@ -329,6 +326,8 @@ static void firstarg(char* argp)
register char* name; register char* name;
name = strrchr(argp, '/'); name = strrchr(argp, '/');
if (!name)
name = strrchr(argp, '\\');
if (name && *(name + 1)) if (name && *(name + 1))
{ {
name++; name++;

View file

@ -141,6 +141,7 @@ static void intrf(void) {
new->t_stdin= YES ; new->t_stdin= YES ;
} else } else
if ( strcmp(ty_name,STD_OUT)==0 ) { if ( strcmp(ty_name,STD_OUT)==0 ) {
fprintf(stderr, "setting stdout for %s\n", new->t_name);
if ( new->t_stdout ) twice=YES ; if ( new->t_stdout ) twice=YES ;
new->t_stdout= YES ; new->t_stdout= YES ;
} else } else

View file

@ -23,7 +23,7 @@ static char rcs_id[] = "$Id$";
#define ARG_MORE 40 /* The size of args chunks to allocate */ #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 void x_arg(char*);
static char** arglist; /* The first argument */ static char** arglist; /* The first argument */
@ -81,11 +81,11 @@ int runphase(trf* phase)
x_arg(l_content(*elem)); x_arg(l_content(*elem));
} }
x_arg((char*)0); x_arg((char*)0);
result = run_exec(phase, prog); result = run_exec(phase);
return result; return result;
} }
static int run_exec(trf* phase, const char* prog) static int run_exec(trf* phase)
{ {
int status, child, waitchild; int status, child, waitchild;
int oldstdin = -1; int oldstdin = -1;
@ -101,7 +101,7 @@ static int run_exec(trf* phase, const char* prog)
oldstdin = dup(0); oldstdin = dup(0);
close(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); error("cannot open %s", in.p_path);
exit(1); exit(1);
@ -111,13 +111,11 @@ static int run_exec(trf* phase, const char* prog)
if (phase->t_stdout) if (phase->t_stdout)
{ {
if (!out.p_path) if (!out.p_path)
{
fatal("no output file for %s", phase->t_name); fatal("no output file for %s", phase->t_name);
}
oldstdout = dup(1); oldstdout = dup(1);
close(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); close(1);
dup(2); 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) if (oldstdin != -1)
{ {
@ -137,7 +135,7 @@ static int run_exec(trf* phase, const char* prog)
if (oldstdout != -1) if (oldstdout != -1)
{ {
close(1); int i = close(1);
dup2(oldstdout, 1); dup2(oldstdout, 1);
close(oldstdout); close(oldstdout);
} }
@ -145,7 +143,7 @@ static int run_exec(trf* phase, const char* prog)
if (status) if (status)
{ {
if (status & 0177) 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 /* The assumption is that processes voluntarely
dying with a non-zero status already produced dying with a non-zero status already produced