-
Notifications
You must be signed in to change notification settings - Fork 1
/
tilde_expansion.c
55 lines (50 loc) · 1.02 KB
/
tilde_expansion.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
/*
** tilde_expansion.c
**
** Made by (Matthieu Lucotte)
** Login <gounter@users.sourceforge.net>
**
** Started on Sun Oct 7 01:04:37 2001 Matthieu Lucotte
** Last update Sun Oct 7 01:25:03 2001 Matthieu Lucotte
*/
#include "zssh.h"
char *pc_get_tilde_expansion(char *pattern)
{
char *str;
struct passwd *pass;
if (!*pattern) {
if ((str = getenv("HOME")))
return str;
else
return getpwuid(getuid())->pw_dir;
}
pass = getpwnam(pattern);
if (!pass)
return 0;
return pass->pw_dir;
}
int pc_tilde_expansion(char **str)
{
int i;
int j;
char *pattern;
char *tmp;
for (i = 0; (*str)[i]; )
if ((j = pc_escape_multi(*str, &i, ESC_COMMON)) < 0)
return -1;
else
if (!j) {
if ((*str)[i] == '~') {
for (j = 1; isalnum((*str)[i + j]); )
j++;
pattern = str_n_dup(*str + i++ + 1, j - 1);
if ((tmp = pc_get_tilde_expansion(pattern))) {
str_sub_repl(str, --i, j, tmp);
i += strlen(tmp);
}
free(pattern);
} else
i++;
}
return 0;
}