Results 1 to 2 of 2
Currently, I am using system command in my source code as follows
main()
{
system("myexe -rate=25 -infile=abc.txt outfile.txt");
}
and I have myexe in the current directory and it works ...
- 06-16-2010 #1Just Joined!
- Join Date
- Apr 2010
- Posts
- 4
Remove system command
Currently, I am using system command in my source code as follows
main()
{
system("myexe -rate=25 -infile=abc.txt outfile.txt");
}
and I have myexe in the current directory and it works fine. Now, I need to replace the system command for security reasons and directly call the program in myexe. Do you have any thoughts?
I tried following and see problem .
main
{
typedef struct
{
char arg0[50];
char arg1[50];
char arg2[50];
char arg3[50];
} arg_struct_t;
strncpy(arg.arg0,"myexe",50);
strncpy(arg.arg1,"-rate=25",50);
strncpy(arg.arg2,"-infile=input.txt",50);
strncpy(arg.arg3,"outfile.txt",50);
modified_main(4,&argv);
}
modified_main(int argc, arg_struct_t * argv_t)
{
fprintf(stderr, "modified_main:%s, %s, %s, %s \n",argv_t->arg0, argv_t->arg1,argv_t->arg2,argv_t->arg3);
/* Good until here. Now, how do I copy argv_t-->arg0 to modified_main()' argv[0], argv[1], argv[2], argv[3]? so that myexe works like in system command.*/
I tried below and The following gave a bus error? why? Need second look.
argv[0] = (char*)malloc(50*sizeof(char));
argv[1] = (char*)malloc(50*sizeof(char));
argv[2] = (char*)malloc(50*sizeof(char));
argv[3] = (char*)malloc(50*sizeof(char));
strcpy(argv[0],argv_t->arg0);
strcpy(argv[1],argv_t->arg1);
strcpy(argv[2],argv_t->arg2);
strcpy(argv[3],argv_t->arg3);
}
Let me know, if you see any problems. I will debug accordingly.
- 06-16-2010 #2Linux Newbie
- Join Date
- Mar 2010
- Posts
- 121
It seems like you are massively, massively overcomplicating this.
In the following example, myexe_main() simply prints its arguments, and main() calls it with whatever arguments it wants:
...or have I got the wrong end of the stick?Code:#include <stdio.h> int myexe_main(int argc, char *argv[]) { for (int i = 0 ; i < argc ; i++) printf("%d: %s\n", i, argv[i]); return 0; } int main(void) { char *args[] = {"prog-name", "--cake", "--is", "--good"}; myexe_main(sizeof(args)/sizeof(*args), args); return 0; }


Reply With Quote