File: util.c

package info (click to toggle)
hx 0.7.10-2
  • links: PTS
  • area: main
  • in suites: potato
  • size: 564 kB
  • ctags: 834
  • sloc: ansic: 7,900; sh: 152; makefile: 84
file content (37 lines) | stat: -rw-r--r-- 640 bytes parent folder | download | duplicates (3)
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
#include <string.h>
#include <pwd.h>

extern char homedir[];

void expand_tilde (char *buf, char *str);

void
expand_tilde (char *buf, char *str)
{
	register char *p = str;
	register int len;

	if (*p == '~') {
		p++;
		if (*p == '/' || !*p) {
			len = strlen(homedir);
			strcpy(&(buf[len]), p);
			memcpy(buf, homedir, len);
		} else {
			char *rest;
			struct passwd *pwe;

			if ((rest = strchr(p, '/')))
				*rest++ = 0;
			if ((pwe = getpwnam(p))) {
				len = strlen(pwe->pw_dir);
				if (rest) {
					strcpy(&(buf[len + 1]), rest);
					buf[len] = '/';
				}
				memcpy(buf, pwe->pw_dir, len);
			}
		}
	} else
		strcpy(buf, str);
}