Blame src/userfile.c

Packit 4a16fb
/*
Packit 4a16fb
 *  Get full filename
Packit 4a16fb
 *  Copyright (c) 2003 by Jaroslav Kysela <perex@perex.cz>
Packit 4a16fb
 *
Packit 4a16fb
 *   This library is free software; you can redistribute it and/or modify
Packit 4a16fb
 *   it under the terms of the GNU Lesser General Public License as
Packit 4a16fb
 *   published by the Free Software Foundation; either version 2.1 of
Packit 4a16fb
 *   the License, or (at your option) any later version.
Packit 4a16fb
 *
Packit 4a16fb
 *   This program is distributed in the hope that it will be useful,
Packit 4a16fb
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 4a16fb
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Packit 4a16fb
 *   GNU Lesser General Public License for more details.
Packit 4a16fb
 *
Packit 4a16fb
 *   You should have received a copy of the GNU Lesser General Public
Packit 4a16fb
 *   License along with this library; if not, write to the Free Software
Packit 4a16fb
 *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
Packit 4a16fb
 *
Packit 4a16fb
 */
Packit 4a16fb
  
Packit 4a16fb
#include <config.h>
Packit 4a16fb
#include <string.h>
Packit 4a16fb
#include <errno.h>
Packit 4a16fb
#include <assert.h>
Packit 4a16fb
Packit 4a16fb
/**
Packit 4a16fb
 * \brief Get the full file name
Packit 4a16fb
 * \param file The file name string to parse
Packit 4a16fb
 * \param result The pointer to store the resultant file name
Packit 4a16fb
 * \return 0 if successful, or a negative error code
Packit 4a16fb
 *
Packit 4a16fb
 * Parses the given file name with POSIX-Shell-like expansion and
Packit 4a16fb
 * stores the first matchine one.  The returned string is strdup'ed.
Packit 4a16fb
 */
Packit 4a16fb
Packit 4a16fb
#ifdef HAVE_WORDEXP
Packit 4a16fb
#include <wordexp.h>
Packit 4a16fb
int snd_user_file(const char *file, char **result)
Packit 4a16fb
{
Packit 4a16fb
	wordexp_t we;
Packit 4a16fb
	int err;
Packit 4a16fb
Packit 4a16fb
	assert(file && result);
Packit 4a16fb
	err = wordexp(file, &we, WRDE_NOCMD);
Packit 4a16fb
	switch (err) {
Packit 4a16fb
	case WRDE_NOSPACE:
Packit 4a16fb
		wordfree(&we);
Packit 4a16fb
		return -ENOMEM;
Packit 4a16fb
	case 0:
Packit 4a16fb
		if (we.we_wordc == 1)
Packit 4a16fb
			break;
Packit 4a16fb
		wordfree(&we);
Packit 4a16fb
		/* fall thru */
Packit 4a16fb
	default:
Packit 4a16fb
		return -EINVAL;
Packit 4a16fb
	}
Packit 4a16fb
	*result = strdup(we.we_wordv[0]);
Packit 4a16fb
	wordfree(&we);
Packit 4a16fb
	if (*result == NULL)
Packit 4a16fb
		return -ENOMEM;
Packit 4a16fb
	return 0;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
#else /* !HAVE_WORDEX */
Packit 4a16fb
Packit 4a16fb
#include <sys/types.h>
Packit 4a16fb
#include <unistd.h>
Packit 4a16fb
#include <pwd.h>
Packit 4a16fb
#include <stdio.h>
Packit 4a16fb
#include <stdlib.h>
Packit 4a16fb
Packit 4a16fb
int snd_user_file(const char *file, char **result)
Packit 4a16fb
{
Packit 4a16fb
	int err;
Packit 4a16fb
	size_t len;
Packit 4a16fb
	char *buf = NULL;
Packit 4a16fb
Packit 4a16fb
	assert(file && result);
Packit 4a16fb
	*result = NULL;
Packit 4a16fb
Packit 4a16fb
	/* expand ~/ if needed */
Packit 4a16fb
	if (file[0] == '~' && file[1] == '/') {
Packit 4a16fb
		const char *home = getenv("HOME");
Packit 4a16fb
		if (home == NULL) {
Packit 4a16fb
			struct passwd pwent, *p = NULL;
Packit 4a16fb
			uid_t id = getuid();
Packit 4a16fb
			size_t bufsize = 1024;
Packit 4a16fb
Packit 4a16fb
			buf = malloc(bufsize);
Packit 4a16fb
			if (buf == NULL)
Packit 4a16fb
				goto out;
Packit 4a16fb
Packit 4a16fb
			while ((err = getpwuid_r(id, &pwent, buf, bufsize, &p)) == ERANGE) {
Packit 4a16fb
				char *newbuf;
Packit 4a16fb
				bufsize += 1024;
Packit 4a16fb
				if (bufsize < 1024)
Packit 4a16fb
					break;
Packit 4a16fb
				newbuf = realloc(buf, bufsize);
Packit 4a16fb
				if (newbuf == NULL)
Packit 4a16fb
					goto out;
Packit 4a16fb
				buf = newbuf;
Packit 4a16fb
			}
Packit 4a16fb
			home = err ? "" : pwent.pw_dir;
Packit 4a16fb
		}
Packit 4a16fb
		len = strlen(home) + strlen(&file[2]) + 2;
Packit 4a16fb
		*result = malloc(len);
Packit 4a16fb
		if (*result)
Packit 4a16fb
			snprintf(*result, len, "%s/%s", home, &file[2]);
Packit 4a16fb
	} else {
Packit 4a16fb
		*result = strdup(file);
Packit 4a16fb
	}
Packit 4a16fb
Packit 4a16fb
out:
Packit 4a16fb
	if (buf)
Packit 4a16fb
		free(buf);
Packit 4a16fb
Packit 4a16fb
	if (*result == NULL)
Packit 4a16fb
		return -ENOMEM;
Packit 4a16fb
	return 0;
Packit 4a16fb
}
Packit 4a16fb
Packit 4a16fb
#endif /* HAVE_WORDEXP */