2 * Copyright (c) 2022 Omar Polo <op@omarpolo.com>
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
27 #include "dictionary.h"
33 pfile(struct dictionary *dict, struct db_entry **entries, size_t *len,
34 size_t *cap, const char *path)
48 t = recallocarray(*entries, *cap, newcap, sizeof(**entries));
50 err(1, "recallocarray");
55 if ((fd = open(path, O_RDONLY)) == -1) {
56 warnx("can't open %s", path);
60 if ((end = lseek(fd, 0, SEEK_END)) == -1)
61 err(1, "lseek %s", path);
64 m = mmap(NULL, end, PROT_READ, MAP_PRIVATE, fd, 0);
66 err(1, "can't mmap %s", path);
68 (*entries)[(*len)++].name = xstrdup(path);
70 if ((toks = tokenize(m)) == NULL)
72 if (!dictionary_add_words(dict, toks, *len - 1))
73 err(1, "dictionary_add_words");
81 idx_files(struct dictionary *dict, struct db_entry **entries, size_t *len,
82 int argc, char **argv)
85 size_t linesize = 0, cap = *len;
91 if (!pfile(dict, entries, len, &cap, *argv))
98 while ((linelen = getline(&line, &linesize, stdin)) != -1) {
99 if (linelen > 1 && line[linelen-1] == '\n')
100 line[linelen-1] = '\0';
102 if (!pfile(dict, entries, len, &cap, line))