static const char *test_c_snprintf(void) { char buf[100]; memset(buf, 'x', sizeof(buf)); ASSERT_EQ(c_snprintf(buf, sizeof(buf), "boo"), 3); ASSERT_STREQ(buf, "boo"); memset(buf, 'x', sizeof(buf)); ASSERT_EQ(c_snprintf(buf, 0, "boo"), 3); ASSERT_EQ(buf[0], 'x'); memset(buf, 'x', sizeof(buf)); ASSERT_EQ( c_snprintf(buf, sizeof(buf), "/%s%c [%.*s]", "foo", 'q', 3, "123456789"), 11); ASSERT_STREQ(buf, "/fooq [123]"); memset(buf, 'x', sizeof(buf)); ASSERT(c_snprintf(buf, sizeof(buf), "%d %x %04x %lu %ld", -2194, 0xabcdef01, 11, (unsigned long) 354523323, (long) 17) > 0); ASSERT_STREQ(buf, "-2194 abcdef01 000b 354523323 17"); memset(buf, 'x', sizeof(buf)); /* ASSERT_EQ(c_snprintf(buf, sizeof(buf), "%*sfoo", 3, ""), 3); */ c_snprintf(buf, sizeof(buf), "%*sfoo", 3, ""); ASSERT_STREQ(buf, " foo"); return NULL; }
static int console_flush_buffer(struct cache *cache) { LOG(LL_DEBUG, ("file id=%d", (int) s_last_file_id)); int ret = 0, fn_len, files_count; FILE *file = NULL; files_count = cache->file_names.len / FILENAME_LEN; if (files_count >= get_cfg()->console.file_cache_max) { LOG(LL_ERROR, ("Out of space")); ret = -1; /* Game is over, no space to flash buffer */ if (files_count == get_cfg()->console.file_cache_max) { mbuf_free(&cache->logs); mbuf_append(&cache->logs, s_out_of_space_msg, sizeof(s_out_of_space_msg) - 1); /* Let write last phrase */ } else { goto clean; } } char file_name[sizeof(LOG_FILENAME_BASE) + FILE_ID_LEN]; fn_len = c_snprintf(file_name, sizeof(file_name), FILENAME_PATTERN, LOG_FILENAME_BASE, s_last_file_id); if (fn_len != FILENAME_LEN - 1) { LOG(LL_ERROR, ("Wrong file name")); /* Internal error, should not happen */ ret = -1; goto clean; } file = fopen(file_name, "w"); if (file == NULL) { LOG(LL_ERROR, ("Failed to open %s", file_name)); ret = -1; goto clean; } /* Put data */ if (fwrite(cache->logs.buf, 1, cache->logs.len, file) != cache->logs.len) { LOG(LL_ERROR, ("Failed to write %d bytes to %s", (int) cache->logs.len, file_name)); ret = -1; goto clean; } /* Using mbuf here instead of list to avoid memory overhead */ mbuf_append(&cache->file_names, file_name, FILENAME_LEN); mbuf_free(&cache->logs); s_last_file_id++; clean: if (file != NULL) { fclose(file); } return ret; }
int ICACHE_FLASH_ATTR prv_getRegisterPayload(lwm2m_context_t * contextP, uint8_t * buffer, size_t length) { int index; int i; int result; // index can not be greater than length index = 0; if ((contextP->altPath != NULL) && (contextP->altPath[0] != 0)) { result = c_snprintf((char *)buffer, length, REG_ALT_PATH_LINK, contextP->altPath); } else { result = c_snprintf((char *)buffer, length, REG_ALT_PATH_LINK, "/"); } if (result > 0 && result <= length) { index = result; } else { return 0; } for (i = 0 ; i < contextP->numObject ; i++) { if (contextP->objectList[i]->objID == LWM2M_SECURITY_OBJECT_ID) continue; if (contextP->objectList[i]->instanceList == NULL) { result = c_snprintf((char *)buffer + index, length - index, REG_OBJECT_PATH, contextP->altPath?contextP->altPath:"", contextP->objectList[i]->objID); if (result > 0 && result <= length - index) { index += result; } else { return 0; } } else { lwm2m_list_t * targetP; for (targetP = contextP->objectList[i]->instanceList ; targetP != NULL ; targetP = targetP->next) { result = c_snprintf((char *)buffer + index, length - index, REG_OBJECT_INSTANCE_PATH, contextP->altPath?contextP->altPath:"", contextP->objectList[i]->objID, targetP->id); if (result > 0 && result <= length - index) { index += result; } else { return 0; } } } } if (index > 0) { index = index - 1; // remove trailing ',' } buffer[index] = 0; return index; }