if (OPT_GIVEN(AFS_DATABASE_DIR))
database_dir = para_strdup(OPT_STRING_VAL(AFS_DATABASE_DIR));
else {
- char *home = para_homedir();
+ const char *home = get_homedir();
database_dir = make_message(
"%s/.paraslash/afs_database-0.7", home);
- free(home);
}
}
PARA_INFO_LOG("afs_database dir %s\n", database_dir);
if (OPT_GIVEN(HISTORY_FILE))
history_file = para_strdup(OPT_STRING_VAL(HISTORY_FILE));
else {
- char *home = para_homedir();
+ const char *home = get_homedir();
history_file = make_message("%s/.paraslash/audioc.history",
home);
- free(home);
}
ici.history_file = history_file;
ici.history_file = para_strdup(CLIENT_OPT_STRING_VAL(
HISTORY_FILE, ct->lpr));
else {
- char *home = para_homedir();
+ const char *home = get_homedir();
ici.history_file = make_message("%s/.paraslash/client.history",
home);
- free(home);
}
act.sa_handler = i9e_signal_dispatch;
sigemptyset(&act.sa_mask);
struct lls_parse_result *lpr;
int ret, ll;
struct client_task *ct;
- char *kf = NULL, *user, *errctx, *home = para_homedir();
+ char *kf = NULL, *user, *errctx;
ret = lls(lls_parse(argc, argv, cmd, &lpr, &errctx));
if (ret < 0)
kf = para_strdup(CLIENT_OPT_STRING_VAL(KEY_FILE, lpr));
else {
struct stat statbuf;
+ const char *home = get_homedir();
kf = make_message("%s/.paraslash/key.%s", home, user);
if (stat(kf, &statbuf) != 0) { /* assume file does not exist */
free(kf);
*ct_ptr = ct;
ret = lls_num_inputs(lpr);
out:
- free(home);
if (ret < 0) {
if (errctx)
PARA_ERROR_LOG("%s\n", errctx);
*/
__must_check __malloc static char *random_filename(void)
{
- char *result, *home = para_homedir();
+ char *result;
+ const char *home = get_homedir();
srandom(clock_get_realtime(NULL)->tv_usec);
result = make_message("%s/.paraslash/%08ld", home,
para_random(99999999));
- free(home);
return result;
}
if (path)
cf = para_strdup(path);
else {
- char *home = para_homedir();
+ const char *home = get_homedir();
cf = make_message("%s/.paraslash/%s", home, dflt);
- free(home);
}
ret = mmap_full_file(cf, O_RDONLY, &map, &sz, NULL);
if (ret < 0) {
if (OPT_GIVEN(HISTORY_FILE))
history_file = para_strdup(OPT_STRING_VAL(HISTORY_FILE));
else {
- char *home = para_homedir();
+ const char *home = get_homedir();
char *dot_para = make_message("%s/.paraslash", home);
- free(home);
ret = para_mkdir(dot_para);
/* warn, but otherwise ignore mkdir error */
if (ret < 0)
if (OPT_GIVEN(USER_LIST))
user_list_file = para_strdup(OPT_STRING_VAL(USER_LIST));
else {
- char *home = para_homedir();
+ const char *home = get_homedir();
user_list_file = make_message("%s/.paraslash/server.users", home);
- free(home);
}
user_list_init(user_list_file);
free(user_list_file);
}
/**
- * Get the home directory of the calling user.
+ * Investigate $HOME to get the home directory of the calling user.
*
- * \return A dynamically allocated string that must be freed by the caller. If
- * no entry is found which matches the UID of the calling process, or any other
- * error occurs, the function prints an error message and aborts.
+ * \return A pointer to read-only memory that must not be freed by the caller.
+ * If the environment variable HOME is unset or empty, the function prints an
+ * error message and aborts.
*
- * \sa getpwuid(3), getuid(2).
+ * \sa getenv(3), getuid(2).
*/
-__must_check __malloc char *para_homedir(void)
+const char *get_homedir(void)
{
- struct passwd *pw;
-
- /*
- * To distinguish between the error case and the "not found" case we
- * have to check errno after getpwuid(3). The manual page recommends to
- * set it to zero before the call.
- */
- errno = 0;
- pw = getpwuid(getuid());
- if (pw)
- return para_strdup(pw->pw_dir);
- if (errno != 0)
- PARA_EMERG_LOG("getpwuid error: %s\n", strerror(errno));
- else
- PARA_EMERG_LOG("no pw entry for uid %u\n", (unsigned)getuid());
+ const char *home = getenv("HOME");
+ if (home && *home)
+ return home;
+ PARA_EMERG_LOG("fatal: HOME is unset or empty");
exit(EXIT_FAILURE);
}
__must_check __malloc __printf_1_2 char *make_message(const char *fmt, ...);
__must_check __malloc char *para_strcat(char *a, const char *b);
__must_check __malloc char *para_logname(void);
-__must_check __malloc char *para_homedir(void);
+const char *get_homedir(void);
__malloc char *para_hostname(void);
__printf_2_3 int para_printf(struct para_buffer *b, const char *fmt, ...);
int para_atoi64(const char *str, int64_t *result);
static void set_paths(const struct lls_parse_result *lpr)
{
- char *home = para_homedir();
+ const char *home = get_homedir();
if (OPT_GIVEN(SRC_DATABASE_DIR, lpr))
src_db_dir = para_strdup(OPT_STRING_VAL(SRC_DATABASE_DIR,
else
dst_db_dir = make_message(
"%s/.paraslash/afs_database-0.7", home);
- free(home);
src_aft_dir = make_message("%s/audio_files", src_db_dir);
dst_aft_dir = make_message("%s/audio-files", src_db_dir);
PARA_NOTICE_LOG("source aft dir: %s\n", src_aft_dir);