1: /*
2: * Copyright (c) 1987 Regents of the University of California.
3: * All rights reserved. The Berkeley software License Agreement
4: * specifies the terms and conditions for redistribution.
5: */
6:
7: #if defined(LIBC_SCCS) && !defined(lint)
8: static char sccsid[] = "@(#)mktemp.c 5.4 (Berkeley) 9/14/87";
9: #endif LIBC_SCCS and not lint
10:
11: #include <sys/types.h>
12: #include <sys/file.h>
13: #include <sys/stat.h>
14: #include <errno.h>
15: #include <stdio.h>
16: #include <ctype.h>
17:
18: #define YES 1
19: #define NO 0
20:
21: mkstemp(as)
22: char *as;
23: {
24: int fd;
25:
26: return (_gettemp(as, &fd) ? fd : -1);
27: }
28:
29: char *
30: mktemp(as)
31: char *as;
32: {
33: return(_gettemp(as, (int *)NULL) ? as : (char *)NULL);
34: }
35:
36: static
37: _gettemp(as, doopen)
38: char *as;
39: register int *doopen;
40: {
41: extern int errno;
42: register char *start, *trv;
43: struct stat sbuf;
44: u_int pid;
45:
46: pid = getpid();
47:
48: /* extra X's get set to 0's */
49: for (trv = as; *trv; ++trv);
50: while (*--trv == 'X') {
51: *trv = (pid % 10) + '0';
52: pid /= 10;
53: }
54:
55: /*
56: * check for write permission on target directory; if you have
57: * six X's and you can't write the directory, this will run for
58: * a *very* long time.
59: */
60: for (start = ++trv; trv > as && *trv != '/'; --trv);
61: if (*trv == '/') {
62: *trv = '\0';
63: if (stat(as, &sbuf) || !(sbuf.st_mode & S_IFDIR))
64: return(NO);
65: *trv = '/';
66: }
67: else if (stat(".", &sbuf) == -1)
68: return(NO);
69:
70: for (;;) {
71: if (doopen) {
72: if ((*doopen = open(as, O_CREAT|O_EXCL|O_RDWR, 0600)) >= 0)
73: return(YES);
74: if (errno != EEXIST)
75: return(NO);
76: }
77: else if (stat(as, &sbuf))
78: return(errno == ENOENT ? YES : NO);
79:
80: /* tricky little algorithm for backward compatibility */
81: for (trv = start;;) {
82: if (!*trv)
83: return(NO);
84: if (*trv == 'z')
85: *trv++ = 'a';
86: else {
87: if (isdigit(*trv))
88: *trv = 'a';
89: else
90: ++*trv;
91: break;
92: }
93: }
94: }
95: /*NOTREACHED*/
96: }
Defined functions
mktemp
defined in line
29; used 105 times
- in /usr/include/unistd.h line
81
- in /usr/src/bin/diff/diff.h line
82
- in /usr/src/bin/diff/diffreg.c line
277
- in /usr/src/bin/disklabel/disklabel.c line
623-625(2)
- in /usr/src/bin/ed.c line
95,
138
- in /usr/src/bin/ld/ld.c line
1185
- in /usr/src/bin/tar.c line
109,
158
- in /usr/src/lib/ccom/cvopt.c line
61-66(2)
- in /usr/src/lib/libc/compat-sys5/tmpnam.c line
45-50(2),
57,
64-76(4)
- in /usr/src/libexec/popper/pop_dropcopy.c line
57
- in /usr/src/libexec/popper/pop_xmit.c line
35
- in /usr/src/local/mtools/mcopy.c line
96,
110
- in /usr/src/new/PORT/perl.1.0.0/perly.c line
44
- in /usr/src/new/PORT/perl.1.0.10/perly.c line
54
- in /usr/src/new/ansi/vmsprep.c line
43
- in /usr/src/new/nntp/server/common.h line
98
- in /usr/src/new/nntp/server/newnews.c line
128
- in /usr/src/new/nntp/server/profile.c line
22
- in /usr/src/new/nntp/server/spawn.c line
65
- in /usr/src/new/nntp/xmit/nntpxmit.c line
148,
914
- in /usr/src/new/pathalias/getstruct.c line
54,
69-79(3)
- in /usr/src/new/rcs/src/rcsfnms.c line
99,
288
- in /usr/src/new/rcs/src/rcsgen.c line
82
- in /usr/src/old/512dumpdir.c line
147
- in /usr/src/old/512restor.c line
230
- in /usr/src/old/arcv/arcv.c line
21,
31-35(2)
- in /usr/src/old/dump/dumpdir.c line
59
- in /usr/src/sbin/dcheck/dcheck.c line
67
- in /usr/src/sbin/dump/dumpdir.c line
67
- in /usr/src/sbin/dump/dumptape.c line
329
- in /usr/src/sbin/restor/restor.c line
116
- in /usr/src/ucb/Mail/def.h line
298
- in /usr/src/ucb/Mail/quit.c line
389,
420
- in /usr/src/ucb/Mail/temp.c line
57-65(5)
- in /usr/src/ucb/PORT/pascal/pdx/command/remake.c line
37
- in /usr/src/ucb/PORT/pascal/pdx/defs.h line
48
- in /usr/src/ucb/PORT/pascal/pdx/library.c line
40
- in /usr/src/ucb/PORT/pascal/src/savenl.c line
80-83(4)
- in /usr/src/ucb/PORT/pascal/utilities/pc.c line
37,
265-267(2)
- in /usr/src/ucb/PORT/pascal/utilities/pix.c line
56
- in /usr/src/ucb/PORT/pascal/utilities/pmerge.c line
41,
82-88(7)
- in /usr/src/ucb/error/errortouch.c line
505
- in /usr/src/ucb/ftp/cmds.c line
752
- in /usr/src/ucb/ftp/ftp_var.h line
110
- in /usr/src/ucb/msgs/msgs.c line
720
- in /usr/src/ucb/pascal/pi/pix.c line
36
- in /usr/src/ucb/rdist/main.c line
164
- in /usr/src/ucb/sccs.c line
1153
- in /usr/src/ucb/sendbug/bugfiler.c line
224,
606,
738
- in /usr/src/ucb/strcompact.c line
92-94(2)
- in /usr/src/ucb/symcompact.c line
217
- in /usr/src/ucb/symorder.c line
139-143(3)
- in /usr/src/ucb/xstr.c line
34,
81
- in /usr/src/usr.bin/m4/eval.c line
290
- in /usr/src/usr.bin/m4/extern.h line
97
- in /usr/src/usr.bin/m4/main.c line
184
- in /usr/src/usr.bin/patch/common.h line
153
- in /usr/src/usr.bin/ptx.c line
36,
220
- in /usr/src/usr.bin/refer/sortbib.c line
21,
45
- in /usr/src/usr.bin/roff/common_source/n1.c line
343,
349
- in /usr/src/usr.lib/libI77/open.c line
49
Defined variables
sccsid
defined in line
8;
never used
Defined macros
NO
defined in line
19; used 5 times
YES
defined in line
18; used 2 times