]> mj.ucw.cz Git - libucw.git/blob - lib/fb-temp.c
Replaced %L by %ll, the former is obsolete and specific to Linux libc5.
[libucw.git] / lib / fb-temp.c
1 /*
2  *      UCW Library -- Temporary Fastbufs
3  *
4  *      (c) 2002--2006 Martin Mares <mj@ucw.cz>
5  *
6  *      This software may be freely distributed and used according to the terms
7  *      of the GNU Lesser General Public License.
8  */
9
10 #include "lib/lib.h"
11 #include "lib/conf.h"
12 #include "lib/fastbuf.h"
13 #include "lib/threads.h"
14
15 #include <unistd.h>
16 #include <sys/fcntl.h>
17
18 static byte *temp_prefix = "/tmp/temp";
19
20 static struct cf_section temp_config = {
21   CF_ITEMS {
22     CF_STRING("Prefix", &temp_prefix),
23     CF_END
24   }
25 };
26
27 static void CONSTRUCTOR temp_global_init(void)
28 {
29   cf_declare_section("Tempfiles", &temp_config, 0);
30 }
31
32 void
33 temp_file_name(byte *buf)
34 {
35   struct ucwlib_context *ctx = ucwlib_thread_context();
36   int cnt = ++ctx->temp_counter;
37   int pid = getpid();
38   if (ctx->thread_id == pid)
39     sprintf(buf, "%s%d-%d", temp_prefix, pid, cnt);
40   else
41     sprintf(buf, "%s%d-%d-%d", temp_prefix, pid, ctx->thread_id, cnt);
42 }
43
44 struct fastbuf *
45 bopen_tmp(uns buflen)
46 {
47   byte buf[TEMP_FILE_NAME_LEN];
48   struct fastbuf *f;
49
50   temp_file_name(buf);
51   f = bopen(buf, O_RDWR | O_CREAT | O_TRUNC, buflen);
52   bconfig(f, BCONFIG_IS_TEMP_FILE, 1);
53   return f;
54 }
55
56 #ifdef TEST
57
58 #include "lib/getopt.h"
59
60 int main(int argc, char **argv)
61 {
62   log_init(NULL);
63   if (cf_getopt(argc, argv, CF_SHORT_OPTS, CF_NO_LONG_OPTS, NULL) >= 0)
64     die("Hey, whaddya want?");
65
66   struct fastbuf *f = bopen_tmp(65536);
67   bputsn(f, "Hello, world!");
68   bclose(f);
69   return 0;
70 }
71
72 #endif