annotate lib/mkstemps.c @ 952:a490d94a5b8e

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