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