]> git.ipfire.org Git - thirdparty/util-linux.git/blame - libmount/src/version.c
Merge branch 'lsns--Q' of https://github.com/masatake/util-linux
[thirdparty/util-linux.git] / libmount / src / version.c
CommitLineData
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"
e087a188 22#include "mount-api-utils.h"
f4ab4ae8
KZ
23
24static const char *lib_version = LIBMOUNT_VERSION;
df51de6b
KZ
25static const char *lib_features[] = {
26#ifdef HAVE_LIBSELINUX
27 "selinux",
28#endif
83a78332
KZ
29#ifdef HAVE_SMACK
30 "smack",
4569bbea 31#endif
5a971329
KZ
32#ifdef HAVE_BTRFS_SUPPORT
33 "btrfs",
34#endif
cbc390da
KZ
35#ifdef HAVE_CRYPTSETUP
36 "verity",
37#endif
4f7acf32
KZ
38#ifdef USE_LIBMOUNT_SUPPORT_NAMESPACES
39 "namespaces",
40#endif
5502e73d 41#if defined(HAVE_MOUNTFD_API) && defined(HAVE_LINUX_MOUNT_H)
e087a188 42 "idmapping",
9040c090
KZ
43#endif
44#ifdef USE_LIBMOUNT_MOUNTFD_SUPPORT
f3a8eebd 45 "fd-based-mount",
e087a188 46#endif
e82d1897
KZ
47#if defined(HAVE_STATX) && defined(HAVE_STRUCT_STATX) && defined(AT_STATX_DONT_SYNC)
48 "statx",
49#endif
2c656779
KZ
50#if !defined(NDEBUG)
51 "assert", /* libc assert.h stuff */
df51de6b 52#endif
83a78332 53 "debug", /* always enabled */
df51de6b
KZ
54 NULL
55};
f4ab4ae8
KZ
56
57/**
58 * mnt_parse_version_string:
59 * @ver_string: version string (e.g "2.18.0")
60 *
61 * Returns: release version code.
62 */
63int mnt_parse_version_string(const char *ver_string)
64{
65 const char *cp;
66 int version = 0;
67
4569bbea
KZ
68 assert(ver_string);
69
f4ab4ae8
KZ
70 for (cp = ver_string; *cp; cp++) {
71 if (*cp == '.')
72 continue;
73 if (!isdigit(*cp))
74 break;
75 version = (version * 10) + (*cp - '0');
76 }
77 return version;
78}
79
80/**
81 * mnt_get_library_version:
df51de6b 82 * @ver_string: return pointer to the static library version string if not NULL
f4ab4ae8
KZ
83 *
84 * Returns: release version number.
85 */
86int mnt_get_library_version(const char **ver_string)
87{
88 if (ver_string)
89 *ver_string = lib_version;
90
91 return mnt_parse_version_string(lib_version);
92}
05592793 93
df51de6b
KZ
94/**
95 * mnt_get_library_features:
d58b3157 96 * @features: returns a pointer to the static array of strings, the array is
df51de6b
KZ
97 * terminated by NULL.
98 *
ee314075 99 * Returns: number of items in the features array not including the last NULL,
d58b3157 100 * or less than zero in case of error
df51de6b 101 *
ee314075
KZ
102 * Example:
103 * <informalexample>
104 * <programlisting>
83212eb3 105 * const char **features;
df51de6b
KZ
106 *
107 * mnt_get_library_features(&features);
108 * while (features && *features)
109 * printf("%s\n", *features++);
ee314075
KZ
110 * </programlisting>
111 * </informalexample>
df51de6b 112 *
df51de6b
KZ
113 */
114int mnt_get_library_features(const char ***features)
115{
116 if (!features)
117 return -EINVAL;
118
119 *features = lib_features;
120 return ARRAY_SIZE(lib_features) - 1;
121}
122
05592793 123#ifdef TEST_PROGRAM
2a472ae8
TW
124static int test_version(struct libmnt_test *ts __attribute__((unused)),
125 int argc, char *argv[])
05592793
KZ
126{
127 const char *ver;
df51de6b 128 const char **features;
05592793 129
799d9e84
KZ
130 if (argc == 2)
131 printf("Your version: %d\n",
132 mnt_parse_version_string(argv[1]));
133
05592793
KZ
134 mnt_get_library_version(&ver);
135
136 printf("Library version: %s\n", ver);
137 printf("Library API version: " LIBMOUNT_VERSION "\n");
df51de6b
KZ
138 printf("Library features:");
139
140 mnt_get_library_features(&features);
141 while (features && *features)
142 printf(" %s", *features++);
05592793 143
799d9e84
KZ
144 printf("\n");
145
05592793
KZ
146 if (mnt_get_library_version(NULL) ==
147 mnt_parse_version_string(LIBMOUNT_VERSION))
148 return 0;
149
150 return -1;
151}
152
153int main(int argc, char *argv[])
154{
68164f6c 155 struct libmnt_test ts[] = {
05592793
KZ
156 { "--print", test_version, "print versions" },
157 { NULL }
158 };
159
160 return mnt_run_test(ts, argc, argv);
161}
162#endif