Get fed up with trying to make mkstemp work and wrote my own function
which does what I want.
This commit is contained in:
parent
0cadd6a299
commit
0cd26b1d0c
28
modules/src/system/aprintf.c
Normal file
28
modules/src/system/aprintf.c
Normal file
|
@ -0,0 +1,28 @@
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <stdarg.h>
|
||||||
|
#include "system.h"
|
||||||
|
|
||||||
|
char* aprintf(const char* fmt, ...)
|
||||||
|
{
|
||||||
|
int n;
|
||||||
|
char* p;
|
||||||
|
va_list ap;
|
||||||
|
|
||||||
|
va_start(ap, fmt);
|
||||||
|
n = vsnprintf(NULL, 0, fmt, ap) + 1;
|
||||||
|
va_end(ap);
|
||||||
|
|
||||||
|
p = malloc(n);
|
||||||
|
if (!p)
|
||||||
|
return NULL;
|
||||||
|
|
||||||
|
va_start(ap, fmt);
|
||||||
|
vsnprintf(p, n, fmt, ap);
|
||||||
|
va_end(ap);
|
||||||
|
|
||||||
|
return p;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* vim: set sw=4 ts=4 expandtab : */
|
||||||
|
|
|
@ -1,17 +1,30 @@
|
||||||
clibrary {
|
clibrary {
|
||||||
name = "lib",
|
name = "lib",
|
||||||
srcs = {
|
srcs = {
|
||||||
"./access.c", "./break.c", "./chmode.c", "./close.c",
|
"./access.c",
|
||||||
"./create.c", "./filesize.c","./basename.c","./tmpnam.c",
|
"./aprintf.c",
|
||||||
--"./lock.c",
|
"./basename.c",
|
||||||
"./modtime.c", "./open.c", "./read.c", "./remove.c",
|
"./break.c",
|
||||||
"./rename.c", "./seek.c", "./stop.c", "./system.c",
|
"./chmode.c",
|
||||||
--"./unlock.c”,
|
"./close.c",
|
||||||
"./write.c",
|
"./create.c",
|
||||||
"./tmpdir.c",
|
"./filesize.c",
|
||||||
"./syssystem.c",
|
"./maketempfile.c",
|
||||||
"./strndup.c",
|
"./modtime.c",
|
||||||
|
"./open.c",
|
||||||
|
"./read.c",
|
||||||
|
"./remove.c",
|
||||||
|
"./rename.c",
|
||||||
|
"./seek.c",
|
||||||
"./setbinarymode.c",
|
"./setbinarymode.c",
|
||||||
|
"./stop.c",
|
||||||
|
"./strndup.c",
|
||||||
|
"./syssystem.c",
|
||||||
|
"./system.c",
|
||||||
|
"./tmpdir.c",
|
||||||
|
"./write.c",
|
||||||
|
--"./lock.c",
|
||||||
|
--"./unlock.c”,
|
||||||
},
|
},
|
||||||
hdrs = { "./system.h" },
|
hdrs = { "./system.h" },
|
||||||
}
|
}
|
||||||
|
|
52
modules/src/system/maketempfile.c
Normal file
52
modules/src/system/maketempfile.c
Normal file
|
@ -0,0 +1,52 @@
|
||||||
|
/* Copyright (c) 2022. See the file License in
|
||||||
|
* the root directory for more information.
|
||||||
|
*/
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <time.h>
|
||||||
|
#include <fcntl.h>
|
||||||
|
#include <errno.h>
|
||||||
|
#include "system.h"
|
||||||
|
|
||||||
|
#if defined WIN32
|
||||||
|
static unsigned temper(unsigned x)
|
||||||
|
{
|
||||||
|
x ^= x>>11;
|
||||||
|
x ^= x<<7 & 0x9D2C5680;
|
||||||
|
x ^= x<<15 & 0xEFC60000;
|
||||||
|
x ^= x>>18;
|
||||||
|
return x;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int rand_r(unsigned *seed)
|
||||||
|
{
|
||||||
|
return temper(*seed = *seed * 1103515245 + 12345)/2;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
char* sys_maketempfile(const char* prefix, const char* suffix)
|
||||||
|
{
|
||||||
|
struct timespec ts;
|
||||||
|
clock_gettime(CLOCK_REALTIME, &ts);
|
||||||
|
|
||||||
|
unsigned int seed = (unsigned int)(uintptr_t)prefix
|
||||||
|
^ (unsigned int)ts.tv_sec ^ (unsigned int)ts.tv_nsec;
|
||||||
|
const char* tempdir = sys_gettmpdir();
|
||||||
|
|
||||||
|
for (;;)
|
||||||
|
{
|
||||||
|
unsigned int hash = rand_r(&seed);
|
||||||
|
char* filename = aprintf("%s/ack-%s-%x%s",
|
||||||
|
tempdir, prefix, hash,
|
||||||
|
suffix);
|
||||||
|
int fd = open(filename, O_CREAT|O_EXCL|O_RDWR, 0600);
|
||||||
|
if (fd != -1)
|
||||||
|
{
|
||||||
|
close(fd);
|
||||||
|
return filename;
|
||||||
|
}
|
||||||
|
if (errno != EEXIST)
|
||||||
|
perror("could not create temporary file");
|
||||||
|
free(filename);
|
||||||
|
}
|
||||||
|
}
|
|
@ -78,11 +78,11 @@ time_t sys_modtime(char *);
|
||||||
*
|
*
|
||||||
* Supports both DOS and UNIX style paths.
|
* Supports both DOS and UNIX style paths.
|
||||||
* */
|
* */
|
||||||
void sys_basename(const char *str, register char *dst);
|
extern void sys_basename(const char *str, register char *dst);
|
||||||
|
|
||||||
/* Creates a temporary filename. This has
|
/* Creates a temporary filename, makes it, and returns a malloc'd string
|
||||||
* the same semantics as ISO C90 tmpnam() */
|
* containing the filename. */
|
||||||
char* sys_tmpnam(char *buffer);
|
extern char* sys_maketempfile(const char* prefix, const char* suffix);
|
||||||
|
|
||||||
#if defined WIN32
|
#if defined WIN32
|
||||||
/* Really? */
|
/* Really? */
|
||||||
|
@ -93,4 +93,8 @@ extern char* strndup(const char* s, size_t n);
|
||||||
* sane systems. */
|
* sane systems. */
|
||||||
extern void sys_setbinarymode(FILE* fp);
|
extern void sys_setbinarymode(FILE* fp);
|
||||||
|
|
||||||
|
/* As system sprintf(), except uses malloc() to allocate a new buffer of the
|
||||||
|
* right size for the result. */
|
||||||
|
extern char* aprintf(const char* format, ...);
|
||||||
|
|
||||||
#endif /* __SYSTEM_INCLUDED__ */
|
#endif /* __SYSTEM_INCLUDED__ */
|
||||||
|
|
|
@ -1,16 +0,0 @@
|
||||||
/* Copyright (c) 2019. See the file License in
|
|
||||||
* the root directory for more information.
|
|
||||||
*
|
|
||||||
* Created on: 2019-03-13
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
#include <stdio.h>
|
|
||||||
|
|
||||||
/* This has been placed here, because on some famous platforms, this
|
|
||||||
* call is completely broken (e.g Windows up to recent versions of CRT)
|
|
||||||
*/
|
|
||||||
char* sys_tmpnam(char *buffer)
|
|
||||||
{
|
|
||||||
return tmpnam(buffer);
|
|
||||||
}
|
|
||||||
|
|
|
@ -18,10 +18,9 @@
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
#include <unistd.h>
|
||||||
#if defined WIN32
|
#if defined WIN32
|
||||||
#include <io.h>
|
#include <io.h>
|
||||||
#else
|
|
||||||
#include <unistd.h>
|
|
||||||
#endif
|
#endif
|
||||||
# include "extern.h"
|
# include "extern.h"
|
||||||
# include "types.h"
|
# include "types.h"
|
||||||
|
|
|
@ -53,14 +53,11 @@ int setfiles(trf *phase) {
|
||||||
out.p_keeps=NO ;
|
out.p_keeps=NO ;
|
||||||
out.p_keep=YES ;
|
out.p_keep=YES ;
|
||||||
} else {
|
} else {
|
||||||
gr_init(&pathname) ;
|
|
||||||
if ( !phase->t_keep && !t_flag ) {
|
if ( !phase->t_keep && !t_flag ) {
|
||||||
gr_cat(&pathname, sys_gettmpdir());
|
out.p_path = sys_maketempfile("ack", phase->t_out);
|
||||||
gr_cat(&pathname, "/Ack-XXXXXX");
|
|
||||||
int fd = mkstemp(pathname.gr_string);
|
|
||||||
close(fd);
|
|
||||||
out.p_keep=NO ;
|
out.p_keep=NO ;
|
||||||
} else {
|
} else {
|
||||||
|
gr_init(&pathname) ;
|
||||||
if ( !p_basename ) {
|
if ( !p_basename ) {
|
||||||
gr_cat(&pathname,"Ack") ;
|
gr_cat(&pathname,"Ack") ;
|
||||||
p_basename=keeps(gr_start(pathname)) ;
|
p_basename=keeps(gr_start(pathname)) ;
|
||||||
|
@ -69,10 +66,10 @@ int setfiles(trf *phase) {
|
||||||
} else {
|
} else {
|
||||||
gr_cat(&pathname,p_basename) ;
|
gr_cat(&pathname,p_basename) ;
|
||||||
}
|
}
|
||||||
out.p_keep=YES ;
|
|
||||||
}
|
|
||||||
gr_cat(&pathname,phase->t_out) ;
|
gr_cat(&pathname,phase->t_out) ;
|
||||||
out.p_path= gr_final(&pathname) ;
|
out.p_path= gr_final(&pathname) ;
|
||||||
|
out.p_keep=YES ;
|
||||||
|
}
|
||||||
out.p_keeps= YES ;
|
out.p_keeps= YES ;
|
||||||
}
|
}
|
||||||
scanlist( l_first(arguments), elem) {
|
scanlist( l_first(arguments), elem) {
|
||||||
|
|
|
@ -17,7 +17,7 @@
|
||||||
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
char tname[L_tmpnam];
|
char* tname;
|
||||||
FILE *tf;
|
FILE *tf;
|
||||||
struct outhead buf;
|
struct outhead buf;
|
||||||
int readerror, writeerror;
|
int readerror, writeerror;
|
||||||
|
@ -30,12 +30,7 @@ int main(int argc, char **argv)
|
||||||
{
|
{
|
||||||
int status;
|
int status;
|
||||||
|
|
||||||
if (sys_tmpnam(tname)==NULL)
|
tname = sys_maketempfile("ack", "dat");
|
||||||
{
|
|
||||||
fprintf(stderr, "astrip: cannot create temporary filename\n");
|
|
||||||
return(1);
|
|
||||||
}
|
|
||||||
fclose(fopen(tname,"wb"));
|
|
||||||
while(--argc) {
|
while(--argc) {
|
||||||
if ((status = strip(argv[argc])) > 1)
|
if ((status = strip(argv[argc])) > 1)
|
||||||
break;
|
break;
|
||||||
|
|
|
@ -120,8 +120,7 @@ char io_buffer[IO_SIZE];
|
||||||
|
|
||||||
char *progname;
|
char *progname;
|
||||||
|
|
||||||
char temp_buf[L_tmpnam];
|
char *temp_arch;
|
||||||
char *temp_arch = &temp_buf[0];
|
|
||||||
|
|
||||||
void do_object(FILE* f, long size);
|
void do_object(FILE* f, long size);
|
||||||
void do_names(struct outhead *headp);
|
void do_names(struct outhead *headp);
|
||||||
|
@ -348,10 +347,7 @@ int main(int argc, char *argv[])
|
||||||
distr_time = statbuf.st_mtime;
|
distr_time = statbuf.st_mtime;
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
if (sys_tmpnam(temp_arch) == NULL)
|
temp_arch = sys_maketempfile("aal", "dat");
|
||||||
{
|
|
||||||
error(TRUE, "Cannot create a temporary filename\n", NULL);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (app_fl + ex_fl + del_fl + rep_fl + show_fl + pr_fl != 1)
|
if (app_fl + ex_fl + del_fl + rep_fl + show_fl + pr_fl != 1)
|
||||||
usage();
|
usage();
|
||||||
|
|
|
@ -17,7 +17,7 @@
|
||||||
* this file contains several library routines.
|
* this file contains several library routines.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
static char filename[L_tmpnam];
|
static char* filename;
|
||||||
|
|
||||||
|
|
||||||
/* VARARGS1 */
|
/* VARARGS1 */
|
||||||
|
@ -384,9 +384,5 @@ void set_mode(int mode)
|
||||||
|
|
||||||
char* tmpfil(void)
|
char* tmpfil(void)
|
||||||
{
|
{
|
||||||
if (sys_tmpnam(filename)==NULL)
|
return sys_maketempfile("ack-ass", "dat");
|
||||||
{
|
|
||||||
fatal("Cannot create temporary filename.");
|
|
||||||
}
|
|
||||||
return filename;
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -59,7 +59,7 @@ static const struct
|
||||||
#define MAXARGS 1024 /* mar # of args */
|
#define MAXARGS 1024 /* mar # of args */
|
||||||
#define NTEMPS 4 /* # of temporary files; not tunable */
|
#define NTEMPS 4 /* # of temporary files; not tunable */
|
||||||
|
|
||||||
static char tmpbase[PATH_MAX];
|
static char* tmpbase;
|
||||||
static char ddump[PATH_MAX]; /* data label dump file */
|
static char ddump[PATH_MAX]; /* data label dump file */
|
||||||
static char pdump[PATH_MAX]; /* procedure name dump file */
|
static char pdump[PATH_MAX]; /* procedure name dump file */
|
||||||
static char tmpbufs[NTEMPS * 2][PATH_MAX];
|
static char tmpbufs[NTEMPS * 2][PATH_MAX];
|
||||||
|
@ -389,11 +389,7 @@ int main(int argc, char* argv[])
|
||||||
fatal("no correct -P flag given");
|
fatal("no correct -P flag given");
|
||||||
}
|
}
|
||||||
|
|
||||||
{
|
tmpbase = sys_maketempfile("ego", "");
|
||||||
strcpy(tmpbase, sys_gettmpdir());
|
|
||||||
strcat(tmpbase, "/ego.XXXXXX");
|
|
||||||
close(mkstemp(tmpbase));
|
|
||||||
}
|
|
||||||
|
|
||||||
strcpy(ddump, tmpbase);
|
strcpy(ddump, tmpbase);
|
||||||
strcpy(pdump, tmpbase);
|
strcpy(pdump, tmpbase);
|
||||||
|
|
|
@ -38,12 +38,12 @@ calcnt_p cchead; /* call-count info of current proc */
|
||||||
STATIC long space = 0;
|
STATIC long space = 0;
|
||||||
STATIC long total_size = 0;
|
STATIC long total_size = 0;
|
||||||
|
|
||||||
STATIC char cname[PATH_MAX];
|
STATIC char* cname;
|
||||||
STATIC char ccname[PATH_MAX];
|
STATIC char* ccname;
|
||||||
STATIC char cname2[PATH_MAX];
|
STATIC char* cname2;
|
||||||
|
|
||||||
/* For debugging only */
|
/* For debugging only */
|
||||||
STATIC char sname[PATH_MAX];
|
STATIC char* sname;
|
||||||
STATIC int kp_temps = 0;
|
STATIC int kp_temps = 0;
|
||||||
|
|
||||||
int Ssubst;
|
int Ssubst;
|
||||||
|
@ -337,20 +337,11 @@ char* argv[];
|
||||||
go(argc, argv, no_action, no_action, no_action, il_flags);
|
go(argc, argv, no_action, no_action, no_action, il_flags);
|
||||||
il_extptab(fproc); /* add extended data structures */
|
il_extptab(fproc); /* add extended data structures */
|
||||||
|
|
||||||
strcpy(cname, tmpdir);
|
cname = sys_maketempfile("il", "i1");
|
||||||
strcpy(ccname, tmpdir);
|
ccname = sys_maketempfile("il", "i2");
|
||||||
strcpy(sname, tmpdir);
|
sname = sys_maketempfile("il", "i3");
|
||||||
strcpy(cname2, tmpdir);
|
cname2 = sys_maketempfile("il", "i4");
|
||||||
|
|
||||||
strcat(cname, "/ego.i1.XXXXXX");
|
|
||||||
strcat(ccname, "/ego.i2.XXXXXX");
|
|
||||||
strcat(sname, "/ego.i3.XXXXXX");
|
|
||||||
strcat(cname2, "/ego.i4.XXXXXX");
|
|
||||||
|
|
||||||
close(mkstemp(cname));
|
|
||||||
close(mkstemp(ccname));
|
|
||||||
close(mkstemp(sname));
|
|
||||||
close(mkstemp(cname2));
|
|
||||||
pass1(files->lname_in, files->bname_in, cname); /* grep calls, analyse procedures */
|
pass1(files->lname_in, files->bname_in, cname); /* grep calls, analyse procedures */
|
||||||
space = total_size * space / 100;
|
space = total_size * space / 100;
|
||||||
pass2(cname, space); /* select calls to be expanded */
|
pass2(cname, space); /* select calls to be expanded */
|
||||||
|
|
|
@ -16,7 +16,7 @@ extern bool repl_longmuls;
|
||||||
extern byte em_flag[];
|
extern byte em_flag[];
|
||||||
extern line_p instrs,pseudos;
|
extern line_p instrs,pseudos;
|
||||||
extern FILE *outfile;
|
extern FILE *outfile;
|
||||||
extern char tempname[];
|
extern char *tempname;
|
||||||
extern offset wordsize;
|
extern offset wordsize;
|
||||||
extern offset pointersize;
|
extern offset pointersize;
|
||||||
extern char *progname;
|
extern char *progname;
|
||||||
|
|
|
@ -48,11 +48,7 @@ void fileinit(void)
|
||||||
error("wrong input file");
|
error("wrong input file");
|
||||||
if (Lflag)
|
if (Lflag)
|
||||||
{
|
{
|
||||||
|
tempname = sys_maketempfile("opt", "dat");
|
||||||
if (sys_tmpnam(tempname)==NULL)
|
|
||||||
{
|
|
||||||
error("can't create temporary file.");
|
|
||||||
}
|
|
||||||
outfile = fopen(tempname, "wb");
|
outfile = fopen(tempname, "wb");
|
||||||
if (outfile == NULL)
|
if (outfile == NULL)
|
||||||
error("can't create %s", tempname);
|
error("can't create %s", tempname);
|
||||||
|
|
|
@ -22,7 +22,7 @@ bool repl_longmuls = 0; /* replacing longmuls as well? */
|
||||||
line_p instrs,pseudos; /* pointers to chains */
|
line_p instrs,pseudos; /* pointers to chains */
|
||||||
sym_p symhash[NSYMHASH]; /* array of pointers to chains */
|
sym_p symhash[NSYMHASH]; /* array of pointers to chains */
|
||||||
FILE *outfile;
|
FILE *outfile;
|
||||||
char tempname[L_tmpnam];
|
char *tempname;
|
||||||
offset wordsize = 0;
|
offset wordsize = 0;
|
||||||
offset pointersize = 0;
|
offset pointersize = 0;
|
||||||
char *progname;
|
char *progname;
|
||||||
|
|
Loading…
Reference in a new issue