getRecentName(): use realpath(3) instead of errorprone 'chdir()' calls
[util-vserver.git] / util-vserver / lib / getvservername.c
index 00dfd3a..212c9df 100644 (file)
 #include <fcntl.h>
 #include <errno.h>
 #include <libgen.h>
+#include <unistd.h>
+#include <limits.h>
 
 static char *
 getRecentName(char *start, char *end)
 {
   char         *res = 0;
   int          fd;
+  char         buf[PATH_MAX];
   
   strcpy(end, "/name");
   fd = open(start, O_RDONLY);
@@ -46,6 +49,7 @@ getRecentName(char *start, char *end)
 
       if (TEMP_FAILURE_RETRY(read(fd, buf, len+1))==len) {
        while (len>0 && buf[len-1]=='\n') --len;
+       buf[len] = '\0';
        if (len>0) res = buf;
       }
 
@@ -58,7 +62,11 @@ getRecentName(char *start, char *end)
 
   if (res==0) {
     *end = '\0';
-    res  = basename(start);
+    res  = realpath(start, buf);
+    //printf("start='%s', res='%s'\n", start,res);
+    if (res==0) res = start;
+
+    res = basename(res);
   }
 
   return strdup(res);