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.
17 /* Name of this, eg. lib */
19 /* Full path name, eg. /usr/gnemul/x86-linux/lib. */
21 struct pathelem
*parent
;
23 unsigned int num_entries
;
24 struct pathelem
*entries
[0];
27 static struct pathelem
*base
;
29 /* First N chars of S1 match S2, and S2 is N chars long. */
30 static int strneq(const char *s1
, unsigned int n
, const char *s2
)
34 for (i
= 0; i
< n
; i
++)
40 static struct pathelem
*add_entry(struct pathelem
*root
, const char *name
);
42 static struct pathelem
*new_entry(const char *root
,
43 struct pathelem
*parent
,
46 struct pathelem
*new = malloc(sizeof(*new));
47 new->name
= strdup(name
);
48 asprintf(&new->pathname
, "%s/%s", root
, name
);
53 #define streq(a,b) (strcmp((a), (b)) == 0)
55 static struct pathelem
*add_dir_maybe(struct pathelem
*path
)
59 if ((dir
= opendir(path
->pathname
)) != NULL
) {
60 struct dirent
*dirent
;
62 while ((dirent
= readdir(dir
)) != NULL
) {
63 if (!streq(dirent
->d_name
,".") && !streq(dirent
->d_name
,"..")){
64 path
= add_entry(path
, dirent
->d_name
);
72 static struct pathelem
*add_entry(struct pathelem
*root
, const char *name
)
76 root
= realloc(root
, sizeof(*root
)
77 + sizeof(root
->entries
[0])*root
->num_entries
);
79 root
->entries
[root
->num_entries
-1] = new_entry(root
->pathname
, root
, name
);
80 root
->entries
[root
->num_entries
-1]
81 = add_dir_maybe(root
->entries
[root
->num_entries
-1]);
85 /* This needs to be done after tree is stabalized (ie. no more reallocs!). */
86 static void set_parents(struct pathelem
*child
, struct pathelem
*parent
)
90 child
->parent
= parent
;
91 for (i
= 0; i
< child
->num_entries
; i
++)
92 set_parents(child
->entries
[i
], child
);
95 /* FIXME: Doesn't handle DIR/.. where DIR is not in emulated dir. */
97 follow_path(const struct pathelem
*cursor
, const char *name
)
99 unsigned int i
, namelen
;
101 name
+= strspn(name
, "/");
102 namelen
= strcspn(name
, "/");
105 return cursor
->pathname
;
107 if (strneq(name
, namelen
, ".."))
108 return follow_path(cursor
->parent
, name
+ namelen
);
110 if (strneq(name
, namelen
, "."))
111 return follow_path(cursor
, name
+ namelen
);
113 for (i
= 0; i
< cursor
->num_entries
; i
++)
114 if (strneq(name
, namelen
, cursor
->entries
[i
]->name
))
115 return follow_path(cursor
->entries
[i
], name
+ namelen
);
121 void init_paths(const char *prefix
)
123 char pref_buf
[PATH_MAX
];
125 if (prefix
[0] == '\0' ||
126 !strcmp(prefix
, "/"))
129 if (prefix
[0] != '/') {
130 char *cwd
= get_current_dir_name();
133 strcpy(pref_buf
, cwd
);
134 strcat(pref_buf
, "/");
135 strcat(pref_buf
, prefix
);
138 strcpy(pref_buf
,prefix
+ 1);
140 base
= new_entry("", NULL
, pref_buf
);
141 base
= add_dir_maybe(base
);
142 if (base
->num_entries
== 0) {
146 set_parents(base
, base
);
150 /* Look for path in emulation dir, otherwise return name. */
151 const char *path(const char *name
)
153 /* Only do absolute paths: quick and dirty, but should mostly be OK.
154 Could do relative by tracking cwd. */
155 if (!base
|| name
[0] != '/')
158 return follow_path(base
, name
) ?: name
;