replace sprintf and snprintf as necessary
parent
63ef9aa8f4
commit
ec2db7cd33
3
HISTORY
3
HISTORY
|
|
@ -1,9 +1,10 @@
|
||||||
|
|
||||||
Version 1.78 2025-08-06
|
Version 1.78 2025-08-07
|
||||||
* getIpaddrByName: normalize ip addr when input addr is IPv4 or IPv6
|
* getIpaddrByName: normalize ip addr when input addr is IPv4 or IPv6
|
||||||
* add files: spinlock.[hc]
|
* add files: spinlock.[hc]
|
||||||
* shared_func.[hc]: change int2buff, buff2int etc. functions to static inline
|
* shared_func.[hc]: change int2buff, buff2int etc. functions to static inline
|
||||||
* shared_func.[hc]: add functions short2hex, int2hex, long2hex etc.
|
* shared_func.[hc]: add functions short2hex, int2hex, long2hex etc.
|
||||||
|
* performance opt.: replace sprintf and snprintf as necessary
|
||||||
|
|
||||||
Version 1.77 2025-03-18
|
Version 1.77 2025-03-18
|
||||||
* impl. shorten_path for /./ and /../
|
* impl. shorten_path for /./ and /../
|
||||||
|
|
|
||||||
|
|
@ -47,7 +47,7 @@ int array_allocator_init_ex(ArrayAllocatorContext *ctx,
|
||||||
start = end;
|
start = end;
|
||||||
}
|
}
|
||||||
|
|
||||||
snprintf(name, sizeof(name), "%s-array", name_prefix);
|
fc_combine_two_string(name_prefix, "array", '-', name);
|
||||||
return fast_allocator_init_ex(&ctx->allocator, name,
|
return fast_allocator_init_ex(&ctx->allocator, name,
|
||||||
obj_size, NULL, regions, region - regions, 0,
|
obj_size, NULL, regions, region - regions, 0,
|
||||||
0.9999, reclaim_interval, need_lock);
|
0.9999, reclaim_interval, need_lock);
|
||||||
|
|
|
||||||
|
|
@ -48,7 +48,7 @@ int buffered_file_writer_open_ex(BufferedFileWriter *writer,
|
||||||
return ENOMEM;
|
return ENOMEM;
|
||||||
}
|
}
|
||||||
|
|
||||||
snprintf(writer->filename, sizeof(writer->filename), "%s", filename);
|
fc_safe_strcpy(writer->filename, filename);
|
||||||
writer->fd = open(writer->filename, O_WRONLY |
|
writer->fd = open(writer->filename, O_WRONLY |
|
||||||
O_CREAT | O_TRUNC | O_CLOEXEC, mode);
|
O_CREAT | O_TRUNC | O_CLOEXEC, mode);
|
||||||
if (writer->fd < 0)
|
if (writer->fd < 0)
|
||||||
|
|
|
||||||
|
|
@ -540,6 +540,8 @@ static inline int fc_fallocate(int fd, const int64_t size)
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#define FC_MACRO_STRINGIFY(x) #x
|
||||||
|
#define FC_MACRO_TOSTRING(x) FC_MACRO_STRINGIFY(x)
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -584,7 +584,7 @@ int fast_mblock_init_ex2(struct fast_mblock_man *mblock, const char *name,
|
||||||
|
|
||||||
if (name != NULL)
|
if (name != NULL)
|
||||||
{
|
{
|
||||||
snprintf(mblock->info.name, sizeof(mblock->info.name), "%s", name);
|
fc_safe_strcpy(mblock->info.name, name);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
|
|
||||||
|
|
@ -142,7 +142,7 @@ int free_queue_init_ex2(struct fast_task_queue *queue, const char *name,
|
||||||
(int64_t)max_data_size);
|
(int64_t)max_data_size);
|
||||||
*/
|
*/
|
||||||
|
|
||||||
snprintf(aname, sizeof(aname), "%s-task", name);
|
fc_combine_two_string(name, "task", '-', aname);
|
||||||
return fast_mblock_init_ex1(&queue->allocator, aname,
|
return fast_mblock_init_ex1(&queue->allocator, aname,
|
||||||
queue->block_size, alloc_once, max_connections,
|
queue->block_size, alloc_once, max_connections,
|
||||||
(fast_mblock_object_init_func)task_alloc_init,
|
(fast_mblock_object_init_func)task_alloc_init,
|
||||||
|
|
|
||||||
|
|
@ -198,7 +198,7 @@ static int iniAnnotationFuncLocalIpGet(IniContext *context,
|
||||||
(int)(square_start - param), param);
|
(int)(square_start - param), param);
|
||||||
index = atoi(square_start + 1);
|
index = atoi(square_start + 1);
|
||||||
} else {
|
} else {
|
||||||
snprintf(name_part, sizeof(name_part) - 1, "%s", param);
|
fc_safe_strcpy(name_part, param);
|
||||||
index = -2;
|
index = -2;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -322,7 +322,7 @@ static char *doReplaceVars(IniContext *pContext, const char *param,
|
||||||
logWarning("file: "__FILE__", line: %d, "
|
logWarning("file: "__FILE__", line: %d, "
|
||||||
"NO set directives before, set value to %s",
|
"NO set directives before, set value to %s",
|
||||||
__LINE__, param);
|
__LINE__, param);
|
||||||
snprintf(output, FAST_INI_ITEM_VALUE_SIZE, "%s", param);
|
fc_strlcpy(output, param, FAST_INI_ITEM_VALUE_SIZE);
|
||||||
return output;
|
return output;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -652,7 +652,7 @@ int iniLoadFromFileEx(const char *szFilename, IniContext *pContext,
|
||||||
if (IS_URL_RESOURCE(szFilename))
|
if (IS_URL_RESOURCE(szFilename))
|
||||||
{
|
{
|
||||||
*pContext->config_path = '\0';
|
*pContext->config_path = '\0';
|
||||||
snprintf(full_filename, sizeof(full_filename), "%s", szFilename);
|
fc_safe_strcpy(full_filename, szFilename);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
|
@ -675,8 +675,7 @@ int iniLoadFromFileEx(const char *szFilename, IniContext *pContext,
|
||||||
|
|
||||||
memcpy(pContext->config_path, szFilename, len);
|
memcpy(pContext->config_path, szFilename, len);
|
||||||
*(pContext->config_path + len) = '\0';
|
*(pContext->config_path + len) = '\0';
|
||||||
snprintf(full_filename, sizeof(full_filename), \
|
fc_safe_strcpy(full_filename, szFilename);
|
||||||
"%s", szFilename);
|
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
|
@ -699,9 +698,8 @@ int iniLoadFromFileEx(const char *szFilename, IniContext *pContext,
|
||||||
*(pContext->config_path + len) = '\0';
|
*(pContext->config_path + len) = '\0';
|
||||||
}
|
}
|
||||||
|
|
||||||
snprintf(full_filename, sizeof(full_filename), \
|
fc_combine_full_filename(pContext->config_path,
|
||||||
"%s/%s", pContext->config_path, szFilename);
|
szFilename, full_filename);
|
||||||
|
|
||||||
pLast = strrchr(szFilename, '/');
|
pLast = strrchr(szFilename, '/');
|
||||||
if (pLast != NULL)
|
if (pLast != NULL)
|
||||||
{
|
{
|
||||||
|
|
@ -914,7 +912,7 @@ static int iniAddAnnotation(char *params)
|
||||||
return EFAULT;
|
return EFAULT;
|
||||||
}
|
}
|
||||||
|
|
||||||
snprintf(symbol, sizeof(symbol), "%s_init_annotation", func_name);
|
fc_combine_two_string(func_name, "init_annotation", '_', symbol);
|
||||||
init_func = dlsym(dlhandle, symbol);
|
init_func = dlsym(dlhandle, symbol);
|
||||||
if (init_func == NULL)
|
if (init_func == NULL)
|
||||||
{
|
{
|
||||||
|
|
@ -1013,14 +1011,12 @@ static int iniDoLoadItemsFromBuffer(char *content, IniContext *pContext)
|
||||||
strncasecmp(pLine+1, "include", 7) == 0 && \
|
strncasecmp(pLine+1, "include", 7) == 0 && \
|
||||||
(*(pLine+8) == ' ' || *(pLine+8) == '\t'))
|
(*(pLine+8) == ' ' || *(pLine+8) == '\t'))
|
||||||
{
|
{
|
||||||
snprintf(pIncludeFilename, sizeof(pIncludeFilename),
|
fc_safe_strcpy(pIncludeFilename, pLine + 9);
|
||||||
"%s", pLine + 9);
|
|
||||||
fc_trim(pIncludeFilename);
|
fc_trim(pIncludeFilename);
|
||||||
if (IS_URL_RESOURCE(pIncludeFilename))
|
if (IS_URL_RESOURCE(pIncludeFilename))
|
||||||
{
|
{
|
||||||
snprintf(full_filename, sizeof(full_filename),
|
fc_safe_strcpy(full_filename, pIncludeFilename);
|
||||||
"%s", pIncludeFilename);
|
}
|
||||||
}
|
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
if (IS_FILE_RESOURCE(pIncludeFilename))
|
if (IS_FILE_RESOURCE(pIncludeFilename))
|
||||||
|
|
@ -1034,25 +1030,23 @@ static int iniDoLoadItemsFromBuffer(char *content, IniContext *pContext)
|
||||||
|
|
||||||
if (*pTrueFilename == '/')
|
if (*pTrueFilename == '/')
|
||||||
{
|
{
|
||||||
snprintf(full_filename, sizeof(full_filename), \
|
fc_safe_strcpy(full_filename, pTrueFilename);
|
||||||
"%s", pTrueFilename);
|
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
snprintf(full_filename, sizeof(full_filename), \
|
fc_combine_full_filename(pContext->config_path,
|
||||||
"%s/%s", pContext->config_path, \
|
pTrueFilename, full_filename);
|
||||||
pTrueFilename);
|
}
|
||||||
}
|
|
||||||
|
|
||||||
if (!fileExists(full_filename))
|
if (!fileExists(full_filename))
|
||||||
{
|
{
|
||||||
logError("file: "__FILE__", line: %d, " \
|
logError("file: "__FILE__", line: %d, "
|
||||||
"include file \"%s\" not exists, " \
|
"include file \"%s\" not exists, "
|
||||||
"line: \"%s\"", __LINE__, \
|
"line: \"%s\"", __LINE__,
|
||||||
pTrueFilename, pLine);
|
pTrueFilename, pLine);
|
||||||
result = ENOENT;
|
result = ENOENT;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pContext->current_section = &pContext->global;
|
pContext->current_section = &pContext->global;
|
||||||
|
|
@ -2816,7 +2810,7 @@ do { \
|
||||||
break; \
|
break; \
|
||||||
} \
|
} \
|
||||||
\
|
\
|
||||||
snprintf(targetItem.name, sizeof(targetItem.name), "%s", szItemName); \
|
fc_safe_strcpy(targetItem.name, szItemName); \
|
||||||
pItem = (IniItem *)bsearch(&targetItem, pSection->items, \
|
pItem = (IniItem *)bsearch(&targetItem, pSection->items, \
|
||||||
pSection->count, sizeof(IniItem), iniCompareByItemName); \
|
pSection->count, sizeof(IniItem), iniCompareByItemName); \
|
||||||
} while (0)
|
} while (0)
|
||||||
|
|
|
||||||
32
src/logger.c
32
src/logger.c
|
|
@ -50,21 +50,21 @@ static int log_fsync(LogContext *pContext, const bool bNeedLock);
|
||||||
|
|
||||||
static int check_and_mk_log_dir(const char *base_path)
|
static int check_and_mk_log_dir(const char *base_path)
|
||||||
{
|
{
|
||||||
char data_path[MAX_PATH_SIZE];
|
char log_path[MAX_PATH_SIZE];
|
||||||
|
|
||||||
snprintf(data_path, sizeof(data_path), "%s/logs", base_path);
|
fc_combine_full_filename(base_path, "logs", log_path);
|
||||||
if (!fileExists(data_path))
|
if (!fileExists(log_path))
|
||||||
{
|
{
|
||||||
if (mkdir(data_path, 0755) != 0)
|
if (mkdir(log_path, 0755) != 0)
|
||||||
{
|
{
|
||||||
fprintf(stderr, "mkdir \"%s\" fail, " \
|
fprintf(stderr, "mkdir \"%s\" fail, "
|
||||||
"errno: %d, error info: %s\n", \
|
"errno: %d, error info: %s\n",
|
||||||
data_path, errno, STRERROR(errno));
|
log_path, errno, STRERROR(errno));
|
||||||
return errno != 0 ? errno : EPERM;
|
return errno != 0 ? errno : EPERM;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int log_init()
|
int log_init()
|
||||||
|
|
@ -249,7 +249,7 @@ int log_set_filename_ex(LogContext *pContext, const char *log_filename)
|
||||||
|
|
||||||
if (*(pContext->log_filename) == '\0')
|
if (*(pContext->log_filename) == '\0')
|
||||||
{
|
{
|
||||||
snprintf(pContext->log_filename, MAX_PATH_SIZE, "%s", log_filename);
|
fc_strlcpy(pContext->log_filename, log_filename, MAX_PATH_SIZE);
|
||||||
return log_open(pContext);
|
return log_open(pContext);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -258,7 +258,7 @@ int log_set_filename_ex(LogContext *pContext, const char *log_filename)
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
snprintf(pContext->log_filename, MAX_PATH_SIZE, "%s", log_filename);
|
fc_strlcpy(pContext->log_filename, log_filename, MAX_PATH_SIZE);
|
||||||
return log_reopen_ex(pContext);
|
return log_reopen_ex(pContext);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -390,7 +390,7 @@ static int log_delete_old_file(LogContext *pContext,
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
snprintf(full_filename, sizeof(full_filename), "%s", old_filename);
|
fc_safe_strcpy(full_filename, old_filename);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (unlink(full_filename) != 0)
|
if (unlink(full_filename) != 0)
|
||||||
|
|
|
||||||
|
|
@ -313,8 +313,8 @@ static inline void fc_server_set_ip_prefix(FCServerGroupInfo *ginfo,
|
||||||
{
|
{
|
||||||
ginfo->filter.ip_prefix.str = ginfo->filter.prefix_buff;
|
ginfo->filter.ip_prefix.str = ginfo->filter.prefix_buff;
|
||||||
if (ip_prefix != NULL) {
|
if (ip_prefix != NULL) {
|
||||||
ginfo->filter.ip_prefix.len = snprintf(ginfo->filter.prefix_buff,
|
ginfo->filter.ip_prefix.len = fc_safe_strcpy(
|
||||||
sizeof(ginfo->filter.prefix_buff) - 1, "%s", ip_prefix);
|
ginfo->filter.prefix_buff, ip_prefix);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -146,7 +146,7 @@ char *getAbsolutePath(const char *filename, char *szAbsPath, \
|
||||||
|
|
||||||
if (szPath[0] == '/')
|
if (szPath[0] == '/')
|
||||||
{
|
{
|
||||||
snprintf(szAbsPath, pathSize, "%s", szPath);
|
fc_strlcpy(szAbsPath, szPath, pathSize);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
|
@ -166,12 +166,13 @@ char *getAbsolutePath(const char *filename, char *szAbsPath, \
|
||||||
|
|
||||||
if (szPath[0] != '\0')
|
if (szPath[0] != '\0')
|
||||||
{
|
{
|
||||||
snprintf(szAbsPath, pathSize, "%s/%s", cwd, szPath);
|
fc_get_full_filepath_ex(cwd, strlen(cwd), szPath,
|
||||||
}
|
strlen(szPath), szAbsPath, pathSize);
|
||||||
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
snprintf(szAbsPath, pathSize, "%s", cwd);
|
fc_strlcpy(szAbsPath, cwd, pathSize);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return szAbsPath;
|
return szAbsPath;
|
||||||
|
|
@ -206,8 +207,7 @@ char *getExeAbsoluteFilename(const char *exeFilename, char *szAbsFilename, \
|
||||||
filename = exeFilename;
|
filename = exeFilename;
|
||||||
for (i=0; i<3; i++)
|
for (i=0; i<3; i++)
|
||||||
{
|
{
|
||||||
snprintf(cwd, sizeof(cwd), "%s/%s", \
|
fc_combine_full_filename(search_paths[i], filename, cwd);
|
||||||
search_paths[i], filename);
|
|
||||||
if (fileExists(cwd))
|
if (fileExists(cwd))
|
||||||
{
|
{
|
||||||
strcpy(szPath, search_paths[i]);
|
strcpy(szPath, search_paths[i]);
|
||||||
|
|
@ -226,11 +226,11 @@ char *getExeAbsoluteFilename(const char *exeFilename, char *szAbsFilename, \
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
snprintf(szAbsFilename, maxSize, "%s/%s", \
|
fc_get_full_filename_ex(szPath, strlen(szPath), filename,
|
||||||
szPath, filename);
|
strlen(filename), szAbsFilename, maxSize);
|
||||||
return szAbsFilename;
|
return szAbsFilename;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
|
@ -242,8 +242,9 @@ char *getExeAbsoluteFilename(const char *exeFilename, char *szAbsFilename, \
|
||||||
|
|
||||||
if (*szPath == '/')
|
if (*szPath == '/')
|
||||||
{
|
{
|
||||||
snprintf(szAbsFilename, maxSize, "%s/%s", szPath, filename);
|
fc_get_full_filename_ex(szPath, strlen(szPath), filename,
|
||||||
}
|
strlen(filename), szAbsFilename, maxSize);
|
||||||
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
if (getcwd(cwd, sizeof(cwd)) == NULL)
|
if (getcwd(cwd, sizeof(cwd)) == NULL)
|
||||||
|
|
@ -262,14 +263,14 @@ char *getExeAbsoluteFilename(const char *exeFilename, char *szAbsFilename, \
|
||||||
|
|
||||||
if (*szPath != '\0')
|
if (*szPath != '\0')
|
||||||
{
|
{
|
||||||
snprintf(szAbsFilename, maxSize, "%s/%s/%s", \
|
snprintf(szAbsFilename, maxSize, "%s/%s/%s",
|
||||||
cwd, szPath, filename);
|
cwd, szPath, filename);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
snprintf(szAbsFilename, maxSize, "%s/%s", \
|
fc_get_full_filename_ex(cwd, strlen(cwd), filename,
|
||||||
cwd, filename);
|
strlen(filename), szAbsFilename, maxSize);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return szAbsFilename;
|
return szAbsFilename;
|
||||||
|
|
@ -328,16 +329,16 @@ int getUserProcIds(const char *progName, const bool bAllOwners, \
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
sprintf(fullpath, "%s/%s", path, dirp->d_name);
|
fc_combine_full_filepath(path, dirp->d_name, fullpath);
|
||||||
memset(&statbuf, 0, sizeof(statbuf));
|
memset(&statbuf, 0, sizeof(statbuf));
|
||||||
if (lstat(fullpath, &statbuf) < 0)
|
if (lstat(fullpath, &statbuf) < 0)
|
||||||
{
|
{
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
if ((bAllOwners || (statbuf.st_uid == myuid)) && S_ISDIR(statbuf.st_mode))
|
if ((bAllOwners || (statbuf.st_uid == myuid)) && S_ISDIR(statbuf.st_mode))
|
||||||
{
|
{
|
||||||
sprintf(filepath, "%s/cmdline", fullpath);
|
fc_combine_full_filename(fullpath, "cmdline", filepath);
|
||||||
if ((fd=open(filepath, O_RDONLY | O_CLOEXEC))<0)
|
if ((fd=open(filepath, O_RDONLY | O_CLOEXEC))<0)
|
||||||
{
|
{
|
||||||
continue;
|
continue;
|
||||||
|
|
@ -358,12 +359,12 @@ int getUserProcIds(const char *progName, const bool bAllOwners, \
|
||||||
ptr = strrchr(buf, '/');
|
ptr = strrchr(buf, '/');
|
||||||
if (ptr == NULL)
|
if (ptr == NULL)
|
||||||
{
|
{
|
||||||
snprintf(procname, 64, "%s", buf);
|
fc_safe_strcpy(procname, buf);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
snprintf(procname, 64, "%s", ptr + 1);
|
fc_safe_strcpy(procname, ptr + 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (strcmp(procname, pTargetProg) == 0)
|
if (strcmp(procname, pTargetProg) == 0)
|
||||||
{
|
{
|
||||||
|
|
@ -1452,13 +1453,13 @@ int writeToFile(const char *filename, const char *buff, const int file_size)
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int safeWriteToFile(const char *filename, const char *buff, \
|
int safeWriteToFile(const char *filename, const char *buff,
|
||||||
const int file_size)
|
const int file_size)
|
||||||
{
|
{
|
||||||
char tmpFilename[PATH_MAX];
|
char tmpFilename[PATH_MAX];
|
||||||
int result;
|
int result;
|
||||||
|
|
||||||
snprintf(tmpFilename, sizeof(tmpFilename), "%s.tmp", filename);
|
fc_combine_two_string(filename, "tmp", '.', tmpFilename);
|
||||||
if ((result=writeToFile(tmpFilename, buff, file_size)) != 0)
|
if ((result=writeToFile(tmpFilename, buff, file_size)) != 0)
|
||||||
{
|
{
|
||||||
return result;
|
return result;
|
||||||
|
|
@ -1555,13 +1556,11 @@ int fc_copy_to_path(const char *src_filename, const char *dest_path)
|
||||||
fname = strrchr(src_filename, '/');
|
fname = strrchr(src_filename, '/');
|
||||||
if (fname == NULL)
|
if (fname == NULL)
|
||||||
{
|
{
|
||||||
snprintf(dest_filename, sizeof(dest_filename),
|
fc_combine_full_filename(dest_path, src_filename, dest_filename);
|
||||||
"%s/%s", dest_path, src_filename);
|
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
snprintf(dest_filename, sizeof(dest_filename),
|
fc_combine_full_filename(dest_path, fname, dest_filename);
|
||||||
"%s%s", dest_path, fname);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return fc_copy_file(src_filename, dest_filename);
|
return fc_copy_file(src_filename, dest_filename);
|
||||||
|
|
@ -3233,8 +3232,7 @@ static void add_thousands_separator(char *str, const int len)
|
||||||
const char *int2str(const int n, char *buff, const bool thousands_separator)
|
const char *int2str(const int n, char *buff, const bool thousands_separator)
|
||||||
{
|
{
|
||||||
int len;
|
int len;
|
||||||
len = fc_itoa(n, buff);
|
len = fc_ltostr(n, buff);
|
||||||
*(buff + len) = '\0';
|
|
||||||
if (thousands_separator)
|
if (thousands_separator)
|
||||||
{
|
{
|
||||||
add_thousands_separator(buff, len);
|
add_thousands_separator(buff, len);
|
||||||
|
|
@ -3245,8 +3243,7 @@ const char *int2str(const int n, char *buff, const bool thousands_separator)
|
||||||
const char *long2str(const int64_t n, char *buff, const bool thousands_separator)
|
const char *long2str(const int64_t n, char *buff, const bool thousands_separator)
|
||||||
{
|
{
|
||||||
int len;
|
int len;
|
||||||
len = fc_itoa(n, buff);
|
len = fc_ltostr(n, buff);
|
||||||
*(buff + len) = '\0';
|
|
||||||
if (thousands_separator)
|
if (thousands_separator)
|
||||||
{
|
{
|
||||||
add_thousands_separator(buff, len);
|
add_thousands_separator(buff, len);
|
||||||
|
|
@ -4216,15 +4213,13 @@ int fc_safe_write_file_init(SafeWriteFileInfo *fi,
|
||||||
{
|
{
|
||||||
char full_filename[PATH_MAX];
|
char full_filename[PATH_MAX];
|
||||||
|
|
||||||
snprintf(full_filename, sizeof(full_filename), "%s/%s",
|
fc_combine_full_filename(file_path, redo_filename, full_filename);
|
||||||
file_path, redo_filename);
|
|
||||||
if ((fi->filename=fc_strdup(full_filename)) == NULL)
|
if ((fi->filename=fc_strdup(full_filename)) == NULL)
|
||||||
{
|
{
|
||||||
return ENOMEM;
|
return ENOMEM;
|
||||||
}
|
}
|
||||||
|
|
||||||
snprintf(full_filename, sizeof(full_filename), "%s/%s",
|
fc_combine_full_filename(file_path, tmp_filename, full_filename);
|
||||||
file_path, tmp_filename);
|
|
||||||
if ((fi->tmp_filename=fc_strdup(full_filename)) == NULL)
|
if ((fi->tmp_filename=fc_strdup(full_filename)) == NULL)
|
||||||
{
|
{
|
||||||
return ENOMEM;
|
return ENOMEM;
|
||||||
|
|
|
||||||
|
|
@ -1230,6 +1230,67 @@ static inline int resolve_path(const char *from, const char *filename,
|
||||||
size, NORMALIZE_FLAGS_URL_ENABLED_AND_APPEND_PARAMS);
|
size, NORMALIZE_FLAGS_URL_ENABLED_AND_APPEND_PARAMS);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static inline int fc_combine_two_string_ex(
|
||||||
|
const char *first_str, const int first_len,
|
||||||
|
const char *second_str, const int second_len,
|
||||||
|
const char seperator, char *combined_str, const int size)
|
||||||
|
{
|
||||||
|
char *p;
|
||||||
|
|
||||||
|
if (first_len + 1 + second_len >= size) {
|
||||||
|
return snprintf(combined_str, size, "%s%c%s",
|
||||||
|
first_str, seperator, second_str);
|
||||||
|
}
|
||||||
|
|
||||||
|
memcpy(combined_str, first_str, first_len);
|
||||||
|
p = combined_str + first_len;
|
||||||
|
*p++ = seperator;
|
||||||
|
memcpy(p, second_str, second_len);
|
||||||
|
p += second_len;
|
||||||
|
*p = '\0';
|
||||||
|
return p - combined_str;
|
||||||
|
}
|
||||||
|
|
||||||
|
#define fc_combine_two_string_s(first, second, seperator, combined, size) \
|
||||||
|
fc_combine_two_string_ex(first, strlen(first), second, strlen(second), \
|
||||||
|
seperator, combined, size)
|
||||||
|
|
||||||
|
#define fc_combine_two_string(first, second, seperator, combined) \
|
||||||
|
fc_combine_two_string_s(first, second, seperator, \
|
||||||
|
combined, sizeof(combined))
|
||||||
|
|
||||||
|
static inline int fc_get_full_filename_ex(
|
||||||
|
const char *base_path_str, const int base_path_len,
|
||||||
|
const char *filename_str, const int filename_len,
|
||||||
|
char *full_filename, const int size)
|
||||||
|
{
|
||||||
|
const char seperator = '/';
|
||||||
|
return fc_combine_two_string_ex(base_path_str, base_path_len,
|
||||||
|
filename_str, filename_len, seperator, full_filename, size);
|
||||||
|
}
|
||||||
|
|
||||||
|
#define fc_get_full_filename(base_path_str, base_path_len, \
|
||||||
|
filename_str, filename_len, full_filename) \
|
||||||
|
fc_get_full_filename_ex(base_path_str, base_path_len, \
|
||||||
|
filename_str, filename_len, full_filename, sizeof(full_filename))
|
||||||
|
|
||||||
|
#define fc_combine_full_filename(base_path, filename, full_filename) \
|
||||||
|
fc_get_full_filename(base_path, strlen(base_path), \
|
||||||
|
filename, strlen(filename), full_filename)
|
||||||
|
|
||||||
|
#define fc_get_full_filepath_ex(base_path_str, base_path_len, \
|
||||||
|
filepath_str, filepath_len, full_filename, size) \
|
||||||
|
fc_get_full_filename_ex(base_path_str, base_path_len, \
|
||||||
|
filepath_str, filepath_len, full_filename, size)
|
||||||
|
|
||||||
|
#define fc_get_full_filepath(base_path_str, base_path_len, \
|
||||||
|
filepath_str, filepath_len, full_filename) \
|
||||||
|
fc_get_full_filename(base_path_str, base_path_len, \
|
||||||
|
filepath_str, filepath_len, full_filename)
|
||||||
|
|
||||||
|
#define fc_combine_full_filepath(base_path, filepath, full_filename) \
|
||||||
|
fc_combine_full_filename(base_path, filepath, full_filename)
|
||||||
|
|
||||||
/** get gzip command full filename
|
/** get gzip command full filename
|
||||||
* return: the gzip command full filename
|
* return: the gzip command full filename
|
||||||
*/
|
*/
|
||||||
|
|
@ -1365,6 +1426,29 @@ bool fc_path_contains(const string_t *path, const string_t *needle,
|
||||||
*/
|
*/
|
||||||
int fc_itoa(int64_t n, char *buff);
|
int fc_itoa(int64_t n, char *buff);
|
||||||
|
|
||||||
|
static inline int fc_ltostr_ex(int64_t n, char *buff, const int padding_len)
|
||||||
|
{
|
||||||
|
int len;
|
||||||
|
int fill_len;
|
||||||
|
|
||||||
|
len = fc_itoa(n, buff);
|
||||||
|
*(buff + len) = '\0';
|
||||||
|
if (padding_len <= len) {
|
||||||
|
return len;
|
||||||
|
}
|
||||||
|
|
||||||
|
fill_len = padding_len - len;
|
||||||
|
memmove(buff + fill_len, buff, len + 1);
|
||||||
|
memset(buff, '0', fill_len);
|
||||||
|
return padding_len;
|
||||||
|
}
|
||||||
|
|
||||||
|
static inline int fc_ltostr(int64_t n, char *buff)
|
||||||
|
{
|
||||||
|
const int padding_len = 0;
|
||||||
|
return fc_ltostr_ex(n, buff, padding_len);
|
||||||
|
}
|
||||||
|
|
||||||
static inline size_t fc_strlcpy(char *dest, const char *src, const size_t size)
|
static inline size_t fc_strlcpy(char *dest, const char *src, const size_t size)
|
||||||
{
|
{
|
||||||
int len;
|
int len;
|
||||||
|
|
@ -1381,6 +1465,8 @@ static inline size_t fc_strlcpy(char *dest, const char *src, const size_t size)
|
||||||
return len;
|
return len;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#define fc_safe_strcpy(dest, src) fc_strlcpy(dest, src, sizeof(dest))
|
||||||
|
|
||||||
/** sleep in microseconds
|
/** sleep in microseconds
|
||||||
* parameters:
|
* parameters:
|
||||||
* microseconds: microseconds to sleep
|
* microseconds: microseconds to sleep
|
||||||
|
|
|
||||||
|
|
@ -1194,7 +1194,7 @@ char *getHostnameByIp(const char *szIpAddr, char *buff, const int bufferSize)
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
snprintf(buff, bufferSize, "%s", ent->h_name);
|
fc_strlcpy(buff, ent->h_name, bufferSize);
|
||||||
}
|
}
|
||||||
|
|
||||||
return buff;
|
return buff;
|
||||||
|
|
@ -1402,14 +1402,12 @@ int nbaccept(int sock, const int timeout, int *err_no)
|
||||||
int socketBind2(int af, int sock, const char *bind_ipaddr, const int port)
|
int socketBind2(int af, int sock, const char *bind_ipaddr, const int port)
|
||||||
{
|
{
|
||||||
sockaddr_convert_t convert;
|
sockaddr_convert_t convert;
|
||||||
char bind_ip_prompt[256];
|
|
||||||
int result;
|
int result;
|
||||||
|
|
||||||
memset(&convert, 0, sizeof(convert));
|
memset(&convert, 0, sizeof(convert));
|
||||||
convert.sa.addr.sa_family = af;
|
convert.sa.addr.sa_family = af;
|
||||||
if (bind_ipaddr == NULL || *bind_ipaddr == '\0')
|
if (bind_ipaddr == NULL || *bind_ipaddr == '\0')
|
||||||
{
|
{
|
||||||
*bind_ip_prompt = '\0';
|
|
||||||
if (af == AF_INET)
|
if (af == AF_INET)
|
||||||
{
|
{
|
||||||
convert.len = sizeof(convert.sa.addr4);
|
convert.len = sizeof(convert.sa.addr4);
|
||||||
|
|
@ -1429,11 +1427,15 @@ int socketBind2(int af, int sock, const char *bind_ipaddr, const int port)
|
||||||
{
|
{
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
sprintf(bind_ip_prompt, "bind ip %s, ", bind_ipaddr);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (bind(sock, &convert.sa.addr, convert.len) < 0)
|
if (bind(sock, &convert.sa.addr, convert.len) < 0) {
|
||||||
{
|
char bind_ip_prompt[256];
|
||||||
|
if (bind_ipaddr == NULL || *bind_ipaddr == '\0') {
|
||||||
|
*bind_ip_prompt = '\0';
|
||||||
|
} else {
|
||||||
|
sprintf(bind_ip_prompt, "bind ip %s, ", bind_ipaddr);
|
||||||
|
}
|
||||||
logError("file: "__FILE__", line: %d, "
|
logError("file: "__FILE__", line: %d, "
|
||||||
"%sbind port %d failed, "
|
"%sbind port %d failed, "
|
||||||
"errno: %d, error info: %s.",
|
"errno: %d, error info: %s.",
|
||||||
|
|
@ -2577,10 +2579,14 @@ static inline int formatifmac(char *buff, const int buff_size,
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
dest = buff + sprintf(buff, "%02x", *hwaddr);
|
dest = buff;
|
||||||
|
*dest++ = g_lower_hex_chars[*hwaddr >> 4];
|
||||||
|
*dest++ = g_lower_hex_chars[*hwaddr & 0x0F];
|
||||||
for (ptr=hwaddr+1; ptr<end; ptr++)
|
for (ptr=hwaddr+1; ptr<end; ptr++)
|
||||||
{
|
{
|
||||||
dest += sprintf(dest, ":%02x", *ptr);
|
*dest++ = ':';
|
||||||
|
*dest++ = g_lower_hex_chars[*ptr >> 4];
|
||||||
|
*dest++ = g_lower_hex_chars[*ptr & 0x0F];
|
||||||
}
|
}
|
||||||
return dest - buff;
|
return dest - buff;
|
||||||
}
|
}
|
||||||
|
|
@ -2629,7 +2635,7 @@ static int getifmac(FastIFConfig *config)
|
||||||
fc_trim(output);
|
fc_trim(output);
|
||||||
if (*output != '\0')
|
if (*output != '\0')
|
||||||
{
|
{
|
||||||
snprintf(config->mac, sizeof(config->mac), "%s", output);
|
fc_safe_strcpy(config->mac, output);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -2733,7 +2739,7 @@ int getifconfigs(FastIFConfig *if_configs, const int max_count, int *count)
|
||||||
return ENOSPC;
|
return ENOSPC;
|
||||||
}
|
}
|
||||||
|
|
||||||
sprintf(config->name, "%s", ifc->ifa_name);
|
strcpy(config->name, ifc->ifa_name);
|
||||||
(*count)++;
|
(*count)++;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -194,9 +194,9 @@ int get_boot_time(struct timeval *boot_time)
|
||||||
|
|
||||||
#define SET_MNT_FIELDS(left, fstypename, mntfromname, mntonname) \
|
#define SET_MNT_FIELDS(left, fstypename, mntfromname, mntonname) \
|
||||||
do { \
|
do { \
|
||||||
snprintf(left.f_fstypename, sizeof(left.f_fstypename), "%s", fstypename); \
|
fc_safe_strcpy(left.f_fstypename, fstypename); \
|
||||||
snprintf(left.f_mntfromname, sizeof(left.f_mntfromname), "%s", mntfromname); \
|
fc_safe_strcpy(left.f_mntfromname, mntfromname); \
|
||||||
snprintf(left.f_mntonname, sizeof(left.f_mntonname), "%s", mntonname); \
|
fc_safe_strcpy(left.f_mntonname, mntonname); \
|
||||||
} while (0)
|
} while (0)
|
||||||
|
|
||||||
#ifdef OS_LINUX
|
#ifdef OS_LINUX
|
||||||
|
|
@ -286,12 +286,9 @@ int get_mounted_filesystems(struct fast_statfs *stats,
|
||||||
toLowercase(fstypename);
|
toLowercase(fstypename);
|
||||||
if (get_device_type(fstypename, &stats[*count].device_type) == 0)
|
if (get_device_type(fstypename, &stats[*count].device_type) == 0)
|
||||||
{
|
{
|
||||||
snprintf(stats[*count].f_mntfromname, sizeof(stats[*count].
|
fc_safe_strcpy(stats[*count].f_mntfromname, mntfromname);
|
||||||
f_mntfromname), "%s", mntfromname);
|
fc_safe_strcpy(stats[*count].f_mntonname, mntonname);
|
||||||
snprintf(stats[*count].f_mntonname, sizeof(stats[*count].
|
fc_safe_strcpy(stats[*count].f_fstypename, fstypename);
|
||||||
f_mntonname), "%s", mntonname);
|
|
||||||
snprintf(stats[*count].f_fstypename, sizeof(stats[*count].
|
|
||||||
f_fstypename), "%s", fstypename);
|
|
||||||
(*count)++;
|
(*count)++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -841,8 +838,7 @@ int get_processes(struct fast_process_info **processes, int *count)
|
||||||
for (i=0; i<nproc; i++)
|
for (i=0; i<nproc; i++)
|
||||||
{
|
{
|
||||||
process->field_count = 9;
|
process->field_count = 9;
|
||||||
snprintf(process->comm, sizeof(process->comm),
|
fc_safe_strcpy(process->comm, procs[i].ki_comm);
|
||||||
"%s", procs[i].ki_comm);
|
|
||||||
process->pid = procs[i].ki_pid;
|
process->pid = procs[i].ki_pid;
|
||||||
process->ppid = procs[i].ki_ppid;
|
process->ppid = procs[i].ki_ppid;
|
||||||
process->starttime = procs[i].ki_start;
|
process->starttime = procs[i].ki_start;
|
||||||
|
|
@ -1041,8 +1037,7 @@ static int get_block_size_by_write(const char *path, int *block_size)
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
snprintf(tmp_filename, sizeof(tmp_filename),
|
fc_combine_full_filename(path, ".blksize-test.tmp", tmp_filename);
|
||||||
"%s/.blksize-test.tmp", path);
|
|
||||||
if ((fd=open(tmp_filename, O_WRONLY | O_CREAT |
|
if ((fd=open(tmp_filename, O_WRONLY | O_CREAT |
|
||||||
O_DIRECT | O_CLOEXEC, 0755)) < 0)
|
O_DIRECT | O_CLOEXEC, 0755)) < 0)
|
||||||
{
|
{
|
||||||
|
|
|
||||||
|
|
@ -210,7 +210,7 @@ int fc_thread_pool_init_ex(FCThreadPool *pool, const char *name,
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
snprintf(pool->name, sizeof(pool->name), "%s", name);
|
fc_safe_strcpy(pool->name, name);
|
||||||
pool->stack_size = stack_size;
|
pool->stack_size = stack_size;
|
||||||
pool->max_idle_time = max_idle_time;
|
pool->max_idle_time = max_idle_time;
|
||||||
if (min_idle_count > limit) {
|
if (min_idle_count > limit) {
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue