Commit Graph

87 Commits

Author SHA1 Message Date
Karel Zak 9fc0f69c25 include/strutils: consolidate string to number conversion
Signed-off-by: Karel Zak <kzak@redhat.com>
2021-06-24 16:36:58 +02:00
Karel Zak 84825b161b include/strutils: cleanup strto..() functions
* add ul_strtos64() and ul_strtou64()
* add simple test

Addresses: https://github.com/karelzak/util-linux/issues/1358
Signed-off-by: Karel Zak <kzak@redhat.com>
2021-06-22 14:42:15 +02:00
Karel Zak f3e5d34cac lib/strutils: make test_strutils_normalize() more robust
Signed-off-by: Karel Zak <kzak@redhat.com>
2021-06-15 10:25:06 +02:00
Karel Zak c862d0e177 lib/strutils: improve normalize_whitespace()
Let's make it possible to use the function to normalize the string
between two buffers (from source to destination).

Signed-off-by: Karel Zak <kzak@redhat.com>
2021-06-14 16:24:59 +02:00
Karel Zak 35c84bf76a lib/strutils: assume 64-bit time_t
Signed-off-by: Karel Zak <kzak@redhat.com>
2021-05-06 11:06:45 +02:00
Karel Zak 27e6b3a92e lib/strutils: add ul_stralnumcmp()
Signed-off-by: Karel Zak <kzak@redhat.com>
2021-02-23 09:19:48 +01:00
Karel Zak 5d68f974b4 lib/strutils: add normalize_whitespace()
This function removes extra whitespace.

Signed-off-by: Karel Zak <kzak@redhat.com>
2020-11-13 09:19:02 +01:00
Karel Zak 1aa0f1abf4 lib/strutils: use directly err()
Signed-off-by: Karel Zak <kzak@redhat.com>
2020-06-09 11:45:55 +02:00
Karel Zak 0c960dd8b9 lib/strutils: remove unnecessary include
Signed-off-by: Karel Zak <kzak@redhat.com>
2020-06-09 10:56:28 +02:00
Karel Zak 330b09976f build-sys: remove redundard includes
Signed-off-by: Karel Zak <kzak@redhat.com>
2020-06-09 10:53:14 +02:00
Karel Zak 191836bec2 lib/strutils: fix parse_size() for large numbers
For example
  ./test_strutils  --size '1.0000000000000000000000000000018000000000000000010M'
  ./test_strutils  --size '1.18446744073709551615M'

ends with infinite loop due to frac_div variable overflow.

Addresses: https://github.com/karelzak/util-linux/issues/1023
Signed-off-by: Karel Zak <kzak@redhat.com>
2020-05-18 13:34:55 +02:00
Karel Zak 1186cdf336 lib/strutils: fix floating point exception
Addresses: https://github.com/karelzak/util-linux/issues/1017
Signed-off-by: Karel Zak <kzak@redhat.com>
2020-04-28 12:28:59 +02:00
Rosen Penev 042f62dfc5
[clang-tidy] do not use else after return
Found with readability-else-after-return

Signed-off-by: Rosen Penev <rosenp@gmail.com>
2020-04-20 13:20:59 -07:00
Karel Zak f12d5ad279 lib/strutils: fix uint64_t overflow
Addresses: https://github.com/karelzak/util-linux/issues/998
Signed-off-by: Karel Zak <kzak@redhat.com>
2020-04-17 10:25:17 +02:00
Karel Zak b38c20e13a ilib/strutils: fix rounding in size_to_human_string()
Thanks to bub75 for the idea.

The patch adds SIZE_DECIMAL_2DIGITS into regression tests.

Addresses: https://github.com/karelzak/util-linux/issues/998
Signed-off-by: Karel Zak <kzak@redhat.com>
2020-04-15 15:06:37 +02:00
Karel Zak 0683647c70 lib/strutils: add test for strdup_to_struct_member()
Signed-off-by: Karel Zak <kzak@redhat.com>
2020-01-28 11:34:04 +01:00
Karel Zak b82237dfb2 lib: add _PATH_TMP fallback
- cleanup header of the file
- fix fileutils.c warnings
- add _PATH_TMP fallback

Addresses: https://github.com/karelzak/util-linux/issues/931
Signed-off-by: Karel Zak <kzak@redhat.com>
2020-01-13 10:49:44 +01:00
Karel Zak 4e36662e52 lib: add missing license headers
Signed-off-by: Karel Zak <kzak@redhat.com>
2019-11-07 09:38:06 +01:00
Karel Zak 6d92f7d7bf lib/strutils: keep static analyzer happy [coverity scan]
Signed-off-by: Karel Zak <kzak@redhat.com>
2019-05-15 16:09:16 +02:00
Karel Zak 8c368dc6d3 lib/strutils: parse_size() fix frac digit calculation
Old code:

	./test_strutils --size 0.5MiB
		0.5MiB :               512000 :     500K :      500 KiB
	./test_strutils --size 0.50MiB
		0.50MiB :              5120000 :     4.9M :      4.9 MiB

New code:
	./test_strutils --size 0.5MiB
		0.5MiB :               524288 :     512K :      512 KiB
	./test_strutils --size 0.50MiB
	       0.50MiB :               524288 :     512K :      512 KiB

Note that the new implementation also does not use float points,
because we need to support PiB and so on... it seems good enough for
things like:

        ./test_strutils --size 7.13G
                7.13G :           7656104581 :     7.1G :      7.1 GiB
        ./test_strutils --size 7.16G
                7.16G :           7690675814 :     7.2G :      7.2 GiB

to avoid situation where cfdisk creates partition with completely
crazy numbers.

Addresses: https://github.com/karelzak/util-linux/issues/782
Signed-off-by: Karel Zak <kzak@redhat.com>
2019-05-13 17:07:14 +02:00
Karel Zak 482e0a0754 lib/strutils: parse_size() fix frac with zeros
Fix 0.001G as well as accept 0.000G as valid number.

Signed-off-by: Karel Zak <kzak@redhat.com>
2019-05-13 16:15:58 +02:00
Karel Zak 07b94c9f32 lib/strutils: support two decimal places in size_to_human_string() output
Signed-off-by: Karel Zak <kzak@redhat.com>
2019-02-12 14:20:55 +01:00
Karel Zak 5b82289b60 lib/strutils: return from xstrmode()
Signed-off-by: Karel Zak <kzak@redhat.com>
2018-09-14 16:26:38 +02:00
Karel Zak d54b83156f lib/strutils: follow const in parse_size()
* don't cast from char to const char

* don't share endptr from strtoxxx() with rest of the code
  as the end pointer is char, but code works with const chars

Signed-off-by: Karel Zak <kzak@redhat.com>
2018-07-20 12:54:58 +02:00
Karel Zak 8deb816173 lib/strutils: remove redundant condition
[util-linux-2.32.1/lib/strutils.c:122]: (style) Redundant condition: If 'EXPR == '0'', the comparison 'EXPR' is always true.

Reported-by: David Binderman <dcb314@hotmail.com>
Signed-off-by: Karel Zak <kzak@redhat.com>
2018-07-18 09:48:19 +02:00
Karel Zak 64af1a2987 lib/strutils: fix strnlen() fallback
Addresses: https://github.com/karelzak/util-linux/issues/643
Signed-off-by: Karel Zak <kzak@redhat.com>
2018-05-28 12:30:34 +02:00
Ruediger Meier 9c8b9fbacc lib: fix strutils.h, remove STRTOXX_EXIT_CODE
As discussed on the mailing list. We fix all places
where the non-working define STRTOXX_EXIT_CODE was used.

Regarding tunelp, also see 7e3c80a7.

Signed-off-by: Ruediger Meier <ruediger.meier@ga-group.nl>
2017-06-22 22:40:12 +02:00
Karel Zak cab84ea39c lib/strutils: fix typos in huge size suffixes
Signed-off-by: Karel Zak <kzak@redhat.com>
2017-04-03 12:52:53 +02:00
Karel Zak 61cbc8a3f5 lib/strutils: return end pointer by isdigit_string()
Signed-off-by: Karel Zak <kzak@redhat.com>
2017-01-28 12:39:30 +01:00
Karel Zak e8f7acb0d3 lib: use unique ifdefs for tests
Let's use unique TEST_PROGRAM_<NAME> ifdefs to make build system more
robust.

