add a missing section header table index conversion
[tangerine.git] / compiler / clib / tmpfile.c
blob0b1f1127e03d39bb6b0b308e8b9c4b424d39bfc2
1 /*
2 This file has been released into the Public Domain.
3 $Id$
5 POSIX function tmpfile().
6 */
8 #include <sys/param.h>
9 #include <stdlib.h>
10 #include <string.h>
11 #include <errno.h>
12 #include <fcntl.h>
13 #include <proto/dos.h>
14 #include <unistd.h>
16 /*****************************************************************************
18 NAME */
19 #include <stdio.h>
21 FILE * tmpfile(
23 /* SYNOPSIS */
24 void)
26 /* FUNCTION
27 The tmpfile() function returns a pointer to a stream
28 associated with a file descriptor returned by the routine
29 mkstemp(3). The created file is unlinked before tmpfile()
30 returns, causing the file to be automatically deleted when the
31 last reference to it is closed. The file is opened with the
32 access value `w+'. The file is created in the T: directory,
33 which is the standard AROS temp directory.
36 INPUTS
39 RESULT
40 The tmpfile() function returns a pointer to an open file stream on
41 success. On error, a NULL pointer is returned and errno is set
42 appropriately.
44 ERRORS
45 The tmpfile() function may fail and set the global variable
46 errno for any of the errors specified for the library functions
47 fdopen() or mkstemp().
49 NOTES
51 EXAMPLE
52 #include <errno.h>
53 #include <stdio.h>
54 #include <string.h>
56 main()
58 FILE * fp;
60 fp = tmpfile();
61 if ( fp == NULL)
63 perror(strerror(errno));
64 return;
67 fprintf(fp, "do a bit of writing to the temp file");
70 BUGS
71 BUG1: The temporary file is neither closed nor deleted. Ideally,
72 unlink() could be used to mark the temp file for removal (see
73 BUG1 in the source code) - but I suspect a bug in unlink() itself,
74 whereby it tries to remove the file straight away, rather than
75 waiting for all references to it to be closed. The bug is not too
76 serious, because all temp files are written to the T: directory,
77 which get zapped when AROS is closed down. However, problems may
78 exist when you start creating over 26 temp files with the same PID.
81 SEE ALSO
82 fopen(), mkstemp()
84 INTERNALS
86 ******************************************************************************/
88 #define TEMPLATE "T:temp.XXXXXX"
89 char * filename;
90 FILE *fp;
92 filename = (char *)malloc(MAXPATHLEN);
93 if (!filename) { puts("FIXME: mktemp() malloc failed"); return NULL;}
94 strcpy(filename, TEMPLATE);
96 mktemp(filename);
97 fp = fopen(filename, "w+");
98 /* unlink(filename); -- see BUG1 in BUGS section */
99 free(filename);
100 return fp;
102 } /* tmpfile() */