LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-19-2007, 12:27 AM   #1
munna_dude
Member
 
Registered: Dec 2006
Posts: 362

Rep: Reputation: 30
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
 
Old 07-19-2007, 01:27 AM   #2
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600
Escape it like
Code:
i="/root/Desktop/one of file/one.txt"; echo \"${i// /\\ }\"
?
 
Old 07-19-2007, 01:32 AM   #3
munna_dude
Member
 
Registered: Dec 2006
Posts: 362

Original Poster
Rep: Reputation: 30
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
 
Old 07-19-2007, 02:26 AM   #4
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
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.

Last edited by bigearsbilly; 07-19-2007 at 02:29 AM.
 
Old 07-19-2007, 03:30 AM   #5
Guttorm
Senior Member
 
Registered: Dec 2003
Location: Trondheim, Norway
Distribution: Debian and Ubuntu
Posts: 1,453

Rep: Reputation: 448Reputation: 448Reputation: 448Reputation: 448Reputation: 448
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.
 
Old 07-19-2007, 04:05 AM   #6
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
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.
 
Old 07-19-2007, 04:47 AM   #7
Guttorm
Senior Member
 
Registered: Dec 2003
Location: Trondheim, Norway
Distribution: Debian and Ubuntu
Posts: 1,453

Rep: Reputation: 448Reputation: 448Reputation: 448Reputation: 448Reputation: 448
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.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem with choosing a mirror while installing debian pawankumarch Linux - Newbie 3 02-26-2007 01:07 AM
Choosing a File System cjnodell Linux - Newbie 15 06-10-2005 10:55 AM
3Gb of disk space lost! Disk space problem or mother board conflicts with HDD Mistreated Linux - Hardware 4 12-06-2004 03:58 PM
script #!/bin/bash, problem with space in file names existent Linux - General 3 06-17-2004 08:13 AM
Large tar file taking huge disk space in ext3 file system pcwulf Linux - General 2 10-20-2003 07:45 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:58 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration