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

Subversion Repositories or1k

[/] [or1k/] [branches/] [newlib/] [newlib/] [newlib/] [libc/] [stdio/] [mktemp.c] - Blame information for rev 39

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

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

powered by: WebSVN 2.1.0

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