1 ///////////////////////////////////////////////////////////////////////////////
4 /// \brief Miscellaneous utility functions
6 // Copyright (C) 2007 Lasse Collin
8 // This program is free software; you can redistribute it and/or
9 // modify it under the terms of the GNU Lesser General Public
10 // License as published by the Free Software Foundation; either
11 // version 2.1 of the License, or (at your option) any later version.
13 // This program is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 // Lesser General Public License for more details.
18 ///////////////////////////////////////////////////////////////////////////////
20 /// \brief Safe malloc() that never returns NULL
22 /// \note xmalloc(), xrealloc(), and xstrdup() must not be used when
23 /// there are files open for writing, that should be cleaned up
25 #define xmalloc(size) xrealloc(NULL, size)
28 /// \brief Safe realloc() that never returns NULL
29 extern void *xrealloc(void *ptr, size_t size);
32 /// \brief Safe strdup() that never returns NULL
33 extern char *xstrdup(const char *src);
36 /// \brief Fancy version of strtoull()
38 /// \param name Name of the option to show in case of an error
39 /// \param value String containing the number to be parsed; may
40 /// contain suffixes "k", "M", "G", "Ki", "Mi", or "Gi"
41 /// \param min Minimum valid value
42 /// \param max Maximum valid value
44 /// \return Parsed value that is in the range [min, max]. Does not return
45 /// if an error occurs.
47 extern uint64_t str_to_uint64(const char *name, const char *value,
48 uint64_t min, uint64_t max);
51 /// \brief Check if filename is empty and print an error message
52 extern bool is_empty_filename(const char *filename);
55 /// \brief Test if stdin is a terminal
57 /// If stdin is a terminal, an error message is printed and exit status set
59 extern bool is_tty_stdin(void);
62 /// \brief Test if stdout is a terminal
64 /// If stdout is a terminal, an error message is printed and exit status set
66 extern bool is_tty_stdout(void);