Code

OS utils: Use readdir() instead of the now deprecated readdir_r().
[sysdb.git] / src / utils / os.c
index d8f707418b9cadb57040ef20473e6ee7d9b1de7d..70142f7ba8d6ab093bf907e38e47cc8e8ae30159 100644 (file)
 #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)
 {
@@ -114,26 +172,25 @@ sdb_remove_all(const char *pathname)
                        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;
@@ -152,7 +209,7 @@ sdb_get_current_user(void)
 
        uid_t uid;
 
-       char buf[1024];
+       char buf[4096];
        int status;
 
        uid = geteuid();
@@ -265,7 +322,7 @@ sdb_resolve(int network, const char *address, struct addrinfo **res)
 
        if (address) {
                host = address;
-               port = strchr(host, ':');
+               port = strrchr(host, ':');
                if (port) {
                        *port = '\0';
                        ++port;
@@ -286,6 +343,7 @@ sdb_resolve(int network, const char *address, struct addrinfo **res)
                ai_hints.ai_family = AF_INET6;
        else
                ai_hints.ai_family = AF_UNSPEC;
+
        if ((network & SDB_NET_IP) == SDB_NET_IP) {
                ai_hints.ai_socktype = 0;
                ai_hints.ai_protocol = 0;
@@ -296,7 +354,7 @@ sdb_resolve(int network, const char *address, struct addrinfo **res)
        }
        else if (network & SDB_NET_UDP) {
                ai_hints.ai_socktype = SOCK_DGRAM;
-               ai_hints.ai_socktype = IPPROTO_UDP;
+               ai_hints.ai_protocol = IPPROTO_UDP;
        }
 
        status = getaddrinfo(host, port, &ai_hints, res);