#include "ctrlfd.h" #include #include #include #include #include void ctrlfd_t::printf(const char *fmt, ...) { char buf[1024]; int len; if (c_last_was_nl && !c_in_init) { strcpy(buf, "[0] 0: "); len = 7; } else len = 0; va_list va; va_start(va, fmt); len += vsnprintf(buf+len, sizeof(buf), fmt, va); va_end(va); write(c_fd, buf, len); c_last_was_nl = (buf[len-1] == '\n'); } void ctrlfd_t::perror(const char *fmt, ...) { int err = errno; char buf[1024]; int len; if (c_last_was_nl && !c_in_init) { strcpy(buf, "[0] 0: "); len = 7; } else len = 0; va_list va; va_start(va, fmt); len += vsnprintf(buf+len, sizeof(buf), fmt, va); va_end(va); len += snprintf(buf+len, sizeof(buf), ": %s\n", strerror(err)); write(c_fd, buf, len); c_last_was_nl = (buf[len-1] == '\n'); } void ctrlfd_t::done(int rc) { char buf[64]; int len; if (!c_last_was_nl) write(c_fd, "\n", 1); if (!rc) rc = -1; if (!c_in_init) { len = sprintf(buf, "%s[-1] %d:\n", c_last_was_nl ? "" : "\n", rc); write(c_fd, buf, len); } delete this; } void ctrlfd_t::printsin(const struct sockaddr_in *sin) { char buf[64]; int len; len = sprintf(buf, "%s%s:%d", c_last_was_nl ? "[0] 0: " : "", inet_ntoa(sin->sin_addr), ntohs(sin->sin_port)); write(c_fd, buf, len); c_last_was_nl = 0; } void ctrlfd_t::my_putchar(char ch) { write(c_fd, &ch, 1); c_last_was_nl = (ch == '\n'); } void ctrlfd_t::dfs_print_bytes(const char *name, unsigned char *bytes, size_t len) { size_t i; this->printf("%s(\"", name); for (i=0; iprintf("\\x%02x", bytes[i]); else this->my_putchar(bytes[i]); } this->printf("\")\n"); }