Archived
1
0
Fork 0

buffer: add buffer_write()

This commit is contained in:
Henrik Hautakoski 2011-11-11 12:23:25 +01:00
parent e0976e0d23
commit 4c664f2400
2 changed files with 18 additions and 0 deletions

View file

@ -20,6 +20,8 @@
#include <stdlib.h> #include <stdlib.h>
#include <stdio.h> #include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include "error.h" #include "error.h"
#include "buffer.h" #include "buffer.h"
@ -99,3 +101,17 @@ void buffer_append(struct buffer *b, const void *ptr, size_t len) {
memcpy(b->block + b->len, ptr, len); memcpy(b->block + b->len, ptr, len);
buffer_setlen(b, b->len + len); buffer_setlen(b, b->len + len);
} }
int buffer_write(struct buffer *b, const char *filename) {
int fd, rc;
fd = open(filename, O_WRONLY | O_CREAT | O_EXCL, 0664);
if (fd < 0)
return error("%s: %s", filename, strerror(errno));
rc = write(fd, b->block, b->len);
close(fd);
return rc;
}

View file

@ -95,4 +95,6 @@ static inline void buffer_str_term(struct buffer *b, char ch) {
buffer_append_ch(b, ch); buffer_append_ch(b, ch);
} }
int buffer_write(struct buffer *b, const char *filename);
#endif /* BUFFER_H */ #endif /* BUFFER_H */