2 * UCW Library -- Temporary Fastbufs
4 * (c) 2002--2008 Martin Mares <mj@ucw.cz>
5 * (c) 2008 Michal Vaner <vorner@ucw.cz>
7 * This software may be freely distributed and used according to the terms
8 * of the GNU Lesser General Public License.
12 #include <ucw/fastbuf.h>
18 bopen_tmp_file(struct fb_params *params)
20 char name[TEMP_FILE_NAME_LEN];
21 int fd = open_tmp(name, O_RDWR | O_CREAT | O_TRUNC, 0600);
22 struct fastbuf *fb = bopen_fd_name(fd, params, name);
23 bconfig(fb, BCONFIG_IS_TEMP_FILE, 1);
30 return bopen_tmp_file(&(struct fb_params){ .type = FB_STD, .buffer_size = buflen });
33 void bfix_tmp_file(struct fastbuf *fb, const char *name)
35 int was_temp = bconfig(fb, BCONFIG_IS_TEMP_FILE, 0);
36 ASSERT(was_temp == 1);
37 if (rename(fb->name, name))
38 bthrow(fb, "tmp", "Cannot rename %s to %s: %m", fb->name, name);
44 #include <ucw/getopt.h>
46 int main(int argc, char **argv)
49 if (cf_getopt(argc, argv, CF_SHORT_OPTS, CF_NO_LONG_OPTS, NULL) >= 0)
50 die("Hey, whaddya want?");
52 struct fastbuf *f = bopen_tmp(65536);
54 bputsn(f, "Hello, world!");