Skip to content

Commit

Permalink
Windows: accept backslashes in file names
Browse files Browse the repository at this point in the history
Without this patch, a warning gets printed for any .yang files that are
read using native path names:

 libyang[1]: File name "D:\a\oopt-gnpy-libyang\oopt-gnpy-libyang\tests\yang\ietf-network@2018-02-26.yang" does not match module name "ietf-network".
  • Loading branch information
jktjkt committed Aug 31, 2023
1 parent b5992f5 commit 41f2002
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 3 deletions.
20 changes: 20 additions & 0 deletions compat/compat.c
Original file line number Diff line number Diff line change
Expand Up @@ -347,3 +347,23 @@ setenv(const char *name, const char *value, int overwrite)
#error No setenv() implementation for this platform is available.
#endif
#endif

/**
* @brief Equivalent of strrchr(s, '/') which also supports Windows path on Windows
*/
char *strrchr_fname_slash(const char *s)
{
#ifndef _WIN32
return strrchr(s, '/');
#else
char *slash = strrchr(s, '/');
char *backslash = strrchr(s, '\\');
if (slash && backslash) {
retturn slash > backslash ? slash : backslash;
} else if (slash) {
return slash;
} else {
return backslash;
}
#endif
}
2 changes: 2 additions & 0 deletions compat/compat.h.in
Original file line number Diff line number Diff line change
Expand Up @@ -210,4 +210,6 @@ char *strptime(const char *s, const char *format, struct tm *tm);
int setenv(const char *name, const char *value, int overwrite);
#endif

char *strrchr_fname_slash(const char *s);

#endif /* _COMPAT_H_ */
2 changes: 1 addition & 1 deletion src/tree_schema.c
Original file line number Diff line number Diff line change
Expand Up @@ -1832,7 +1832,7 @@ lys_parse_in(struct ly_ctx *ctx, struct ly_in *in, LYS_INFORMAT format,
switch (in->type) {
case LY_IN_FILEPATH:
/* check that name and revision match filename */
filename = strrchr(in->method.fpath.filepath, '/');
filename = strrchr_fname_slash(in->method.fpath.filepath);
if (!filename) {
filename = in->method.fpath.filepath;
} else {
Expand Down
2 changes: 1 addition & 1 deletion src/tree_schema_common.c
Original file line number Diff line number Diff line change
Expand Up @@ -740,7 +740,7 @@ lysp_load_module_check(const struct ly_ctx *ctx, struct lysp_module *mod, struct
}
if (info->path) {
/* check that name and revision match filename */
filename = strrchr(info->path, '/');
filename = strrchr_fname_slash(info->path);
if (!filename) {
filename = info->path;
} else {
Expand Down
2 changes: 1 addition & 1 deletion tools/lint/common.c
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ parse_schema_path(const char *path, char **dir, char **module)

/* split the path to dirname and basename for further work */
*dir = strdup(path);
*module = strrchr(*dir, '/');
*module = strrchr_fname_slash(*dir);
if (!(*module)) {
*module = *dir;
*dir = strdup("./");
Expand Down

0 comments on commit 41f2002

Please sign in to comment.