getRecentName(): return basename of directory but not this of the link anymore
authorEnrico Scholz <enrico.scholz@informatik.tu-chemnitz.de>
Tue, 2 Dec 2003 02:17:17 +0000 (02:17 +0000)
committerEnrico Scholz <enrico.scholz@informatik.tu-chemnitz.de>
Tue, 2 Dec 2003 02:17:17 +0000 (02:17 +0000)
git-svn-id: http://svn.linux-vserver.org/svn/util-vserver/trunk@459 94cd875c-1c1d-0410-91d2-eb244daf1a30

util-vserver/lib/getvservername.c

index 0482537..046f828 100644 (file)
 #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);
@@ -59,8 +61,21 @@ getRecentName(char *start, char *end)
   }
 
   if (res==0) {
+    int                old_fd = open(".", O_RDONLY);
+
     *end = '\0';
-    res  = basename(start);
+    
+    if (old_fd!=-1 &&
+       chdir(start)!=-1 &&
+       getcwd(buf, sizeof buf)==buf)
+      start = buf;
+
+    res = basename(start);
+
+    if (old_fd!=-1) {
+      fchdir(old_fd);
+      close(old_fd);
+    }
   }
 
   return strdup(res);