1 /* 2 * Copyright (c) 1988 Regents of the University of California. 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms are permitted 6 * provided that the above copyright notice and this paragraph are 7 * duplicated in all such forms and that any documentation, 8 * advertising materials, and other materials related to such 9 * distribution and use acknowledge that the software was developed 10 * by the University of California, Berkeley. The name of the 11 * University may not be used to endorse or promote products derived 12 * from this software without specific prior written permission. 13 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR 14 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED 15 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. 16 */ 17 18 #if defined(LIBC_SCCS) && !defined(lint) 19 static char sccsid[] = "@(#)tempnam.c 4.6 (Berkeley) 07/23/88"; 20 #endif /* LIBC_SCCS and not lint */ 21 22 #include <sys/param.h> 23 #include <stdio.h> 24 25 #define P_tmpdir "/usr/tmp" 26 27 FILE * 28 tmpfile() 29 { 30 FILE *fp; 31 char *f, *tmpnam(); 32 33 if (!(f = tmpnam((char *)NULL)) || !(fp = fopen(f, "w+"))) { 34 fprintf(stderr, "tmpfile: cannot open %s.\n", f); 35 return(NULL); 36 } 37 (void)unlink(f); 38 return(fp); 39 } 40 41 char * 42 tmpnam(s) 43 char *s; 44 { 45 char *malloc(), *mktemp(); 46 47 if (!s && !(s = malloc((u_int)MAXPATHLEN))) 48 return(NULL); 49 (void)sprintf(s, "%s/XXXXXX", P_tmpdir); 50 return(mktemp(s)); 51 } 52 53 char * 54 tempnam(dir, pfx) 55 char *dir, *pfx; 56 { 57 char *f, *name, *getenv(), *malloc(), *mktemp(); 58 59 if (!(name = malloc((u_int)MAXPATHLEN))) 60 return(NULL); 61 62 if (f = getenv("TMPDIR")) { 63 (void)sprintf(name, "%s/%sXXXXXX", f, pfx ? "" : pfx); 64 if (f = mktemp(name)) 65 return(f); 66 } 67 if (dir) { 68 (void)sprintf(name, "%s/%sXXXXXX", dir, pfx ? "" : pfx); 69 if (f = mktemp(name)) 70 return(f); 71 } 72 (void)sprintf(name, "%s/%sXXXXXX", P_tmpdir, pfx ? "" : pfx); 73 if (f = mktemp(name)) 74 return(f); 75 (void)sprintf(name, "/tmp/%sXXXXXX", pfx ? "" : pfx); 76 return(mktemp(name)); 77 } 78