util.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. /*
  2. * isync - mbsync wrapper: IMAP4 to maildir mailbox synchronizer
  3. * Copyright (C) 2000-2002 Michael R. Elkins <me@mutt.org>
  4. * Copyright (C) 2002-2004 Oswald Buddenhagen <ossi@users.sf.net>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include "isync.h"
  20. #include <stdio.h>
  21. #include <stdlib.h>
  22. #include <unistd.h>
  23. #include <string.h>
  24. #include <pwd.h>
  25. #include <ctype.h>
  26. void
  27. sys_error( const char *msg, ... )
  28. {
  29. va_list va;
  30. char buf[1024];
  31. va_start( va, msg );
  32. if ((unsigned)vsnprintf( buf, sizeof(buf), msg, va ) >= sizeof(buf))
  33. oob();
  34. va_end( va );
  35. perror( buf );
  36. }
  37. char *
  38. next_arg( char **s )
  39. {
  40. char *ret;
  41. if (!s || !*s)
  42. return 0;
  43. while (isspace( (unsigned char) **s ))
  44. (*s)++;
  45. if (!**s) {
  46. *s = 0;
  47. return 0;
  48. }
  49. if (**s == '"') {
  50. ++*s;
  51. ret = *s;
  52. *s = strchr( *s, '"' );
  53. } else {
  54. ret = *s;
  55. while (**s && !isspace( (unsigned char) **s ))
  56. (*s)++;
  57. }
  58. if (*s) {
  59. if (**s)
  60. *(*s)++ = 0;
  61. if (!**s)
  62. *s = 0;
  63. }
  64. return ret;
  65. }
  66. #ifndef HAVE_VASPRINTF
  67. static int
  68. vasprintf( char **strp, const char *fmt, va_list ap )
  69. {
  70. int len;
  71. char tmp[1024];
  72. if ((len = vsnprintf( tmp, sizeof(tmp), fmt, ap )) < 0 || !(*strp = malloc( len + 1 )))
  73. return -1;
  74. if (len >= (int)sizeof(tmp))
  75. vsprintf( *strp, fmt, ap );
  76. else
  77. memcpy( *strp, tmp, len + 1 );
  78. return len;
  79. }
  80. #endif
  81. void
  82. oob( void )
  83. {
  84. fputs( "Fatal: buffer too small. Please report a bug.\n", stderr );
  85. abort();
  86. }
  87. int
  88. nfsnprintf( char *buf, int blen, const char *fmt, ... )
  89. {
  90. int ret;
  91. va_list va;
  92. va_start( va, fmt );
  93. if (blen <= 0 || (unsigned)(ret = vsnprintf( buf, blen, fmt, va )) >= (unsigned)blen)
  94. oob();
  95. va_end( va );
  96. return ret;
  97. }
  98. static void ATTR_NORETURN
  99. oom( void )
  100. {
  101. fputs( "Fatal: Out of memory\n", stderr );
  102. abort();
  103. }
  104. void *
  105. nfmalloc( size_t sz )
  106. {
  107. void *ret;
  108. if (!(ret = malloc( sz )))
  109. oom();
  110. return ret;
  111. }
  112. void *
  113. nfrealloc( void *mem, size_t sz )
  114. {
  115. char *ret;
  116. if (!(ret = realloc( mem, sz )) && sz)
  117. oom();
  118. return ret;
  119. }
  120. char *
  121. nfstrdup( const char *str )
  122. {
  123. char *ret;
  124. if (!(ret = strdup( str )))
  125. oom();
  126. return ret;
  127. }
  128. int
  129. nfvasprintf( char **str, const char *fmt, va_list va )
  130. {
  131. int ret = vasprintf( str, fmt, va );
  132. if (ret < 0)
  133. oom();
  134. return ret;
  135. }
  136. int
  137. nfasprintf( char **str, const char *fmt, ... )
  138. {
  139. int ret;
  140. va_list va;
  141. va_start( va, fmt );
  142. ret = nfvasprintf( str, fmt, va );
  143. va_end( va );
  144. return ret;
  145. }