#include <fcntl.h>
#include <string.h>
#include <errno.h>
+#include <err.h>
#include "prepstdreply.h"
#include "controls.h"
}
+static bool
+open_textdirs(struct mlmmj_list *list)
+{
+ if (list->textfd == -1)
+ list->textfd = openat(list->fd, "text", O_DIRECTORY|O_CLOEXEC);
+ if (list->defaulttextfd == -1)
+ list->defaulttextfd = openat(list->fd, list->defaulttext, O_DIRECTORY|O_CLOEXEC);
+ if (list->entextfd == -1)
+ list->entextfd = openat(list->fd, list->entext, O_DIRECTORY|O_CLOEXEC);
+ if (list->textfd == -1 && list->defaulttextfd == -1 && list->entextfd == -1)
+ return (false);
+ return (true);
+}
+
text *open_text_file(struct mlmmj_list *list, const char *filename)
{
- char *tmp;
text *txt;
txt = mymalloc(sizeof(text));
txt->cond = NULL;
txt->skip = NULL;
- myasprintf(&tmp, "text/%s", filename);
- txt->src->fd = openat(list->fd, tmp, O_RDONLY);
- myfree(tmp);
+ if (!open_textdirs(list)) {
+ warnx("Unable to open any text directory");
+ return (NULL);
+ }
+
+ txt->src->fd = openat(list->textfd, filename, O_RDONLY);
if (txt->src->fd >= 0) return txt;
- tmp = concatstr(2, DEFAULTTEXTDIR "/default/", filename);
- txt->src->fd = open(tmp, O_RDONLY);
- myfree(tmp);
+ txt->src->fd = openat(list->defaulttextfd, filename, O_RDONLY);
if (txt->src->fd >= 0) return txt;
- tmp = concatstr(2, DEFAULTTEXTDIR "/en/", filename);
- txt->src->fd = open(tmp, O_RDONLY);
- myfree(tmp);
+ txt->src->fd = openat(list->entextfd, filename, O_RDONLY);
if (txt->src->fd >= 0) return txt;
return NULL;