1 /* Code to mangle pathnames into those matching a given prefix.
2 eg. open("/lib/foo.so") => open("/usr/gnemul/i386-linux/lib/foo.so");
4 The assumption is that this area does not change.
18 /* Name of this, eg. lib */
20 /* Full path name, eg. /usr/gnemul/x86-linux/lib. */
22 struct pathelem *parent;
24 unsigned int num_entries;
25 struct pathelem *entries[0];
28 static struct pathelem *base;
30 /* First N chars of S1 match S2, and S2 is N chars long. */
31 static int strneq(const char *s1, unsigned int n, const char *s2)
35 for (i = 0; i < n; i++)
41 static struct pathelem *add_entry(struct pathelem *root, const char *name);
43 static struct pathelem *new_entry(const char *root,
44 struct pathelem *parent,
47 struct pathelem *new = malloc(sizeof(*new));
48 new->name = strdup(name);
49 asprintf(&new->pathname, "%s/%s", root, name);
54 #define streq(a,b) (strcmp((a), (b)) == 0)
57 * Checks whether directory entry (dent) is valid. This
58 * means that symlinks pointing to '.' and '..' should
59 * be skipped by main recursion code. Returns 1 when
63 is_dentry_valid(const char *path, const struct dirent *dent)
65 char fullpath[PATH_MAX];
66 char linkbuf[PATH_MAX];
72 if (dent->d_type != DT_LNK)
75 (void) snprintf(fullpath, sizeof (fullpath), "%s/%s",
78 if ((len = readlink(fullpath, linkbuf, sizeof (linkbuf) - 1)) != -1) {
80 if (streq(linkbuf, ".") || streq(linkbuf, ".."))
87 /* TODO: add recursion count check */
88 static struct pathelem *add_dir_maybe(struct pathelem *path)
92 if ((dir = opendir(path->pathname)) != NULL) {
93 struct dirent *dirent;
95 while ((dirent = readdir(dir)) != NULL) {
96 if (!streq(dirent->d_name,".") && !streq(dirent->d_name,"..")){
97 if (is_dentry_valid(path->pathname, dirent)) {
98 path = add_entry(path, dirent->d_name);
107 static struct pathelem *add_entry(struct pathelem *root, const char *name)
111 root = realloc(root, sizeof(*root)
112 + sizeof(root->entries[0])*root->num_entries);
114 root->entries[root->num_entries-1] = new_entry(root->pathname, root, name);
115 root->entries[root->num_entries-1]
116 = add_dir_maybe(root->entries[root->num_entries-1]);
120 /* This needs to be done after tree is stabilized (ie. no more reallocs!). */
121 static void set_parents(struct pathelem *child, struct pathelem *parent)
125 child->parent = parent;
126 for (i = 0; i < child->num_entries; i++)
127 set_parents(child->entries[i], child);
130 /* FIXME: Doesn't handle DIR/.. where DIR is not in emulated dir. */
132 follow_path(const struct pathelem *cursor, const char *name)
134 unsigned int i, namelen;
136 name += strspn(name, "/");
137 namelen = strcspn(name, "/");
140 return cursor->pathname;
142 if (strneq(name, namelen, ".."))
143 return follow_path(cursor->parent, name + namelen);
145 if (strneq(name, namelen, "."))
146 return follow_path(cursor, name + namelen);
148 for (i = 0; i < cursor->num_entries; i++)
149 if (strneq(name, namelen, cursor->entries[i]->name))
150 return follow_path(cursor->entries[i], name + namelen);
156 void init_paths(const char *prefix)
158 char pref_buf[PATH_MAX];
160 if (prefix[0] == '\0' ||
161 !strcmp(prefix, "/"))
164 if (prefix[0] != '/') {
165 char *cwd = get_current_dir_name();
168 strcpy(pref_buf, cwd);
169 strcat(pref_buf, "/");
170 strcat(pref_buf, prefix);
173 strcpy(pref_buf,prefix + 1);
175 base = new_entry("", NULL, pref_buf);
176 base = add_dir_maybe(base);
177 if (base->num_entries == 0) {
181 set_parents(base, base);
185 /* Look for path in emulation dir, otherwise return name. */
186 const char *path(const char *name)
188 /* Only do absolute paths: quick and dirty, but should mostly be OK.
189 Could do relative by tracking cwd. */
190 if (!base || !name || name[0] != '/')
193 return follow_path(base, name) ?: name;