lib9: add mode parameter to opentemp

This commit is contained in:
Russ Cox 2008-07-10 11:10:10 -04:00
parent c224dda84e
commit a58a827f2a
9 changed files with 23 additions and 34 deletions

View file

@ -391,7 +391,7 @@ extern int iounit(int);
/* extern double ldexp(double, int); <math.h> */ /* extern double ldexp(double, int); <math.h> */
extern void p9longjmp(p9jmp_buf, int); extern void p9longjmp(p9jmp_buf, int);
extern char* mktemp(char*); extern char* mktemp(char*);
extern int opentemp(char*); extern int opentemp(char*, int);
/* extern double modf(double, double*); <math.h> */ /* extern double modf(double, double*); <math.h> */
extern void p9notejmp(void*, p9jmp_buf, int); extern void p9notejmp(void*, p9jmp_buf, int);
extern void perror(const char*); extern void perror(const char*);

View file

@ -1,13 +1,13 @@
.TH OPENTEMP 3 .TH OPENTEMP 3
.SH NAME .SH NAME
opentemp \- create a uniquely-named file opentemp \- create and open a uniquely-named file
.SH SYNOPSIS .SH SYNOPSIS
.B #include <u.h> .B #include <u.h>
.br .br
.B #include <libc.h> .B #include <libc.h>
.PP .PP
.B .B
int opentemp(char *template) int opentemp(char *template, int mode)
.SH DESCRIPTION .SH DESCRIPTION
.I Opentemp .I Opentemp
replaces replaces
@ -28,8 +28,12 @@ are tried until the name of a file that does not yet exist
.IR access (2)) .IR access (2))
is generated. is generated.
.I Opentemp .I Opentemp
then creates the file for reading and writing then opens the file for the given
.I mode
and returns the file descriptor. and returns the file descriptor.
Most calls should use a mode
of
.BR ORDWR|ORCLOSE .
.PP .PP
If no such name can be generated, If no such name can be generated,
.I opentemp .I opentemp

View file

@ -7,14 +7,11 @@
#include <libc.h> #include <libc.h>
#include <bio.h> #include <bio.h>
#define opentemp idiffopentemp
int diffbflag; int diffbflag;
int diffwflag; int diffwflag;
void copy(Biobuf*, char*, Biobuf*, char*); void copy(Biobuf*, char*, Biobuf*, char*);
void idiff(Biobuf*, char*, Biobuf*, char*, Biobuf*, char*, Biobuf*, char*); void idiff(Biobuf*, char*, Biobuf*, char*, Biobuf*, char*, Biobuf*, char*);
int opentemp(char*, int, long);
void rundiff(char*, char*, int); void rundiff(char*, char*, int);
void void
@ -63,9 +60,9 @@ main(int argc, char **argv)
sysfatal("open %s: %r", argv[1]); sysfatal("open %s: %r", argv[1]);
strcpy(diffout, "/tmp/idiff.XXXXXX"); strcpy(diffout, "/tmp/idiff.XXXXXX");
fd = opentemp(diffout, ORDWR|ORCLOSE, 0); fd = opentemp(diffout, ORDWR|ORCLOSE);
strcpy(idiffout, "/tmp/idiff.XXXXXX"); strcpy(idiffout, "/tmp/idiff.XXXXXX");
ofd = opentemp(idiffout, ORDWR|ORCLOSE, 0); ofd = opentemp(idiffout, ORDWR|ORCLOSE);
rundiff(argv[0], argv[1], fd); rundiff(argv[0], argv[1], fd);
seek(fd, 0, 0); seek(fd, 0, 0);
Binit(&bdiff, fd, OREAD); Binit(&bdiff, fd, OREAD);
@ -80,22 +77,6 @@ main(int argc, char **argv)
exits(nil); exits(nil);
} }
int
opentemp(char *template, int mode, long perm)
{
int fd;
Dir d;
fd = mkstemp(template);
if(fd < 0)
sysfatal("could not create temporary file");
nulldir(&d);
d.mode = perm;
dirfwstat(fd, &d);
return fd;
}
void void
rundiff(char *arg1, char *arg2, int outfd) rundiff(char *arg1, char *arg2, int outfd)
{ {

View file

@ -32,7 +32,7 @@ initfilt(Biobuf *b, int argc, char **argv, uchar *buf, int nbuf, char *type, cha
p[1] = open("/dev/null", ORDWR); p[1] = open("/dev/null", ORDWR);
} }
ofd = opentemp(template); ofd = opentemp(template, ORDWR|ORCLOSE);
switch(fork()){ switch(fork()){
case -1: case -1:
fprint(2, "fork fails: %r\n"); fprint(2, "fork fails: %r\n");

View file

@ -96,7 +96,6 @@ int min(int, int);
void wexits(char*); void wexits(char*);
Image* xallocimage(Display*, Rectangle, ulong, int, ulong); Image* xallocimage(Display*, Rectangle, ulong, int, ulong);
int bell(void*, char*); int bell(void*, char*);
int opentemp(char *template);
Image* convert(Graphic *g); Image* convert(Graphic *g);
extern int stdinfd; extern int stdinfd;

View file

@ -53,7 +53,7 @@ spooltodisk(uchar *ibuf, int in, char **name)
char temp[40]; char temp[40];
strcpy(temp, "/tmp/pagespoolXXXXXXXXX"); strcpy(temp, "/tmp/pagespoolXXXXXXXXX");
fd = opentemp(temp); fd = opentemp(temp, ORDWR|ORCLOSE);
if(name) if(name)
*name = estrdup(temp); *name = estrdup(temp);

View file

@ -732,7 +732,7 @@ viewer(Document *dd)
} }
if(showdata(pm)) { if(showdata(pm)) {
s = estrdup("/tmp/pageplumbXXXXXXX"); s = estrdup("/tmp/pageplumbXXXXXXX");
fd = opentemp(s); fd = opentemp(s, ORDWR|ORCLOSE);
write(fd, pm->data, pm->ndata); write(fd, pm->data, pm->ndata);
/* lose fd reference on purpose; the file is open ORCLOSE */ /* lose fd reference on purpose; the file is open ORCLOSE */
} else if(pm->data[0] == '/') { } else if(pm->data[0] == '/') {

View file

@ -260,7 +260,7 @@ threadmain(int argc, char **argv)
if(tmpnam == nil) if(tmpnam == nil)
sysfatal("smprint: %r"); sysfatal("smprint: %r");
if((fd = opentemp(tmpnam)) < 0) if((fd = opentemp(tmpnam, ORDWR|ORCLOSE)) < 0)
sysfatal("opentemp %s: %r", tmpnam); sysfatal("opentemp %s: %r", tmpnam);
if(statustime) if(statustime)
print("# %T reading scores into %s\n", tmpnam); print("# %T reading scores into %s\n", tmpnam);

View file

@ -2,14 +2,19 @@
#include <libc.h> #include <libc.h>
int int
opentemp(char *template) opentemp(char *template, int mode)
{ {
int fd; int fd, fd1;
fd = mkstemp(template); fd = mkstemp(template);
if(fd < 0) if(fd < 0)
return -1; return -1;
if((fd1 = open(template, mode)) < 0){
remove(template); remove(template);
return fd; close(fd);
return -1;
}
close(fd);
return fd1;
} }