Signed-off-by: Karel Zak <kzak@redhat.com>
2017-01-04 11:44:37 +01:00
Heiko Carstens 54394eab03 lib,strutils: add strtoux[16|32|64]_or_err functions
Add helper functions which allow to parse hexadecimal numbers.
Based on a patch from Clemens von Mann.

Signed-off-by: Heiko Carstens <heiko.carstens@de.ibm.com>
2016-11-09 10:02:32 +01:00
Karel Zak d4e89dea4e libmount: ignore redundant slashes
///aaa/bbb and /aaa/bbb/ are the same paths. This is important
especially with NFS where number of slashes are not the same in
the /proc/self/mountinfo and fstab or utab. The regular URI is

 euler://tmp

but /proc contains

 euler:/tmp

Reported-by: Ales Novak <alnovak@suse.cz>
Signed-off-by: Karel Zak <kzak@redhat.com>
2016-08-08 17:23:54 +02:00
Sebastian Rasmussen 9e93004171 misc: Fix various typos
Fix various typos in error messages, warnings, debug strings,
comments and names of static functions.

Signed-off-by: Sebastian Rasmussen <sebras@gmail.com>
2016-05-31 23:40:21 +02:00
Karel Zak 3c201431ee lib/timeutils: add strxxx_iso() functions
Signed-off-by: Karel Zak <kzak@redhat.com>
2016-05-11 15:31:02 +02:00
Karel Zak d551d66891 lib/strutils: use int for fgetc()
Signed-off-by: Karel Zak <kzak@redhat.com>
2016-04-04 11:01:15 +02:00
Karel Zak 2d47fa3921 partx: fix --nr usage
Reported-by: Serge van den Boom <serge+util-linux@vdboom.org>
Signed-off-by: Karel Zak <kzak@redhat.com>
2016-03-22 15:49:00 +01:00
Karel Zak 40f00b4f8e libmount: improve conversion from root= to the devname
Currently the code supports /dev/name or PARTUUID= only. We also
need to support 'maj:min' and 'hexhex' notations.

Reported-by: George Spelvin <linux@horizon.com>
Signed-off-by: Karel Zak <kzak@redhat.com>
2016-03-15 14:07:34 +01:00
Ruediger Meier fea1cbf748 misc: never cast void* from malloc(3) and friends
Such cast could hide serious compiler warnings in case we are
missing includes (e.g. <stdlib.h> or "xalloc.h").

See
http://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc

Signed-off-by: Ruediger Meier <ruediger.meier@ga-group.nl>
2016-03-07 23:29:27 +01:00
Ruediger Meier f643d3349e lib/strutils: parse_size(), sync errno and return value
Maybe strtosize_or_err() is the only function which uses
this errno (wrongly). But it doesnt hurt to maintain rc
as well as errno.

Signed-off-by: Ruediger Meier <ruediger.meier@ga-group.nl>
2016-02-21 18:10:00 +01:00
Ruediger Meier ca8eff2ade lib/strutils: add more ERANGE messages
Signed-off-by: Ruediger Meier <ruediger.meier@ga-group.nl>
2016-02-21 18:09:28 +01:00
Ruediger Meier 73b9e094a8 lib/strutils: fix errno usage in strtoint functions
Don't use undefined errno:
$ ./logger --no-act -t "wtf" --id="" message
logger: failed to parse id: '': No such file or directory

Don't print useless EINVAL message but ERANGE only:
$ ./logger --no-act -t "wtf" --id="XX" message
logger: failed to parse id: 'XX': Invalid argument

Note the ERANGE-only improvement does not change anything on
Linux/c99 as strtoul(3) only set ERANGE there.

Signed-off-by: Ruediger Meier <ruediger.meier@ga-group.nl>
2016-02-21 18:08:26 +01:00
Ruediger Meier b0b24b11f8 lib: rename strmode() and setmode()
On BSD they are part of the standard C library.

Signed-off-by: Ruediger Meier <ruediger.meier@ga-group.nl>
2016-02-18 12:53:48 +01:00
Ruediger Meier 7231fb2a5b misc: fix some printf format strings
Fix the warnings below for OSX clang and add a few more
casts for timeval:

lib/at.c:131:27: warning: format specifies type 'intmax_t' (aka 'long') but the argument has type 'off_t' (aka 'long long') [-Wformat]

                        printf("%16jd bytes ", st.st_size);

                                ~~~~~          ^~~~~~~~~~

lib/strutils.c:522:52: warning: format specifies type 'intmax_t' (aka 'long') but the argument has type 'uint64_t' (aka 'unsigned long long') [-Wformat]

                snprintf(buf, sizeof(buf), "%d%s%jd%s", dec, dp, frac, suffix);

                                                ~~~              ^~~~

lib/sysfs.c:468:42: warning: format specifies type 'uintmax_t' (aka 'unsigned long') but the argument has type 'uint64_t' (aka 'unsigned long long') [-Wformat]

        len = snprintf(buf, sizeof(buf), "%ju", num);

                                          ~~~   ^~~

libuuid/src/gen_uuid.c:316:34: warning: format specifies type 'unsigned long' but the argument has type '__darwin_suseconds_t' (aka 'int') [-Wformat]

                              clock_seq, last.tv_sec, last.tv_usec, adjustment);

                                                      ^~~~~~~~~~~~

Signed-off-by: Ruediger Meier <ruediger.meier@ga-group.nl>
2016-02-11 13:39:56 +01:00
Andreas Henriksson 74819b5f64 lib/strutil: fix parse_switch resource leak [coverity scan]
Signed-off-by: Andreas Henriksson <andreas@fatal.se>
2015-08-31 10:09:43 +02:00
Karel Zak 7015df4936 lib/strutils: make strmode() more generic
Signed-off-by: Karel Zak <kzak@redhat.com>
2015-06-30 12:41:13 +02:00
Sami Kerola 3e5a54554d rtcwake: improve read_clock_mode()
Make skipping two lines more robust, and add message about unexpected
adjfile contents when running with --verbose.

Reviewed-by: Karel Zak <kzak@redhat.com>
Signed-off-by: Sami Kerola <kerolasa@iki.fi>
2015-06-29 13:39:37 +02:00
Karel Zak 548b9714e8 lib/strv: add new functions (from systemd)
Signed-off-by: Karel Zak <kzak@redhat.com>
2015-05-15 14:57:04 +02:00
Karel Zak 40b175084f lib/strutils: fix string_add_to_idarray() int vs. size_t
The function uses "int" as argument, but for array size (and index) is better
to use unsigned type (size_t). If we mix "size_t" in util (e.g. fdisk)
and "int" in lib/strutils.c then result is unexpected behavior on
ppc64.

	# sfdisk --list -o DEVICE,START,SIZE /dev/sdb
	Disk /dev/sdb: 50 MiB, 52428800 bytes, 102400 sectors
	Units: sectors of 1 * 512 = 512 bytes
	Sector size (logical/physical): 512 bytes / 4096 bytes
	I/O size (minimum/optimal): 4096 bytes / 32768 bytes
	Disklabel type: gpt
	Disk identifier: 3B8559DB-33AF-43E9-BEFC-C331D829B539
	lt-sfdisk: libfdisk/src/label.c:178: fdisk_label_get_field: Assertion `id > 0' failed.

The patch cleanup all code to use size_t everywhere.

Signed-off-by: Karel Zak <kzak@redhat.com>
2015-04-22 11:08:05 +02:00
Karel Zak 30b294c491 lib/strutils: extend parse_switch() to accept more options
* allow to specify more 0|1 pairs
* allow to specify error message

Signed-off-by: Karel Zak <kzak@redhat.com>
2015-02-24 12:04:22 +01:00
Sami Kerola e5cf147655 lib/strutils: move parse_switch() from setterm(1) to library
To allow sharing the code with other utilities.

Signed-off-by: Sami Kerola <kerolasa@iki.fi>
Signed-off-by: Karel Zak <kzak@redhat.com>
2015-02-24 11:33:31 +01:00