1 /* $NetBSD: utils.c,v 1.1 2002/10/04 18:37:21 elric Exp $ */ 2 3 /*- 4 * Copyright (c) 2002 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by Roland C. Dowdeswell. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 3. All advertising materials mentioning features or use of this software 19 * must display the following acknowledgement: 20 * This product includes software developed by the NetBSD 21 * Foundation, Inc. and its contributors. 22 * 4. Neither the name of The NetBSD Foundation nor the names of its 23 * contributors may be used to endorse or promote products derived 24 * from this software without specific prior written permission. 25 * 26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 36 * POSSIBILITY OF SUCH DAMAGE. 37 */ 38 39 #include <sys/cdefs.h> 40 #ifndef lint 41 __RCSID("$NetBSD: utils.c,v 1.1 2002/10/04 18:37:21 elric Exp $"); 42 #endif 43 44 #include <malloc.h> 45 #include <string.h> 46 47 #include "utils.h" 48 49 /* just strsep(3), but skips empty fields. */ 50 51 static char * 52 strsep_getnext(char **stringp, const char *delim) 53 { 54 char *ret; 55 56 ret = strsep(stringp, delim); 57 while (ret && index(delim, *ret)) 58 ret = strsep(stringp, delim); 59 return ret; 60 } 61 62 /* 63 * this function returns a dynamically sized char ** of the words 64 * in the line. the caller is responsible for both free(3)ing 65 * each word and the superstructure by calling words_free(). 66 */ 67 char ** 68 words(const char *line, int *num) 69 { 70 int i = 0; 71 int nwords = 0; 72 char *cur; 73 char **ret; 74 char *tmp; 75 char *tmp1; 76 77 *num = 0; 78 tmp = (char *)line; 79 if (tmp[0] == '\0') 80 return NULL; 81 while (tmp[0]) { 82 if ((tmp[1] == ' ' || tmp[1] == '\t' || tmp[1] == '\0') && 83 (tmp[0] != ' ' && tmp[0] != '\t')) 84 nwords++; 85 tmp++; 86 } 87 ret = malloc((nwords+1) * sizeof(char *)); 88 tmp1 = tmp = strdup(line); 89 while ((cur = strsep_getnext(&tmp, " \t")) != NULL) 90 ret[i++] = strdup(cur); 91 ret[i] = NULL; 92 free(tmp1); 93 *num = nwords; 94 return ret; 95 } 96 97 void 98 words_free(char **w, int num) 99 { 100 int i; 101 102 for (i=0; i < num; i++) 103 free(w[i]); 104 } 105 106 /* 107 * this is a simple xor that has the same calling conventions as 108 * memcpy(3). 109 */ 110 111 void 112 memxor(void *res, const void *src, size_t len) 113 { 114 int i; 115 char *r; 116 const char *s; 117 118 r = res; 119 s = src; 120 for (i=0; i < len; i++) 121 r[i] ^= s[i]; 122 } 123