New expand_tilde() utility function

Add new `expand_tilde' utility function which substitutes
leading `~/' for the current user's home directory in path
names; NB: returns a new string the caller must free when
it's not needed anymore.
This commit is contained in:
Tamas TEVESZ 2011-07-28 17:20:44 +02:00 committed by Daniel Beer
parent 457e5a6a3f
commit d045d8feec
5 changed files with 76 additions and 4 deletions

View File

@ -333,7 +333,15 @@ struct hexout_data {
static int hexout_start(struct hexout_data *hexout, const char *filename)
{
hexout->file = fopen(filename, "w");
char * path = NULL;
path = expand_tilde(filename);
if (!path)
return -1;
hexout->file = fopen(path, "w");
free(path);
if (!hexout->file) {
pr_error("hexout: couldn't open output file");
return -1;
@ -492,11 +500,17 @@ static int do_cmd_prog(char **arg, int prog_flags)
{
FILE *in;
struct prog_data prog;
char * path;
if (prompt_abort(MODIFY_SYMS))
return 0;
in = fopen(*arg, "rb");
path = expand_tilde(*arg);
if (!path)
return -1;
in = fopen(path, "rb");
free(path);
if (!in) {
printc_err("prog: %s: %s\n", *arg, last_error());
return -1;

View File

@ -227,10 +227,15 @@ int process_command(char *cmd)
int process_file(const char *filename, int show)
{
FILE *in;
char buf[1024];
char buf[1024], *path;
int line_no = 0;
path = expand_tilde(filename);
if (!path)
return -1;
in = fopen(filename, "r");
free(path);
if (!in) {
printc_err("read: can't open %s: %s\n",
filename, last_error());

9
sym.c
View File

@ -52,11 +52,17 @@ int cmd_eval(char **arg)
static int cmd_sym_load_add(int clear, char **arg)
{
FILE *in;
char * path;
if (clear && prompt_abort(MODIFY_SYMS))
return 0;
in = fopen(*arg, "rb");
path = expand_tilde(*arg);
if (!path)
return -1;
in = fopen(path, "rb");
free(path);
if (!in) {
printc_err("sym: %s: %s\n", *arg, last_error());
return -1;
@ -75,6 +81,7 @@ static int cmd_sym_load_add(int clear, char **arg)
}
fclose(in);
return 0;
}

43
util.c
View File

@ -283,3 +283,46 @@ const char *last_error(void)
return strerror(errno);
}
#endif
/* Expand leading `~/' in path names. Caller must free the returned ptr */
char *expand_tilde(const char *path)
{
char *home, *expanded;
size_t len;
if (!path)
return NULL;
if (!*path)
return strdup("");
expanded = NULL;
if (*path == '~' && *(path + 1) == '/') {
home = getenv("HOME");
if (home) {
/* Trailing '\0' will fit in leading '~'s place */
len = strlen(home) + strlen(path);
expanded = (char *)malloc(len);
if (expanded)
snprintf(expanded, len, "%s%s", home, path + 1);
else
printc_err("%s: malloc: %s\n", __FUNCTION__, last_error());
} else {
printc_err("%s: getenv: %s\n", __FUNCTION__, last_error());
}
} else {
expanded = (char *)malloc(strlen(path) + 1);
if (expanded)
strcpy(expanded, path);
else
printc_err("%s: malloc: %s\n", __FUNCTION__, last_error());
}
/* Caller must free()! */
return expanded;
}

3
util.h
View File

@ -65,4 +65,7 @@ char *strsep(char **strp, const char *delim);
HANDLE ctrlc_win32_event(void);
#endif
/* Expand `~' in path names. Caller must free the returned ptr */
char *expand_tilde(const char *path);
#endif