util.c revision dbea04f585a30d001b574317c068cd03a4fa332b
/*
Authors:
Simo Sorce <ssorce@redhat.com>
Copyright (C) 2009 Red Hat
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <ctype.h>
#include <netdb.h>
#include "talloc.h"
#include "util/sss_utf8.h"
#include "dhash.h"
/* split a string into an allocated array of strings.
* the separator is a string, and is case-sensitive.
* optionally single values can be trimmed of of spaces and tabs */
{
const char *t, *p, *n;
char **list, **r;
t = str;
l = 0;
/* trim leading whitespace */
if (trim)
while (isspace(*t)) t++;
/* find substrings separated by the separator */
len = p - t;
n = p + 1; /* save next string starting point */
if (trim) {
/* strip whitespace after the separator
* so it's not in the next token */
while (isspace(*t)) {
t++;
len--;
if (len == 0) break;
}
p--;
/* strip whitespace before the separator
* so it's not in the current token */
len--;
p--;
}
}
/* Add the token to the array, +2 b/c of the trailing NULL */
if (!r) {
return ENOMEM;
} else {
list = r;
}
if (len == 0) {
} else {
}
if (!list[l]) {
return ENOMEM;
}
l++;
t = n; /* move to next string */
}
/* Handle the last remaining token */
if (t) {
if (!r) {
return ENOMEM;
} else {
list = r;
}
if (trim) {
/* trim leading whitespace */
while (isspace(*t)) {
t++;
len--;
if (len == 0) break;
}
/* trim trailing whitespace */
p = t + len - 1;
len--;
p--;
}
if (len == 0) {
} else {
}
} else {
}
if (!list[l]) {
return ENOMEM;
}
l++;
}
return EOK;
}
{
int i;
if (args) {
}
}
/* parse a string into arguments.
* arguments are separated by a space
* '\' is an escape character and can be used only to escape
* itself or the white space.
*/
char **parse_args(const char *str)
{
const char *p;
char **ret, **r;
char *tmp;
int num;
int i;
bool e, w;
num = 0;
i = 0;
e = false;
w = false;
p = str;
while (*p) {
if (*p == '\\') {
w = false;
if (e) {
/* if we were already escaping, add a '\' literal */
tmp[i] = '\\';
i++;
e = false;
} else {
/* otherwise just start escaping */
e = true;
}
} else if (isspace(*p)) {
if (e) {
/* Add escaped whitespace literally */
tmp[i] = *p;
i++;
e = false;
} else if (w == false) {
/* If previous character was non-whitespace, arg break */
tmp[i] = '\0';
i++;
w = true;
}
/* previous char was whitespace as well, skip it */
} else {
w = false;
if (e) {
/* Prepend escaped chars with a literal \ */
tmp[i] = '\\';
i++;
e = false;
}
/* Copy character from the source string */
tmp[i] = *p;
i++;
}
p++;
/* check if this was the last char */
if (*p == '\0') {
if (e) {
tmp[i] = '\\';
i++;
e = false;
}
tmp[i] = '\0';
i++;
}
/* check next char and skip multiple spaces */
continue;
}
if (!r) goto fail;
ret = r;
num++;
i = 0;
}
return ret;
fail:
return NULL;
}
{
int i = 0;
int j = 0;
char **dup_list;
if (!str_list) {
return NULL;
}
/* Find the size of the list */
while (str_list[i]) i++;
if (!dup_list) {
return NULL;
}
/* Copy the elements */
for (j = 0; j < i; j++) {
if (!dup_list[j]) {
return NULL;
}
}
/* NULL-terminate the list */
return dup_list;
}
/* Take two string lists (terminated on a NULL char*)
* and return up to three arrays of strings based on
* shared ownership.
*
* Pass NULL to any return type you don't care about
*/
char **_list1,
char **_list2,
char ***_list1_only,
char ***_list2_only,
char ***_both_lists)
{
int error;
int i;
int i2 = 0;
int i12 = 0;
char **list1_only = NULL;
char **list2_only = NULL;
char **both_lists = NULL;
unsigned long count;
if (!tmp_ctx) {
return ENOMEM;
}
if (!_list1) {
if (!list1) {
return ENOMEM;
}
}
else {
}
if (!_list2) {
if (!list2) {
return ENOMEM;
}
}
else {
}
if (error != HASH_SUCCESS) {
return EIO;
}
/* Add all entries from list 1 into a hash table */
i = 0;
while (list1[i]) {
if (error != HASH_SUCCESS) {
goto done;
}
i++;
}
/* Iterate through list 2 and remove matching items */
i = 0;
while (list2[i]) {
if (error == HASH_SUCCESS) {
if (_both_lists) {
/* String was present in both lists */
i12++;
if (!both_lists) {
goto done;
}
goto done;
}
}
}
else if (error == HASH_ERROR_KEY_NOT_FOUND) {
if (_list2_only) {
/* String was present only in list2 */
i2++;
char *, i2+1);
if (!list2_only) {
goto done;
}
goto done;
}
}
}
else {
/* An error occurred */
goto done;
}
i++;
}
/* Get the leftover entries in the hash table */
if (_list1_only) {
if (error != HASH_SUCCESS) {
goto done;
}
if (!list1_only) {
goto done;
}
for (i = 0; i < count; i++) {
if (!list1_only[i]) {
goto done;
}
}
}
if (_list2_only) {
if (list2_only) {
}
else {
if (!(*_list2_only)) {
goto done;
}
*_list2_only[0] = NULL;
}
}
if (_both_lists) {
if (both_lists) {
}
else {
if (!(*_both_lists)) {
goto done;
}
*_both_lists[0] = NULL;
}
}
done:
return ret;
}
{
}
{
}
unsigned long count,
hash_table_t **tbl,
unsigned int directory_bits,
unsigned int segment_bits,
unsigned long min_load_factor,
unsigned long max_load_factor,
void *delete_private_data)
{
int hret;
if (!internal_ctx) {
return ENOMEM;
}
switch (hret) {
case HASH_SUCCESS:
/* Steal the table pointer onto the mem_ctx,
* then make the internal_ctx a child of
* table.
*
* This way, we can clean up the values when
* we talloc_free() the table
*/
return EOK;
case HASH_ERROR_NO_MEMORY:
break;
default:
}
DEBUG(0, ("Could not create hash table: [%d][%s]\n",
return ret;
}
hash_table_t **tbl)
{
}
const char *input,
char **sanitized)
{
char *output;
size_t i = 0;
size_t j = 0;
/* Assume the worst-case. We'll resize it later, once */
if (!output) {
return ENOMEM;
}
while (input[i]) {
switch(input[i]) {
case '*':
output[j++] = '\\';
output[j++] = '2';
output[j++] = 'a';
break;
case '(':
output[j++] = '\\';
output[j++] = '2';
output[j++] = '8';
break;
case ')':
output[j++] = '\\';
output[j++] = '2';
output[j++] = '9';
break;
case '\\':
output[j++] = '\\';
output[j++] = '5';
output[j++] = 'c';
break;
default:
}
i++;
}
output[j] = '\0';
if (!*sanitized) {
return ENOMEM;
}
return EOK;
}
char *
{
}