ratox

FIFO based tox client
git clone git://git.2f30.org/ratox
Log | Files | Refs | README | LICENSE

commit 688f6b04c0d6e818bf8c2c3a38e202ca39320669
parent e7fb3a49412474b913134b819c4c049f5e1c88fc
Author: FRIGN <dev@frign.de>
Date:   Mon, 22 Sep 2014 16:46:25 +0200

Change datasave() and localinit() to use fd's instead of fp's

Diffstat:
Mratox.c | 31+++++++++++++++----------------
1 file changed, 15 insertions(+), 16 deletions(-)

diff --git a/ratox.c b/ratox.c @@ -601,13 +601,13 @@ dataload(void) static void datasave(void) { - FILE *fp; - size_t sz; + off_t sz; uint8_t *data; + int fd; - fp = fopen(DATAFILE, "w"); - if (!fp) { - fprintf(stderr, "can't open %s for writing\n", DATAFILE); + fd = open(DATAFILE, O_WRONLY | O_CREAT , 0644); + if (fd < 0) { + perror("open"); exit(EXIT_FAILURE); } @@ -622,13 +622,13 @@ datasave(void) tox_encrypted_save(tox, data, passphrase, pplen); else tox_save(tox, data); - if (fwrite(data, 1, sz, fp) != sz || ferror(fp)) { - fprintf(stderr, "failed to write %s\n", DATAFILE); + if (write(fd, data, sz) != sz) { + perror("write"); exit(EXIT_FAILURE); } free(data); - fclose(fp); + close(fd); } static int @@ -637,9 +637,8 @@ localinit(void) uint8_t name[TOX_MAX_NAME_LENGTH + 1]; uint8_t address[TOX_FRIEND_ADDRESS_SIZE]; uint8_t status[TOX_MAX_STATUSMESSAGE_LENGTH + 1]; - FILE *fp; DIR *d; - int r; + int r, fd; size_t i, m; for (i = 0; i < LEN(gslots); i++) { @@ -722,16 +721,16 @@ localinit(void) dprintf(gslots[STATUS].fd[OUT], "%s\n", name); /* Dump ID */ - fp = fopen("id", "w"); - if (!fp) { - perror("fopen"); + fd = open("id", O_WRONLY | O_CREAT, 0644); + if (fd < 0) { + perror("open"); exit(EXIT_FAILURE); } tox_get_address(tox, address); for (i = 0; i < TOX_FRIEND_ADDRESS_SIZE; i++) - fprintf(fp, "%02X", address[i]); - fputc('\n', fp); - fclose(fp); + dprintf(fd, "%02X", address[i]); + dprintf(fd, "\n"); + close(fd); return 0; }