Expand PMF_FN_* macros.
[netbsd-mini2440.git] / dist / nvi / common / util.c
blob2c71554aa2540dd55dbe7cc08f1e75b15906686c
1 /* $NetBSD: util.c,v 1.2 2008/08/27 10:18:41 christos Exp $ */
3 /*-
4 * Copyright (c) 1991, 1993, 1994
5 * The Regents of the University of California. All rights reserved.
6 * Copyright (c) 1991, 1993, 1994, 1995, 1996
7 * Keith Bostic. All rights reserved.
9 * See the LICENSE file for redistribution information.
12 #include "config.h"
14 #ifndef lint
15 static const char sccsid[] = "Id: util.c,v 10.22 2001/06/25 15:19:12 skimo Exp (Berkeley) Date: 2001/06/25 15:19:12";
16 #endif /* not lint */
18 #include <sys/types.h>
19 #include <sys/queue.h>
21 #include <bitstring.h>
22 #include <errno.h>
23 #include <limits.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <unistd.h>
29 #include "common.h"
32 * binc --
33 * Increase the size of a buffer.
35 * PUBLIC: void *binc __P((SCR *, void *, size_t *, size_t));
37 void *
38 binc(SCR *sp, void *bp, size_t *bsizep, size_t min)
39 /* sp MAY BE NULL!!! */
43 size_t csize;
45 /* If already larger than the minimum, just return. */
46 if (min && *bsizep >= min)
47 return (bp);
49 csize = *bsizep + MAX(min, 256);
50 REALLOC(sp, bp, void *, csize);
52 if (bp == NULL) {
54 * Theoretically, realloc is supposed to leave any already
55 * held memory alone if it can't get more. Don't trust it.
57 *bsizep = 0;
58 return (NULL);
61 * Memory is guaranteed to be zero-filled, various parts of
62 * nvi depend on this.
64 memset((char *)bp + *bsizep, 0, csize - *bsizep);
65 *bsizep = csize;
66 return (bp);
70 * nonblank --
71 * Set the column number of the first non-blank character
72 * including or after the starting column. On error, set
73 * the column to 0, it's safest.
75 * PUBLIC: int nonblank __P((SCR *, db_recno_t, size_t *));
77 int
78 nonblank(SCR *sp, db_recno_t lno, size_t *cnop)
80 CHAR_T *p;
81 size_t cnt, len, off;
82 int isempty;
84 /* Default. */
85 off = *cnop;
86 *cnop = 0;
88 /* Get the line, succeeding in an empty file. */
89 if (db_eget(sp, lno, &p, &len, &isempty))
90 return (!isempty);
92 /* Set the offset. */
93 if (len == 0 || off >= len)
94 return (0);
96 for (cnt = off, p = &p[off],
97 len -= off; len && isblank(*p); ++cnt, ++p, --len);
99 /* Set the return. */
100 *cnop = len ? cnt : cnt - 1;
101 return (0);
105 * tail --
106 * Return tail of a path.
108 * PUBLIC: char *tail __P((char *));
110 const char *
111 tail(const char *path)
113 const char *p;
115 if ((p = strrchr(path, '/')) == NULL)
116 return (path);
117 return (p + 1);
121 * v_strdup --
122 * Strdup for wide character strings with an associated length.
124 * PUBLIC: char *v_strdup __P((SCR *, const char *, size_t));
126 char *
127 v_strdup(SCR *sp, const char *str, size_t len)
129 char *copy;
131 MALLOC(sp, copy, char *, (len + 1));
132 if (copy == NULL)
133 return (NULL);
134 memcpy(copy, str, len);
135 copy[len] = '\0';
136 return (copy);
140 * v_strdup --
141 * Strdup for wide character strings with an associated length.
143 * PUBLIC: CHAR_T *v_wstrdup __P((SCR *, const CHAR_T *, size_t));
145 CHAR_T *
146 v_wstrdup(SCR *sp, const CHAR_T *str, size_t len)
148 CHAR_T *copy;
150 MALLOC(sp, copy, CHAR_T *, (len + 1) * sizeof(CHAR_T));
151 if (copy == NULL)
152 return (NULL);
153 MEMCPYW(copy, str, len);
154 copy[len] = '\0';
155 return (copy);
159 * nget_uslong --
160 * Get an unsigned long, checking for overflow.
162 * PUBLIC: enum nresult nget_uslong __P((SCR *, u_long *, const CHAR_T *, CHAR_T **, int));
164 enum nresult
165 nget_uslong(SCR *sp, u_long *valp, const CHAR_T *p, CHAR_T **endp, int base)
167 errno = 0;
168 *valp = STRTOUL(p, (RCHAR_T **)endp, base);
169 if (errno == 0)
170 return (NUM_OK);
171 if (errno == ERANGE && *valp == ULONG_MAX)
172 return (NUM_OVER);
173 return (NUM_ERR);
177 * nget_slong --
178 * Convert a signed long, checking for overflow and underflow.
180 * PUBLIC: enum nresult nget_slong __P((SCR *, long *, const CHAR_T *, CHAR_T **, int));
182 enum nresult
183 nget_slong(SCR *sp, long int *valp, const CHAR_T *p, CHAR_T **endp, int base)
185 errno = 0;
186 *valp = STRTOL(p, (RCHAR_T **)endp, base);
187 if (errno == 0)
188 return (NUM_OK);
189 if (errno == ERANGE) {
190 if (*valp == LONG_MAX)
191 return (NUM_OVER);
192 if (*valp == LONG_MIN)
193 return (NUM_UNDER);
195 return (NUM_ERR);