diff --git a/src/utils/os.c b/src/utils/os.c
index 4afc466683138aed81667a446638698bbe96da91..70142f7ba8d6ab093bf907e38e47cc8e8ae30159 100644 (file)
--- a/src/utils/os.c
+++ b/src/utils/os.c
#include <errno.h>
#include <sys/types.h>
+#include <sys/select.h>
#include <sys/socket.h>
#include <sys/stat.h>
+#include <sys/time.h>
+#include <sys/types.h>
#include <dirent.h>
#include <libgen.h>
#include <netdb.h>
#include <pwd.h>
+#include <time.h>
/*
* public API
*/
+char *
+sdb_get_homedir(void)
+{
+ char *username = sdb_get_current_user();
+
+ struct passwd pw_entry;
+ struct passwd *result = NULL;
+
+ char buf[4096];
+
+ int status;
+
+ if (username) {
+ memset(&pw_entry, 0, sizeof(pw_entry));
+ status = getpwnam_r(username, &pw_entry, buf, sizeof(buf), &result);
+ }
+ else
+ status = -1;
+
+ if (status || (! result)) {
+ char errbuf[1024];
+ sdb_log(SDB_LOG_WARNING, "os: Failed to determine home directory "
+ "for user %s: %s", username,
+ sdb_strerror(errno, errbuf, sizeof(errbuf)));
+ free(username);
+ return NULL;
+ }
+ free(username);
+ return strdup(result->pw_dir);
+} /* sdb_get_homedir */
+
+char *
+sdb_realpath(const char *path)
+{
+ if (! path)
+ return NULL;
+
+ if ((strlen(path) >= 2) && (path[0] == '~') && (path[1] == '/')) {
+ char *homedir = sdb_get_homedir();
+ char tmp[(homedir ? strlen(homedir) : 0) + strlen(path)];
+ char *ret;
+
+ if (! homedir)
+ return NULL;
+
+ snprintf(tmp, sizeof(tmp), "%s/%s", homedir, path + 2);
+ ret = realpath(tmp, NULL);
+ free(homedir);
+ return ret;
+ }
+
+ return realpath(path, NULL);
+} /* sdb_realpath */
+
int
sdb_mkdir_all(const char *pathname, mode_t mode)
{
return -1;
while (42) {
- struct dirent de;
- struct dirent *res = NULL;
+ struct dirent *de;
+ char filename[strlen(pathname) + sizeof(de->d_name) + 2];
- char filename[strlen(pathname) + sizeof(de.d_name) + 2];
+ errno = 0;
+ de = readdir(d);
+ if (! de) {
+ if (errno == 0)
+ break;
- memset(&de, 0, sizeof(de));
- if (readdir_r(d, &de, &res)) {
closedir(d);
return -1;
}
- if (! res)
- break;
-
- if ((de.d_name[0] == '.') && ((de.d_name[1] == '\0')
- || ((de.d_name[1] == '.')&& (de.d_name[2] == '\0'))))
+ if ((de->d_name[0] == '.') && ((de->d_name[1] == '\0')
+ || ((de->d_name[1] == '.')&& (de->d_name[2] == '\0'))))
continue;
snprintf(filename, sizeof(filename),
- "%s/%s", pathname, de.d_name);
+ "%s/%s", pathname, de->d_name);
if (sdb_remove_all(filename)) {
closedir(d);
return -1;
uid_t uid;
- char buf[1024];
+ char buf[4096];
int status;
uid = geteuid();
if (address) {
host = address;
- port = strchr(host, ':');
+ port = strrchr(host, ':');
if (port) {
*port = '\0';
++port;