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

Subversion Repositories or1k

[/] [or1k/] [trunk/] [newlib-1.10.0/] [newlib/] [libc/] [stdio/] [mktemp.c] - Blame information for rev 1773

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

Line No. Rev Author Line
1 1010 ivang
/* This is file MKTEMP.C */
2
/* This file may have been modified by DJ Delorie (Jan 1991).  If so,
3
** these modifications are Copyright (C) 1991 DJ Delorie
4
*/
5
 
6
/*
7
 * Copyright (c) 1987 Regents of the University of California.
8
 * All rights reserved.
9
 *
10
 * Redistribution and use in source and binary forms are permitted
11
 * provided that: (1) source distributions retain this entire copyright
12
 * notice and comment, and (2) distributions including binaries display
13
 * the following acknowledgement:  ``This product includes software
14
 * developed by the University of California, Berkeley and its contributors''
15
 * in the documentation or other materials provided with the distribution
16
 * and in all advertising materials mentioning features or use of this
17
 * software. Neither the name of the University nor the names of its
18
 * contributors may be used to endorse or promote products derived
19
 * from this software without specific prior written permission.
20
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
21
 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
22
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
23
 */
24
 
25
/*
26
FUNCTION
27
<<mktemp>>, <<mkstemp>>---generate unused file name
28
 
29
INDEX
30
        mktemp
31
INDEX
32
        mkstemp
33
INDEX
34
        _mktemp_r
35
INDEX
36
        _mkstemp_r
37
 
38
ANSI_SYNOPSIS
39
        #include <stdio.h>
40
        char *mktemp(char *<[path]>);
41
        int mkstemp(char *<[path]>);
42
 
43
        char *_mktemp_r(void *<[reent]>, char *<[path]>);
44
        int *_mkstemp_r(void *<[reent]>, char *<[path]>);
45
 
46
TRAD_SYNOPSIS
47
        #include <stdio.h>
48
        char *mktemp(<[path]>)
49
        char *<[path]>;
50
 
51
        int mkstemp(<[path]>)
52
        char *<[path]>;
53
 
54
        char *_mktemp_r(<[reent]>, <[path]>)
55
        char *<[reent]>;
56
        char *<[path]>;
57
 
58
        int _mkstemp_r(<[reent]>, <[path]>)
59
        char *<[reent]>;
60
        char *<[path]>;
61
 
62
DESCRIPTION
63
<<mktemp>> and <<mkstemp>> attempt to generate a file name that is not
64
yet in use for any existing file.  <<mkstemp>> creates the file and
65
opens it for reading and writing; <<mktemp>> simply generates the file name.
66
 
67
You supply a simple pattern for the generated file name, as the string
68
at <[path]>.  The pattern should be a valid filename (including path
69
information if you wish) ending with some number of `<<X>>'
70
characters.  The generated filename will match the leading part of the
71
name you supply, with the trailing `<<X>>' characters replaced by some
72
combination of digits and letters.
73
 
74
The alternate functions <<_mktemp_r>> and <<_mkstemp_r>> are reentrant
75
versions.  The extra argument <[reent]> is a pointer to a reentrancy
76
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
<<mkstemp>> returns a file descriptor to the newly created file,
85
unless it could not generate an unused filename, or the pattern you
86
provided is not suitable for a filename; in that case, it returns
87
<<-1>>.
88
 
89
PORTABILITY
90
ANSI C does not require either <<mktemp>> or <<mkstemp>>; the System
91
V Interface Definition requires <<mktemp>> as of Issue 2.
92
 
93
Supporting OS subroutines required: <<getpid>>, <<open>>, <<stat>>.
94
*/
95
 
96
#include <sys/types.h>
97
#include <fcntl.h>
98
#include <sys/stat.h>
99
#include <errno.h>
100
#include <stdio.h>
101
#include <ctype.h>
102
#include <reent.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
  struct stat sbuf;
112
  unsigned int pid;
113
 
114
  pid = _getpid_r (ptr);
115
  for (trv = path; *trv; ++trv)         /* extra X's get set to 0's */
116
    continue;
117
  while (*--trv == 'X')
118
    {
119
      *trv = (pid % 10) + '0';
120
      pid /= 10;
121
    }
122
 
123
  /*
124
   * Check the target directory; if you have six X's and it
125
   * doesn't exist this runs for a *very* long time.
126
   */
127
 
128
  for (start = trv + 1;; --trv)
129
    {
130
      if (trv <= path)
131
        break;
132
      if (*trv == '/')
133
        {
134
          *trv = '\0';
135
          if (_stat_r (ptr, path, &sbuf))
136
            return (0);
137
          if (!(sbuf.st_mode & S_IFDIR))
138
            {
139
              ptr->_errno = ENOTDIR;
140
              return (0);
141
            }
142
          *trv = '/';
143
          break;
144
        }
145
    }
146
 
147
  for (;;)
148
    {
149
      if (doopen)
150
        {
151
          if ((*doopen = _open_r (ptr, path, O_CREAT | O_EXCL | O_RDWR, 0600))
152
              >= 0)
153
            return 1;
154
#if defined(__CYGWIN32__) || defined(__CYGWIN__)
155
          if (ptr->_errno != EEXIST && ptr->_errno != EACCES)
156
#else
157
          if (ptr->_errno != EEXIST)
158
#endif
159
            return 0;
160
        }
161
      else if (_stat_r (ptr, path, &sbuf))
162
        return (ptr->_errno == ENOENT ? 1 : 0);
163
 
164
      /* tricky little algorithm for backward compatibility */
165
      for (trv = start;;)
166
        {
167
          if (!*trv)
168
            return 0;
169
          if (*trv == 'z')
170
            *trv++ = 'a';
171
          else
172
            {
173
              if (isdigit (*trv))
174
                *trv = 'a';
175
              else
176
                ++ * trv;
177
              break;
178
            }
179
        }
180
    }
181
  /*NOTREACHED*/
182
}
183
 
184
int
185
_DEFUN (_mkstemp_r, (ptr, path),
186
        struct _reent *ptr _AND
187
        char *path)
188
{
189
  int fd;
190
 
191
  return (_gettemp (ptr, path, &fd) ? fd : -1);
192
}
193
 
194
char *
195
_DEFUN (_mktemp_r, (ptr, path),
196
        struct _reent *ptr _AND
197
        char *path)
198
{
199
  return (_gettemp (ptr, path, (int *) NULL) ? path : (char *) NULL);
200
}
201
 
202
#ifndef _REENT_ONLY
203
 
204
int
205
_DEFUN (mkstemp, (path),
206
        char *path)
207
{
208
  int fd;
209
 
210
  return (_gettemp (_REENT, path, &fd) ? fd : -1);
211
}
212
 
213
char *
214
_DEFUN (mktemp, (path),
215
        char *path)
216
{
217
  return (_gettemp (_REENT, path, (int *) NULL) ? path : (char *) NULL);
218
}
219
 
220
#endif /* ! defined (_REENT_ONLY) */

powered by: WebSVN 2.1.0

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