* Given two normalized paths (a trailing slash is ok), if subdir is * outside dir, return -1. Otherwise return the offset in subdir that * can be used as relative path to dir. */
| 3212 | * can be used as relative path to dir. |
| 3213 | */ |
| 3214 | int dir_inside_of(const char *subdir, const char *dir) |
| 3215 | { |
| 3216 | int offset = 0; |
| 3217 | |
| 3218 | assert(dir && subdir && *dir && *subdir); |
| 3219 | |
| 3220 | while (*dir && *subdir && !cmp_icase(*dir, *subdir)) { |
| 3221 | dir++; |
| 3222 | subdir++; |
| 3223 | offset++; |
| 3224 | } |
| 3225 | |
| 3226 | /* hel[p]/me vs hel[l]/yeah */ |
| 3227 | if (*dir && *subdir) |
| 3228 | return -1; |
| 3229 | |
| 3230 | if (!*subdir) |
| 3231 | return !*dir ? offset : -1; /* same dir */ |
| 3232 | |
| 3233 | /* foo/[b]ar vs foo/[] */ |
| 3234 | if (is_dir_sep(dir[-1])) |
| 3235 | return is_dir_sep(subdir[-1]) ? offset : -1; |
| 3236 | |
| 3237 | /* foo[/]bar vs foo[] */ |
| 3238 | return is_dir_sep(*subdir) ? offset + 1 : -1; |
| 3239 | } |
| 3240 | |
| 3241 | int is_inside_dir(const char *dir) |
| 3242 | { |
no test coverage detected