From 935c63ef7e949bbffcce5a66c3bc627e450d1f18 Mon Sep 17 00:00:00 2001 From: sin Date: Thu, 18 Sep 2014 15:00:41 +0100 Subject: Remove writeline() --- ratox.c | 177 ++++++++++++++++++++++++++++++---------------------------------- 1 file changed, 82 insertions(+), 95 deletions(-) (limited to 'ratox.c') diff --git a/ratox.c b/ratox.c index a508b20..e8ddac0 100644 --- a/ratox.c +++ b/ratox.c @@ -68,12 +68,12 @@ enum { }; static void setname(void *); -static void setstatusmsg(void *); +static void setstatus(void *); static void sendfriendreq(void *); static struct slot gslots[] = { [NAME] = { .name = "name", .cb = setname, .outtype = STATIC }, - [STATUS] = { .name = "status", .cb = setstatusmsg, .outtype = STATIC }, + [STATUS] = { .name = "status", .cb = setstatus, .outtype = STATIC }, [REQUEST] = { .name = "request", .cb = sendfriendreq, .outtype = FOLDER, .outmode = 0755 }, }; @@ -84,15 +84,22 @@ static struct file gfiles[] = { }; enum { - TEXT_IN_FIFO, - FILE_IN_FIFO, - NR_FFIFOS + FTEXT_IN, + FFILE_IN, + FONLINE, + FNAME, + FSTATUS, + FTEXT_OUT, + NR_FFILES }; -/* Friend related FIFOs, they go in fid == fid) { - snprintf(path, sizeof(path), "%s/online", f->idstr); - writeline(path, "w", status == 0 ? "0\n" : "1\n"); + dprintf(f->fd[FONLINE], status == 0 ? "0\n" : "1\n"); return; } } @@ -227,7 +231,6 @@ cbfriendmessage(Tox *m, int32_t fid, const uint8_t *data, uint16_t len, void *ud struct friend *f; uint8_t msg[len + 1]; char buft[64]; - char path[PATH_MAX]; time_t t; memcpy(msg, data, len); @@ -237,8 +240,7 @@ cbfriendmessage(Tox *m, int32_t fid, const uint8_t *data, uint16_t len, void *ud if (f->fid == fid) { t = time(NULL); strftime(buft, sizeof(buft), "%F %R", localtime(&t)); - snprintf(path, sizeof(path), "%s/text_out", f->idstr); - writeline(path, "a", "%s %s\n", buft, msg); + dprintf(f->fd[FTEXT_OUT], "%s %s\n", buft, msg); printout("%s %s\n", f->namestr[0] == '\0' ? "Anonymous" : f->namestr, msg); break; @@ -293,15 +295,13 @@ cbnamechange(Tox *m, int32_t fid, const uint8_t *data, uint16_t len, void *user) { struct friend *f; uint8_t name[len + 1]; - char path[PATH_MAX]; memcpy(name, data, len); name[len] = '\0'; TAILQ_FOREACH(f, &friendhead, entry) { if (f->fid == fid) { - snprintf(path, sizeof(path), "%s/name", f->idstr); - writeline(path, "w", "%s\n", name); + dprintf(f->fd[FNAME], "%s\n", name); if (memcmp(f->namestr, name, len + 1) == 0) break; printout("%s -> %s\n", f->namestr[0] == '\0' ? @@ -317,18 +317,16 @@ static void cbstatusmessage(Tox *m, int32_t fid, const uint8_t *data, uint16_t len, void *udata) { struct friend *f; - uint8_t statusmsg[len + 1]; - char path[PATH_MAX]; + uint8_t status[len + 1]; - memcpy(statusmsg, data, len); - statusmsg[len] = '\0'; + memcpy(status, data, len); + status[len] = '\0'; TAILQ_FOREACH(f, &friendhead, entry) { if (f->fid == fid) { - snprintf(path, sizeof(path), "%s/statusmsg", f->idstr); - writeline(path, "w", "%s\n", statusmsg); + dprintf(f->fd[FSTATUS], "%s\n", status); printout("%s changed status: %s\n", - f->namestr[0] == '\0' ? "Anonymous" : f->namestr, statusmsg); + f->namestr[0] == '\0' ? "Anonymous" : f->namestr, status); break; } } @@ -422,7 +420,7 @@ sendfriendfile(struct friend *f) f->t.pending = 0; } /* grab another buffer from the FIFO */ - n = read(f->fd[FILE_IN_FIFO], f->t.buf, f->t.chunksz); + n = read(f->fd[FFILE_IN], f->t.buf, f->t.chunksz); if (n < 0) { if (errno == EINTR) continue; @@ -450,7 +448,7 @@ sendfriendtext(struct friend *f) ssize_t n; again: - n = read(f->fd[TEXT_IN_FIFO], buf, sizeof(buf)); + n = read(f->fd[FTEXT_IN], buf, sizeof(buf)); if (n < 0) { if (errno == EINTR) goto again; @@ -538,7 +536,7 @@ localinit(void) { uint8_t name[TOX_MAX_NAME_LENGTH + 1]; uint8_t address[TOX_FRIEND_ADDRESS_SIZE]; - uint8_t statusmsg[TOX_MAX_STATUSMESSAGE_LENGTH + 1]; + uint8_t status[TOX_MAX_STATUSMESSAGE_LENGTH + 1]; FILE *fp; DIR *d; int r; @@ -608,11 +606,11 @@ localinit(void) dprintf(gslots[NAME].fd[OUT], "%s\n", name); /* Dump status message */ - r = tox_get_self_status_message(tox, statusmsg, - sizeof(statusmsg) - 1); - if (r > sizeof(statusmsg) - 1) - r = sizeof(statusmsg) - 1; - statusmsg[r] = '\0'; + r = tox_get_self_status_message(tox, status, + sizeof(status) - 1); + if (r > sizeof(status) - 1) + r = sizeof(status) - 1; + status[r] = '\0'; ftruncate(gslots[STATUS].fd[OUT], 0); dprintf(gslots[STATUS].fd[OUT], "%s\n", name); @@ -687,9 +685,8 @@ str2id(char *idstr, uint8_t *id) static struct friend * friendcreate(int32_t fid) { - char path[PATH_MAX]; struct friend *f; - uint8_t statusmsg[TOX_MAX_STATUSMESSAGE_LENGTH + 1]; + uint8_t status[TOX_MAX_STATUSMESSAGE_LENGTH + 1]; size_t i; int r; @@ -716,34 +713,42 @@ friendcreate(int32_t fid) exit(EXIT_FAILURE); } - for (i = 0; i < LEN(ffifos); i++) { - snprintf(path, sizeof(path), "%s/%s", f->idstr, - ffifos[i].name); - r = mkfifo(path, ffifos[i].mode); - if (r < 0 && errno != EEXIST) { - perror("mkfifo"); - exit(EXIT_FAILURE); - } - r = open(path, ffifos[i].flags, 0); - if (r < 0) { - perror("open"); - exit(EXIT_FAILURE); + r = chdir(f->idstr); + if (r < 0) { + perror("chdir"); + exit(EXIT_FAILURE); + } + for (i = 0; i < LEN(ffiles); i++) { + if (ffiles[i].type == FIFO) { + r = mkfifo(ffiles[i].name, ffiles[i].mode); + if (r < 0 && errno != EEXIST) { + perror("mkfifo"); + exit(EXIT_FAILURE); + } + r = open(ffiles[i].name, ffiles[i].flags); + if (r < 0) { + perror("open"); + exit(EXIT_FAILURE); + } + } else if (ffiles[i].type == OUT_F) { + r = open(ffiles[i].name, ffiles[i].flags, ffiles[i].mode); + if (r < 0) { + perror("open"); + exit(EXIT_FAILURE); + } } f->fd[i] = r; } + chdir(".."); - snprintf(path, sizeof(path), "%s/name", f->idstr); - writeline(path, "w", "%s\n", f->namestr); - snprintf(path, sizeof(path), "%s/online", f->idstr); - writeline(path, "w", tox_get_friend_connection_status(tox, fid) == 0 ? "0\n" : "1\n"); + dprintf(f->fd[FNAME], "%s\n", f->namestr); + dprintf(f->fd[FONLINE], "%s\n", + tox_get_friend_connection_status(tox, fid) == 0 ? "0" : "1"); r = tox_get_status_message_size(tox, fid); - if (r > sizeof(statusmsg) - 1) - r = sizeof(statusmsg) - 1; - statusmsg[r] = '\0'; - snprintf(path, sizeof(path), "%s/statusmsg", f->idstr); - writeline(path, "w", "%s\n", statusmsg); - snprintf(path, sizeof(path), "%s/text_out", f->idstr); - writeline(path, "a", ""); + if (r > sizeof(status) - 1) + r = sizeof(status) - 1; + status[r] = '\0'; + dprintf(f->fd[FSTATUS], "%s\n", status); TAILQ_INSERT_TAIL(&friendhead, f, entry); @@ -772,24 +777,6 @@ friendload(void) free(fids); } -static void -writeline(const char *path, const char *mode, - const char *fmt, ...) -{ - FILE *fp; - va_list ap; - - fp = fopen(path, mode); - if (!fp) { - perror("fopen"); - exit(EXIT_FAILURE); - } - va_start(ap, fmt); - vfprintf(fp, fmt, ap); - va_end(ap); - fclose(fp); -} - static void setname(void *data) { @@ -817,13 +804,13 @@ again: } static void -setstatusmsg(void *data) +setstatus(void *data) { - uint8_t statusmsg[TOX_MAX_STATUSMESSAGE_LENGTH + 1]; + uint8_t status[TOX_MAX_STATUSMESSAGE_LENGTH + 1]; int r; again: - r = read(gslots[STATUS].fd[IN], statusmsg, sizeof(statusmsg) - 1); + r = read(gslots[STATUS].fd[IN], status, sizeof(status) - 1); if (r < 0) { if (errno == EINTR) goto again; @@ -832,14 +819,14 @@ again: perror("read"); return; } - if (statusmsg[r - 1] == '\n') + if (status[r - 1] == '\n') r--; - statusmsg[r] = '\0'; - tox_set_status_message(tox, statusmsg, r); + status[r] = '\0'; + tox_set_status_message(tox, status, r); datasave(); - printout("Changed status message to %s\n", statusmsg); + printout("Changed status message to %s\n", status); ftruncate(gslots[STATUS].fd[OUT], 0); - dprintf(gslots[STATUS].fd[OUT], "%s\n", statusmsg); + dprintf(gslots[STATUS].fd[OUT], "%s\n", status); } static void @@ -957,9 +944,9 @@ loop(void) TAILQ_FOREACH(f, &friendhead, entry) { /* Only monitor friends that are online */ if (tox_get_friend_connection_status(tox, f->fid) == 1) { - FD_SET(f->fd[TEXT_IN_FIFO], &rfds); - if (f->fd[TEXT_IN_FIFO] > fdmax) - fdmax = f->fd[TEXT_IN_FIFO]; + FD_SET(f->fd[FTEXT_IN], &rfds); + if (f->fd[FTEXT_IN] > fdmax) + fdmax = f->fd[FTEXT_IN]; /* If the transfer has just been initiated * wait until we have a state change before we start * polling. Avoids spinning endlessly while waiting @@ -968,9 +955,9 @@ loop(void) */ if (f->t.state == TRANSFER_INITIATED) continue; - FD_SET(f->fd[FILE_IN_FIFO], &rfds); - if (f->fd[FILE_IN_FIFO] > fdmax) - fdmax = f->fd[FILE_IN_FIFO]; + FD_SET(f->fd[FFILE_IN], &rfds); + if (f->fd[FFILE_IN] > fdmax) + fdmax = f->fd[FFILE_IN]; } } @@ -1070,14 +1057,14 @@ loop(void) } TAILQ_FOREACH(f, &friendhead, entry) { - for (i = 0; i < NR_FFIFOS; i++) { + for (i = 0; i < NR_FFILES; i++) { if (FD_ISSET(f->fd[i], &rfds) == 0) continue; switch (i) { - case TEXT_IN_FIFO: + case FTEXT_IN: sendfriendtext(f); break; - case FILE_IN_FIFO: + case FFILE_IN: switch (f->t.state) { case TRANSFER_NONE: /* prepare a new transfer */ -- cgit v1.2.3