OpenCores
URL https://opencores.org/ocsvn/openrisc_me/openrisc_me/trunk

Subversion Repositories openrisc_me

[/] [openrisc/] [trunk/] [gnu-src/] [newlib-1.18.0/] [newlib/] [libc/] [stdio/] [mktemp.c] - Blame information for rev 309

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 207 jeremybenn
/*
2
 * Copyright (c) 1987 Regents of the University of California.
3
 * All rights reserved.
4
 *
5
 * Redistribution and use in source and binary forms are permitted
6
 * provided that: (1) source distributions retain this entire copyright
7
 * notice and comment, and (2) distributions including binaries display
8
 * the following acknowledgement:  ``This product includes software
9
 * developed by the University of California, Berkeley and its contributors''
10
 * in the documentation or other materials provided with the distribution
11
 * and in all advertising materials mentioning features or use of this
12
 * software. Neither the name of the University nor the names of its
13
 * contributors may be used to endorse or promote products derived
14
 * from this software without specific prior written permission.
15
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
16
 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
17
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
18
 */
19
/* This is file MKTEMP.C */
20
/* This file may have been modified by DJ Delorie (Jan 1991).  If so,
21
** these modifications are Copyright (C) 1991 DJ Delorie.
22
*/
23
 
24
/*
25
FUNCTION
26
<<mktemp>>, <<mkstemp>>, <<mkstemps>>---generate unused file name
27
<<mkdtemp>>---generate unused directory
28
 
29
INDEX
30
        mktemp
31
INDEX
32
        mkdtemp
33
INDEX
34
        mkstemp
35
INDEX
36
        mkstemps
37
INDEX
38
        _mktemp_r
39
INDEX
40
        _mkdtemp_r
41
INDEX
42
        _mkstemp_r
43
INDEX
44
        _mkstemps_r
45
 
46
ANSI_SYNOPSIS
47
        #include <stdlib.h>
48
        char *mktemp(char *<[path]>);
49
        char *mkdtemp(char *<[path]>);
50
        int mkstemp(char *<[path]>);
51
        int mkstemps(char *<[path]>, int <[suffixlen]>);
52
 
53
        char *_mktemp_r(struct _reent *<[reent]>, char *<[path]>);
54
        char *_mkdtemp_r(struct _reent *<[reent]>, char *<[path]>);
55
        int *_mkstemp_r(struct _reent *<[reent]>, char *<[path]>);
56
        int *_mkstemps_r(struct _reent *<[reent]>, char *<[path]>, int <[len]>);
57
 
58
DESCRIPTION
59
<<mktemp>>, <<mkstemp>>, and <<mkstemps>> attempt to generate a file name
60
that is not yet in use for any existing file.  <<mkstemp>> and <<mkstemps>>
61
create the file and open it for reading and writing; <<mktemp>> simply
62
generates the file name (making <<mktemp>> a security risk).  <<mkdtemp>>
63
attempts to create a directory instead of a file, with a permissions
64
mask of 0700.
65
 
66
You supply a simple pattern for the generated file name, as the string
67
at <[path]>.  The pattern should be a valid filename (including path
68
information if you wish) ending with at least six `<<X>>'
69
characters.  The generated filename will match the leading part of the
70
name you supply, with the trailing `<<X>>' characters replaced by some
71
combination of digits and letters.  With <<mkstemps>>, the `<<X>>'
72
characters end <[suffixlen]> bytes before the end of the string.
73
 
74
The alternate functions <<_mktemp_r>>, <<_mkdtemp_r>>, <<_mkstemp_r>>,
75
and <<_mkstemps_r>> are reentrant versions.  The extra argument <[reent]>
76
is a pointer to a reentrancy structure.
77
 
78
RETURNS
79
<<mktemp>> returns the pointer <[path]> to the modified string
80
representing an unused filename, unless it could not generate one, or
81
the pattern you provided is not suitable for a filename; in that case,
82
it returns <<NULL>>.
83
 
84
<<mkdtemp>> returns the pointer <[path]> to the modified string if the
85
directory was created, otherwise it returns <<NULL>>.
86
 
87
<<mkstemp>> and <<mkstemps>> return a file descriptor to the newly created
88
file, unless it could not generate an unused filename, or the pattern you
89
provided is not suitable for a filename; in that case, it returns
90
<<-1>>.
91
 
92
NOTES
93
Never use <<mktemp>>.  The generated filenames are easy to guess and
94
there's a race between the test if the file exists and the creation
95
of the file.  In combination this makes <<mktemp>> prone to attacks
96
and using it is a security risk.  Whenever possible use <<mkstemp>>
97
instead.  It doesn't suffer the race condition.
98
 
99
PORTABILITY
100
ANSI C does not require either <<mktemp>> or <<mkstemp>>; the System
101
V Interface Definition requires <<mktemp>> as of Issue 2.  POSIX 2001
102
requires <<mkstemp>>, and POSIX 2008 requires <<mkdtemp>>, but
103
<<mkstemps>> is not standardized.
104
 
105
Supporting OS subroutines required: <<getpid>>, <<mkdir>>, <<open>>, <<stat>>.
106
*/
107
 
108
#include <_ansi.h>
109
#include <stdlib.h>
110
#include <reent.h>
111
#include <sys/types.h>
112
#include <fcntl.h>
113
#include <sys/stat.h>
114
#include <errno.h>
115
#include <stdio.h>
116
#include <ctype.h>
117
 
118
static int
119
_DEFUN(_gettemp, (ptr, path, doopen, domkdir, suffixlen),
120
       struct _reent *ptr _AND
121
       char *path         _AND
122
       register int *doopen _AND
123
       int domkdir        _AND
124
       size_t suffixlen)
