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

Subversion Repositories openrisc_me

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

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

Line No. Rev Author Line
1 148 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>>---generate unused file name
27
 
28
INDEX
29
        mktemp
30
INDEX
31
        mkstemp
32
INDEX
33
        _mktemp_r
34
INDEX
35
        _mkstemp_r
36
 
37
ANSI_SYNOPSIS
38
        #include <stdio.h>
39
        char *mktemp(char *<[path]>);
40
        int mkstemp(char *<[path]>);
41
 
42
        char *_mktemp_r(struct _reent *<[reent]>, char *<[path]>);
43
        int *_mkstemp_r(struct _reent *<[reent]>, char *<[path]>);
44
 
45
TRAD_SYNOPSIS
46
        #include <stdio.h>
47
        char *mktemp(<[path]>)
48
        char *<[path]>;
49
 
50
        int mkstemp(<[path]>)
51
        char *<[path]>;
52
 
53
        char *_mktemp_r(<[reent]>, <[path]>)
54
        struct _reent *<[reent]>;
55
        char *<[path]>;
56
 
57
        int _mkstemp_r(<[reent]>, <[path]>)
58
        struct _reent *<[reent]>;
59
        char *<[path]>;
60
 
61
DESCRIPTION
62
<<mktemp>> and <<mkstemp>> attempt to generate a file name that is not
63
yet in use for any existing file.  <<mkstemp>> creates the file and
64
opens it for reading and writing; <<mktemp>> simply generates the file name.
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 some number of `<<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.
72
 
73
The alternate functions <<_mktemp_r>> and <<_mkstemp_r>> are reentrant
74
versions.  The extra argument <[reent]> is a pointer to a reentrancy
75
structure.
76
 
77
RETURNS
78
<<mktemp>> returns the pointer <[path]> to the modified string
79
representing an unused filename, unless it could not generate one, or
80
the pattern you provided is not suitable for a filename; in that case,
81
it returns <<NULL>>.
82
 
83
<<mkstemp>> returns a file descriptor to the newly created file,
84
unless it could not generate an unused filename, or the pattern you
85
provided is not suitable for a filename; in that case, it returns
86
<<-1>>.
87
 
88
PORTABILITY
89
ANSI C does not require either <<mktemp>> or <<mkstemp>>; the System
90
V Interface Definition requires <<mktemp>> as of Issue 2.
91
 
92
Supporting OS subroutines required: <<getpid>>, <<open>>, <<stat>>.
93
*/
94
 
95
#include <_ansi.h>
96
#include <reent.h>
97
#include <sys/types.h>
98
#include <fcntl.h>
99
#include <sys/stat.h>
100
#include <errno.h>
101
#include <stdio.h>
102
#include <ctype.h>
103
 
104
static int
105
_DEFUN(_gettemp, (ptr, path, doopen),
106
       struct _reent *ptr _AND
107
       char *path         _AND
108
       register int *doopen)
109
{
110
  register char *start, *trv;
111
#ifdef __USE_INTERNAL_STAT64
112
  struct stat64 sbuf;
113
#else
114
  struct stat sbuf;
115
#endif
116
  unsigned int pid;
117
 
118
  pid = _getpid_r (ptr);
119
  for (trv = path; *trv; ++trv)         /* extra X's get set to 0's */
120
    continue;
121
  while (*--trv == 'X')
122
    {
123
      *trv = (pid % 10) + '0';
124
      pid /= 10;
125
    }
126
 
127
  /*
128
   * Check the target directory; if you have six X's and it
129
   * doesn't exist this runs for a *very* long time.
130
   */
131
 
132
  for (start = trv + 1;; --trv)
133
    {
134
      if (trv <= path)
135
        break;
136
      if (*trv == '/')
137
        {
138
          *trv = '\0';
139
#ifdef __USE_INTERNAL_STAT64
140
          if (_stat64_r (ptr, path, &sbuf))
141
#else
142
          if (_stat_r (ptr, path, &sbuf))
143
#endif
144
            return (0);
145
          if (!(sbuf.st_mode & S_IFDIR))
146
            {
147
              ptr->_errno = ENOTDIR;
148
              return (0);
149
            }
150
          *trv = '/';
151
          break;
152
        }
153
    }
154
 
155
  for (;;)
156
    {
157
      if (doopen)
158
        {
159
          if ((*doopen = _open_r (ptr, path, O_CREAT | O_EXCL | O_RDWR, 0600))
160
              >= 0)
161
            return 1;
162
#if defined(__CYGWIN__)
163
          if (ptr->_errno != EEXIST && ptr->_errno != EACCES)
164
#else
165
          if (ptr->_errno != EEXIST)
166
#endif
167
            return 0;
168
        }
169
#ifdef __USE_INTERNAL_STAT64
170
      else if (_stat64_r (ptr, path, &sbuf))
171
#else
172
      else if (_stat_r (ptr, path, &sbuf))
173
#endif
174
        return (ptr->_errno == ENOENT ? 1 : 0);
175
 
176
      /* tricky little algorithm for backward compatibility */
177
      for (trv = start;;)
178
        {
179
          if (!*trv)
180
            return 0;
181
          if (*trv == 'z')
182
            *trv++ = 'a';
183
          else
184
            {
185
              if (isdigit (*trv))
186
                *trv = 'a';
187
              else
188
                ++ * trv;
189
              break;
190
            }
191
        }
192
    }
193
  /*NOTREACHED*/
194
}
195
 
196
int
197
_DEFUN(_mkstemp_r, (ptr, path),
198
       struct _reent *ptr _AND
199
       char *path)
200
{
201
  int fd;
202
 
203
  return (_gettemp (ptr, path, &fd) ? fd : -1);
204
}
205
 
206
char *
207
_DEFUN(_mktemp_r, (ptr, path),
208
       struct _reent *ptr _AND
209
       char *path)
210
{
211
  return (_gettemp (ptr, path, (int *) NULL) ? path : (char *) NULL);
212
}
213
 
214
#ifndef _REENT_ONLY
215
 
216
int
217
_DEFUN(mkstemp, (path),
218
       char *path)
219
{
220
  int fd;
221
 
222
  return (_gettemp (_REENT, path, &fd) ? fd : -1);
223
}
224
 
225
char *
226
_DEFUN(mktemp, (path),
227
       char *path)
228
{
229
  return (_gettemp (_REENT, path, (int *) NULL) ? path : (char *) NULL);
230
}
231
 
232
#endif /* ! defined (_REENT_ONLY) */

powered by: WebSVN 2.1.0

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