J
joel.winteregg
hi all,
I'm a newbie in C programming (i'm coming from Java world "nobody is
perfect") and i have a question about possibilities in C software
architecture.
I saw that C functions where often using the return value to give
informations about execution of the function (0 = exec OK, 1 = exec
mistake). I'm wondering how i can do the cleanest code to put a command
result (like the "ls" Linux command) in a buffer. The code below show a
possible way of the function header:
//function executing a command (command) and putting the result in
buf
int exec(char* command, char** buf){
...
}
Here, the programmer (the user of this function) will need to give a
char** to get a char pointer back. So, he will need to create a pointer
and give it to the function (like this):
char* myBufPointer;
exec("ls -l", &myBufPointer);
The exec function will allocate a memory buffer (malloc) and the
programmer will need to free it afterwards... I don't know if it's a
really good idea to allocate memory in a function and to let the
programmer free it later ??
The other problem is coming from the way the programmer is creating the
buffer for the function. If he gonna do a static buffer (char
myBuf[200]) the function will not have to allocate memory... If the 200
char are not enough for the result (buf) how could i do ? Should i
check in the function if the buffer is static (if the pointer is
pointing to a memory area or not) and if the memory area is big enough
?? If it's a static buffer how can i extend it ? should i create an
other one ?
Is there an other way (a cleanest one) to create my function exec ??
Thanks a lot for your help and hope to read you soon...
Joël.W
I'm a newbie in C programming (i'm coming from Java world "nobody is
perfect") and i have a question about possibilities in C software
architecture.
I saw that C functions where often using the return value to give
informations about execution of the function (0 = exec OK, 1 = exec
mistake). I'm wondering how i can do the cleanest code to put a command
result (like the "ls" Linux command) in a buffer. The code below show a
possible way of the function header:
//function executing a command (command) and putting the result in
buf
int exec(char* command, char** buf){
...
}
Here, the programmer (the user of this function) will need to give a
char** to get a char pointer back. So, he will need to create a pointer
and give it to the function (like this):
char* myBufPointer;
exec("ls -l", &myBufPointer);
The exec function will allocate a memory buffer (malloc) and the
programmer will need to free it afterwards... I don't know if it's a
really good idea to allocate memory in a function and to let the
programmer free it later ??
The other problem is coming from the way the programmer is creating the
buffer for the function. If he gonna do a static buffer (char
myBuf[200]) the function will not have to allocate memory... If the 200
char are not enough for the result (buf) how could i do ? Should i
check in the function if the buffer is static (if the pointer is
pointing to a memory area or not) and if the memory area is big enough
?? If it's a static buffer how can i extend it ? should i create an
other one ?
Is there an other way (a cleanest one) to create my function exec ??
Thanks a lot for your help and hope to read you soon...
Joël.W