/[MITgcm]/mitgcm.org/devel/buildweb/pkg/swish-e/src/win32/mkstemp.c
ViewVC logotype

Contents of /mitgcm.org/devel/buildweb/pkg/swish-e/src/win32/mkstemp.c

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.1.1.1 - (show annotations) (download) (vendor branch)
Fri Sep 20 19:47:29 2002 UTC (22 years, 10 months ago) by adcroft
Branch: Import, MAIN
CVS Tags: baseline, HEAD
Changes since 1.1: +0 -0 lines
File MIME type: text/plain
Importing web-site building process.

1 /* Copyright (C) 1991, 1992, 1996, 1998, 2001 Free Software Foundation, Inc.
2 This file is derived from mkstemps.c from the GNU Libiberty Library
3 which in turn is derived from the GNU C Library.
4
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public License as
7 published by the Free Software Foundation; either version 2 of the
8 License, or (at your option) any later version.
9
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Library General Public License for more details.
14
15 You should have received a copy of the GNU Library General Public
16 License along with the GNU C Library; see the file COPYING.LIB. If not,
17 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
19
20 */
21
22 /* SWISH-E Porting */
23 #define _SWISH_PORT
24 #include "config.h"
25
26 #ifdef HAVE_STDLIB_H
27 #include <stdlib.h>
28 #endif
29 #ifdef HAVE_STRING_H
30 #include <string.h>
31 #endif
32 #include <errno.h>
33 #include <stdio.h>
34 #include <fcntl.h>
35 #ifdef HAVE_UNISTD_H
36 #include <unistd.h>
37 #endif
38 #ifdef HAVE_SYS_TIME_H
39 #include <sys/time.h>
40 #endif
41 #ifdef HAVE_PROCESS_H
42 #include <process.h>
43 #endif
44
45 /* We need to provide a type for gcc_uint64_t. */
46 #ifdef __GNUC__
47 typedef unsigned long long gcc_uint64_t;
48 #else
49 typedef unsigned long gcc_uint64_t;
50 #endif
51
52 #ifndef TMP_MAX
53 #define TMP_MAX 16384
54 #endif
55
56 /* Generate a unique temporary file name from TEMPLATE.
57
58 TEMPLATE has the form:
59
60 <path>/ccXXXXXX
61
62 The last six characters of TEMPLATE must be "XXXXXX"; they are
63 replaced with a string that makes the filename unique.
64
65 Returns a file descriptor open on the file for reading and writing. */
66 int
67 mkstemp (template)
68 char *template;
69 {
70 static const char letters[]
71 = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
72 static gcc_uint64_t value;
73 #ifdef HAVE_GETTIMEOFDAY
74 struct timeval tv;
75 #endif
76 char *XXXXXX;
77 size_t len;
78 int count;
79
80 len = strlen (template);
81
82 if ((int) len < 6
83 || strncmp (&template[len - 6], "XXXXXX", 6))
84 {
85 return -1;
86 }
87
88 XXXXXX = &template[len - 6];
89
90 #ifdef HAVE_GETTIMEOFDAY
91 /* Get some more or less random data. */
92 gettimeofday (&tv, NULL);
93 value += ((gcc_uint64_t) tv.tv_usec << 16) ^ tv.tv_sec ^ getpid ();
94 #else
95 value += getpid ();
96 #endif
97
98 for (count = 0; count < TMP_MAX; ++count)
99 {
100 gcc_uint64_t v = value;
101 int fd;
102
103 /* Fill in the random bits. */
104 XXXXXX[0] = letters[v % 62];
105 v /= 62;
106 XXXXXX[1] = letters[v % 62];
107 v /= 62;
108 XXXXXX[2] = letters[v % 62];
109 v /= 62;
110 XXXXXX[3] = letters[v % 62];
111 v /= 62;
112 XXXXXX[4] = letters[v % 62];
113 v /= 62;
114 XXXXXX[5] = letters[v % 62];
115
116 fd = open (template, O_RDWR|O_CREAT|O_EXCL|O_BINARY, 0600);
117 if (fd >= 0)
118 /* The file does not exist. */
119 return fd;
120
121 /* This is a random value. It is only necessary that the next
122 TMP_MAX values generated by adding 7777 to VALUE are different
123 with (module 2^32). */
124 value += 7777;
125 }
126
127 /* We return the null string if we can't find a unique file name. */
128 template[0] = '\0';
129 return -1;
130 }

  ViewVC Help
Powered by ViewVC 1.1.22