English 中文(简体)
是什么导致此程序无法可靠执行?
原标题:what is causing this program to not execute reliably
  • 时间:2012-05-22 14:25:00
  •  标签:
  • c

我最近制定了一个程序, 程序应该包含两个环境变量, 将其插入一个字符串, 然后发送字符串打开, 大部分时间都很好, 但出于某种原因, 它不会经常随机执行, 所以我在想有没有人看到我 做的任何可能的错误或错误?

#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
", 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;
}
最佳回答

嗯........

  • You re not checking that malloc() succeeds.
  • You re not handling that the environment variables might not exist.
  • You re calling strcmp() on a pointer that might be NULL; which might kill your program right there.
  • You re not checking that the file open succeeds.
  • You have weird § characters in the commands strings, not sure if those are intended.
  • You re not checking if popen() succeeds.
  • I don t think your usage of popen() makes sense; you re not using the pipes once opened.
问题回答

暂无回答




相关问题
Fastest method for running a binary search on a file in C?

For example, let s say I want to find a particular word or number in a file. The contents are in sorted order (obviously). Since I want to run a binary search on the file, it seems like a real waste ...

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

Tips for debugging a made-for-linux application on windows?

I m trying to find the source of a bug I have found in an open-source application. I have managed to get a build up and running on my Windows machine, but I m having trouble finding the spot in the ...

Trying to split by two delimiters and it doesn t work - C

I wrote below code to readin line by line from stdin ex. city=Boston;city=New York;city=Chicago and then split each line by ; delimiter and print each record. Then in yet another loop I try to ...

Good, free, easy-to-use C graphics libraries? [closed]

I was wondering if there were any good free graphics libraries for C that are easy to use? It s for plotting 2d and 3d graphs and then saving to a file. It s on a Linux system and there s no gnuplot ...

Encoding, decoding an integer to a char array

Please note that this is not homework and i did search before starting this new thread. I got Store an int in a char array? I was looking for an answer but didn t get any satisfactory answer in the ...

热门标签