VirtualBox

source: kBuild/trunk/src/sed/lib/mkstemp.c@ 805

Last change on this file since 805 was 599, checked in by bird, 18 years ago

GNU sed 4.1.5.

File size: 1.3 KB
Line 
1#ifdef HAVE_CONFIG_H
2#include "config.h"
3#endif
4
5#ifdef HAVE_STRINGS_H
6# include <strings.h>
7#else
8# include <string.h>
9#endif /* HAVE_STRINGS_H */
10
11#ifdef HAVE_STDLIB_H
12# include <stdlib.h>
13#endif /* HAVE_STDLIB_H */
14
15#ifdef HAVE_SYS_FILE_H
16# include <sys/file.h>
17#endif /* HAVE_SYS_FILE_H */
18
19#ifdef HAVE_IO_H
20# include <io.h>
21#endif /* HAVE_IO_H */
22
23#ifdef HAVE_UNISTD_H
24# include <unistd.h>
25#endif /* HAVE_UNISTD_H */
26
27#ifdef HAVE_FCNTL_H
28#include <fcntl.h>
29#endif /* HAVE_FCNTL_H */
30
31#include <limits.h>
32#include <errno.h>
33
34/* Generate a unique temporary file name from template. The last six characters of
35 template must be XXXXXX and these are replaced with a string that makes the
36 filename unique. */
37
38int
39mkstemp (template)
40 char *template;
41{
42 int i, j, n, fd;
43 char *data = template + strlen(template) - 6;
44
45 if (data < template) {
46 errno = EINVAL;
47 return -1;
48 }
49
50 for (n = 0; n <= 5; n++)
51 if (data[n] != 'X') {
52 errno = EINVAL;
53 return -1;
54 }
55
56 for (i = 0; i < INT_MAX; i++) {
57 j = i ^ 827714841; /* Base 36 DOSSUX :-) */
58 for (n = 5; n >= 0; n--) {
59 data[n] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ" [j % 36];
60 j /= 36;
61 }
62
63 fd = open (template, O_CREAT|O_EXCL|O_RDWR, 0600);
64 if (fd != -1)
65 return fd;
66 }
67
68 errno = EEXIST;
69 return -1;
70}
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette