diff --git a/dtree.c b/dtree.c index be92a7e..e50e913 100644 --- a/dtree.c +++ b/dtree.c @@ -54,6 +54,24 @@ struct dtree_dev_t *dtree_byname(const char *name) return curr; } +struct dtree_dev_t *dtree_byname_prefix(const char *name_prefix) +{ + struct dtree_dev_t *curr = NULL; + int string_length = strlen(name_prefix); + + if(name_prefix == NULL || string_length == 0) + return NULL; + + while((curr = dtree_next()) != NULL) { + if(!strncmp(name_prefix, curr->name, string_length)) + break; + + dtree_dev_free(curr); + } + + return curr; +} + static int is_compatible(const struct dtree_dev_t *dev, const char *compat) { diff --git a/dtree.h b/dtree.h index 273709d..3a4aac5 100644 --- a/dtree.h +++ b/dtree.h @@ -137,6 +137,20 @@ struct dtree_dev_t *dtree_next(void); */ struct dtree_dev_t *dtree_byname(const char *name); +/** + * Look up for device which starts with the name prefix. + * Returns the first occurence of device with the given + * name prefix. + * The entry should be free'd by dtree_dev_free(). + * + * Uses shared internal iterator. + * To search from beginning call dtree_reset(). + * + * Returns NULL when not found or on error. + * On error sets error state. + */ +struct dtree_dev_t *dtree_byname_prefix(const char *name_prefix); + /** * Looks up for device compatible with the given type. * The entry should be free'd by dtree_dev_free().