view src/lib/home-expand.c @ 3749:194295062e5e HEAD

Added kqueue support. Patch by Vaclav Haisman.
author Timo Sirainen <tss@iki.fi>
date Wed, 14 Dec 2005 20:51:52 +0200
parents e0006f30b496
children 0caa09cc3edb
line wrap: on
line source

/* Copyright (C) 2003 Timo Sirainen */

#include "lib.h"
#include "home-expand.h"

#include <stdlib.h>
#include <pwd.h>

/* expand ~/ or ~user/ in beginning of path */
const char *home_expand(const char *path)
{
	const char *home, *p, *orig_path;
	struct passwd *pw;

	if (path == NULL || *path != '~')
		return path;

	orig_path = path++;
	if (*path == '/' || *path == '\0') {
		home = getenv("HOME");
		if (*path != '\0') path++;
	} else {
		p = strchr(path, '/');
		if (p == NULL) {
			pw = getpwnam(path);
			path = "";
		} else {
			pw = getpwnam(t_strdup_until(path, p));
			path = p+1;
		}

		home = pw == NULL ? NULL : pw->pw_dir;
	}

	if (home == NULL)
		return orig_path;
	else if (*path == '\0')
		return t_strdup(home);
	else
		return t_strconcat(home, "/", path, NULL);
}