| OLD | NEW |
| 1 /////////////////////////////////////////////////////////////////////////////// | 1 /////////////////////////////////////////////////////////////////////////////// |
| 2 // | 2 // |
| 3 /// \file util.c | 3 /// \file util.c |
| 4 /// \brief Miscellaneous utility functions | 4 /// \brief Miscellaneous utility functions |
| 5 // | 5 // |
| 6 // Author: Lasse Collin | 6 // Author: Lasse Collin |
| 7 // | 7 // |
| 8 // This file has been put into the public domain. | 8 // This file has been put into the public domain. |
| 9 // You can do whatever you want with this file. | 9 // You can do whatever you want with this file. |
| 10 // | 10 // |
| 11 /////////////////////////////////////////////////////////////////////////////// | 11 /////////////////////////////////////////////////////////////////////////////// |
| 12 | 12 |
| 13 #include "private.h" | 13 #include "private.h" |
| 14 #include <stdarg.h> | 14 #include <stdarg.h> |
| 15 | 15 |
| 16 | 16 |
| 17 /// Buffers for uint64_to_str() and uint64_to_nicestr() | 17 /// Buffers for uint64_to_str() and uint64_to_nicestr() |
| 18 static char bufs[4][128]; | 18 static char bufs[4][128]; |
| 19 | 19 |
| 20 /// Thousand separator support in uint64_to_str() and uint64_to_nicestr() | 20 /// Thousand separator support in uint64_to_str() and uint64_to_nicestr() |
| 21 static enum { UNKNOWN, WORKS, BROKEN } thousand = UNKNOWN; | 21 static enum { UNKNOWN, WORKS, BROKEN } thousand = UNKNOWN; |
| 22 | 22 |
| 23 | 23 |
| 24 extern void * | 24 extern void * |
| 25 xrealloc(void *ptr, size_t size) | 25 xrealloc(void *ptr, size_t size) |
| 26 { | 26 { |
| 27 assert(size > 0); | 27 assert(size > 0); |
| 28 | 28 |
| 29 // Save ptr so that we can free it if realloc fails. |
| 30 // The point is that message_fatal ends up calling stdio functions |
| 31 // which in some libc implementations might allocate memory from |
| 32 // the heap. Freeing ptr improves the chances that there's free |
| 33 // memory for stdio functions if they need it. |
| 34 void *p = ptr; |
| 29 ptr = realloc(ptr, size); | 35 ptr = realloc(ptr, size); |
| 30 » if (ptr == NULL) | 36 |
| 31 » » message_fatal("%s", strerror(errno)); | 37 » if (ptr == NULL) { |
| 38 » » const int saved_errno = errno; |
| 39 » » free(p); |
| 40 » » message_fatal("%s", strerror(saved_errno)); |
| 41 » } |
| 32 | 42 |
| 33 return ptr; | 43 return ptr; |
| 34 } | 44 } |
| 35 | 45 |
| 36 | 46 |
| 37 extern char * | 47 extern char * |
| 38 xstrdup(const char *src) | 48 xstrdup(const char *src) |
| 39 { | 49 { |
| 40 assert(src != NULL); | 50 assert(src != NULL); |
| 41 const size_t size = strlen(src) + 1; | 51 const size_t size = strlen(src) + 1; |
| (...skipping 227 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 269 is_tty_stdout(void) | 279 is_tty_stdout(void) |
| 270 { | 280 { |
| 271 const bool ret = isatty(STDOUT_FILENO); | 281 const bool ret = isatty(STDOUT_FILENO); |
| 272 | 282 |
| 273 if (ret) | 283 if (ret) |
| 274 message_error(_("Compressed data cannot be written to " | 284 message_error(_("Compressed data cannot be written to " |
| 275 "a terminal")); | 285 "a terminal")); |
| 276 | 286 |
| 277 return ret; | 287 return ret; |
| 278 } | 288 } |
| OLD | NEW |