exec - Failed to Open source file while system call in C -


i trying create call system using custom parameters. think incorrectly malloc-ing size of final char*. instead failed open source file error during system call. doing wrong in terms of syntax?

#include <stdio.h> #include <stdlib.h> #include <string.h>  int main(void) {   char program_name[] = "/usr/local/bin/some_program";   char argument_1[] = "foo";   char argument_2[] = "foo2";   char space[] = " ";   char *runprogram = malloc( strlen(program_name) + strlen(argument_1)+                              strlen(argument_2) + 2*strlen(space) + 1);    strcpy(runprogram, program_name);   strcat(runprogram, space);   strcat(runprogram, argument_1);   strcat(runprogram, space);   strcat(runprogram, argument_2);    system(runprogram);   free(runprogram);   exit(0); } 

when run code, string appears contain needs to called. have suggestion simplify building though:

... int ret=0; int len = strlen(program_name) + strlen(argument_1)+ strlen(argument_2) + 2*strlen(space) + 1; char *runprogram = malloc( len);  ret = snprintf(runprogram, len, "%s %s %s", program_name, argument_1, argument_2); if(ret < 0) {     //handle if truncation occurred (returns -1 truncation) } if(ret >= len) {     //use runprogram buffer } system(runprogram); free(runprogram); //exit(0); return 0; ... 

have verified chmod settings on file executed correct?

chmod +x /usr/local/bin/some_program 

edit:
address concern in comments regarding variations in implementation/documentation snprintf(). when discussing standard c functions, hope implementations of same function equal across platforms, snprintf(), appears implementations more equal others...
1) snprintf have referred microsoft's broken implementation.
2) snprintf skrenta.com
3) snprintf opengroup.org.
4) snprintf linux.die.net, (includes glibc references, macros , other comments)


Comments

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

android - Keyboard hides my half of edit-text and button below it even in scroll view -

css - Make div keyboard-scrollable in jQuery Mobile? -