/src/util-linux/libmount/src/version.c
Line | Count | Source (jump to first uncovered line) |
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) 2008-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: version-utils |
15 | | * @title: Version functions |
16 | | * @short_description: functions to get the library version. |
17 | | */ |
18 | | |
19 | | #include <ctype.h> |
20 | | |
21 | | #include "mountP.h" |
22 | | |
23 | | static const char *lib_version = LIBMOUNT_VERSION; |
24 | | static const char *lib_features[] = { |
25 | | #ifdef HAVE_LIBSELINUX |
26 | | "selinux", |
27 | | #endif |
28 | | #ifdef HAVE_SMACK |
29 | | "smack", |
30 | | #endif |
31 | | #ifdef HAVE_BTRFS_SUPPORT |
32 | | "btrfs", |
33 | | #endif |
34 | | #ifdef HAVE_CRYPTSETUP |
35 | | "verity", |
36 | | #endif |
37 | | #ifdef USE_LIBMOUNT_SUPPORT_NAMESPACES |
38 | | "namespaces", |
39 | | #endif |
40 | | #if defined(HAVE_MOUNTFD_API) && defined(HAVE_LINUX_MOUNT_H) |
41 | | "idmapping", |
42 | | #endif |
43 | | #ifdef USE_LIBMOUNT_MOUNTFD_SUPPORT |
44 | | "fd-based-mount", |
45 | | #endif |
46 | | #ifdef HAVE_STATMOUNT_API |
47 | | "statmount", |
48 | | #endif |
49 | | #if defined(HAVE_STATX) && defined(HAVE_STRUCT_STATX) && defined(AT_STATX_DONT_SYNC) |
50 | | "statx", |
51 | | #endif |
52 | | #ifdef HAVE_STRUCT_FANOTIFY_EVENT_INFO_HEADER |
53 | | "fanotify", |
54 | | #endif |
55 | | #if !defined(NDEBUG) |
56 | | "assert", /* libc assert.h stuff */ |
57 | | #endif |
58 | | "debug", /* always enabled */ |
59 | | NULL |
60 | | }; |
61 | | |
62 | | /** |
63 | | * mnt_parse_version_string: |
64 | | * @ver_string: version string (e.g "2.18.0") |
65 | | * |
66 | | * Returns: release version code. |
67 | | */ |
68 | | int mnt_parse_version_string(const char *ver_string) |
69 | 0 | { |
70 | 0 | const char *cp; |
71 | 0 | int version = 0; |
72 | |
|
73 | 0 | assert(ver_string); |
74 | | |
75 | 0 | for (cp = ver_string; *cp; cp++) { |
76 | 0 | if (*cp == '.') |
77 | 0 | continue; |
78 | 0 | if (!isdigit(*cp)) |
79 | 0 | break; |
80 | 0 | version = (version * 10) + (*cp - '0'); |
81 | 0 | } |
82 | 0 | return version; |
83 | 0 | } |
84 | | |
85 | | /** |
86 | | * mnt_get_library_version: |
87 | | * @ver_string: return pointer to the static library version string if not NULL |
88 | | * |
89 | | * Returns: release version number. |
90 | | */ |
91 | | int mnt_get_library_version(const char **ver_string) |
92 | 0 | { |
93 | 0 | if (ver_string) |
94 | 0 | *ver_string = lib_version; |
95 | |
|
96 | 0 | return mnt_parse_version_string(lib_version); |
97 | 0 | } |
98 | | |
99 | | /** |
100 | | * mnt_get_library_features: |
101 | | * @features: returns a pointer to the static array of strings, the array is |
102 | | * terminated by NULL. |
103 | | * |
104 | | * Returns: number of items in the features array not including the last NULL, |
105 | | * or less than zero in case of error |
106 | | * |
107 | | * Example: |
108 | | * <informalexample> |
109 | | * <programlisting> |
110 | | * const char **features; |
111 | | * |
112 | | * mnt_get_library_features(&features); |
113 | | * while (features && *features) |
114 | | * printf("%s\n", *features++); |
115 | | * </programlisting> |
116 | | * </informalexample> |
117 | | * |
118 | | */ |
119 | | int mnt_get_library_features(const char ***features) |
120 | 0 | { |
121 | 0 | if (!features) |
122 | 0 | return -EINVAL; |
123 | | |
124 | 0 | *features = lib_features; |
125 | 0 | return ARRAY_SIZE(lib_features) - 1; |
126 | 0 | } |
127 | | |
128 | | #ifdef TEST_PROGRAM |
129 | | static int test_version(struct libmnt_test *ts __attribute__((unused)), |
130 | | int argc, char *argv[]) |
131 | | { |
132 | | const char *ver; |
133 | | const char **features; |
134 | | |
135 | | if (argc == 2) |
136 | | printf("Your version: %d\n", |
137 | | mnt_parse_version_string(argv[1])); |
138 | | |
139 | | mnt_get_library_version(&ver); |
140 | | |
141 | | printf("Library version: %s\n", ver); |
142 | | printf("Library API version: " LIBMOUNT_VERSION "\n"); |
143 | | printf("Library features:"); |
144 | | |
145 | | mnt_get_library_features(&features); |
146 | | while (features && *features) |
147 | | printf(" %s", *features++); |
148 | | |
149 | | printf("\n"); |
150 | | |
151 | | if (mnt_get_library_version(NULL) == |
152 | | mnt_parse_version_string(LIBMOUNT_VERSION)) |
153 | | return 0; |
154 | | |
155 | | return -1; |
156 | | } |
157 | | |
158 | | int main(int argc, char *argv[]) |
159 | | { |
160 | | struct libmnt_test ts[] = { |
161 | | { "--print", test_version, "print versions" }, |
162 | | { NULL } |
163 | | }; |
164 | | |
165 | | return mnt_run_test(ts, argc, argv); |
166 | | } |
167 | | #endif |