]>
Commit | Line | Data |
---|---|---|
2c37ca7c | 1 | /* SPDX-License-Identifier: LGPL-2.1-or-later */ |
f4ab4ae8 | 2 | /* |
2c37ca7c | 3 | * This file is part of libmount from util-linux project. |
f4ab4ae8 | 4 | * |
2c37ca7c | 5 | * Copyright (C) 2008-2018 Karel Zak <kzak@redhat.com> |
f4ab4ae8 | 6 | * |
2c37ca7c KZ |
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. | |
f4ab4ae8 KZ |
11 | */ |
12 | ||
192c6aad | 13 | /** |
7a1c36ab | 14 | * SECTION: version-utils |
192c6aad | 15 | * @title: Version functions |
d58b3157 | 16 | * @short_description: functions to get the library version. |
192c6aad KZ |
17 | */ |
18 | ||
f4ab4ae8 KZ |
19 | #include <ctype.h> |
20 | ||
21 | #include "mountP.h" | |
22 | ||
23 | static const char *lib_version = LIBMOUNT_VERSION; | |
df51de6b KZ |
24 | static const char *lib_features[] = { |
25 | #ifdef HAVE_LIBSELINUX | |
26 | "selinux", | |
27 | #endif | |
83a78332 KZ |
28 | #ifdef HAVE_SMACK |
29 | "smack", | |
4569bbea | 30 | #endif |
5a971329 KZ |
31 | #ifdef HAVE_BTRFS_SUPPORT |
32 | "btrfs", | |
33 | #endif | |
3904d876 KZ |
34 | #ifdef USE_LIBMOUNT_SUPPORT_MTAB |
35 | "mtab", | |
314c3358 | 36 | #endif |
4f7acf32 KZ |
37 | #ifdef USE_LIBMOUNT_SUPPORT_NAMESPACES |
38 | "namespaces", | |
39 | #endif | |
2c656779 KZ |
40 | #if !defined(NDEBUG) |
41 | "assert", /* libc assert.h stuff */ | |
df51de6b | 42 | #endif |
83a78332 | 43 | "debug", /* always enabled */ |
df51de6b KZ |
44 | NULL |
45 | }; | |
f4ab4ae8 KZ |
46 | |
47 | /** | |
48 | * mnt_parse_version_string: | |
49 | * @ver_string: version string (e.g "2.18.0") | |
50 | * | |
51 | * Returns: release version code. | |
52 | */ | |
53 | int mnt_parse_version_string(const char *ver_string) | |
54 | { | |
55 | const char *cp; | |
56 | int version = 0; | |
57 | ||
4569bbea KZ |
58 | assert(ver_string); |
59 | ||
f4ab4ae8 KZ |
60 | for (cp = ver_string; *cp; cp++) { |
61 | if (*cp == '.') | |
62 | continue; | |
63 | if (!isdigit(*cp)) | |
64 | break; | |
65 | version = (version * 10) + (*cp - '0'); | |
66 | } | |
67 | return version; | |
68 | } | |
69 | ||
70 | /** | |
71 | * mnt_get_library_version: | |
df51de6b | 72 | * @ver_string: return pointer to the static library version string if not NULL |
f4ab4ae8 KZ |
73 | * |
74 | * Returns: release version number. | |
75 | */ | |
76 | int mnt_get_library_version(const char **ver_string) | |
77 | { | |
78 | if (ver_string) | |
79 | *ver_string = lib_version; | |
80 | ||
81 | return mnt_parse_version_string(lib_version); | |
82 | } | |
05592793 | 83 | |
df51de6b KZ |
84 | /** |
85 | * mnt_get_library_features: | |
d58b3157 | 86 | * @features: returns a pointer to the static array of strings, the array is |
df51de6b KZ |
87 | * terminated by NULL. |
88 | * | |
ee314075 | 89 | * Returns: number of items in the features array not including the last NULL, |
d58b3157 | 90 | * or less than zero in case of error |
df51de6b | 91 | * |
ee314075 KZ |
92 | * Example: |
93 | * <informalexample> | |
94 | * <programlisting> | |
83212eb3 | 95 | * const char **features; |
df51de6b KZ |
96 | * |
97 | * mnt_get_library_features(&features); | |
98 | * while (features && *features) | |
99 | * printf("%s\n", *features++); | |
ee314075 KZ |
100 | * </programlisting> |
101 | * </informalexample> | |
df51de6b | 102 | * |
df51de6b KZ |
103 | */ |
104 | int mnt_get_library_features(const char ***features) | |
105 | { | |
106 | if (!features) | |
107 | return -EINVAL; | |
108 | ||
109 | *features = lib_features; | |
110 | return ARRAY_SIZE(lib_features) - 1; | |
111 | } | |
112 | ||
05592793 | 113 | #ifdef TEST_PROGRAM |
5fde1d9f | 114 | static int test_version(struct libmnt_test *ts, int argc, char *argv[]) |
05592793 KZ |
115 | { |
116 | const char *ver; | |
df51de6b | 117 | const char **features; |
05592793 | 118 | |
799d9e84 KZ |
119 | if (argc == 2) |
120 | printf("Your version: %d\n", | |
121 | mnt_parse_version_string(argv[1])); | |
122 | ||
05592793 KZ |
123 | mnt_get_library_version(&ver); |
124 | ||
125 | printf("Library version: %s\n", ver); | |
126 | printf("Library API version: " LIBMOUNT_VERSION "\n"); | |
df51de6b KZ |
127 | printf("Library features:"); |
128 | ||
129 | mnt_get_library_features(&features); | |
130 | while (features && *features) | |
131 | printf(" %s", *features++); | |
05592793 | 132 | |
799d9e84 KZ |
133 | printf("\n"); |
134 | ||
05592793 KZ |
135 | if (mnt_get_library_version(NULL) == |
136 | mnt_parse_version_string(LIBMOUNT_VERSION)) | |
137 | return 0; | |
138 | ||
139 | return -1; | |
140 | } | |
141 | ||
142 | int main(int argc, char *argv[]) | |
143 | { | |
68164f6c | 144 | struct libmnt_test ts[] = { |
05592793 KZ |
145 | { "--print", test_version, "print versions" }, |
146 | { NULL } | |
147 | }; | |
148 | ||
149 | return mnt_run_test(ts, argc, argv); | |
150 | } | |
151 | #endif |