Code

Fix compilation with GCC when _GNU_SOURCE isn't defined.
[sysdb.git] / src / utils / os.c
index e975f96e743b6510102426d6450dbe9270690e1c..8ab52c75bc023c2bc5b68ca61d2cc0f13ce0f916 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>
 
@@ -48,6 +51,7 @@
 #include <libgen.h>
 #include <netdb.h>
 #include <pwd.h>
+#include <time.h>
 
 /*
  * public API
@@ -84,6 +88,29 @@ sdb_get_homedir(void)
        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)
 {
@@ -296,7 +323,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;