~ubuntu-branches/ubuntu/quantal/gclcvs/quantal

« back to all changes in this revision

Viewing changes to binutils/libiberty/tmpnam.c

  • Committer: Bazaar Package Importer
  • Author(s): Camm Maguire
  • Date: 2004-06-24 15:13:46 UTC
  • Revision ID: james.westby@ubuntu.com-20040624151346-xh0xaaktyyp7aorc
Tags: 2.7.0-26
C_GC_OFFSET is 2 on m68k-linux

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 
 
3
@deftypefn Supplemental char* tmpnam (char *@var{s})
 
4
 
 
5
This function attempts to create a name for a temporary file, which
 
6
will be a valid file name yet not exist when @code{tmpnam} checks for
 
7
it.  @var{s} must point to a buffer of at least @code{L_tmpnam} bytes,
 
8
or be @code{NULL}.  Use of this function creates a security risk, and it must
 
9
not be used in new projects.  Use @code{mkstemp} instead.
 
10
 
 
11
@end deftypefn
 
12
 
 
13
*/
 
14
 
 
15
#include <stdio.h>
 
16
 
 
17
#ifndef L_tmpnam
 
18
#define L_tmpnam 100
 
19
#endif
 
20
#ifndef P_tmpdir
 
21
#define P_tmpdir "/usr/tmp"
 
22
#endif
 
23
 
 
24
static char tmpnam_buffer[L_tmpnam];
 
25
static int tmpnam_counter;
 
26
 
 
27
extern int getpid ();
 
28
 
 
29
char *
 
30
tmpnam (s)
 
31
     char *s;
 
32
{
 
33
  int pid = getpid ();
 
34
 
 
35
  if (s == NULL)
 
36
    s = tmpnam_buffer;
 
37
 
 
38
  /*  Generate the filename and make sure that there isn't one called
 
39
      it already.  */
 
40
 
 
41
  while (1)
 
42
    {
 
43
      FILE *f;
 
44
      sprintf (s, "%s/%s%x.%x", P_tmpdir, "t", pid, tmpnam_counter);
 
45
      f = fopen (s, "r");
 
46
      if (f == NULL)
 
47
        break;
 
48
      tmpnam_counter++;
 
49
      fclose (f);
 
50
    }
 
51
 
 
52
  return s;
 
53
}