Main Page | Class Hierarchy | Data Structures | Directories | File List | Data Fields | Related Pages

util_arg.c

00001 /*-
00002  * See the file LICENSE for redistribution information.
00003  *
00004  * Copyright (c) 2001-2005
00005  *      Sleepycat Software.  All rights reserved.
00006  *
00007  * $Id: util_arg.c,v 12.1 2005/06/16 20:20:53 bostic Exp $
00008  */
00009 
00010 #include "db_config.h"
00011 
00012 #ifndef NO_SYSTEM_INCLUDES
00013 #include <sys/types.h>
00014 #endif
00015 
00016 #include "db_int.h"
00017 
00018 static char *__db_strsep __P((char **, const char *));
00019 
00020 /*
00021  * __db_util_arg --
00022  *      Convert a string into an argc/argv pair.
00023  *
00024  * PUBLIC: int __db_util_arg __P((char *, char *, int *, char ***));
00025  */
00026 int
00027 __db_util_arg(arg0, str, argcp, argvp)
00028         char *arg0, *str, ***argvp;
00029         int *argcp;
00030 {
00031         int n, ret;
00032         char **ap, **argv;
00033 
00034 #define MAXARGS 25
00035         if ((ret =
00036             __os_malloc(NULL, (MAXARGS + 1) * sizeof(char **), &argv)) != 0)
00037                 return (ret);
00038 
00039         ap = argv;
00040         *ap++ = arg0;
00041         for (n = 1; (*ap = __db_strsep(&str, " \t")) != NULL;)
00042                 if (**ap != '\0') {
00043                         ++ap;
00044                         if (++n == MAXARGS)
00045                                 break;
00046                 }
00047         *ap = NULL;
00048 
00049         *argcp = ap - argv;
00050         *argvp = argv;
00051 
00052         return (0);
00053 }
00054 
00055 /*-
00056  * Copyright (c) 1990, 1993
00057  *      The Regents of the University of California.  All rights reserved.
00058  *
00059  * Redistribution and use in source and binary forms, with or without
00060  * modification, are permitted provided that the following conditions
00061  * are met:
00062  * 1. Redistributions of source code must retain the above copyright
00063  *    notice, this list of conditions and the following disclaimer.
00064  * 2. Redistributions in binary form must reproduce the above copyright
00065  *    notice, this list of conditions and the following disclaimer in the
00066  *    documentation and/or other materials provided with the distribution.
00067  * 3. All advertising materials mentioning features or use of this software
00068  *    must display the following acknowledgement:
00069  *      This product includes software developed by the University of
00070  *      California, Berkeley and its contributors.
00071  * 4. Neither the name of the University nor the names of its contributors
00072  *    may be used to endorse or promote products derived from this software
00073  *    without specific prior written permission.
00074  *
00075  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
00076  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00077  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00078  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
00079  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00080  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
00081  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
00082  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00083  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
00084  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
00085  * SUCH DAMAGE.
00086  */
00087 /*
00088  * Get next token from string *stringp, where tokens are possibly-empty
00089  * strings separated by characters from delim.
00090  *
00091  * Writes NULs into the string at *stringp to end tokens.
00092  * delim need not remain constant from call to call.
00093  * On return, *stringp points past the last NUL written (if there might
00094  * be further tokens), or is NULL (if there are definitely no more tokens).
00095  *
00096  * If *stringp is NULL, strsep returns NULL.
00097  */
00098 static char *
00099 __db_strsep(stringp, delim)
00100         char **stringp;
00101         const char *delim;
00102 {
00103         const char *spanp;
00104         int c, sc;
00105         char *s, *tok;
00106 
00107         if ((s = *stringp) == NULL)
00108                 return (NULL);
00109         for (tok = s;;) {
00110                 c = *s++;
00111                 spanp = delim;
00112                 do {
00113                         if ((sc = *spanp++) == c) {
00114                                 if (c == 0)
00115                                         s = NULL;
00116                                 else
00117                                         s[-1] = 0;
00118                                 *stringp = s;
00119                                 return (tok);
00120                         }
00121                 } while (sc != 0);
00122         }
00123         /* NOTREACHED */
00124 }

Generated on Sun Dec 25 12:14:17 2005 for Berkeley DB 4.4.16 by  doxygen 1.4.2