comparison lib/mkstemps.c @ 402:57087926bf03

2004-2-16 Brian Masney <masneyb@gftp.org> * src/gtk/view_dialog.c - call mkstemps() to create the temporary file. This is so that the extension of the file will be preserved so that syntax highlighting will still work (from Aurelien Jarno <aurelien@aurel32.net>) * lib/Makefile.am lib/gftp.h lib/mkstemps.c po/POTFILES.in - added mksteps() from the GNU C Library (from Aurelien Jarno <aurelien@aurel32.net>)
author masneyb
date Tue, 17 Feb 2004 00:49:17 +0000
parents
children
comparison
equal deleted inserted replaced
401:3bb0cb1b8389 402:57087926bf03
1 /* Copyright (C) 1991, 1992, 1996, 1998 Free Software Foundation, Inc.
2 This file is derived from mkstemp.c from the GNU C Library.
3
4 The GNU C Library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Library General Public License as
6 published by the Free Software Foundation; either version 2 of the
7 License, or (at your option) any later version.
8
9 The GNU C Library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Library General Public License for more details.
13
14 You should have received a copy of the GNU Library General Public
15 License along with the GNU C Library; see the file COPYING.LIB. If not,
16 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 Boston, MA 02111-1307, USA. */
18
19 #ifdef HAVE_CONFIG_H
20 #include "config.h"
21 #endif
22
23 #include <sys/types.h>
24 #ifdef HAVE_STDLIB_H
25 #include <stdlib.h>
26 #endif
27 #ifdef HAVE_STRING_H
28 #include <string.h>
29 #endif
30 #include <errno.h>
31 #include <stdio.h>
32 #include <fcntl.h>
33 #ifdef HAVE_UNISTD_H
34 #include <unistd.h>
35 #endif
36 #ifdef HAVE_SYS_TIME_H
37 #include <sys/time.h>
38 #endif
39
40 /* We need to provide a type for gcc_uint64_t. */
41 #ifdef __GNUC__
42 __extension__ typedef unsigned long long gcc_uint64_t;
43 #else
44 typedef unsigned long gcc_uint64_t;
45 #endif
46
47 #ifndef TMP_MAX
48 #define TMP_MAX 16384
49 #endif
50
51 /*
52
53 @deftypefn Replacement int mkstemps (char *@var{template}, int @var{suffix_len})
54
55 Generate a unique temporary file name from @var{template}.
56 @var{template} has the form:
57
58 @example
59 @var{path}/ccXXXXXX@var{suffix}
60 @end example
61
62 @var{suffix_len} tells us how long @var{suffix} is (it can be zero
63 length). The last six characters of @var{template} before @var{suffix}
64 must be @samp{XXXXXX}; they are replaced with a string that makes the
65 filename unique. Returns a file descriptor open on the file for
66 reading and writing.
67
68 @end deftypefn
69
70 */
71
72 int
73 mkstemps (template, suffix_len)
74 char *template;
75 int suffix_len;
76 {
77 static const char letters[]
78 = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
79 static gcc_uint64_t value;
80 #ifdef HAVE_GETTIMEOFDAY
81 struct timeval tv;
82 #endif
83 char *XXXXXX;
84 size_t len;
85 int count;
86
87 len = strlen (template);
88
89 if ((int) len < 6 + suffix_len
90 || strncmp (&template[len - 6 - suffix_len], "XXXXXX", 6))
91 {
92 return -1;
93 }
94
95 XXXXXX = &template[len - 6 - suffix_len];
96
97 #ifdef HAVE_GETTIMEOFDAY
98 /* Get some more or less random data. */
99 gettimeofday (&tv, NULL);
100 value += ((gcc_uint64_t) tv.tv_usec << 16) ^ tv.tv_sec ^ getpid ();
101 #else
102 value += getpid ();
103 #endif
104
105 for (count = 0; count < TMP_MAX; ++count)
106 {
107 gcc_uint64_t v = value;
108 int fd;
109
110 /* Fill in the random bits. */
111 XXXXXX[0] = letters[v % 62];
112 v /= 62;
113 XXXXXX[1] = letters[v % 62];
114 v /= 62;
115 XXXXXX[2] = letters[v % 62];
116 v /= 62;
117 XXXXXX[3] = letters[v % 62];
118 v /= 62;
119 XXXXXX[4] = letters[v % 62];
120 v /= 62;
121 XXXXXX[5] = letters[v % 62];
122
123 #ifdef VMS
124 fd = open (template, O_RDWR|O_CREAT|O_EXCL, 0600, "fop=tmd");
125 #else
126 fd = open (template, O_RDWR|O_CREAT|O_EXCL, 0600);
127 #endif
128 if (fd >= 0)
129 /* The file does not exist. */
130 return fd;
131
132 /* This is a random value. It is only necessary that the next
133 TMP_MAX values generated by adding 7777 to VALUE are different
134 with (module 2^32). */
135 value += 7777;
136 }
137
138 /* We return the null string if we can't find a unique file name. */
139 template[0] = '\0';
140 return -1;
141 }