view src/lib/home-expand.c @ 1329:ae229b7acb4c HEAD

Mailbox names are now sent through imap-quoter instead of just escaping it. This means that mailbox names that would require escapes are instead sent as literals now.
author Timo Sirainen <tss@iki.fi>
date Wed, 02 Apr 2003 05:05:38 +0300
parents 058f6c26f405
children e0006f30b496
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 != '~')
		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);
}