125
{
126
  register char *start, *trv;
127
  char *end;
128
#ifdef __USE_INTERNAL_STAT64
129
  struct stat64 sbuf;
130
#else
131
  struct stat sbuf;
132
#endif
133
  unsigned int pid;
134
 
135
  pid = _getpid_r (ptr);
136
  for (trv = path; *trv; ++trv)         /* extra X's get set to 0's */
137
    continue;
138
  if (trv - path < suffixlen)
139
    {
140
      ptr->_errno = EINVAL;
141
      return 0;
142
    }
143
  trv -= suffixlen;
144
  end = trv;
145
  while (path < trv && *--trv == 'X')
146
    {
147
      *trv = (pid % 10) + '0';
148
      pid /= 10;
149
    }
150
  if (end - trv < 6)
151
    {
152
      ptr->_errno = EINVAL;
153
      return 0;
154
    }
155
 
156
  /*
157
   * Check the target directory; if you have six X's and it
158
   * doesn't exist this runs for a *very* long time.
159
   */
160
 
161
  for (start = trv + 1;; --trv)
162
    {
163
      if (trv <= path)
164
        break;
165
      if (*trv == '/')
166
        {
167
          *trv = '\0';
168
#ifdef __USE_INTERNAL_STAT64
169
          if (_stat64_r (ptr, path, &sbuf))
170
#else
171
          if (_stat_r (ptr, path, &sbuf))
172
#endif
173
            return (0);
174
          if (!(sbuf.st_mode & S_IFDIR))
175
            {
176
              ptr->_errno = ENOTDIR;
177
              return (0);
178
            }
179
          *trv = '/';
180
          break;
181
        }
182
    }
183
 
184
  for (;;)
185
    {
186
#if !defined _ELIX_LEVEL || _ELIX_LEVEL >= 4
187
      if (domkdir)
188
        {
189
#ifdef HAVE_MKDIR
190
          if (_mkdir_r (ptr, path, 0700) == 0)
191
            return 1;
192
          if (ptr->_errno != EEXIST)
193
            return 0;
194
#else /* !HAVE_MKDIR */
195
          ptr->_errno = ENOSYS;
196
          return 0;
197
#endif /* !HAVE_MKDIR */
198
        }
199
      else
200
#endif /* _ELIX_LEVEL */
201
      if (doopen)
202
        {
203
          if ((*doopen = _open_r (ptr, path, O_CREAT | O_EXCL | O_RDWR, 0600))
204
              >= 0)
205
            return 1;
206
          if (ptr->_errno != EEXIST)
207
            return 0;
208
        }
209
#ifdef __USE_INTERNAL_STAT64
210
      else if (_stat64_r (ptr, path, &sbuf))
211
#else
212
      else if (_stat_r (ptr, path, &sbuf))
213
#endif
214
        return (ptr->_errno == ENOENT ? 1 : 0);
215
 
216
      /* tricky little algorithm for backward compatibility */
217
      for (trv = start;;)
218
        {
219
          if (trv == end)
220
            return 0;
221
          if (*trv == 'z')
222
            *trv++ = 'a';
223
          else
224
            {
225
              /* Safe, since it only encounters 7-bit characters.  */
226
              if (isdigit (*trv))
227
                *trv = 'a';
228
              else
229
                ++ * trv;
230
              break;
231
            }
232
        }
233
    }
234
  /*NOTREACHED*/
235
}
236
 
237
int
238
_DEFUN(_mkstemp_r, (ptr, path),
239
       struct _reent *ptr _AND
240
       char *path)
241
{
242
  int fd;
243
 
244
  return (_gettemp (ptr, path, &fd, 0, 0) ? fd : -1);
245
}
246
 
247
#if !defined _ELIX_LEVEL || _ELIX_LEVEL >= 4
248
char *
249
_DEFUN(_mkdtemp_r, (ptr, path),
250
       struct _reent *ptr _AND
251
       char *path)
252
{
253
  return (_gettemp (ptr, path, (int *) NULL, 1, 0) ? path : NULL);
254
}
255
 
256
int
257
_DEFUN(_mkstemps_r, (ptr, path, len),
258
       struct _reent *ptr _AND
259
       char *path _AND
260
       int len)
261
{
262
  int fd;
263
 
264
  return (_gettemp (ptr, path, &fd, 0, len) ? fd : -1);
265
}
266
#endif /* _ELIX_LEVEL */
267
 
268
char *
269
_DEFUN(_mktemp_r, (ptr, path),
270
       struct _reent *ptr _AND
271
       char *path)
272
{
273
  return (_gettemp (ptr, path, (int *) NULL, 0, 0) ? path : (char *) NULL);
274
}
275
 
276
#ifndef _REENT_ONLY
277
 
278
int
279
_DEFUN(mkstemp, (path),
280
       char *path)
281
{
282
  int fd;
283
 
284
  return (_gettemp (_REENT, path, &fd, 0, 0) ? fd : -1);
285
}
286
 
287
# if !defined _ELIX_LEVEL || _ELIX_LEVEL >= 4
288
char *
289
_DEFUN(mkdtemp, (path),
290
       char *path)
291
{
292
  return (_gettemp (_REENT, path, (int *) NULL, 1, 0) ? path : NULL);
293
}
294
 
295
int
296
_DEFUN(mkstemps, (path, len),
297
       char *path _AND
298
       int len)
299
{
300
  int fd;
301
 
302
  return (_gettemp (_REENT, path, &fd, 0, len) ? fd : -1);
303
}
304
# endif /* _ELIX_LEVEL */
305
 
306
char *
307
_DEFUN(mktemp, (path),
308
       char *path)
309
{
310
  return (_gettemp (_REENT, path, (int *) NULL, 0, 0) ? path : (char *) NULL);
311
}
312
 
313
#endif /* ! defined (_REENT_ONLY) */

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.