Revised several variables and deleted unused variables.
[AROS.git] / compiler / posixc / tmpfile.c
blob79837ce7f39bbcb34be907e1429c1c270f14913a
1 /*
2 This file has been released into the Public Domain.
3 $Id$
5 POSIX.1-2008 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 #define DEBUG 0
17 #include <aros/debug.h>
19 /*****************************************************************************
21 NAME */
22 #include <stdio.h>
24 FILE * tmpfile(
26 /* SYNOPSIS */
27 void)
29 /* FUNCTION
30 The tmpfile() function returns a pointer to a stream
31 associated with a file descriptor returned by the routine
32 mkstemp(3). The created file is unlinked before tmpfile()
33 returns, causing the file to be automatically deleted when the
34 last reference to it is closed. The file is opened with the
35 access value `w+'. The file is created in the T: directory,
36 which is the standard AROS temp directory.
39 INPUTS
42 RESULT
43 The tmpfile() function returns a pointer to an open file stream on
44 success. On error, a NULL pointer is returned and errno is set
45 appropriately.
47 ERRORS
48 The tmpfile() function may fail and set the global variable
49 errno for any of the errors specified for the library functions
50 fdopen() or mkstemp().
52 NOTES
54 EXAMPLE
55 #include <errno.h>
56 #include <stdio.h>
57 #include <string.h>
59 main()
61 FILE * fp;
63 fp = tmpfile();
64 if ( fp == NULL)
66 perror(strerror(errno));
67 return;
70 fprintf(fp, "do a bit of writing to the temp file");
73 BUGS
74 BUG1: The temporary file is neither closed nor deleted. Ideally,
75 unlink() could be used to mark the temp file for removal (see
76 BUG1 in the source code) - but I suspect a bug in unlink() itself,
77 whereby it tries to remove the file straight away, rather than
78 waiting for all references to it to be closed. The bug is not too
79 serious, because all temp files are written to the T: directory,
80 which get zapped when AROS is closed down. However, problems may
81 exist when you start creating over 26 temp files with the same PID.
84 SEE ALSO
85 fopen(), mkstemp()
87 INTERNALS
89 ******************************************************************************/
91 #define TEMPLATE "T:temp.XXXXXX"
92 char * filename;
93 FILE *fp;
95 filename = (char *)malloc(MAXPATHLEN);
96 if (!filename) { puts("FIXME: mktemp() malloc failed"); return NULL;}
97 strcpy(filename, TEMPLATE);
99 mktemp(filename);
100 D(bug("[posixc/tmpfile()] filename: %s\n", filename));
101 fp = fopen(filename, "w+");
102 /* unlink(filename); -- see BUG1 in BUGS section */
103 free(filename);
104 return fp;
106 } /* tmpfile() */