我最近做了一个程序,它应该采用两个环境变量,将它们插入一个字符串,然后将字符串发送到 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;
}