1*0Sstevel@tonic-gate /*-
2*0Sstevel@tonic-gate * See the file LICENSE for redistribution information.
3*0Sstevel@tonic-gate *
4*0Sstevel@tonic-gate * Copyright (c) 1997, 1998
5*0Sstevel@tonic-gate * Sleepycat Software. All rights reserved.
6*0Sstevel@tonic-gate */
7*0Sstevel@tonic-gate
8*0Sstevel@tonic-gate #include "config.h"
9*0Sstevel@tonic-gate
10*0Sstevel@tonic-gate #ifndef lint
11*0Sstevel@tonic-gate static const char sccsid[] = "@(#)log_archive.c 10.44 (Sleepycat) 10/9/98";
12*0Sstevel@tonic-gate #endif /* not lint */
13*0Sstevel@tonic-gate
14*0Sstevel@tonic-gate #ifndef NO_SYSTEM_INCLUDES
15*0Sstevel@tonic-gate #include <sys/types.h>
16*0Sstevel@tonic-gate
17*0Sstevel@tonic-gate #include <errno.h>
18*0Sstevel@tonic-gate #include <stdlib.h>
19*0Sstevel@tonic-gate #include <string.h>
20*0Sstevel@tonic-gate #include <unistd.h>
21*0Sstevel@tonic-gate #endif
22*0Sstevel@tonic-gate
23*0Sstevel@tonic-gate #include "db_int.h"
24*0Sstevel@tonic-gate #include "db_dispatch.h"
25*0Sstevel@tonic-gate #include "shqueue.h"
26*0Sstevel@tonic-gate #include "log.h"
27*0Sstevel@tonic-gate #include "common_ext.h"
28*0Sstevel@tonic-gate #include "clib_ext.h" /* XXX: needed for getcwd. */
29*0Sstevel@tonic-gate
30*0Sstevel@tonic-gate static int __absname __P((char *, char *, char **));
31*0Sstevel@tonic-gate static int __build_data __P((DB_LOG *, char *, char ***, void *(*)(size_t)));
32*0Sstevel@tonic-gate static int __cmpfunc __P((const void *, const void *));
33*0Sstevel@tonic-gate static int __usermem __P((char ***, void *(*)(size_t)));
34*0Sstevel@tonic-gate
35*0Sstevel@tonic-gate /*
36*0Sstevel@tonic-gate * log_archive --
37*0Sstevel@tonic-gate * Supporting function for db_archive(1).
38*0Sstevel@tonic-gate */
39*0Sstevel@tonic-gate int
log_archive(dblp,listp,flags,db_malloc)40*0Sstevel@tonic-gate log_archive(dblp, listp, flags, db_malloc)
41*0Sstevel@tonic-gate DB_LOG *dblp;
42*0Sstevel@tonic-gate char ***listp;
43*0Sstevel@tonic-gate u_int32_t flags;
44*0Sstevel@tonic-gate void *(*db_malloc) __P((size_t));
45*0Sstevel@tonic-gate {
46*0Sstevel@tonic-gate DBT rec;
47*0Sstevel@tonic-gate DB_LSN stable_lsn;
48*0Sstevel@tonic-gate u_int32_t fnum;
49*0Sstevel@tonic-gate int array_size, n, ret;
50*0Sstevel@tonic-gate char **array, **arrayp, *name, *p, *pref, buf[MAXPATHLEN];
51*0Sstevel@tonic-gate
52*0Sstevel@tonic-gate name = NULL;
53*0Sstevel@tonic-gate COMPQUIET(fnum, 0);
54*0Sstevel@tonic-gate
55*0Sstevel@tonic-gate LOG_PANIC_CHECK(dblp);
56*0Sstevel@tonic-gate
57*0Sstevel@tonic-gate #define OKFLAGS (DB_ARCH_ABS | DB_ARCH_DATA | DB_ARCH_LOG)
58*0Sstevel@tonic-gate if (flags != 0) {
59*0Sstevel@tonic-gate if ((ret =
60*0Sstevel@tonic-gate __db_fchk(dblp->dbenv, "log_archive", flags, OKFLAGS)) != 0)
61*0Sstevel@tonic-gate return (ret);
62*0Sstevel@tonic-gate if ((ret =
63*0Sstevel@tonic-gate __db_fcchk(dblp->dbenv,
64*0Sstevel@tonic-gate "log_archive", flags, DB_ARCH_DATA, DB_ARCH_LOG)) != 0)
65*0Sstevel@tonic-gate return (ret);
66*0Sstevel@tonic-gate }
67*0Sstevel@tonic-gate
68*0Sstevel@tonic-gate /*
69*0Sstevel@tonic-gate * Get the absolute pathname of the current directory. It would
70*0Sstevel@tonic-gate * be nice to get the shortest pathname of the database directory,
71*0Sstevel@tonic-gate * but that's just not possible.
72*0Sstevel@tonic-gate */
73*0Sstevel@tonic-gate if (LF_ISSET(DB_ARCH_ABS)) {
74*0Sstevel@tonic-gate errno = 0;
75*0Sstevel@tonic-gate if ((pref = getcwd(buf, sizeof(buf))) == NULL)
76*0Sstevel@tonic-gate return (errno == 0 ? ENOMEM : errno);
77*0Sstevel@tonic-gate } else
78*0Sstevel@tonic-gate pref = NULL;
79*0Sstevel@tonic-gate
80*0Sstevel@tonic-gate switch (LF_ISSET(~DB_ARCH_ABS)) {
81*0Sstevel@tonic-gate case DB_ARCH_DATA:
82*0Sstevel@tonic-gate return (__build_data(dblp, pref, listp, db_malloc));
83*0Sstevel@tonic-gate case DB_ARCH_LOG:
84*0Sstevel@tonic-gate memset(&rec, 0, sizeof(rec));
85*0Sstevel@tonic-gate if (F_ISSET(dblp, DB_AM_THREAD))
86*0Sstevel@tonic-gate F_SET(&rec, DB_DBT_MALLOC);
87*0Sstevel@tonic-gate if ((ret = log_get(dblp, &stable_lsn, &rec, DB_LAST)) != 0)
88*0Sstevel@tonic-gate return (ret);
89*0Sstevel@tonic-gate if (F_ISSET(dblp, DB_AM_THREAD))
90*0Sstevel@tonic-gate __os_free(rec.data, rec.size);
91*0Sstevel@tonic-gate fnum = stable_lsn.file;
92*0Sstevel@tonic-gate break;
93*0Sstevel@tonic-gate case 0:
94*0Sstevel@tonic-gate if ((ret = __log_findckp(dblp, &stable_lsn)) != 0) {
95*0Sstevel@tonic-gate /*
96*0Sstevel@tonic-gate * A return of DB_NOTFOUND means that we didn't find
97*0Sstevel@tonic-gate * any records in the log (so we are not going to be
98*0Sstevel@tonic-gate * deleting any log files).
99*0Sstevel@tonic-gate */
100*0Sstevel@tonic-gate if (ret != DB_NOTFOUND)
101*0Sstevel@tonic-gate return (ret);
102*0Sstevel@tonic-gate *listp = NULL;
103*0Sstevel@tonic-gate return (0);
104*0Sstevel@tonic-gate }
105*0Sstevel@tonic-gate /* Remove any log files before the last stable LSN. */
106*0Sstevel@tonic-gate fnum = stable_lsn.file - 1;
107*0Sstevel@tonic-gate break;
108*0Sstevel@tonic-gate }
109*0Sstevel@tonic-gate
110*0Sstevel@tonic-gate #define LIST_INCREMENT 64
111*0Sstevel@tonic-gate /* Get some initial space. */
112*0Sstevel@tonic-gate array_size = 10;
113*0Sstevel@tonic-gate if ((ret = __os_malloc(sizeof(char *) * array_size, NULL, &array)) != 0)
114*0Sstevel@tonic-gate return (ret);
115*0Sstevel@tonic-gate array[0] = NULL;
116*0Sstevel@tonic-gate
117*0Sstevel@tonic-gate /* Build an array of the file names. */
118*0Sstevel@tonic-gate for (n = 0; fnum > 0; --fnum) {
119*0Sstevel@tonic-gate if ((ret = __log_name(dblp, fnum, &name, NULL, 0)) != 0)
120*0Sstevel@tonic-gate goto err;
121*0Sstevel@tonic-gate if (__os_exists(name, NULL) != 0) {
122*0Sstevel@tonic-gate __os_freestr(name);
123*0Sstevel@tonic-gate name = NULL;
124*0Sstevel@tonic-gate break;
125*0Sstevel@tonic-gate }
126*0Sstevel@tonic-gate
127*0Sstevel@tonic-gate if (n >= array_size - 1) {
128*0Sstevel@tonic-gate array_size += LIST_INCREMENT;
129*0Sstevel@tonic-gate if ((ret = __os_realloc(&array,
130*0Sstevel@tonic-gate sizeof(char *) * array_size)) != 0)
131*0Sstevel@tonic-gate goto err;
132*0Sstevel@tonic-gate }
133*0Sstevel@tonic-gate
134*0Sstevel@tonic-gate if (LF_ISSET(DB_ARCH_ABS)) {
135*0Sstevel@tonic-gate if ((ret = __absname(pref, name, &array[n])) != 0)
136*0Sstevel@tonic-gate goto err;
137*0Sstevel@tonic-gate __os_freestr(name);
138*0Sstevel@tonic-gate } else if ((p = __db_rpath(name)) != NULL) {
139*0Sstevel@tonic-gate if ((ret = __os_strdup(p + 1, &array[n])) != 0)
140*0Sstevel@tonic-gate goto err;
141*0Sstevel@tonic-gate __os_freestr(name);
142*0Sstevel@tonic-gate } else
143*0Sstevel@tonic-gate array[n] = name;
144*0Sstevel@tonic-gate
145*0Sstevel@tonic-gate name = NULL;
146*0Sstevel@tonic-gate array[++n] = NULL;
147*0Sstevel@tonic-gate }
148*0Sstevel@tonic-gate
149*0Sstevel@tonic-gate /* If there's nothing to return, we're done. */
150*0Sstevel@tonic-gate if (n == 0) {
151*0Sstevel@tonic-gate *listp = NULL;
152*0Sstevel@tonic-gate ret = 0;
153*0Sstevel@tonic-gate goto err;
154*0Sstevel@tonic-gate }
155*0Sstevel@tonic-gate
156*0Sstevel@tonic-gate /* Sort the list. */
157*0Sstevel@tonic-gate qsort(array, (size_t)n, sizeof(char *), __cmpfunc);
158*0Sstevel@tonic-gate
159*0Sstevel@tonic-gate /* Rework the memory. */
160*0Sstevel@tonic-gate if ((ret = __usermem(&array, db_malloc)) != 0)
161*0Sstevel@tonic-gate goto err;
162*0Sstevel@tonic-gate
163*0Sstevel@tonic-gate *listp = array;
164*0Sstevel@tonic-gate return (0);
165*0Sstevel@tonic-gate
166*0Sstevel@tonic-gate err: if (array != NULL) {
167*0Sstevel@tonic-gate for (arrayp = array; *arrayp != NULL; ++arrayp)
168*0Sstevel@tonic-gate __os_freestr(*arrayp);
169*0Sstevel@tonic-gate __os_free(array, sizeof(char *) * array_size);
170*0Sstevel@tonic-gate }
171*0Sstevel@tonic-gate if (name != NULL)
172*0Sstevel@tonic-gate __os_freestr(name);
173*0Sstevel@tonic-gate return (ret);
174*0Sstevel@tonic-gate }
175*0Sstevel@tonic-gate
176*0Sstevel@tonic-gate /*
177*0Sstevel@tonic-gate * __build_data --
178*0Sstevel@tonic-gate * Build a list of datafiles for return.
179*0Sstevel@tonic-gate */
180*0Sstevel@tonic-gate static int
__build_data(dblp,pref,listp,db_malloc)181*0Sstevel@tonic-gate __build_data(dblp, pref, listp, db_malloc)
182*0Sstevel@tonic-gate DB_LOG *dblp;
183*0Sstevel@tonic-gate char *pref, ***listp;
184*0Sstevel@tonic-gate void *(*db_malloc) __P((size_t));
185*0Sstevel@tonic-gate {
186*0Sstevel@tonic-gate DBT rec;
187*0Sstevel@tonic-gate DB_LSN lsn;
188*0Sstevel@tonic-gate __log_register_args *argp;
189*0Sstevel@tonic-gate u_int32_t rectype;
190*0Sstevel@tonic-gate int array_size, last, n, nxt, ret;
191*0Sstevel@tonic-gate char **array, **arrayp, *p, *real_name;
192*0Sstevel@tonic-gate
193*0Sstevel@tonic-gate /* Get some initial space. */
194*0Sstevel@tonic-gate array_size = 10;
195*0Sstevel@tonic-gate if ((ret = __os_malloc(sizeof(char *) * array_size, NULL, &array)) != 0)
196*0Sstevel@tonic-gate return (ret);
197*0Sstevel@tonic-gate array[0] = NULL;
198*0Sstevel@tonic-gate
199*0Sstevel@tonic-gate memset(&rec, 0, sizeof(rec));
200*0Sstevel@tonic-gate if (F_ISSET(dblp, DB_AM_THREAD))
201*0Sstevel@tonic-gate F_SET(&rec, DB_DBT_MALLOC);
202*0Sstevel@tonic-gate for (n = 0, ret = log_get(dblp, &lsn, &rec, DB_FIRST);
203*0Sstevel@tonic-gate ret == 0; ret = log_get(dblp, &lsn, &rec, DB_NEXT)) {
204*0Sstevel@tonic-gate if (rec.size < sizeof(rectype)) {
205*0Sstevel@tonic-gate ret = EINVAL;
206*0Sstevel@tonic-gate __db_err(dblp->dbenv, "log_archive: bad log record");
207*0Sstevel@tonic-gate goto lg_free;
208*0Sstevel@tonic-gate }
209*0Sstevel@tonic-gate
210*0Sstevel@tonic-gate memcpy(&rectype, rec.data, sizeof(rectype));
211*0Sstevel@tonic-gate if (rectype != DB_log_register) {
212*0Sstevel@tonic-gate if (F_ISSET(dblp, DB_AM_THREAD)) {
213*0Sstevel@tonic-gate __os_free(rec.data, rec.size);
214*0Sstevel@tonic-gate rec.data = NULL;
215*0Sstevel@tonic-gate }
216*0Sstevel@tonic-gate continue;
217*0Sstevel@tonic-gate }
218*0Sstevel@tonic-gate if ((ret = __log_register_read(rec.data, &argp)) != 0) {
219*0Sstevel@tonic-gate ret = EINVAL;
220*0Sstevel@tonic-gate __db_err(dblp->dbenv,
221*0Sstevel@tonic-gate "log_archive: unable to read log record");
222*0Sstevel@tonic-gate goto lg_free;
223*0Sstevel@tonic-gate }
224*0Sstevel@tonic-gate
225*0Sstevel@tonic-gate if (n >= array_size - 1) {
226*0Sstevel@tonic-gate array_size += LIST_INCREMENT;
227*0Sstevel@tonic-gate if ((ret = __os_realloc(&array,
228*0Sstevel@tonic-gate sizeof(char *) * array_size)) != 0)
229*0Sstevel@tonic-gate goto lg_free;
230*0Sstevel@tonic-gate }
231*0Sstevel@tonic-gate
232*0Sstevel@tonic-gate if ((ret = __os_strdup(argp->name.data, &array[n])) != 0) {
233*0Sstevel@tonic-gate lg_free: if (F_ISSET(&rec, DB_DBT_MALLOC) && rec.data != NULL)
234*0Sstevel@tonic-gate __os_free(rec.data, rec.size);
235*0Sstevel@tonic-gate goto err1;
236*0Sstevel@tonic-gate }
237*0Sstevel@tonic-gate
238*0Sstevel@tonic-gate array[++n] = NULL;
239*0Sstevel@tonic-gate __os_free(argp, 0);
240*0Sstevel@tonic-gate
241*0Sstevel@tonic-gate if (F_ISSET(dblp, DB_AM_THREAD)) {
242*0Sstevel@tonic-gate __os_free(rec.data, rec.size);
243*0Sstevel@tonic-gate rec.data = NULL;
244*0Sstevel@tonic-gate }
245*0Sstevel@tonic-gate }
246*0Sstevel@tonic-gate
247*0Sstevel@tonic-gate /* If there's nothing to return, we're done. */
248*0Sstevel@tonic-gate if (n == 0) {
249*0Sstevel@tonic-gate ret = 0;
250*0Sstevel@tonic-gate *listp = NULL;
251*0Sstevel@tonic-gate goto err1;
252*0Sstevel@tonic-gate }
253*0Sstevel@tonic-gate
254*0Sstevel@tonic-gate /* Sort the list. */
255*0Sstevel@tonic-gate qsort(array, (size_t)n, sizeof(char *), __cmpfunc);
256*0Sstevel@tonic-gate
257*0Sstevel@tonic-gate /*
258*0Sstevel@tonic-gate * Build the real pathnames, discarding nonexistent files and
259*0Sstevel@tonic-gate * duplicates.
260*0Sstevel@tonic-gate */
261*0Sstevel@tonic-gate for (last = nxt = 0; nxt < n;) {
262*0Sstevel@tonic-gate /*
263*0Sstevel@tonic-gate * Discard duplicates. Last is the next slot we're going
264*0Sstevel@tonic-gate * to return to the user, nxt is the next slot that we're
265*0Sstevel@tonic-gate * going to consider.
266*0Sstevel@tonic-gate */
267*0Sstevel@tonic-gate if (last != nxt) {
268*0Sstevel@tonic-gate array[last] = array[nxt];
269*0Sstevel@tonic-gate array[nxt] = NULL;
270*0Sstevel@tonic-gate }
271*0Sstevel@tonic-gate for (++nxt; nxt < n &&
272*0Sstevel@tonic-gate strcmp(array[last], array[nxt]) == 0; ++nxt) {
273*0Sstevel@tonic-gate __os_freestr(array[nxt]);
274*0Sstevel@tonic-gate array[nxt] = NULL;
275*0Sstevel@tonic-gate }
276*0Sstevel@tonic-gate
277*0Sstevel@tonic-gate /* Get the real name. */
278*0Sstevel@tonic-gate if ((ret = __db_appname(dblp->dbenv,
279*0Sstevel@tonic-gate DB_APP_DATA, NULL, array[last], 0, NULL, &real_name)) != 0)
280*0Sstevel@tonic-gate goto err2;
281*0Sstevel@tonic-gate
282*0Sstevel@tonic-gate /* If the file doesn't exist, ignore it. */
283*0Sstevel@tonic-gate if (__os_exists(real_name, NULL) != 0) {
284*0Sstevel@tonic-gate __os_freestr(real_name);
285*0Sstevel@tonic-gate __os_freestr(array[last]);
286*0Sstevel@tonic-gate array[last] = NULL;
287*0Sstevel@tonic-gate continue;
288*0Sstevel@tonic-gate }
289*0Sstevel@tonic-gate
290*0Sstevel@tonic-gate /* Rework the name as requested by the user. */
291*0Sstevel@tonic-gate __os_freestr(array[last]);
292*0Sstevel@tonic-gate array[last] = NULL;
293*0Sstevel@tonic-gate if (pref != NULL) {
294*0Sstevel@tonic-gate ret = __absname(pref, real_name, &array[last]);
295*0Sstevel@tonic-gate __os_freestr(real_name);
296*0Sstevel@tonic-gate if (ret != 0)
297*0Sstevel@tonic-gate goto err2;
298*0Sstevel@tonic-gate } else if ((p = __db_rpath(real_name)) != NULL) {
299*0Sstevel@tonic-gate ret = __os_strdup(p + 1, &array[last]);
300*0Sstevel@tonic-gate __os_freestr(real_name);
301*0Sstevel@tonic-gate if (ret != 0)
302*0Sstevel@tonic-gate goto err2;
303*0Sstevel@tonic-gate } else
304*0Sstevel@tonic-gate array[last] = real_name;
305*0Sstevel@tonic-gate ++last;
306*0Sstevel@tonic-gate }
307*0Sstevel@tonic-gate
308*0Sstevel@tonic-gate /* NULL-terminate the list. */
309*0Sstevel@tonic-gate array[last] = NULL;
310*0Sstevel@tonic-gate
311*0Sstevel@tonic-gate /* Rework the memory. */
312*0Sstevel@tonic-gate if ((ret = __usermem(&array, db_malloc)) != 0)
313*0Sstevel@tonic-gate goto err1;
314*0Sstevel@tonic-gate
315*0Sstevel@tonic-gate *listp = array;
316*0Sstevel@tonic-gate return (0);
317*0Sstevel@tonic-gate
318*0Sstevel@tonic-gate err2: /*
319*0Sstevel@tonic-gate * XXX
320*0Sstevel@tonic-gate * We've possibly inserted NULLs into the array list, so clean up a
321*0Sstevel@tonic-gate * bit so that the other error processing works.
322*0Sstevel@tonic-gate */
323*0Sstevel@tonic-gate if (array != NULL)
324*0Sstevel@tonic-gate for (; nxt < n; ++nxt)
325*0Sstevel@tonic-gate __os_freestr(array[nxt]);
326*0Sstevel@tonic-gate /* FALLTHROUGH */
327*0Sstevel@tonic-gate
328*0Sstevel@tonic-gate err1: if (array != NULL) {
329*0Sstevel@tonic-gate for (arrayp = array; *arrayp != NULL; ++arrayp)
330*0Sstevel@tonic-gate __os_freestr(*arrayp);
331*0Sstevel@tonic-gate __os_free(array, array_size * sizeof(char *));
332*0Sstevel@tonic-gate }
333*0Sstevel@tonic-gate return (ret);
334*0Sstevel@tonic-gate }
335*0Sstevel@tonic-gate
336*0Sstevel@tonic-gate /*
337*0Sstevel@tonic-gate * __absname --
338*0Sstevel@tonic-gate * Return an absolute path name for the file.
339*0Sstevel@tonic-gate */
340*0Sstevel@tonic-gate static int
__absname(pref,name,newnamep)341*0Sstevel@tonic-gate __absname(pref, name, newnamep)
342*0Sstevel@tonic-gate char *pref, *name, **newnamep;
343*0Sstevel@tonic-gate {
344*0Sstevel@tonic-gate size_t l_pref, l_name;
345*0Sstevel@tonic-gate int isabspath, ret;
346*0Sstevel@tonic-gate char *newname;
347*0Sstevel@tonic-gate
348*0Sstevel@tonic-gate l_name = strlen(name);
349*0Sstevel@tonic-gate isabspath = __os_abspath(name);
350*0Sstevel@tonic-gate l_pref = isabspath ? 0 : strlen(pref);
351*0Sstevel@tonic-gate
352*0Sstevel@tonic-gate /* Malloc space for concatenating the two. */
353*0Sstevel@tonic-gate if ((ret = __os_malloc(l_pref + l_name + 2, NULL, &newname)) != 0)
354*0Sstevel@tonic-gate return (ret);
355*0Sstevel@tonic-gate *newnamep = newname;
356*0Sstevel@tonic-gate
357*0Sstevel@tonic-gate /* Build the name. If `name' is an absolute path, ignore any prefix. */
358*0Sstevel@tonic-gate if (!isabspath) {
359*0Sstevel@tonic-gate memcpy(newname, pref, l_pref);
360*0Sstevel@tonic-gate if (strchr(PATH_SEPARATOR, newname[l_pref - 1]) == NULL)
361*0Sstevel@tonic-gate newname[l_pref++] = PATH_SEPARATOR[0];
362*0Sstevel@tonic-gate }
363*0Sstevel@tonic-gate memcpy(newname + l_pref, name, l_name + 1);
364*0Sstevel@tonic-gate
365*0Sstevel@tonic-gate return (0);
366*0Sstevel@tonic-gate }
367*0Sstevel@tonic-gate
368*0Sstevel@tonic-gate /*
369*0Sstevel@tonic-gate * __usermem --
370*0Sstevel@tonic-gate * Create a single chunk of memory that holds the returned information.
371*0Sstevel@tonic-gate * If the user has their own malloc routine, use it.
372*0Sstevel@tonic-gate */
373*0Sstevel@tonic-gate static int
__usermem(listp,db_malloc)374*0Sstevel@tonic-gate __usermem(listp, db_malloc)
375*0Sstevel@tonic-gate char ***listp;
376*0Sstevel@tonic-gate void *(*db_malloc) __P((size_t));
377*0Sstevel@tonic-gate {
378*0Sstevel@tonic-gate size_t len;
379*0Sstevel@tonic-gate int ret;
380*0Sstevel@tonic-gate char **array, **arrayp, **orig, *strp;
381*0Sstevel@tonic-gate
382*0Sstevel@tonic-gate /* Find out how much space we need. */
383*0Sstevel@tonic-gate for (len = 0, orig = *listp; *orig != NULL; ++orig)
384*0Sstevel@tonic-gate len += sizeof(char *) + strlen(*orig) + 1;
385*0Sstevel@tonic-gate len += sizeof(char *);
386*0Sstevel@tonic-gate
387*0Sstevel@tonic-gate /* Allocate it and set up the pointers. */
388*0Sstevel@tonic-gate if ((ret = __os_malloc(len, db_malloc, &array)) != 0)
389*0Sstevel@tonic-gate return (ret);
390*0Sstevel@tonic-gate
391*0Sstevel@tonic-gate strp = (char *)(array + (orig - *listp) + 1);
392*0Sstevel@tonic-gate
393*0Sstevel@tonic-gate /* Copy the original information into the new memory. */
394*0Sstevel@tonic-gate for (orig = *listp, arrayp = array; *orig != NULL; ++orig, ++arrayp) {
395*0Sstevel@tonic-gate len = strlen(*orig);
396*0Sstevel@tonic-gate memcpy(strp, *orig, len + 1);
397*0Sstevel@tonic-gate *arrayp = strp;
398*0Sstevel@tonic-gate strp += len + 1;
399*0Sstevel@tonic-gate
400*0Sstevel@tonic-gate __os_freestr(*orig);
401*0Sstevel@tonic-gate }
402*0Sstevel@tonic-gate
403*0Sstevel@tonic-gate /* NULL-terminate the list. */
404*0Sstevel@tonic-gate *arrayp = NULL;
405*0Sstevel@tonic-gate
406*0Sstevel@tonic-gate __os_free(*listp, 0);
407*0Sstevel@tonic-gate *listp = array;
408*0Sstevel@tonic-gate
409*0Sstevel@tonic-gate return (0);
410*0Sstevel@tonic-gate }
411*0Sstevel@tonic-gate
412*0Sstevel@tonic-gate static int
__cmpfunc(p1,p2)413*0Sstevel@tonic-gate __cmpfunc(p1, p2)
414*0Sstevel@tonic-gate const void *p1, *p2;
415*0Sstevel@tonic-gate {
416*0Sstevel@tonic-gate return (strcmp(*((char * const *)p1), *((char * const *)p2)));
417*0Sstevel@tonic-gate }
418