1. ron minnich
  2. ldd

Commits

rmin...@gmail.com  committed 1e7cacc

Much better. Even does symlinks.

  • Participants
  • Parent commits 8a631ec
  • Branches default

Comments (0)

Files changed (1)

File ldd.go

View file
 )
 
 func Ldd(cmd, root, liblist string) ([]string, os.Error) {
-	var libpath []string
 	/* it might just be static ... */
-	ret := []string{cmd}
-	libpath = strings.Split(liblist, ":")
+	ret := []string{}
+	libpath := strings.Split(liblist, ":")
 	known := make(map[string]string, 16)
 	/* this is far larger than we will ever need, but I am in fright of deadlock. */
 	strings := make(chan string, 256)
 		strings <- cmd
 	}
 	e.Close()
-
 	for len(strings) > 0 {
+		libdir := ""
 		lib := <-strings
 		if _, ok := known[lib]; ok {
 			continue
 		 */
 		if path.IsAbs(lib) {
 			binpath = path.Join(root + lib)
+			libdir = ""
 			e, err = elf.Open(binpath)
 		} else {
-			for _, p := range(libpath) {
-				e, err = elf.Open(root + p + lib)
-				if err != nil {
+			for _, libdir = range(libpath) {
+				binpath = path.Join(root, libdir, lib)
+				e, err = elf.Open(binpath)
+				if err == nil {
 					break
 				}
 			}
 		}
-
 		/* nothing? We're done */
 		if err != nil {
 			return []string{}, err
 		}
 
 		dynlibs, err := e.ImportedLibraries()
-
 		/* maybe we should make the chan a []string */
 		for _, val := range(dynlibs) {
 			strings <- val
 		known[lib] = binpath
 
 		ret = append(ret, binpath)
+
+		/* now, what if it was a symlink? 
+		 * the elf.Open worked; we know it's not a bad
+		 * link. So gather up the symlinks and put it all 
+		 * in there. 
+		 */
+		for {
+			linkname, err := os.Readlink(binpath)
+			if err != nil {
+				break
+			} 
+			
+			/* NEVER normalize the value you return
+			 * to the caller
+			 * ALWAYS normalize the binpath
+			 */
+
+			ret = append(ret, linkname)
+			binpath = path.Join(root, libdir, binpath)
+		}
+			
 	}
 	return ret, nil
 }