ratox

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

commit 5994d52d71816317553d081360980ad6708d295c
parent 7077107778a54cd326e3898318ce53da4de28fc5
Author: sin <sin@2f30.org>
Date:   Fri, 26 Sep 2014 15:55:40 +0100

Add eprintf.c

Diffstat:
Makefile | 3++-
eprintf.c | 59+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
ratox.c | 43+++++++++++++++++--------------------------
util.h | 10++++++++++
4 files changed, 88 insertions(+), 27 deletions(-)

diff --git a/Makefile b/Makefile @@ -3,8 +3,9 @@ include config.mk .POSIX: .SUFFIXES: .c .o -HDR = arg.h config.h readpassphrase.h +HDR = arg.h config.h readpassphrase.h util.h LIB = \ + eprintf.o \ readpassphrase.o SRC = \ ratox.c diff --git a/eprintf.c b/eprintf.c @@ -0,0 +1,59 @@ +/* See LICENSE file for copyright and license details. */ +#include <stdarg.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + +#include "util.h" + +char *argv0; + +static void venprintf(int, const char *, va_list); + +void +enprintf(int status, const char *fmt, ...) +{ + va_list ap; + + va_start(ap, fmt); + venprintf(status, fmt, ap); + va_end(ap); +} + +void +eprintf(const char *fmt, ...) +{ + va_list ap; + + va_start(ap, fmt); + venprintf(EXIT_FAILURE, fmt, ap); + va_end(ap); +} + +void +venprintf(int status, const char *fmt, va_list ap) +{ + vfprintf(stderr, fmt, ap); + + if (fmt[0] && fmt[strlen(fmt)-1] == ':') { + fputc(' ', stderr); + perror(NULL); + } + + exit(status); +} + +void +weprintf(const char *fmt, ...) +{ + va_list ap; + + va_start(ap, fmt); + vfprintf(stderr, fmt, ap); + va_end(ap); + + if (fmt[0] && fmt[strlen(fmt)-1] == ':') { + fputc(' ', stderr); + perror(NULL); + } +} diff --git a/ratox.c b/ratox.c @@ -23,8 +23,8 @@ #include "arg.h" #include "queue.h" #include "readpassphrase.h" +#include "util.h" -#define LEN(x) (sizeof (x) / sizeof *(x)) #define DATAFILE ".ratox.data" const char *reqerr[] = { @@ -157,8 +157,6 @@ struct request { TAILQ_ENTRY(request) entry; }; -char *argv0; - static TAILQ_HEAD(friendhead, friend) friendhead = TAILQ_HEAD_INITIALIZER(friendhead); static TAILQ_HEAD(reqhead, request) reqhead = TAILQ_HEAD_INITIALIZER(reqhead); @@ -277,10 +275,10 @@ cbconnstatus(Tox *m, int32_t frnum, uint8_t status, void *udata) int r; r = tox_get_name(tox, frnum, name); - if (r < 0) { - fprintf(stderr, "tox_get_name() on friend number %d failed\n", frnum); - exit(EXIT_FAILURE); - } + if (r < 0) + eprintf("Failed to get name for friend number %ld\n", + (long)frnum); + name[r] = '\0'; printout("%s %s\n", r == 0 ? (uint8_t *)"Anonymous" : name, @@ -414,7 +412,7 @@ cbuserstatus(Tox *m, int32_t frnum, uint8_t status, void *udata) char *statusstr[] = { "none", "away", "busy" }; if (status >= LEN(statusstr)) { - fprintf(stderr, "received invalid user status: %d\n", status); + weprintf("Received invalid user status: %d\n", status); return; } @@ -503,7 +501,7 @@ cbfilecontrol(Tox *m, int32_t frnum, uint8_t rec_sen, uint8_t fnum, uint8_t ctrl } break; default: - fprintf(stderr, "Unhandled file control type: %d\n", ctrltype); + weprintf("Unhandled file control type: %d\n", ctrltype); break; }; } @@ -699,11 +697,8 @@ dataload(void) sz = lseek(fd, 0, SEEK_END); lseek(fd, 0, SEEK_SET); - if (sz == 0) { - fprintf(stderr, "%s seems to be corrupt\n", - DATAFILE); - exit(EXIT_FAILURE); - } + if (sz == 0) + eprintf("%s seems to be corrupt\n", DATAFILE); data = malloc(sz); if (!data) { @@ -722,10 +717,8 @@ dataload(void) while (readpass("Passphrase: ") < 0 || tox_encrypted_load(tox, data, sz, passphrase, pplen) < 0); } else { - if (tox_load(tox, data, sz) < 0) { - fprintf(stderr, "tox_load() failed\n"); - exit(EXIT_FAILURE); - } + if (tox_load(tox, data, sz) < 0) + eprintf("Failed to load %s\n", DATAFILE); if (encryptdatafile == 1) { printout("%s is not encrypted, but saving in encrypted format\n", DATAFILE); while (readpass("New passphrase: ") < 0); @@ -882,10 +875,8 @@ toxinit(void) } tox = tox_new(&toxopt); - if (!tox) { - fprintf(stderr, "failed to initialize tox core\n"); - exit(EXIT_FAILURE); - } + if (!tox) + eprintf("Failed to initialize tox core\n"); dataload(); datasave(); @@ -959,10 +950,10 @@ friendcreate(int32_t frnum) } r = tox_get_name(tox, frnum, (uint8_t *)f->name); - if (r < 0) { - fprintf(stderr, "tox_get_name() on friend number %d failed\n", frnum); - exit(EXIT_FAILURE); - } + if (r < 0) + eprintf("Failed to get name for friend number %ld\n", + (long)frnum); + f->name[r] = '\0'; f->num = frnum; diff --git a/util.h b/util.h @@ -0,0 +1,10 @@ +/* See LICENSE file for copyright and license details. */ +#include "arg.h" + +#define LEN(x) (sizeof (x) / sizeof *(x)) + +extern char *argv0; + +void enprintf(int, const char *, ...); +void eprintf(const char *, ...); +void weprintf(const char *, ...);