1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
|
/*
* Copyright 2019 Tresys Technology, LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <stdarg.h>
#include <stdlib.h>
#include <string.h>
#include "string_list.h"
#include "xalloc.h"
int str_in_sl(const char *str, const struct string_list *sl)
{
if (!sl) {
return 0;
}
while (sl) {
if (0 == strcmp(sl->string, str)) {
return 1;
}
sl = sl->next;
}
return 0;
}
struct string_list *copy_string_list(const struct string_list *sl)
{
if (!sl) {
return NULL;
}
struct string_list *ret = xmalloc(sizeof(struct string_list));
struct string_list *cur = ret;
while (sl) {
cur->string = xstrdup(sl->string);
cur->has_incorrect_space = sl->has_incorrect_space;
cur->arg_start = sl->arg_start;
if (sl->next) {
cur->next = xmalloc(sizeof(struct string_list));
} else {
cur->next = NULL;
}
sl = sl->next;
cur = cur->next;
}
return ret;
}
struct string_list *sl_from_str(const char *string)
{
struct string_list *ret = xmalloc(sizeof(struct string_list));
ret->string = xstrdup(string);
ret->next = NULL;
ret->has_incorrect_space = 0;
ret->arg_start = 0;
return ret;
}
struct string_list *sl_from_strn(const char *string, size_t len)
{
struct string_list *ret = xmalloc(sizeof(struct string_list));
ret->string = xstrndup(string, len);
ret->next = NULL;
ret->has_incorrect_space = 0;
ret->arg_start = 0;
return ret;
}
struct string_list *sl_from_str_consume(char *string)
{
struct string_list *ret = xmalloc(sizeof(struct string_list));
ret->string = string;
ret->next = NULL;
ret->has_incorrect_space = 0;
ret->arg_start = 0;
return ret;
}
struct string_list *sl_from_strs(int count, ...)
{
struct string_list *ret = NULL;
va_list args;
va_start(args, count);
for (int i = 0; i < count; ++i) {
ret = concat_string_lists(ret, sl_from_str(va_arg(args, const char *)));
}
va_end(args);
return ret;
}
struct string_list *concat_string_lists(struct string_list *head, struct string_list *tail)
{
if (!head) {
return tail;
}
if (!tail) {
return head;
}
struct string_list *cur = head;
while (cur->next) {
cur = cur->next;
}
cur->next = tail;
return head;
}
enum selint_error append_to_sl(struct string_list *sl, const char *string)
{
if (!sl) {
return SELINT_BAD_ARG;
}
while (sl->next) {
sl = sl->next;
}
sl->next = sl_from_str(string);
return SELINT_SUCCESS;
}
void free_string_list(struct string_list *list)
{
if (list == NULL) {
return;
}
struct string_list *cur = list;
while (cur) {
struct string_list *to_free = cur;
cur = cur->next;
free(to_free->string);
free(to_free);
}
}
|