]>
Commit | Line | Data |
---|---|---|
f4ab4ae8 | 1 | /* |
cbe92027 | 2 | * version.c - Return the version of the libmount library |
f4ab4ae8 KZ |
3 | * |
4 | * Copyright (C) 2008 Karel Zak <kzak@redhat.com> | |
5 | * [Based on libblkid/version.c by Theodore Ts'o] | |
6 | * | |
7 | * See COPYING.libmount for the License of this software. | |
8 | */ | |
9 | ||
192c6aad | 10 | /** |
7a1c36ab | 11 | * SECTION: version-utils |
192c6aad | 12 | * @title: Version functions |
d58b3157 | 13 | * @short_description: functions to get the library version. |
192c6aad KZ |
14 | */ |
15 | ||
f4ab4ae8 KZ |
16 | #include <ctype.h> |
17 | ||
18 | #include "mountP.h" | |
19 | ||
20 | static const char *lib_version = LIBMOUNT_VERSION; | |
df51de6b KZ |
21 | static const char *lib_features[] = { |
22 | #ifdef HAVE_LIBSELINUX | |
23 | "selinux", | |
24 | #endif | |
83a78332 KZ |
25 | #ifdef HAVE_SMACK |
26 | "smack", | |
4569bbea KZ |
27 | #endif |
28 | #ifdef CONFIG_LIBMOUNT_ASSERT | |
29 | "assert", | |
df51de6b | 30 | #endif |
83a78332 | 31 | "debug", /* always enabled */ |
df51de6b KZ |
32 | NULL |
33 | }; | |
f4ab4ae8 KZ |
34 | |
35 | /** | |
36 | * mnt_parse_version_string: | |
37 | * @ver_string: version string (e.g "2.18.0") | |
38 | * | |
39 | * Returns: release version code. | |
40 | */ | |
41 | int mnt_parse_version_string(const char *ver_string) | |
42 | { | |
43 | const char *cp; | |
44 | int version = 0; | |
45 | ||
4569bbea KZ |
46 | assert(ver_string); |
47 | ||
f4ab4ae8 KZ |
48 | for (cp = ver_string; *cp; cp++) { |
49 | if (*cp == '.') | |
50 | continue; | |
51 | if (!isdigit(*cp)) | |
52 | break; | |
53 | version = (version * 10) + (*cp - '0'); | |
54 | } | |
55 | return version; | |
56 | } | |
57 | ||
58 | /** | |
59 | * mnt_get_library_version: | |
df51de6b | 60 | * @ver_string: return pointer to the static library version string if not NULL |
f4ab4ae8 KZ |
61 | * |
62 | * Returns: release version number. | |
63 | */ | |
64 | int mnt_get_library_version(const char **ver_string) | |
65 | { | |
66 | if (ver_string) | |
67 | *ver_string = lib_version; | |
68 | ||
69 | return mnt_parse_version_string(lib_version); | |
70 | } | |
05592793 | 71 | |
df51de6b KZ |
72 | /** |
73 | * mnt_get_library_features: | |
d58b3157 | 74 | * @features: returns a pointer to the static array of strings, the array is |
df51de6b KZ |
75 | * terminated by NULL. |
76 | * | |
ee314075 | 77 | * Returns: number of items in the features array not including the last NULL, |
d58b3157 | 78 | * or less than zero in case of error |
df51de6b | 79 | * |
ee314075 KZ |
80 | * Example: |
81 | * <informalexample> | |
82 | * <programlisting> | |
df51de6b KZ |
83 | * const char *features; |
84 | * | |
85 | * mnt_get_library_features(&features); | |
86 | * while (features && *features) | |
87 | * printf("%s\n", *features++); | |
ee314075 KZ |
88 | * </programlisting> |
89 | * </informalexample> | |
df51de6b | 90 | * |
df51de6b KZ |
91 | */ |
92 | int mnt_get_library_features(const char ***features) | |
93 | { | |
94 | if (!features) | |
95 | return -EINVAL; | |
96 | ||
97 | *features = lib_features; | |
98 | return ARRAY_SIZE(lib_features) - 1; | |
99 | } | |
100 | ||
05592793 | 101 | #ifdef TEST_PROGRAM |
68164f6c | 102 | int test_version(struct libmnt_test *ts, int argc, char *argv[]) |
05592793 KZ |
103 | { |
104 | const char *ver; | |
df51de6b | 105 | const char **features; |
05592793 KZ |
106 | |
107 | mnt_get_library_version(&ver); | |
108 | ||
109 | printf("Library version: %s\n", ver); | |
110 | printf("Library API version: " LIBMOUNT_VERSION "\n"); | |
df51de6b KZ |
111 | printf("Library features:"); |
112 | ||
113 | mnt_get_library_features(&features); | |
114 | while (features && *features) | |
115 | printf(" %s", *features++); | |
05592793 KZ |
116 | |
117 | if (mnt_get_library_version(NULL) == | |
118 | mnt_parse_version_string(LIBMOUNT_VERSION)) | |
119 | return 0; | |
120 | ||
121 | return -1; | |
122 | } | |
123 | ||
124 | int main(int argc, char *argv[]) | |
125 | { | |
68164f6c | 126 | struct libmnt_test ts[] = { |
05592793 KZ |
127 | { "--print", test_version, "print versions" }, |
128 | { NULL } | |
129 | }; | |
130 | ||
131 | return mnt_run_test(ts, argc, argv); | |
132 | } | |
133 | #endif |