*** empty log message ***
[nvi.git] / common / util.c
blob7ebda976dccbd4f3c7bd8d63ea0b2c4bd9d5dbe6
1 /*-
2 * Copyright (c) 1991, 1993, 1994
3 * The Regents of the University of California. All rights reserved.
4 * Copyright (c) 1991, 1993, 1994, 1995, 1996
5 * Keith Bostic. All rights reserved.
7 * See the LICENSE file for redistribution information.
8 */
10 #include "config.h"
12 #ifndef lint
13 static const char sccsid[] = "$Id: util.c,v 10.14 2000/06/27 17:19:05 skimo Exp $ (Berkeley) $Date: 2000/06/27 17:19:05 $";
14 #endif /* not lint */
16 #include <sys/types.h>
17 #include <sys/queue.h>
19 #include <bitstring.h>
20 #include <errno.h>
21 #include <limits.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <unistd.h>
27 #include "common.h"
30 * binc --
31 * Increase the size of a buffer.
33 * PUBLIC: void *binc __P((SCR *, void *, size_t *, size_t));
35 void *
36 binc(sp, bp, bsizep, min)
37 SCR *sp; /* sp MAY BE NULL!!! */
38 void *bp;
39 size_t *bsizep, min;
41 size_t csize;
43 /* If already larger than the minimum, just return. */
44 if (min && *bsizep >= min)
45 return (bp);
47 csize = *bsizep + MAX(min, 256);
48 REALLOC(sp, bp, void *, csize);
50 if (bp == NULL) {
52 * Theoretically, realloc is supposed to leave any already
53 * held memory alone if it can't get more. Don't trust it.
55 *bsizep = 0;
56 return (NULL);
59 * Memory is guaranteed to be zero-filled, various parts of
60 * nvi depend on this.
62 memset((char *)bp + *bsizep, 0, csize - *bsizep);
63 *bsizep = csize;
64 return (bp);
68 * nonblank --
69 * Set the column number of the first non-blank character
70 * including or after the starting column. On error, set
71 * the column to 0, it's safest.
73 * PUBLIC: int nonblank __P((SCR *, db_recno_t, size_t *));
75 int
76 nonblank(sp, lno, cnop)
77 SCR *sp;
78 db_recno_t lno;
79 size_t *cnop;
81 CHAR_T *p;
82 size_t cnt, len, off;
83 int isempty;
85 /* Default. */
86 off = *cnop;
87 *cnop = 0;
89 /* Get the line, succeeding in an empty file. */
90 if (db_eget(sp, lno, &p, &len, &isempty))
91 return (!isempty);
93 /* Set the offset. */
94 if (len == 0 || off >= len)
95 return (0);
97 for (cnt = off, p = &p[off],
98 len -= off; len && isblank(*p); ++cnt, ++p, --len);
100 /* Set the return. */
101 *cnop = len ? cnt : cnt - 1;
102 return (0);
106 * tail --
107 * Return tail of a path.
109 * PUBLIC: char *tail __P((char *));
111 char *
112 tail(path)
113 char *path;
115 char *p;
117 if ((p = strrchr(path, '/')) == NULL)
118 return (path);
119 return (p + 1);
123 * v_strdup --
124 * Strdup for wide character strings with an associated length.
126 * PUBLIC: CHAR_T *v_strdup __P((SCR *, const CHAR_T *, size_t));
128 CHAR_T *
129 v_strdup(sp, str, len)
130 SCR *sp;
131 const CHAR_T *str;
132 size_t len;
134 CHAR_T *copy;
136 MALLOC(sp, copy, CHAR_T *, len + 1);
137 if (copy == NULL)
138 return (NULL);
139 memcpy(copy, str, len * sizeof(CHAR_T));
140 copy[len] = '\0';
141 return (copy);
145 * nget_uslong --
146 * Get an unsigned long, checking for overflow.
148 * PUBLIC: enum nresult nget_uslong __P((u_long *, const char *, char **, int));
150 enum nresult
151 nget_uslong(valp, p, endp, base)
152 u_long *valp;
153 const char *p;
154 char **endp;
155 int base;
157 errno = 0;
158 *valp = strtoul(p, endp, base);
159 if (errno == 0)
160 return (NUM_OK);
161 if (errno == ERANGE && *valp == ULONG_MAX)
162 return (NUM_OVER);
163 return (NUM_ERR);
167 * nget_slong --
168 * Convert a signed long, checking for overflow and underflow.
170 * PUBLIC: enum nresult nget_slong __P((long *, const char *, char **, int));
172 enum nresult
173 nget_slong(valp, p, endp, base)
174 long *valp;
175 const char *p;
176 char **endp;
177 int base;
179 errno = 0;
180 *valp = strtol(p, endp, base);
181 if (errno == 0)
182 return (NUM_OK);
183 if (errno == ERANGE) {
184 if (*valp == LONG_MAX)
185 return (NUM_OVER);
186 if (*valp == LONG_MIN)
187 return (NUM_UNDER);
189 return (NUM_ERR);