Commit Diff


commit - c224dda84efaeb28ce66e59213f3cbfde06735ac
commit + a58a827f2ae0d989102dc4d8c113b9282ef177b3
blob - aa23d30645619afa3312e1e604b363e88467ec85
blob + 0427dadf90b07743ebbf5fc4b07e7fe4b50040f4
--- include/libc.h
+++ include/libc.h
@@ -391,7 +391,7 @@ extern	int	iounit(int);
 /* extern	double	ldexp(double, int); <math.h> */
 extern	void	p9longjmp(p9jmp_buf, int);
 extern	char*	mktemp(char*);
-extern	int		opentemp(char*);
+extern	int		opentemp(char*, int);
 /* extern	double	modf(double, double*); <math.h> */
 extern	void	p9notejmp(void*, p9jmp_buf, int);
 extern	void	perror(const char*);
blob - 6177b44acc2f0d17ec96146fd01d62394b0744a6
blob + 63af2da7faf069a05014cdcaff4fcf3062959e40
--- man/man3/opentemp.3
+++ man/man3/opentemp.3
@@ -1,13 +1,13 @@
 .TH OPENTEMP 3
 .SH NAME
-opentemp \- create a uniquely-named file
+opentemp \- create and open a uniquely-named file
 .SH SYNOPSIS
 .B #include <u.h>
 .br
 .B #include <libc.h>
 .PP
 .B
-int opentemp(char *template)
+int opentemp(char *template, int mode)
 .SH DESCRIPTION
 .I Opentemp
 replaces
@@ -28,8 +28,12 @@ are tried until the name of a file that does not yet e
 .IR access (2))
 is generated.
 .I Opentemp
-then creates the file for reading and writing
+then opens the file for the given
+.I mode
 and returns the file descriptor.
+Most calls should use a mode
+of
+.BR ORDWR|ORCLOSE .
 .PP
 If no such name can be generated,
 .I opentemp
blob - 234333fa82b382c9e0e4454bd17d28d989129a2e
blob + aaf78be30cb6f4c09b64a02c4d0978ae57fe7cf8
--- src/cmd/idiff.c
+++ src/cmd/idiff.c
@@ -7,14 +7,11 @@
 #include <libc.h>
 #include <bio.h>
 
-#define opentemp idiffopentemp
-
 int diffbflag;
 int diffwflag;
 
 void copy(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
@@ -63,9 +60,9 @@ main(int argc, char **argv)
 		sysfatal("open %s: %r", argv[1]);
 
 	strcpy(diffout, "/tmp/idiff.XXXXXX");
-	fd = opentemp(diffout, ORDWR|ORCLOSE, 0);
+	fd = opentemp(diffout, ORDWR|ORCLOSE);
 	strcpy(idiffout, "/tmp/idiff.XXXXXX");
-	ofd = opentemp(idiffout, ORDWR|ORCLOSE, 0);
+	ofd = opentemp(idiffout, ORDWR|ORCLOSE);
 	rundiff(argv[0], argv[1], fd);
 	seek(fd, 0, 0);
 	Binit(&bdiff, fd, OREAD);
@@ -80,22 +77,6 @@ main(int argc, char **argv)
 	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
 rundiff(char *arg1, char *arg2, int outfd)
 {
blob - 2e0482fcdee6eb6f65949493f180824d536f2a07
blob + f9b2a8a2d09184d57e282d85d645ad317fe6bc13
--- src/cmd/page/filter.c
+++ src/cmd/page/filter.c
@@ -32,7 +32,7 @@ initfilt(Biobuf *b, int argc, char **argv, uchar *buf,
 		p[1] = open("/dev/null", ORDWR);
 	}
 
-	ofd = opentemp(template);
+	ofd = opentemp(template, ORDWR|ORCLOSE);
 	switch(fork()){
 	case -1:
 		fprint(2, "fork fails: %r\n");
blob - 7e6c526eb1b4d54353876983c54b5f7846a3d36e
blob + c31c1b924b20c2bdb4b39dc39cb61b688ec6d19c
--- src/cmd/page/page.h
+++ src/cmd/page/page.h
@@ -96,7 +96,6 @@ int	min(int, int);
 void	wexits(char*);
 Image*	xallocimage(Display*, Rectangle, ulong, int, ulong);
 int	bell(void*, char*);
-int	opentemp(char *template);
 Image*	convert(Graphic *g);
 
 extern int stdinfd;
blob - 1f6fdeffddae8ed83822daf6aa8b50b7833e9b34
blob + 74dd9bfefdaa56ac52c54ac29c871ec407d88783
--- src/cmd/page/util.c
+++ src/cmd/page/util.c
@@ -53,7 +53,7 @@ spooltodisk(uchar *ibuf, int in, char **name)
 	char temp[40];
 
 	strcpy(temp, "/tmp/pagespoolXXXXXXXXX");
-	fd = opentemp(temp);
+	fd = opentemp(temp, ORDWR|ORCLOSE);
 	if(name)
 		*name = estrdup(temp);
 
blob - ab409cc7920702da8cd8f8f2f26cbbfcfba9fc3f
blob + 8fc6fa793ecce7beecd791a089fb43ad0f9d2b71
--- src/cmd/page/view.c
+++ src/cmd/page/view.c
@@ -732,7 +732,7 @@ viewer(Document *dd)
 			}
 			if(showdata(pm)) {
 				s = estrdup("/tmp/pageplumbXXXXXXX");
-				fd = opentemp(s);
+				fd = opentemp(s, ORDWR|ORCLOSE);
 				write(fd, pm->data, pm->ndata);
 				/* lose fd reference on purpose; the file is open ORCLOSE */
 			} else if(pm->data[0] == '/') {
blob - 07000b1fe5e88f18c8c189b8f0c8bebdfbd422f6
blob + db26bc32226d6bb865ab2587f219117a5b39bbbf
--- src/cmd/vbackup/vbackup.c
+++ src/cmd/vbackup/vbackup.c
@@ -260,7 +260,7 @@ threadmain(int argc, char **argv)
 		if(tmpnam == nil)
 			sysfatal("smprint: %r");
 	
-		if((fd = opentemp(tmpnam)) < 0)
+		if((fd = opentemp(tmpnam, ORDWR|ORCLOSE)) < 0)
 			sysfatal("opentemp %s: %r", tmpnam);
 		if(statustime)
 			print("# %T reading scores into %s\n", tmpnam);
blob - f90bf771b67e5a0b18c94065c930368dd3f9c236
blob + 9d4e2d077235c8b6f56a1d0c30837543c1dc6659
--- src/lib9/opentemp.c
+++ src/lib9/opentemp.c
@@ -2,14 +2,19 @@
 #include <libc.h>
 
 int
-opentemp(char *template)
+opentemp(char *template, int mode)
 {
-	int fd;
+	int fd, fd1;
 
 	fd = mkstemp(template);
 	if(fd < 0)
 		return -1;
-	remove(template);
-	return fd;
+	if((fd1 = open(template, mode)) < 0){
+		remove(template);
+		close(fd);
+		return -1;
+	}
+	close(fd);
+	return fd1;
 }