diff options
author | Omar Polo <op@omarpolo.com> | 2021-01-18 18:48:37 +0000 |
---|---|---|
committer | Omar Polo <op@omarpolo.com> | 2021-01-18 18:48:37 +0000 |
commit | a010b0ddc5aa8cf49207b0ab87d36be6ebb320cf (patch) | |
tree | 90e5f634d00fd548c378c9852a0891765bc7e297 /mime.c | |
parent | 982069a120a3d36483427ce00b9cf90a8bf4daab (diff) |
move mimes into the struct config
Diffstat (limited to 'mime.c')
-rw-r--r-- | mime.c | 50 |
1 files changed, 19 insertions, 31 deletions
@@ -20,31 +20,18 @@ #include "gmid.h" -struct etm { /* extension to mime */ - const char *mime; - const char *ext; -}; - -struct mimes { - char *def; - struct etm *t; - size_t len; - size_t cap; -}; - -struct mimes mimes; - void init_mime(void) { - mimes.len = 0; - mimes.cap = 2; + conf.mimes.len = 0; + conf.mimes.cap = 2; - if ((mimes.t = calloc(mimes.cap, sizeof(struct etm))) == NULL) + conf.mimes.t = calloc(conf.mimes.cap, sizeof(struct etm)); + if (conf.mimes.t == NULL) fatal("calloc: %s", strerror(errno)); - mimes.def = strdup("application/octet-stream"); - if (mimes.def == NULL) + conf.mimes.def = strdup("application/octet-stream"); + if (conf.mimes.def == NULL) fatal("strdup: %s", strerror(errno)); } @@ -52,8 +39,8 @@ init_mime(void) void set_default_mime(const char *m) { - free(mimes.def); - if ((mimes.def = strdup(m)) == NULL) + free(conf.mimes.def); + if ((conf.mimes.def = strdup(m)) == NULL) fatal("strdup: %s", strerror(errno)); } @@ -61,16 +48,17 @@ set_default_mime(const char *m) void add_mime(const char *mime, const char *ext) { - if (mimes.len == mimes.cap) { - mimes.cap *= 1.5; - mimes.t = realloc(mimes.t, mimes.cap * sizeof(struct etm)); - if (mimes.t == NULL) + if (conf.mimes.len == conf.mimes.cap) { + conf.mimes.cap *= 1.5; + conf.mimes.t = realloc(conf.mimes.t, + conf.mimes.cap * sizeof(struct etm)); + if (conf.mimes.t == NULL) fatal("realloc: %s", strerror(errno)); } - mimes.t[mimes.len].mime = mime; - mimes.t[mimes.len].ext = ext; - mimes.len++; + conf.mimes.t[conf.mimes.len].mime = mime; + conf.mimes.t[conf.mimes.len].ext = ext; + conf.mimes.len++; } /* load a default set of common mime-extension associations */ @@ -120,11 +108,11 @@ mime(const char *path) struct etm *t; if ((ext = path_ext(path)) == NULL) - return mimes.def; + return conf.mimes.def; - for (t = mimes.t; t->mime != NULL; ++t) + for (t = conf.mimes.t; t->mime != NULL; ++t) if (!strcmp(ext, t->ext)) return t->mime; - return mimes.def; + return conf.mimes.def; } |