LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   space problem when choosing a file (https://www.linuxquestions.org/questions/programming-9/space-problem-when-choosing-a-file-570581/)

munna_dude 07-19-2007 12:27 AM

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

unSpawn 07-19-2007 01:27 AM

Escape it like
Code:

i="/root/Desktop/one of file/one.txt"; echo \"${i// /\\ }\"
?

munna_dude 07-19-2007 01:32 AM

Quote:

Originally Posted by unSpawn
Escape it like
Code:

i="/root/Desktop/one of file/one.txt"; echo \"${i// /\\ }\"
?

thank you ..
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

bigearsbilly 07-19-2007 02:26 AM

something like this,
note you will need to check the destination is large enough.

Code:

#include <stdio.h>
#include <ctype.h>


char * name =  "this has spaces in it";
char buffer[128];

main ()
{

    char *b = buffer;
    char *p = name;

    while(*p) {

        if (isspace(*p)) *b++ = '\\';
        *b++ = *p++;
    }
    puts(name);
    puts(buffer);
}

Code:

gcc  1.o  -o 1
this has spaces in it
this\ has\ spaces\ in\ it

can you work out how it works?
the loop is checking for *p, as the end of a string is a NULL character.

Guttorm 07-19-2007 03:30 AM

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)
{
        const char *src = arg;
        char *dst;
        char ret = malloc(strlen(arg) * 4 + 3);
        char c;
       
        if (ret != NULL) {
                dst = ret;
                *dst++ = '\'';
                while ( (c = *src++) != '\0' ) {
                        if (c == '\'') {
                                *dst++ = c;
                                *dst++ = '\\';
                                *dst++ = c;
                        }
                        *dst++ = c;
                }
                *dst++ = '\'';
                *dst = '\0';
        }
        return ret;
}

This function works like the PHP function with the same name. In fact I looked at the PHP source to see how it did it. Just remember to free the result after you have used it.

bigearsbilly 07-19-2007 04:05 AM

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.

Guttorm 07-19-2007 04:47 AM

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.