]> diplodocus.org Git - nmh/blob - h/utils.h
Fix invalid pointer arithmetic.
[nmh] / h / utils.h
1 /* utils.h -- utility prototypes
2 */
3
4 /* PLURALS gives a pointer to the string "s" when n isn't 1, and to the
5 * empty string "" when it is. Suitable for obtaining the plural `s'
6 * used for English nouns. It treats -1 as plural, as does GNU gettext.
7 * Having output vary for plurals is annoying for those writing parsers;
8 * better to phrase the output such that no test is needed, e.g.
9 * "messages found: 42". */
10 extern const char plurals[];
11 #define PLURALS(n) (plurals + ((n) == 1))
12
13 /* Call malloc(3), exiting on NULL return. */
14 void *mh_xmalloc(size_t size) MALLOC ALLOC_SIZE(1);
15
16 /* Call realloc(3), exiting on NULL return. */
17 void *mh_xrealloc(void *ptr, size_t size) ALLOC_SIZE(2);
18
19 /* Call calloc(3), exiting on NULL return. */
20 void *mh_xcalloc(size_t nelem, size_t elsize) MALLOC ALLOC_SIZE(1, 2);
21
22 /* Duplicate a NUL-terminated string, exit on failure. */
23 char *mh_xstrdup(const char *src) MALLOC;
24
25 /* Set p to point to newly allocated, uninitialised, memory. */
26 #define NEW(p) ((p) = mh_xmalloc(sizeof *(p)))
27
28 /* Set p to point to newly allocated, zeroed, memory. */
29 #define NEW0(p) ((p) = mh_xcalloc(1, sizeof *(p)))
30
31 /* Zero the bytes to which p points. */
32 #define ZERO(p) memset((p), 0, sizeof *(p))
33
34 char *pwd(void);
35 char *add(const char *, char *) MALLOC;
36 char *addlist(char *, const char *) MALLOC;
37 int folder_exists(const char *);
38 void create_folder(char *, int, void (*)(int));
39 int num_digits(int) PURE;
40
41 /*
42 * A vector of char array, used to hold a list of string message numbers
43 * or command arguments.
44 */
45
46 struct msgs_array {
47 int max, size;
48 char **msgs;
49 };
50
51 /*
52 * Same as msgs_array, but for a vector of ints
53 */
54
55 struct msgnum_array {
56 int max, size;
57 int *msgnums;
58 };
59
60 /*
61 * Add a argument to the given msgs_array or msgnum_array structure; extend
62 * the array size if necessary
63 */
64
65 void app_msgarg(struct msgs_array *, char *);
66 void app_msgnum(struct msgnum_array *, int);
67
68 char *find_str (const char [], size_t, const char *) PURE;
69 char *rfind_str (const char [], size_t, const char *) PURE;
70 char *nmh_strcasestr (const char *, const char *) PURE;
71
72 void trunccpy(char *dst, const char *src, size_t size);
73 /* A convenience for the common case of dst being an array. */
74 #define TRUNCCPY(dst, src) trunccpy(dst, src, sizeof (dst))
75
76 bool has_prefix(const char *s, const char *prefix) PURE;
77 bool has_suffix(const char *s, const char *suffix) PURE;
78 bool has_suffix_c(const char *s, int c) PURE;
79 void trim_suffix_c(char *s, int c);
80 void to_lower(char *s);
81 void to_upper(char *s);
82
83 bool contains8bit(const char *start, const char *end);
84
85 /*
86 * See if file has any 8-bit bytes.
87 * Arguments include:
88 *
89 * fd - file descriptor
90 * eightbit - address of result, will be set to 1 if the file contains
91 * any 8-bit bytes, 0 otherwise.
92 *
93 * Returns OK on success, NOTOK on read failure.
94 *
95 */
96 int scan_input (int fd, int *eightbit);
97
98 /*
99 * Returns string representation of int, in static memory.
100 */
101 char *m_str(int value);
102
103 /*
104 * Returns string representation of an int, in static memory. If width
105 * == 0, does not limit the width. If width > 0 and value will not fit
106 * in field of that size, including any negative sign but excluding
107 * terminating null, then returns "?".
108 */
109 char *m_strn(int value, unsigned int width);
110
111 /*
112 * program initialization
113 *
114 * argv0 - argv[0], presumably the program name
115 * read_context - 0: don't read context
116 * - 1: read context, check nmh version, and issue warning message
117 * if non-existent or old
118 * - 2: read context, don't check nmh version
119 */
120 int nmh_init(const char *argv0, int read_context);
121
122 /*
123 * Compares prior version of nmh with current version. Returns 1
124 * if they compare the be the same, 0 if not.
125 *
126 * older - 0 for difference comparison, 1 for only if older
127 */
128 int nmh_version_changed (int older);