Commit | Line | Data |
---|---|---|
503553be | 1 | /* |
2267ab9a KB |
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 | |
50c7758a KB |
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. | |
503553be KM |
16 | */ |
17 | ||
2ce81398 | 18 | #if defined(LIBC_SCCS) && !defined(lint) |
49b91625 | 19 | static char sccsid[] = "@(#)tempnam.c 4.6 (Berkeley) %G%"; |
2267ab9a | 20 | #endif /* LIBC_SCCS and not lint */ |
503553be | 21 | |
2267ab9a | 22 | #include <sys/param.h> |
2267ab9a KB |
23 | #include <stdio.h> |
24 | ||
25 | #define P_tmpdir "/usr/tmp" | |
26 | ||
27 | FILE * | |
28 | tmpfile() | |
29 | { | |
30 | FILE *fp; | |
49b91625 | 31 | char *f, *tmpnam(); |
2267ab9a | 32 | |
49b91625 KB |
33 | if (!(f = tmpnam((char *)NULL)) || !(fp = fopen(f, "w+"))) { |
34 | fprintf(stderr, "tmpfile: cannot open %s.\n", f); | |
2267ab9a KB |
35 | return(NULL); |
36 | } | |
37 | (void)unlink(f); | |
38 | return(fp); | |
39 | } | |
40 | ||
41 | char * | |
42 | tmpnam(s) | |
43 | char *s; | |
44 | { | |
49b91625 | 45 | char *malloc(), *mktemp(); |
2267ab9a | 46 | |
49b91625 KB |
47 | if (!s && !(s = malloc((u_int)MAXPATHLEN))) |
48 | return(NULL); | |
2267ab9a KB |
49 | (void)sprintf(s, "%s/XXXXXX", P_tmpdir); |
50 | return(mktemp(s)); | |
51 | } | |
52 | ||
53 | char * | |
54 | tempnam(dir, pfx) | |
55 | char *dir, *pfx; | |
93ced3e8 | 56 | { |
49b91625 | 57 | char *f, *name, *getenv(), *malloc(), *mktemp(); |
93ced3e8 | 58 | |
2267ab9a KB |
59 | if (!(name = malloc((u_int)MAXPATHLEN))) |
60 | return(NULL); | |
49b91625 KB |
61 | |
62 | if (f = getenv("TMPDIR")) { | |
63 | (void)sprintf(name, "%s/%sXXXXXX", f, pfx ? "" : pfx); | |
64 | if (f = mktemp(name)) | |
65 | return(f); | |
2267ab9a | 66 | } |
49b91625 KB |
67 | if (dir) { |
68 | (void)sprintf(name, "%s/%sXXXXXX", dir, pfx ? "" : pfx); | |
69 | if (f = mktemp(name)) | |
70 | return(f); | |
2267ab9a | 71 | } |
49b91625 KB |
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); | |
2267ab9a | 76 | return(mktemp(name)); |
93ced3e8 | 77 | } |