about summary refs log tree commit diff
path: root/util.c
blob: 8f345361be213a3f9841e08ac5f96b4831d785a2 (plain) (blame)
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
/*
 * Copyright 2018 Google 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
 *
 *     https://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.
 */

#define _POSIX_C_SOURCE 200809L

#include <fcntl.h>
#include <stdarg.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#include "util.h"

// character buffers

struct bbuf {
  char *data;
  size_t len;
  size_t size;
  size_t max_size;
};

static bool bbuf_reserve(bbuf *buf, size_t len) {
  if (buf->len + len <= buf->size)
    return true;

  size_t new_size = buf->size;
  while (buf->len + len > new_size && new_size < buf->max_size)
    new_size *= 2;
  if (buf->len + len > new_size)
    return false;

  char *new_data = must_realloc(buf->data, new_size);
  buf->data = new_data;
  buf->size = new_size;
  return true;
}

bbuf *bbuf_alloc(size_t initial_size, size_t max_size) {
  bbuf *buf = must_malloc(sizeof *buf);
  buf->data = must_malloc(initial_size);
  buf->len = 0;
  buf->size = initial_size;
  buf->max_size = max_size;
  return buf;
}

void bbuf_free(bbuf *buf) {
  free(buf->data);
  free(buf);
}

void bbuf_reset(bbuf *buf) {
  buf->len = 0;
}

size_t bbuf_len(bbuf *buf) {
  return buf->len;
}

void bbuf_put(bbuf *buf, const void *src, size_t len) {
  if (!bbuf_reserve(buf, len))
    return;
  memcpy(buf->data + buf->len, src, len);
  buf->len += len;
}

void bbuf_puts(bbuf *buf, const char *src) {
  bbuf_put(buf, src, strlen(src));
}

void bbuf_putc(bbuf *buf, int c) {
  if (!bbuf_reserve(buf, 1))
    return;
  buf->data[buf->len++] = c;
}

void bbuf_putf(bbuf *buf, const char *fmt, ...) {
  va_list ap;

  int len = 0;
  va_start(ap, fmt);
  len = vsnprintf(0, 0, fmt, ap);
  va_end(ap);

  if (len > 0) {
    if (!bbuf_reserve(buf, len + 1))
      return;
    va_start(ap, fmt);
    vsnprintf(buf->data + buf->len, len + 1, fmt, ap);
    va_end(ap);
    buf->len += len;
  }
}

char *bbuf_get(struct bbuf *buf, size_t *len) {
  *len = buf->len;
  return buf->data;
}

int bbuf_cmp(bbuf *buf, const char *other) {
  size_t other_len = strlen(other);
  if (buf->len < other_len)
    return -1;
  else if (buf->len == other_len)
    return memcmp(buf->data, other, buf->len);
  else
    return +1;
}

// string lists

struct slist *slist_split(const char *str, const char *delim) {
  struct slist *list = 0, **prev = &list, *next;

  while (*str) {
    size_t span = strcspn(str, delim);
    if (span == 0) {
      str++;
      continue;
    }

    next = must_malloc(sizeof *next + span + 1);
    memcpy(next->data, str, span);
    next->data[span] = '\0';
    *prev = next;
    prev = &next->next;

    str += span;
    while (*str && strchr(delim, *str))
      str++;
  }
  *prev = 0;

  return list;
}

struct slist **slist_append(struct slist **prev, const char *str) {
  size_t len = strlen(str);
  struct slist *new = *prev = must_malloc(sizeof *new + len + 1);
  new->next = 0;
  memcpy(new->data, str, len + 1);
  return &new->next;
}

struct slist *slist_prepend(struct slist *list, const char *str) {
  size_t len = strlen(str);
  struct slist *new = must_malloc(sizeof *new + len + 1);
  new->next = list;
  memcpy(new->data, str, len + 1);
  return new;
}

bool slist_contains(const struct slist *list, const char *key) {
  for (; list; list = list->next)
    if (strcmp(list->data, key) == 0)
      return true;
  return false;
}

bool slist_matches(const struct slist *list, const char *key) {
  size_t key_len = strlen(key);

  for (; list; list = list->next) {
    size_t match_len = strlen(list->data);
    if (match_len > 0 && list->data[match_len-1] == '*') {
      if (match_len-1 <= key_len && memcmp(list->data, key, match_len-1) == 0)
        return true;
    } else {
      if (match_len == key_len && memcmp(list->data, key, key_len) == 0)
        return true;
    }
  }
  return false;
}

// miscellaneous utilities

void *must_malloc(size_t size) {
  void *ptr = malloc(size);
  if (!ptr) {
    perror("malloc");
    abort();
  }
  return ptr;
}

void *must_realloc(void *ptr, size_t size) {
  void *new_ptr = realloc(ptr, size);
  if (!new_ptr) {
    perror("realloc");
    abort();
  }
  return new_ptr;
}

char *must_strdup(const char *src) {
  char *dst = strdup(src);
  if (!dst) {
    perror("strdup");
    abort();
  }
  return dst;
}

char *fgets_line(char *s, int size, FILE *stream) {
  s[size - 1] = '\0';
  char *got = fgets(s, size, stream);
  if (!got)
    return 0;
  if (s[size - 1] == '\0' || s[size - 1] == '\n')
    return got;

  int c;
  while ((c = fgetc(stream)) != EOF)
    if (c == '\n')
      return got;
  return 0;
}

int write_all(int fd, const void *buf_ptr, size_t len) {
  const char *buf = buf_ptr;

  while (len > 0) {
    ssize_t wrote = write(fd, buf, len);
    if (wrote <= 0)
      return -1;
    buf += wrote;
    len -= wrote;
  }

  return 0;
}

ssize_t
read_file_at(int dirfd, char *pathname, char *buf, size_t bufsiz) {
  int fd = openat(dirfd, pathname, O_RDONLY);
  if (fd < 0)
    return -1;

  ssize_t r = read(fd, buf, bufsiz - 1);
  close(fd);
  if (r < 0)
    return -1;

  if (buf[r-1] == '\n')
    r--;

  buf[r] = 0;
  return r;
}