1 /* $OpenBSD: path.c,v 1.7 2017/06/22 20:44:36 benno Exp $ */ 2 3 /* 4 * Copyright (c) 2013 Kurt Miller <kurt@intricatesoftware.com> 5 * 6 * Permission to use, copy, modify, and distribute this software for any 7 * purpose with or without fee is hereby granted, provided that the above 8 * copyright notice and this permission notice appear in all copies. 9 * 10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 17 */ 18 19 #include <sys/types.h> 20 #include "path.h" 21 #include "util.h" 22 23 char ** 24 _dl_split_path(const char *searchpath) 25 { 26 int pos = 0; 27 int count = 1; 28 const char *pp, *p_begin; 29 char **retval; 30 31 if (searchpath == NULL) 32 return (NULL); 33 34 /* Count ':' or ';' in searchpath */ 35 pp = searchpath; 36 while (*pp) { 37 if (*pp == ':' || *pp == ';') 38 count++; 39 pp++; 40 } 41 42 /* one more for NULL entry */ 43 count++; 44 45 retval = _dl_reallocarray(NULL, count, sizeof(*retval)); 46 if (retval == NULL) 47 return (NULL); 48 49 pp = searchpath; 50 while (pp) { 51 p_begin = pp; 52 while (*pp != '\0' && *pp != ':' && *pp != ';') 53 pp++; 54 55 if (p_begin != pp) { 56 retval[pos] = _dl_malloc(pp - p_begin + 1); 57 if (retval[pos] == NULL) 58 goto badret; 59 60 _dl_bcopy(p_begin, retval[pos], pp - p_begin); 61 retval[pos++][pp - p_begin] = '\0'; 62 } 63 64 if (*pp) 65 pp++; 66 else 67 pp = NULL; 68 } 69 70 retval[pos] = NULL; 71 return (retval); 72 73 badret: 74 _dl_free_path(retval); 75 return (NULL); 76 } 77 78 void 79 _dl_free_path(char **path) 80 { 81 char **p = path; 82 83 if (path == NULL) 84 return; 85 86 while (*p != NULL) 87 _dl_free(*p++); 88 89 _dl_free(path); 90 } 91