2012-07-10 11:13:03 +00:00
|
|
|
/*
|
|
|
|
* \brief Utilities
|
|
|
|
* \author Norman Feske
|
|
|
|
* \author Christian Prochaska
|
|
|
|
* \date 2012-04-11
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef _UTIL_H_
|
|
|
|
#define _UTIL_H_
|
|
|
|
|
|
|
|
/* Genode includes */
|
|
|
|
#include <util/string.h>
|
2015-06-04 12:59:40 +00:00
|
|
|
#include <file_system/util.h>
|
2012-07-10 11:13:03 +00:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return base-name portion of null-terminated path string
|
|
|
|
*/
|
|
|
|
static inline char const *basename(char const *path)
|
|
|
|
{
|
|
|
|
char const *start = path;
|
|
|
|
|
|
|
|
for (; *path; path++)
|
|
|
|
if (*path == '/')
|
|
|
|
start = path + 1;
|
|
|
|
|
|
|
|
return start;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return true if null-terminated string 'substr' occurs in null-terminated
|
|
|
|
* string 'str'
|
|
|
|
*/
|
|
|
|
static bool string_contains(char const *str, char const *substr)
|
|
|
|
{
|
|
|
|
using namespace Genode;
|
2016-09-15 12:40:37 +00:00
|
|
|
using Genode::size_t;
|
2012-07-10 11:13:03 +00:00
|
|
|
|
|
|
|
size_t str_len = strlen(str);
|
|
|
|
size_t substr_len = strlen(substr);
|
|
|
|
|
|
|
|
if (str_len < substr_len)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
for (size_t i = 0; i <= (str_len - substr_len); i++)
|
|
|
|
if (strcmp(&str[i], substr, substr_len) == 0)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return true if 'str' is a valid file name
|
|
|
|
*/
|
|
|
|
static inline bool valid_filename(char const *str)
|
|
|
|
{
|
|
|
|
if (!str) return false;
|
|
|
|
|
|
|
|
/* must have at least one character */
|
|
|
|
if (str[0] == 0) return false;
|
|
|
|
|
|
|
|
/* must not contain '/' or '\' or ':' */
|
2015-06-04 12:59:40 +00:00
|
|
|
if (File_system::string_contains(str, '/') ||
|
|
|
|
File_system::string_contains(str, '\\') ||
|
|
|
|
File_system::string_contains(str, ':'))
|
2012-07-10 11:13:03 +00:00
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return true if 'str' is a valid path
|
|
|
|
*/
|
|
|
|
static inline bool valid_path(char const *str)
|
|
|
|
{
|
|
|
|
if (!str) return false;
|
|
|
|
|
|
|
|
/* must start with '/' */
|
|
|
|
if (str[0] != '/')
|
|
|
|
return false;
|
|
|
|
|
|
|
|
/* must not contain '\' or ':' */
|
2015-06-04 12:59:40 +00:00
|
|
|
if (File_system::string_contains(str, '\\') ||
|
|
|
|
File_system::string_contains(str, ':'))
|
2012-07-10 11:13:03 +00:00
|
|
|
return false;
|
|
|
|
|
|
|
|
/* must not contain "/../" */
|
|
|
|
if (string_contains(str, "/../")) return false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return true if 'str' is "/"
|
|
|
|
*/
|
|
|
|
static inline bool is_root(const char *str)
|
|
|
|
{
|
|
|
|
return (Genode::strcmp(str, "/") == 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* _UTIL_H_ */
|