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 ///////////////////////////////////////////////////////////////////////////////
23 /// \brief Safe malloc() that never returns NULL
25 /// \note xmalloc(), xrealloc(), and xstrdup() must not be used when
26 /// there are files open for writing, that should be cleaned up
28 #define xmalloc(size) xrealloc(NULL, size)
31 /// \brief Safe realloc() that never returns NULL
32 extern void *xrealloc(void *ptr, size_t size);
35 /// \brief Safe strdup() that never returns NULL
36 extern char *xstrdup(const char *src);
39 /// \brief Fancy version of strtoull()
41 /// \param name Name of the option to show in case of an error
42 /// \param value String containing the number to be parsed; may
43 /// contain suffixes "k", "M", "G", "Ki", "Mi", or "Gi"
44 /// \param min Minimum valid value
45 /// \param max Maximum valid value
47 /// \return Parsed value that is in the range [min, max]. Does not return
48 /// if an error occurs.
50 extern uint64_t str_to_uint64(const char *name, const char *value,
51 uint64_t min, uint64_t max);
54 /// \brief Check if filename is empty and print an error message
55 extern bool is_empty_filename(const char *filename);
58 /// \brief Test if stdin is a terminal
60 /// If stdin is a terminal, an error message is printed and exit status set
62 extern bool is_tty_stdin(void);
65 /// \brief Test if stdout is a terminal
67 /// If stdout is a terminal, an error message is printed and exit status set
69 extern bool is_tty_stdout(void);