我最近创建了一个程序,它应该带两个环境变量,将它们插入一个字符串,然后将字符串发送到popen,它在大多数情况下工作正常,但由于某种原因它会随机不执行每个所以经常,我只是想知道是否有人看到我正在制作任何可能的错误或错误?
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, char *argv[], char *envp[]) {
char *output;
char *ccode;
char *command;
char *log;
command = malloc(1024);
log = malloc(1024);
const char *parg = getenv("MCEXEC_ARGS");
const char *pname = getenv("MCEXEC_PLAYERNAME");
if(strcmp(parg,"")==0) {
output = "Usage: /staff <message>";
printf( "%s\n", output );
return 0;
}
freopen("/dev/null","w",stdout);
if (argv[1] == NULL) {
snprintf(command, 1024,
"/home/minecraft/remoteclient01a.py 'sendmsgtogroup staff §f(§bSTAFF§f) <%s§f> %s'",
pname, parg);
snprintf(log, 1024,
"/home/minecraft/remoteclient01a.py 'savetolog info staffmsg: <%s> %s'",
pname, parg);
}
else if (strcmp(argv[1],"me")==0) {
snprintf(command, 1024,
"/home/minecraft/remoteclient01a.py 'sendmsgtogroup staff §f(§bSTAFF§f) * %s§f %s'",
pname, parg);
snprintf(log, 1024,
"/home/minecraft/remoteclient01a.py 'savetolog info staffmsg: * %s %s'",
pname, parg);
}
popen(command, "w");
popen(log, "w");
free(command);
free(log);
return 0;
}
答案 0 :(得分:3)
嗯......
malloc()
是否成功。strcmp()
;这可能会在那里杀死你的程序。popen()
是否成功。popen()
的使用没有意义;一旦打开,你就不会使用这些管道了。