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"
13 #include "lib/threads.h"
16 #include <sys/fcntl.h>
18 static byte *temp_prefix = "/tmp/temp";
20 static struct cf_section temp_config = {
22 CF_STRING("Prefix", &temp_prefix),
27 static void CONSTRUCTOR temp_global_init(void)
29 cf_declare_section("Tempfiles", &temp_config, 0);
33 temp_file_name(byte *buf)
35 struct ucwlib_context *ctx = ucwlib_thread_context();
36 int cnt = ++ctx->temp_counter;
38 if (ctx->thread_id == pid)
39 sprintf(buf, "%s%d-%d", temp_prefix, pid, cnt);
41 sprintf(buf, "%s%d-%d-%d", temp_prefix, pid, ctx->thread_id, cnt);
47 byte buf[TEMP_FILE_NAME_LEN];
51 f = bopen(buf, O_RDWR | O_CREAT | O_TRUNC, buflen);
52 bconfig(f, BCONFIG_IS_TEMP_FILE, 1);
58 #include "lib/getopt.h"
60 int main(int argc, char **argv)
63 if (cf_getopt(argc, argv, CF_SHORT_OPTS, CF_NO_LONG_OPTS, NULL) >= 0)
64 die("Hey, whaddya want?");
66 struct fastbuf *f = bopen_tmp(65536);
67 bputsn(f, "Hello, world!");