PSLIST_FOREACH(info, sl) { cq_info_t *cqi = sl->data; cq_info_check(cqi); if (THREAD_INVALID_ID == cqi->stid) str_printf(s, "%-2s ", "-"); else str_printf(s, "%-2d ", cqi->stid); str_catf(s, "%-6zu ", cqi->event_count); str_catf(s, "%-4zu ", cqi->periodic_count); str_catf(s, "%-4zu ", cqi->idle_count); str_catf(s, "%-5s ", 0 == cqi->last_idle ? "-" : compact_time(delta_time(tm_time(), cqi->last_idle))); str_catf(s, "%'6d ", cqi->period); str_catf(s, "%10zu ", cqi->heartbeat_count); str_catf(s, "%10zu ", cqi->triggered_count); str_catf(s, "\"%s\"%*s", cqi->name, (int) (maxlen - vstrlen(cqi->name)), ""); if (cqi->parent != NULL) str_catf(s, " (%s)", cqi->parent); str_putc(s, '\n'); shell_write(sh, str_2c(s)); }
int main(void) { unsigned char buf[16]; char s[] = "hello, world"; assert(str2b(buf, 16, s) == &buf[16]); assert(strncmp((char *)buf, s, sizeof(s)) == 0); assert(buf[15] == 0); int32_t i = 0x2345678; assert(int32tob(buf, i) == &buf[4]); assert(b2int32(buf) == i); i = -123; int32tob(buf, i); assert(b2int32(buf) == i); float f = 0.0625; assert(float2b(buf, f) == &buf[4]); assert(b2float(buf) == f); assert(vstrsizeof(s) == 14); assert(vstr2b(buf, s) == &buf[14]); assert(vstrlen(buf) == 12); assert(b2vstr(buf, s) == &buf[14]); assert(strncmp(s, "hello, world", sizeof(s)) == 0); return 0; }
static enum shell_reply shell_exec_lib_show_callout(struct gnutella_shell *sh, int argc, const char *argv[]) { pslist_t *info, *sl; str_t *s; size_t maxlen = 0; shell_check(sh); g_assert(argv); g_assert(argc > 0); shell_write(sh, "100~\n"); shell_write(sh, "T Events Per. Idle Last Period Heartbeat Triggered Name (Parent)" "\n"); info = cq_info_list(); s = str_new(80); PSLIST_FOREACH(info, sl) { cq_info_t *cqi = sl->data; size_t len; cq_info_check(cqi); len = vstrlen(cqi->name); maxlen = MAX(len, maxlen); }
/** * Extract the URN from a /Q2/URN and populate the search request info * if it is a SHA1 (or bitprint, which contains a SHA1). */ static void g2_node_extract_urn(const g2_tree_t *t, search_request_info_t *sri) { const char *p; size_t paylen; uint i; /* * If we have more SHA1s already than we can hold, stop. */ if (sri->exv_sha1cnt == N_ITEMS(sri->exv_sha1)) return; p = g2_tree_node_payload(t, &paylen); if (NULL == p) return; /* * We can only search by SHA1, hence we're only interested by URNs * that contain a SHA1. */ if (paylen < SHA1_RAW_SIZE) return; /* Cannot contain a SHA1 */ /* * Since we know there are at least SHA1_RAW_SIZE bytes in the payload, * we can use clamp_memcmp() to see whether we have a known prefix. */ for (i = 0; i < N_ITEMS(g2_q2_urn); i++) { const char *prefix = g2_q2_urn[i]; size_t len = vstrlen(prefix) + 1; /* Wants trailing NUL as well */ if (0 == clamp_memcmp(prefix, len, p, paylen)) { p += len; paylen -= len; g_assert(size_is_positive(paylen)); if (paylen >= SHA1_RAW_SIZE) { uint idx = sri->exv_sha1cnt++; g_assert(idx < N_ITEMS(sri->exv_sha1)); memcpy(&sri->exv_sha1[idx].sha1, p, SHA1_RAW_SIZE); } break; } } }
char * strvcat(const char *src, ...) { size_t len; char *dst; va_list ap; va_start(ap, src); len = vstrlen(src, ap); va_end(ap); dst = xmalloc(len + 1); va_start(ap, src); vstrcpy(dst, src, ap); va_end(ap); return dst; }
/** * Write NUL-terminated string, up to `n' characters or the first seen NUL * in the buffer, whichever comes first. * * The string is written as: <ule64(length)><bytes>, no trailing NUL. */ void pmsg_write_fixed_string(pmsg_t *mb, const char *str, size_t n) { size_t len; g_assert(pmsg_is_writable(mb)); /* Not shared, or would corrupt data */ g_assert(UNSIGNED(pmsg_available(mb)) >= n + 10); /* Need ule64 length */ g_assert_log(size_is_non_negative(n), "%s(): n=%zd", G_STRFUNC, n); len = vstrlen(str); len = MIN(n, len); pmsg_write_ule64(mb, (uint64) len); if (len != 0) { pmsg_write(mb, str, len); } }
/** * Write NUL-terminated string. * * If (size_t) -1 is given as length, then it is computed via strlen(), in * which case the string buffer must be NUL-terminated. Otherwise, the value * is taken to be the pre-computed string length. * * The string is written as: <ule64(length)><bytes>, no trailing NUL. */ void pmsg_write_string(pmsg_t *mb, const char *str, size_t length) { size_t len; g_assert(pmsg_is_writable(mb)); /* Not shared, or would corrupt data */ g_assert_log(size_is_non_negative(length) || (size_t) -1 == length, "%s(): length=%zd", G_STRFUNC, length); len = (size_t) -1 == length ? vstrlen(str) : length; g_assert(UNSIGNED(pmsg_available(mb)) >= len + 10); /* Need ule64 length */ pmsg_write_ule64(mb, (uint64) len); if (len != 0) { pmsg_write(mb, str, len); } }
/** * Attempts to fill the shell buffer using readline(), however, * the buffer is not further filled before it is completely empty. */ static int read_data_with_readline(struct line_buf *line, struct shell_buf *sb) #ifdef USE_READLINE { if (!line || !sb) { return -1; } if (0 == sb->fill) { if (!line->buf) { errno = 0; line->buf = readline(""); if (!line->buf && !is_temporary_error(errno)) { sb->eof = 1; } line->length = line->buf ? vstrlen(line->buf) : 0; line->pos = 0; } if (line->buf) { if (line->pos < line->length) { size_t n; n = line->length - line->pos; if (n > sb->size) { n = sb->size; } memcpy(sb->buf, &line->buf[line->pos], n); sb->fill = n; line->pos += n; } if (line->pos == line->length && sb->fill < sb->size) { sb->buf[sb->fill] = '\n'; sb->fill++; free(line->buf); line->buf = NULL; line->length = 0; line->pos = 0; } } } return 0; }