How do I achieve tilde expansion in C?

chdir("~/") doesn't seem to work. Am I expected to look at the string and substitute tilde by hand, or is there some better way?


POSIX provides wordexp(3) to perform shell-like expansion, including tilde expansion.

Tilde Expansion (The GNU C Library), applies to the ' ~ ' plus all following characters up to whitespace or a slash. The tilde in a path is a shell specific thing. What you can do see if the first character is a tilde and a slash (or a tilde end end of the string), then replace the tilde with the value of the environment variable HOME (which you can get from getenv). If the second character is not a slash, it's most likely in the form of ~user/path. Then you


You can you use wordexp example below

#include <stdio.h>
#include <wordexp.h>

int main(int argc, char* argv[]) {
    wordexp_t exp_result;
    wordexp(argv[1], &exp_result, 0);
    printf("%s\n", exp_result.we_wordv[0]);
}

The Magic ~: Bash Tilde Expansion with 5 Examples, to several specific pathnames: Home directories. Current/previous working directory. Tilde expansion is the process of converting these abbreviations to the directory names that they stand for. Tilde expansion applies to the ‘~’ plus all following characters up to whitespace or a slash. It takes place only at the beginning of a word, and only if none of the characters to be transformed is quoted in any way.


The tilde in a path is a shell specific thing. What you can do see if the first character is a tilde and a slash (or a tilde end end of the string), then replace the tilde with the value of the environment variable HOME (which you can get from getenv).

If the second character is not a slash, it's most likely in the form of ~user/path. Then you have to extract the user-name and use e.g. getpwnam to get the password entry of the user, which contains that users home directory.

TildeSlash < Wikilearn < TWiki, slash (~/) is the beginning of a path to a file or directory below the user's home directory. For example, for user01, file /home/user01/test. The tilde expansion. From Linux Shell Scripting Tutorial - A Beginner's handbook. Jump to navigation Jump to search ← Create and use aliases • Home • Startup


Tilde Expansion (The GNU C Library), 10.4.5 Details of Tilde Expansion. It's a standard part of shell syntax that you can use ' ~ ' at the beginning of a file name to stand for your own home directory. 16 How do I achieve tilde expansion in C? Apr 13 '13. 14 “git push” failed: “You did not specify any refspecs to push


Expanding a leading tilde in C/C++, Tilde expansion provides a way to expand the home directory of the current cat comp.sh #! /bin/bash set -x cd /var/opt/gg if [ -f gg.c ] then echo  If the tilde-prefix, sans the tilde, consists of a number without a leading ‘+’ or ‘-’, ‘+’ is assumed. If the login name is invalid, or the tilde expansion fails, the word is left unchanged. Each variable assignment is checked for unquoted tilde-prefixes immediately following a ‘:’ or the first ‘=’. In these cases, tilde expansion is also performed.


Bash prefix string with tilde to expand to home directory, If you're writing an app that accepts a path to a filename as user-input or in config​-files, you'll have to be able to parse the famous leading tilde  What I want to do. I would like to configure ZSH in order to get any filename tilde expansion I wish. This would be something like setting alias ~foo /foo/bar in my .zshrc. What I do not want. I could create (as root) the user foo and set it’s home to /foo/bar but since any user should be able to do this, such a solution is not acceptable.