1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2 /*
3 * This file is part of libmount from util-linux project.
4 *
5 * Copyright (C) 2009-2018 Karel Zak <kzak@redhat.com>
6 *
7 * libmount is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU Lesser General Public License as published by
9 * the Free Software Foundation; either version 2.1 of the License, or
10 * (at your option) any later version.
11 */
12
13 /**
14 * SECTION: iter
15 * @title: Iterator
16 * @short_description: unified iterator
17 *
18 * The iterator keeps the direction and the last position
19 * for access to the internal library tables/lists.
20 */
21 #include <stdio.h>
22 #include <string.h>
23 #include <stdlib.h>
24
25 #include "mountP.h"
26
27 /**
28 * mnt_new_iter:
29 * @direction: MNT_INTER_{FOR,BACK}WARD direction
30 *
31 * Returns: newly allocated generic libmount iterator.
32 */
33 struct libmnt_iter *mnt_new_iter(int direction)
34 {
35 struct libmnt_iter *itr = calloc(1, sizeof(*itr));
36 if (!itr)
37 return NULL;
38 itr->direction = direction;
39 return itr;
40 }
41
42 /**
43 * mnt_free_iter:
44 * @itr: iterator pointer
45 *
46 * Deallocates the iterator.
47 */
48 void mnt_free_iter(struct libmnt_iter *itr)
49 {
50 free(itr);
51 }
52
53 /**
54 * mnt_reset_iter:
55 * @itr: iterator pointer
56 * @direction: MNT_INTER_{FOR,BACK}WARD or -1 to keep the direction unchanged
57 *
58 * Resets the iterator.
59 */
60 void mnt_reset_iter(struct libmnt_iter *itr, int direction)
61 {
62 if (direction == -1)
63 direction = itr->direction;
64
65 memset(itr, 0, sizeof(*itr));
66 itr->direction = direction;
67 }
68
69 /**
70 * mnt_iter_get_direction:
71 * @itr: iterator pointer
72 *
73 * Returns: MNT_INTER_{FOR,BACK}WARD
74 */
75 int mnt_iter_get_direction(struct libmnt_iter *itr)
76 {
77 return itr->direction;
78 }