Thread: Replacing ~ with getenv("HOME") in a string ?

  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    1

    Replacing ~ with getenv("HOME") in a string ?

    What would be the shortest and easiest way to replace any instance of ~ with getenv("HOME") (returns the current user home directory) in a string? Im writing a little program that deals with system calls, but i need a way to tell C to do the same thing that bash does and expand ~ to the users home directory. I want to make the input foolproof so that the input can either enter /home/ or ~. THanks a lot in advance

    -Nick

  2. #2
    Registered User
    Join Date
    Sep 2005
    Location
    Sydney
    Posts
    60
    Firstly, note that ~ isn't the only thing that bash expands - in fact, bash does a great deal of mucking around with the user input before the command actually gets executed. If you want to do all that sort of substitution, probably the easiest way is the GNU readline library:
    http://cnswww.cns.cwru.edu/~chet/readline/readline.html

    Readline does a lot though, so if all you want is a simple replacement of ~ with the home directory, then I suppose you would just check for ~ whenever you get user input - perhaps you could have a wrapper function for fgets (or whatever you're using) that just does the replacement and then returns the input.

    You can search for a character in a string using strchr, but note that this will only return the first instance, so perhaps it is better to just iterate through the string yourself.

    The easiest way I can think of to generate a new string with '~' replaced by the home directory is this:
    - Iterate through the input string, replacing '~' with '\0'. As you go, count how many times you do this, and keep a record of where you do it
    - Use this to generate a format string of the form: "%s%s%s...". You have broken your input string into pieces - you need one %s per piece, plus one in between every other two.
    - Use sprintf like this:
    sprintf(mystring, format_string, input, home, input[index of first instance of ~], home, input[index of second instance of ~], home, ..., input[index of last instance of ~]);

    There are probably better ways to do it, that's just what I came up with after looking at your problem.

  3. #3
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Note that ~ only gets expanded if it's the first character in the path. And it only replaces it with your home directory if it's followed by nothing or a '/'. If it's something like ~foo it replaces it with foo's home directory, not yours.

    You're asking something different than how bash behaves, but you're saying you want it to work like bash. Those two conflict. How do you actually want it to work?
    If you understand what you're doing, you're not learning anything.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Calculator + LinkedList
    By maro009 in forum C++ Programming
    Replies: 20
    Last Post: 05-17-2005, 12:56 PM
  2. Linked List Help
    By CJ7Mudrover in forum C Programming
    Replies: 9
    Last Post: 03-10-2004, 10:33 PM
  3. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 03:23 PM
  4. creating class, and linking files
    By JCK in forum C++ Programming
    Replies: 12
    Last Post: 12-08-2002, 02:45 PM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM