getExecResult code refine

pull/37/head
Yu Qing 2017-02-02 12:31:19 +08:00
parent 7ba58c02c0
commit ff60187490
2 changed files with 9 additions and 9 deletions

View File

@ -1209,7 +1209,7 @@ static int iniDoProccessSet(char *pSet, char **ppSetEnd,
{ {
char *cmd; char *cmd;
cmd = value + 2; cmd = value + 2;
*(value + value_len - 1) = '\0'; *(value + value_len - 1) = '\0'; //remove '}'
if ((result=getExecResult(cmd, output, sizeof(output))) != 0) if ((result=getExecResult(cmd, output, sizeof(output))) != 0)
{ {
logWarning("file: "__FILE__", line: %d, " logWarning("file: "__FILE__", line: %d, "
@ -1217,6 +1217,11 @@ static int iniDoProccessSet(char *pSet, char **ppSetEnd,
__LINE__, cmd, result, STRERROR(result)); __LINE__, cmd, result, STRERROR(result));
return result; return result;
} }
if (*output == '\0')
{
logWarning("file: "__FILE__", line: %d, "
"empty reply when exec: %s", __LINE__, cmd);
}
value = trim(output); value = trim(output);
value_len = strlen(value); value_len = strlen(value);
} }

View File

@ -387,27 +387,22 @@ int getExecResult(const char *command, char *output, const int buff_size)
if((fp=popen(command, "r")) == NULL) if((fp=popen(command, "r")) == NULL)
{ {
*output = '\0';
return errno != 0 ? errno : EMFILE; return errno != 0 ? errno : EMFILE;
} }
pCurrent = output; pCurrent = output;
remain_bytes = buff_size; remain_bytes = buff_size - 1;
while (remain_bytes > 0 && \ while (remain_bytes > 0 && \
(bytes_read=fread(pCurrent, 1, remain_bytes, fp)) > 0) (bytes_read=fread(pCurrent, 1, remain_bytes, fp)) > 0)
{ {
pCurrent += bytes_read; pCurrent += bytes_read;
remain_bytes -= bytes_read; remain_bytes -= bytes_read;
} }
pclose(fp); pclose(fp);
if (remain_bytes <= 0)
{
return ENOSPC;
}
*pCurrent = '\0'; *pCurrent = '\0';
return 0; return remain_bytes > 0 ? 0 : ENOSPC;
} }
#endif #endif