space problem when choosing a file
hi all
i am in strange problem. i opend a terminal( am in root now) enterd cd Desktop cd one now am in one directory... i am using a gtk filechooser... it will select the file .. and am giving a printf of the choosing file.. now am selected one->one.txt... the print in the terminal is /root/Desktop/one/one.txt.... my problem is.. i would like to choose a file with gtk filechooser the file name is.... "one of file"... the above directory or file having spaces it will print in the termin a like "/root/Desktop/one of file/one.txt" here is the problem with space.. i wanna output like this "/root/Desktop/one\ of\ file/txt" please help me thank you in advance |
Escape it like
Code:
i="/root/Desktop/one of file/one.txt"; echo \"${i// /\\ }\" |
Quote:
tis great.. but i need to write in c program.. how can i do this.. can you please help me a little thank you in advance |
something like this,
note you will need to check the destination is large enough. Code:
#include <stdio.h> Code:
gcc 1.o -o 1 the loop is checking for *p, as the end of a string is a NULL character. |
Hi
If you are using filenames for other calls like system or popen, the problem is not only spaces. Filenames can use any character except /, and if it contains characters like '*[]" and so on, there will be problems. Here is a safer function: Code:
char *escape_shell_arg(const char *arg) |
IMHO
Using malloc in this way is very bad practice unless it's a specific object allocation function. you are asking for memory leaks by relying on callers to free what they use. |
Hi
I agree. But the alternative is worse. Using a fixed length for a buffer might be overlooked easily. Like if the argument came from argv, or perhaps a file choser? Then we would have a security hole instead of a memory leak - much worse IMO. |
All times are GMT -5. The time now is 06:42 AM. |