2 * Copyright (C) 1996-2000,2007 Michael R. Elkins <me@mutt.org>
3 * Copyright (C) 1999-2005,2007 Thomas Roessler <roessler@does-not-exist.org>
5 * This program is free software; you can redistribute it
6 * and/or modify it under the terms of the GNU General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later
11 * This program is distributed in the hope that it will be
12 * useful, but WITHOUT ANY WARRANTY; without even the implied
13 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14 * PURPOSE. See the GNU General Public License for more
17 * You should have received a copy of the GNU General Public
18 * License along with this program; if not, write to the Free
19 * Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20 * Boston, MA 02110-1301, USA.
23 /* mutt functions which are generally useful. */
31 # include <unistd.h> /* needed for SEEK_SET */
33 # include <sys/types.h>
34 # include <sys/stat.h>
40 # ifndef _POSIX_PATH_MAX
46 # define _(a) (gettext (a))
48 # define N_(a) gettext_noop (a)
60 # define HUGE_STRING 5120
61 # define LONG_STRING 1024
63 # define SHORT_STRING 128
66 * Create a format string to be used with scanf.
67 * To use it, write, for instance, MUTT_FORMAT(HUGE_STRING).
69 * See K&R 2nd ed, p. 231 for an explanation.
71 # define _MUTT_FORMAT_2(a,b) "%" a b
72 # define _MUTT_FORMAT_1(a, b) _MUTT_FORMAT_2(#a, b)
73 # define MUTT_FORMAT(a) _MUTT_FORMAT_1(a, "s")
74 # define MUTT_FORMAT2(a,b) _MUTT_FORMAT_1(a, b)
77 # define FREE(x) safe_free(x)
78 # define NONULL(x) x?x:""
79 # define ISSPACE(c) isspace((unsigned char)c)
80 # define strfcpy(A,B,C) strncpy(A,B,C), *(A+(C)-1)=0
84 # define MAX(a,b) ((a) < (b) ? (b) : (a))
85 # define MIN(a,b) ((a) < (b) ? (a) : (b))
87 /* For mutt_format_string() justifications */
88 /* Making left 0 and center -1 is of course completely nonsensical, but
89 * it retains compatibility for any patches that call mutt_format_string.
90 * Once patches are updated to use FMT_*, these can be made sane. */
95 #define FOREVER while (1)
97 /* this macro must check for *c == 0 since isspace(0) has unreliable behavior
99 # define SKIPWS(c) while (*(c) && isspace ((unsigned char) *(c))) c++;
102 * These functions aren't defined in lib.c, but
103 * they are used there.
105 * A non-mutt "implementation" (ahem) can be found in extlib.c.
110 extern void (*mutt_error) (const char *, ...);
114 # define MUTT_LIB_WHERE
115 # define MUTT_LIB_INITVAL(x) = x
117 # define MUTT_LIB_WHERE extern
118 # define MUTT_LIB_INITVAL(x)
121 void mutt_exit (int);
126 MUTT_LIB_WHERE FILE *debugfile MUTT_LIB_INITVAL(0);
127 MUTT_LIB_WHERE int debuglevel MUTT_LIB_INITVAL(0);
129 # define dprint(N,X) do { if(debuglevel>=N && debugfile) fprintf X; } while (0)
138 /* Exit values used in send_msg() */
142 /* The actual library functions. */
144 FILE *safe_fopen (const char *, const char *);
146 char *mutt_concatn_path (char *, size_t, const char *, size_t, const char *, size_t);
147 char *mutt_concat_path (char *, const char *, const char *, size_t);
148 char *mutt_read_line (char *, size_t *, FILE *, int *);
149 char *mutt_skip_whitespace (char *);
150 char *mutt_strlower (char *);
151 char *mutt_substrcpy (char *, const char *, const char *, size_t);
152 char *mutt_substrdup (const char *, const char *);
153 char *safe_strcat (char *, size_t, const char *);
154 char *safe_strncat (char *, size_t, const char *, size_t);
155 char *safe_strdup (const char *);
157 const char *mutt_stristr (const char *, const char *);
158 const char *mutt_basename (const char *);
160 int compare_stat (struct stat *, struct stat *);
161 int mutt_copy_stream (FILE *, FILE *);
162 int mutt_copy_bytes (FILE *, FILE *, size_t);
163 int mutt_rx_sanitize_string (char *, size_t, const char *);
164 int mutt_strcasecmp (const char *, const char *);
165 int mutt_strcmp (const char *, const char *);
166 int mutt_strncasecmp (const char *, const char *, size_t);
167 int mutt_strncmp (const char *, const char *, size_t);
168 int mutt_strcoll (const char *, const char *);
169 int safe_open (const char *, int);
170 int safe_rename (const char *, const char *);
171 int safe_symlink (const char *, const char *);
172 int safe_fclose (FILE **);
173 int safe_fsync_close (FILE **);
174 int mutt_rmtree (const char *);
176 size_t mutt_quote_filename (char *, size_t, const char *);
177 size_t mutt_strlen (const char *);
179 void *safe_calloc (size_t, size_t);
180 void *safe_malloc (size_t);
181 void mutt_nocurses_error (const char *, ...);
182 void mutt_remove_trailing_ws (char *);
183 void mutt_sanitize_filename (char *, short);
184 void mutt_str_replace (char **p, const char *s);
185 void mutt_str_adjust (char **p);
186 void mutt_unlink (const char *);
187 void safe_free (void *);
188 void safe_realloc (void *, size_t);
190 const char *mutt_strsysexit(int e);