It only worked because n was always 1. kmod_list_remove returns a
pointer to the next element, relative to the removed one. Therefore we
need to always get a pointer to the last.
struct kmod_list *kmod_list_remove_n_latest(struct kmod_list *list,
unsigned int n)
{
- struct kmod_list *l;
+ struct kmod_list *l = list;
unsigned int i;
- /*
- * Get last element, remove all appended elments and if list became
- * empty, set return pointer to NULL
- */
- l = kmod_list_prev(list, list);
- if (l == NULL)
- l = list;
-
- for (i = 0; i < n; i++)
+ for (i = 0; i < n; i++) {
+ l = kmod_list_last(l);
l = kmod_list_remove(l);
+ }
- /* If list became empty, save it*/
- if (l == NULL)
- list = NULL;
-
- return list;
+ return l;
}
+
/**
* kmod_list_prev:
* @list: the head of the list