2 * UCW Library -- Temporary Fastbufs
4 * (c) 2002--2006 Martin Mares <mj@ucw.cz>
6 * This software may be freely distributed and used according to the terms
7 * of the GNU Lesser General Public License.
12 #include "lib/fastbuf.h"
15 #include <sys/fcntl.h>
17 static byte *temp_prefix = "/tmp/temp";
19 static struct cf_section temp_config = {
21 CF_STRING("Prefix", &temp_prefix),
26 static void CONSTRUCTOR temp_global_init(void)
28 cf_declare_section("Tempfiles", &temp_config, 0);
31 #ifdef CONFIG_UCW_THREADS
34 static pthread_key_t temp_counter_key;
36 static void CONSTRUCTOR
39 if (pthread_key_create(&temp_counter_key, NULL) < 0)
40 die("Cannot create fbdir_queue_key: %m");
44 temp_file_name(byte *buf)
46 int cnt = (int) pthread_getspecific(temp_counter_key);
48 pthread_setspecific(temp_counter_key, (void *) cnt);
52 /* FIXME: This is Linux-specific and not declared anywhere :( */
58 sprintf(buf, "%s%d-%d", temp_prefix, pid, cnt);
60 sprintf(buf, "%s%d-%d-%d", temp_prefix, pid, tid, cnt);
66 temp_file_name(byte *buf)
69 sprintf(buf, "%s%d-%d", temp_prefix, (int)getpid(), cnt++);
77 byte buf[TEMP_FILE_NAME_LEN];
81 f = bopen(buf, O_RDWR | O_CREAT | O_TRUNC, buflen);
82 bconfig(f, BCONFIG_IS_TEMP_FILE, 1);
88 #include "lib/getopt.h"
90 int main(int argc, char **argv)
93 if (cf_getopt(argc, argv, CF_SHORT_OPTS, CF_NO_LONG_OPTS, NULL) >= 0)
94 die("Hey, whaddya want?");
96 struct fastbuf *f = bopen_tmp(65536);
97 bputsn(f, "Hello, world!");