I have recently made a program that is supposed to take two environment variables, insert them into a string, then send the string to popen, and it works fine most of the time, but for some reason it will randomly not execute every so often, so I was just wondering if anyone saw any possible errors or mistakes that I am making?
#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;
}
Well …
malloc()succeeds.strcmp()on a pointer that might be NULL; which might kill your program right there.popen()succeeds.popen()makes sense; you’re not using the pipes once opened.