diff --git a/.gitignore b/.gitignore index 2c83f39..b702184 100644 --- a/.gitignore +++ b/.gitignore @@ -4,3 +4,6 @@ wmfs #* \#* Makefile +wmfs.1.gz +tags +*.patch diff --git a/Makefile.in b/Makefile.in index 6b2ec3b..020ae06 100644 --- a/Makefile.in +++ b/Makefile.in @@ -19,8 +19,8 @@ src/launcher.c \ src/layout.c \ src/menu.c \ src/mouse.c \ -src/parse/api.c \ -src/parse/parse.c \ +src/parse_api.c \ +src/parse.c \ src/screen.c \ src/status.c \ src/systray.c \ @@ -38,7 +38,7 @@ OBJS= ${SRCS:.c=.o} all: ${PROG} ${MAN}.gz -${PROG}: ${OBJS} src/structs.h src/wmfs.h src/parse/parse.h +${PROG}: ${OBJS} src/structs.h src/wmfs.h src/parse.h ${CC} -o $@ ${OBJS} ${LDFLAGS} ${MAN}.gz: ${MAN} diff --git a/src/barwin.c b/src/barwin.c index b2170fe..4fe0c5e 100644 --- a/src/barwin.c +++ b/src/barwin.c @@ -58,7 +58,7 @@ barwin_create(Window parent, BarWindow *bw; /* Allocate memory */ - bw = emalloc(1, sizeof(BarWindow)); + bw = zcalloc(sizeof(*bw)); /* Barwin attributes */ at.override_redirect = True; diff --git a/src/client.c b/src/client.c index 23d7fbd..9bc81d7 100644 --- a/src/client.c +++ b/src/client.c @@ -536,7 +536,7 @@ client_get_name(Client *c) XFetchName(dpy, c->win, &(c->title)); if(!c->title) - c->title = _strdup("WMFS"); + c->title = xstrdup("WMFS"); } frame_update(c); @@ -676,7 +676,7 @@ client_manage(Window w, XWindowAttributes *wa, Bool ar) screen_get_sel(); - c = emalloc(1, sizeof(Client)); + c = zmalloc(sizeof(Client)); c->win = w; c->screen = selscreen; c->flags = 0; diff --git a/src/config.c b/src/config.c index f256f7d..acf6e86 100644 --- a/src/config.c +++ b/src/config.c @@ -196,7 +196,7 @@ conf_bar_section(void) if ((conf.bars.nmouse = fetch_section_count(mouse)) > 0) { - conf.bars.mouse = emalloc(conf.bars.nmouse, sizeof(MouseBinding)); + conf.bars.mouse = xcalloc(conf.bars.nmouse, sizeof(MouseBinding)); mouse_section(conf.bars.mouse, mouse); } @@ -227,7 +227,7 @@ conf_bar_section(void) if ((conf.selbar.nmouse = fetch_section_count(mouse))) { - conf.selbar.mouse = emalloc(conf.selbar.nmouse, sizeof(MouseBinding)); + conf.selbar.mouse = xcalloc(conf.selbar.nmouse, sizeof(MouseBinding)); mouse_section(conf.selbar.mouse, mouse); } @@ -249,7 +249,7 @@ conf_root_section(void) if ((conf.root.nmouse = fetch_section_count(mouse)) > 0) { - conf.root.mouse = emalloc(conf.root.nmouse, sizeof(MouseBinding)); + conf.root.mouse = xcalloc(conf.root.nmouse, sizeof(MouseBinding)); mouse_section(conf.root.mouse, mouse); } @@ -289,7 +289,7 @@ conf_client_section(void) if((conf.client.nmouse = fetch_section_count(mouse)) > 0) { - conf.client.mouse = emalloc(conf.client.nmouse, sizeof(MouseBinding)); + conf.client.mouse = xcalloc(conf.client.nmouse, sizeof(MouseBinding)); mouse_section(conf.client.mouse, mouse); } @@ -317,7 +317,7 @@ conf_client_section(void) if((conf.titlebar.nmouse = fetch_section_count(mouse)) > 0) { - conf.titlebar.mouse = emalloc(conf.titlebar.nmouse, sizeof(MouseBinding)); + conf.titlebar.mouse = xcalloc(conf.titlebar.nmouse, sizeof(MouseBinding)); mouse_section(conf.titlebar.mouse, mouse); } @@ -328,7 +328,7 @@ conf_client_section(void) if((conf.titlebar.nbutton = fetch_section_count(button)) > 0) { - conf.titlebar.button = emalloc(conf.titlebar.nbutton, sizeof(Button)); + conf.titlebar.button = xcalloc(conf.titlebar.nbutton, sizeof(Button)); for(i = 0; i < conf.titlebar.nbutton; ++i) { flags = fetch_opt_first(button[i], "none", "flags").str; @@ -346,7 +346,7 @@ conf_client_section(void) if((conf.titlebar.button[i].nmouse = fetch_section_count(mouse)) > 0) { - conf.titlebar.button[i].mouse = emalloc(conf.titlebar.button[i].nmouse, sizeof(MouseBinding)); + conf.titlebar.button[i].mouse = xcalloc(conf.titlebar.button[i].nmouse, sizeof(MouseBinding)); mouse_section(conf.titlebar.button[i].mouse, mouse); } @@ -357,7 +357,7 @@ conf_client_section(void) if((conf.titlebar.button[i].nlines = fetch_section_count(line)) > 0) { - conf.titlebar.button[i].linecoord = emalloc(conf.titlebar.button[i].nlines, sizeof(XSegment)); + conf.titlebar.button[i].linecoord = xcalloc(conf.titlebar.button[i].nlines, sizeof(XSegment)); for(j = 0; j < conf.titlebar.button[i].nlines; ++j) { @@ -414,7 +414,7 @@ conf_layout_section(void) { warnx("configuration : Too many or no layouts (%d).", conf.nlayout); conf.nlayout = 1; - conf.layout[0].symbol = _strdup("TILE"); + conf.layout[0].symbol = xstrdup("TILE"); conf.layout[0].func = tile; } @@ -519,10 +519,10 @@ conf_tag_section(void) sc = screen_count(); /* Alloc all */ - conf.ntag = emalloc(sc, sizeof(int)); - tags = emalloc(sc, sizeof(Tag*)); - seltag = emalloc(sc, sizeof(int)); - prevseltag = emalloc(sc, sizeof(int)); + conf.ntag = xcalloc(sc, sizeof(*conf.ntag)); + tags = xcalloc(sc, sizeof(*tags)); + seltag = xcalloc(sc, sizeof(*seltag)); + prevseltag = xcalloc(sc, sizeof(*prevseltag)); for(i = 0; i < sc; ++i) seltag[i] = 1; @@ -532,7 +532,7 @@ conf_tag_section(void) n = fetch_section_count(tag); for(i = 0; i < sc; ++i) - tags[i] = emalloc(n + 2, sizeof(Tag)); + tags[i] = xcalloc(n + 2, sizeof(Tag)); for(i = 0; i < n; i++) { @@ -573,7 +573,7 @@ conf_tag_section(void) if ((count = fetch_opt_count(opt))) { tags[k][conf.ntag[k]].nclients = count; - tags[k][conf.ntag[k]].clients = emalloc(count, sizeof(char *)); + tags[k][conf.ntag[k]].clients = xcalloc(count, sizeof(char *)); for(m = 0; m < count; ++m) tags[k][conf.ntag[k]].clients[m] = opt[m].str; } @@ -585,7 +585,7 @@ conf_tag_section(void) if((tags[k][conf.ntag[k]].nmouse = fetch_section_count(mouse))) { - tags[k][conf.ntag[k]].mouse = emalloc(tags[k][conf.ntag[k]].nmouse, sizeof(MouseBinding)); + tags[k][conf.ntag[k]].mouse = xcalloc(tags[k][conf.ntag[k]].nmouse, sizeof(MouseBinding)); mouse_section(tags[k][conf.ntag[k]].mouse, mouse); } @@ -619,7 +619,7 @@ conf_rule_section(void) CHECK((conf.nrule = fetch_section_count(rule))); - conf.rule = emalloc(conf.nrule, sizeof(Rule)); + conf.rule = xcalloc(conf.nrule, sizeof(Rule)); for(i = 0; i < conf.nrule; ++i) { @@ -650,7 +650,7 @@ conf_menu_section(void) CHECK((conf.nmenu = fetch_section_count(set_menu))); - conf.menu = emalloc(conf.nmenu, sizeof(Menu)); + conf.menu = xcalloc(conf.nmenu, sizeof(Menu)); for(i = 0; i < conf.nmenu; ++i) { @@ -681,7 +681,7 @@ conf_menu_section(void) if((conf.menu[i].nitem = fetch_section_count(item))) { - conf.menu[i].item = emalloc(conf.menu[i].nitem, sizeof(MenuItem)); + conf.menu[i].item = xcalloc(conf.menu[i].nitem, sizeof(MenuItem)); for(j = 0; j < conf.menu[i].nitem; ++j) { conf.menu[i].item[j].name = fetch_opt_first(item[j], "item_wname", "name").str; @@ -709,7 +709,7 @@ conf_launcher_section(void) CHECK((conf.nlauncher = fetch_section_count(set_launcher))); - conf.launcher = emalloc(conf.nlauncher, sizeof(Launcher)); + conf.launcher = xcalloc(conf.nlauncher, sizeof(Launcher)); for(i = 0; i < conf.nlauncher; ++i) { @@ -735,7 +735,7 @@ conf_keybind_section(void) ks = fetch_section(sec, "key"); conf.nkeybind = fetch_section_count(ks); - keys = emalloc(conf.nkeybind, sizeof(Key)); + keys = xcalloc(conf.nkeybind, sizeof(Key)); for(i = 0; i < conf.nkeybind; ++i) { diff --git a/src/draw.c b/src/draw.c index d163c0f..bc2d55a 100644 --- a/src/draw.c +++ b/src/draw.c @@ -62,7 +62,7 @@ draw_image_ofset_text(Drawable d, int x, int y, char* fg, int pad, char *str, in int i, ni, sw = 0; ImageAttr im[128]; - ostr = _strdup(str); + ostr = xstrdup(str); if(strstr(str, "i[")) { @@ -204,7 +204,7 @@ textw(char *text) ImageAttr im[128]; - ostr = _strdup(text); + ostr = xstrdup(text); if(strstr(text, "i[")) parse_image_block(im, text); diff --git a/src/ewmh.c b/src/ewmh.c index 1f0f7b9..940484a 100644 --- a/src/ewmh.c +++ b/src/ewmh.c @@ -50,7 +50,7 @@ ewmh_init_hints(void) s = screen_count(); - net_atom = emalloc(net_last + s, sizeof(Atom)); + net_atom = xcalloc(net_last + s, sizeof(Atom)); /* EWMH hints */ net_atom[net_supported] = ATOM("_NET_SUPPORTED"); @@ -252,7 +252,7 @@ ewmh_get_client_list(void) int win_n; for(win_n = 0, c = clients; c; c = c->next, ++win_n); - list = emalloc(win_n, sizeof(Window)); + list = xcalloc(win_n, sizeof(Window)); for(win_n = 0, c = clients; c; c = c->next, ++win_n) list[win_n] = c->win; @@ -279,7 +279,7 @@ ewmh_get_desktop_names(void) for(i = 1; i < conf.ntag[s] + 1; ++i) len += strlen(tags[s][i].name); - str = emalloc(len + i + 1, sizeof(char*)); + str = xcalloc(len + i + 1, sizeof(char*)); for(s = 0; s < S; ++s) for(i = 1; i < conf.ntag[s] + 1; ++i, ++pos) diff --git a/src/frame.c b/src/frame.c index 9aa474b..9dd8f52 100644 --- a/src/frame.c +++ b/src/frame.c @@ -83,7 +83,7 @@ frame_create(Client *c) /* Buttons */ if(BUTTONWH >= 1) { - c->button = emalloc(conf.titlebar.nbutton, sizeof(Window)); + c->button = xcalloc(conf.titlebar.nbutton, sizeof(Window)); for(i = 0; i < conf.titlebar.nbutton; ++i) { CWIN(c->button[i], c->titlebar->win, diff --git a/src/getinfo.c b/src/getinfo.c index b3b66e2..b4e2928 100644 --- a/src/getinfo.c +++ b/src/getinfo.c @@ -60,14 +60,14 @@ getinfo_tag(void) if(XGetWindowProperty(dpy, ROOT, ATOM("_WMFS_CURRENT_TAG"), 0L, 4096, False, ATOM("UTF8_STRING"), &rt, &rf, &ir, &il, &ret) == Success && ret) { - tag_name = _strdup((char*)ret); + tag_name = xstrdup((char*)ret); XFree(ret); } if(XGetWindowProperty(dpy, ROOT, ATOM("_WMFS_TAG_LIST"), 0L, 4096, False, ATOM("UTF8_STRING"), &rt, &rf, &ir, &il, &ret) == Success && ret) { - tag_list = _strdup((char*)ret); + tag_list = xstrdup((char*)ret); XFree(ret); } @@ -116,7 +116,7 @@ getinfo_layout(void) if(XGetWindowProperty(dpy, ROOT, ATOM("_WMFS_CURRENT_LAYOUT"), 0L, 4096, False, ATOM("UTF8_STRING"), &rt, &rf, &ir, &il, &ret) == Success && ret) { - layout = _strdup((char*)ret); + layout = xstrdup((char*)ret); XFree(ret); } @@ -137,7 +137,7 @@ getinfo_mwfact(void) if(XGetWindowProperty(dpy, ROOT, ATOM("_WMFS_MWFACT"), 0L, 4096, False, XA_STRING, &rt, &rf, &ir, &il, &ret) == Success && ret) { - mwfact = _strdup((char*)ret); + mwfact = xstrdup((char*)ret); XFree(ret); } diff --git a/src/infobar.c b/src/infobar.c index 9b2537e..50935c4 100644 --- a/src/infobar.c +++ b/src/infobar.c @@ -42,7 +42,7 @@ infobar_init(void) s = screen_count(); if(!infobar) - infobar = emalloc(s, sizeof(InfoBar)); + infobar = xcalloc(s, sizeof(InfoBar)); for(sc = 0; sc < s; ++sc) { @@ -127,7 +127,7 @@ infobar_init(void) barwin_refresh(infobar[sc].bar); /* Default statustext is set here */ - infobar[sc].statustext = _strdup(WMFS_VERSION); + infobar[sc].statustext = xstrdup(WMFS_VERSION); infobar_draw(sc); } @@ -190,7 +190,7 @@ infobar_draw_selbar(int sc) if(conf.selbar.maxlength >= 0 && sel) { - str = emalloc(conf.selbar.maxlength + 4, sizeof(char)); + str = xcalloc(conf.selbar.maxlength + 4, sizeof(char)); strncpy(str, sel->title, conf.selbar.maxlength); if(strlen(sel->title) > (size_t)conf.selbar.maxlength) diff --git a/src/init.c b/src/init.c index 063c530..0528fc3 100644 --- a/src/init.c +++ b/src/init.c @@ -196,7 +196,7 @@ init_status(void) return; } - conf.status_path = emalloc(strlen(home) + strlen(DEF_STATUS) + 2, sizeof(char)); + conf.status_path = zmalloc(strlen(home) + strlen(DEF_STATUS) + 2); sprintf(conf.status_path, "%s/"DEF_STATUS, home); } diff --git a/src/launcher.c b/src/launcher.c index c34df81..1fcd5aa 100644 --- a/src/launcher.c +++ b/src/launcher.c @@ -253,12 +253,11 @@ complete_on_command(char *start, size_t hits) char **namelist = NULL; int n = 0, i; - void *temp = NULL; if (!getenv("PATH") || !start || hits <= 0) return NULL; - path = _strdup(getenv("PATH")); + path = xstrdup(getenv("PATH")); dirname = strtok(path, ":"); /* recursively open PATH */ @@ -272,10 +271,8 @@ complete_on_command(char *start, size_t hits) { if (!strncmp(content->d_name, start, strlen(start))) { - temp = realloc(namelist, ++n * sizeof(*namelist)); - if ( temp != NULL ) - namelist = temp; - namelist[n-1] = strdup(content->d_name); + namelist = xrealloc(namelist, ++n, sizeof(*namelist)); + namelist[n-1] = xstrdup(content->d_name); } } } @@ -289,7 +286,7 @@ complete_on_command(char *start, size_t hits) if(n > 0) { - ret = _strdup(namelist[((hits > 0) ? hits - 1 : 0) % n] + strlen(start)); + ret = xstrdup(namelist[((hits > 0) ? hits - 1 : 0) % n] + strlen(start)); for(i = 0; i < n; i++) free(namelist[i]); @@ -324,14 +321,14 @@ complete_on_files(char *start, size_t hits) * the beginning of file to complete on pointer 'p'. */ if (*(++p) == '\0' || !strrchr(p, '/')) - path = _strdup("."); + path = xstrdup("."); else { /* remplace ~ by $HOME in dirname */ if (!strncmp(p, "~/", 2) && getenv("HOME")) xasprintf(&dirname, "%s%s", getenv("HOME"), p+1); else - dirname = _strdup(p); + dirname = xstrdup(p); /* Set p to filename to be complete * and path the directory containing the file @@ -342,11 +339,11 @@ complete_on_files(char *start, size_t hits) if (p != dirname) { *(p++) = '\0'; - path = _strdup(dirname); + path = xstrdup(dirname); } else { - path = _strdup("/"); + path = xstrdup("/"); p++; } } @@ -367,7 +364,7 @@ complete_on_files(char *start, size_t hits) if (S_ISDIR(st.st_mode)) xasprintf(&ret, "%s/", content->d_name + strlen(p)); else - ret = _strdup(content->d_name + strlen(p)); + ret = xstrdup(content->d_name + strlen(p)); } else warn("%s", filepath); diff --git a/src/layout.c b/src/layout.c index cfa5061..fcd92ac 100644 --- a/src/layout.c +++ b/src/layout.c @@ -823,7 +823,7 @@ uicb_set_layout(uicb_t cmd) for(n = 0; layout_list[n].name != NULL && layout_list[n].func != NULL; ++n); for(i = 0; i < n; ++i) - if(!strcmp(cmd, _strdup(layout_list[i].name))) + if(!strcmp(cmd, xstrdup(layout_list[i].name))) for(j = 0; j < LEN(conf.layout); ++j) if(layout_list[i].func == conf.layout[j].func) tags[selscreen][seltag[selscreen]].layout = conf.layout[j]; diff --git a/src/menu.c b/src/menu.c index 12c0a13..c90cedc 100644 --- a/src/menu.c +++ b/src/menu.c @@ -37,7 +37,7 @@ menu_init(Menu *menu, char *name, int nitem, uint bg_f, char *fg_f, uint bg_n, c { /* Item */ menu->nitem = nitem; - menu->item = emalloc(sizeof(MenuItem), nitem); + menu->item = xcalloc(nitem, sizeof(*menu->item)); menu->name = name; /* Colors */ diff --git a/src/parse/parse.c b/src/parse.c similarity index 95% rename from src/parse/parse.c rename to src/parse.c index 1fdab6c..3de5f7f 100644 --- a/src/parse/parse.c +++ b/src/parse.c @@ -14,8 +14,12 @@ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ +#ifndef _GNU_SOURCE #define _GNU_SOURCE +#endif +#ifndef _BSD_SOURCE #define _BSD_SOURCE +#endif #include #include #include @@ -87,7 +91,7 @@ push_keyword(struct keyword *tail, enum keyword_t type, char *buf, size_t *offse if (type == WORD && *offset == 0) return tail; - kw = xcalloc(1, sizeof(*kw)); + kw = zcalloc(sizeof(*kw)); kw->type = type; kw->line = line; kw->file = file; @@ -179,8 +183,8 @@ parse_keywords(const char *filename) return NULL; } - file = xcalloc(1, sizeof(*file)); - bufname = xcalloc(1, sizeof(*bufname) * BUFSIZ); + file = zcalloc(sizeof(*file)); + bufname = zcalloc(sizeof(*bufname) * BUFSIZ); file->name = strdup(path); file->parent = NULL; @@ -394,7 +398,7 @@ get_option(struct keyword **head) size_t j = 0; struct keyword *kw = *head; - o = xcalloc(1, sizeof(*o)); + o = zcalloc(sizeof(*o)); o->name = kw->name; o->used = False; o->line = kw->line; @@ -495,7 +499,7 @@ get_section(struct keyword **head) struct conf_sec *sub; struct keyword *kw = *head; - s = xcalloc(1, sizeof(*s)); + s = zcalloc(sizeof(*s)); s->name = kw->name; TAILQ_INIT(&s->sub); SLIST_INIT(&s->optlist); @@ -616,32 +620,3 @@ get_conf(const char *filename) return 0; } - -/* calloc wrapper */ -void * -xcalloc(size_t nmemb, size_t size) -{ - void *ret; - - if (!(ret = calloc(nmemb, size))) - err(EXIT_FAILURE, "calloc"); - - return ret; -} - -/* asprintf wrapper */ -int -xasprintf(char **strp, const char *fmt, ...) -{ - int ret; - va_list args; - - va_start(args, fmt); - ret = vasprintf(strp, fmt, args); - va_end(args); - - if (ret == -1) - err(EXIT_FAILURE, "asprintf"); - - return ret; -} diff --git a/src/parse/parse.h b/src/parse.h similarity index 96% rename from src/parse/parse.h rename to src/parse.h index 8a0f9f2..f38185c 100644 --- a/src/parse/parse.h +++ b/src/parse.h @@ -17,6 +17,7 @@ #ifndef PARSE_H #define PARSE_H +#include "wmfs.h" #include #define INCLUDE_CMD "@include" @@ -117,10 +118,4 @@ struct opt_type *fetch_opt(struct conf_sec *, char *, char *); */ size_t fetch_opt_count(struct opt_type *); - -/* wrapper for calloc */ -void *xcalloc(size_t, size_t); -/* wrapper for asprintf */ -int xasprintf(char **, const char *, ...); - #endif /* PARSE_H */ diff --git a/src/parse/api.c b/src/parse_api.c similarity index 99% rename from src/parse/api.c rename to src/parse_api.c index 592dbf4..904db95 100644 --- a/src/parse/api.c +++ b/src/parse_api.c @@ -14,7 +14,9 @@ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ +#ifndef _BSD_SOURCE #define _BSD_SOURCE +#endif #include #include #include diff --git a/src/screen.c b/src/screen.c index 5194248..d73f647 100644 --- a/src/screen.c +++ b/src/screen.c @@ -177,8 +177,8 @@ screen_init_geo(void) int i; int s = screen_count(); - sgeo = emalloc(s, sizeof(XRectangle)); - spgeo = emalloc(s, sizeof(XRectangle)); + sgeo = xcalloc(s, sizeof(XRectangle)); + spgeo = xcalloc(s, sizeof(XRectangle)); for(i = 0; i < s; ++i) sgeo[i] = screen_get_geo(i); diff --git a/src/status.c b/src/status.c index 963a321..e8fa792 100644 --- a/src/status.c +++ b/src/status.c @@ -212,7 +212,7 @@ statustext_handle(int sc, char *str) /* save last status text address (for free at the end) */ lastst = infobar[sc].statustext; - infobar[sc].statustext = _strdup(str); + infobar[sc].statustext = xstrdup(str); len = ((strlen(str) > MAXSTATUS) ? MAXSTATUS : strlen(str)); /* Store rectangles, located text & images properties. */ diff --git a/src/systray.c b/src/systray.c index a9366ba..abc8e53 100644 --- a/src/systray.c +++ b/src/systray.c @@ -85,7 +85,7 @@ systray_add(Window win) if(!conf.systray.active) return; - s = emalloc(1, sizeof(Systray)); + s = zcalloc(sizeof(Systray)); s->win = win; s->geo.height = infobar[conf.systray.screen].bar->geo.height; @@ -98,8 +98,8 @@ systray_add(Window win) ewmh_send_message(s->win, s->win, "_XEMBED", CurrentTime, XEMBED_EMBEDDED_NOTIFY, 0, traywin, 0); /* Attach */ - if(trayicons) - trayicons->prev = s; + if(trayicons) + trayicons->prev = s; s->next = trayicons; trayicons = s; diff --git a/src/tag.c b/src/tag.c index a731ecd..c8a7c57 100644 --- a/src/tag.c +++ b/src/tag.c @@ -512,7 +512,7 @@ tag_new(int s, char *name) { if(conf.tagnamecount) { - displayedName = (char*) malloc( sizeof(char)*2 ); + displayedName = zmalloc(2); sprintf(displayedName, "[%d]", conf.ntag[s]); } else diff --git a/src/util.c b/src/util.c index a68baa6..044c1ef 100644 --- a/src/util.c +++ b/src/util.c @@ -32,22 +32,100 @@ #include "wmfs.h" -/** Calloc with an error message if there is a probleme - * \param element Element - * \param size Size - * \return void pointer -*/ -void* -emalloc(uint element, uint size) +/** malloc with error support and size_t overflow protection + * \param nmemb number of objects + * \param size size of single object + * \return non null void pointer + */ +void * +xmalloc(size_t nmemb, size_t size) { - void *ret = calloc(element, size); + void *ret; - if(!ret) - warn("calloc()"); + if (SIZE_MAX / nmemb < size) + err(EXIT_FAILURE, "xmalloc(%zu, %zu), " + "size_t overflow detected", nmemb, size); + + if ((ret = malloc(nmemb * size)) == NULL) + err(EXIT_FAILURE, "malloc(%zu)", nmemb * size); return ret; } +/** calloc with error support + * \param nmemb Number of objects + * \param size size of single object + * \return non null void pointer +*/ +void * +xcalloc(size_t nmemb, size_t size) +{ + void *ret; + + if ((ret = calloc(nmemb, size)) == NULL) + err(EXIT_FAILURE, "calloc(%zu * %zu)", nmemb, size); + + return ret; +} + +/** realloc with error support and size_t overflow check + * \param ptr old pointer + * \param nmemb number of objects + * \param size size of single object + * \return non null void pointer + */ +void * +xrealloc(void *ptr, size_t nmemb, size_t size) +{ + void *ret; + + if (SIZE_MAX / nmemb < size) + err(EXIT_FAILURE, "xrealloc(%p, %zu, %zu), " + "size_t overflow detected", ptr, nmemb, size); + + if ((ret = realloc(ptr, nmemb * size)) == NULL) + err(EXIT_FAILURE, "realloc(%p, %zu)", ptr, nmemb * size); + + return ret; +} + +/** strdup with error support + * \param str char pointer + * \retun non null void pointer + */ +char * +xstrdup(const char *str) +{ + char *ret; + + if (str == NULL || (ret = strdup(str)) == NULL) + err(EXIT_FAILURE, "strdup(%s)", str); + + return ret; +} + +/** asprintf wrapper + * \param strp target string + * \param fmt format + * \return non zero integer + */ +int +xasprintf(char **strp, const char *fmt, ...) +{ + int ret; + va_list args; + + va_start(args, fmt); + ret = vasprintf(strp, fmt, args); + va_end(args); + + if (ret == -1) + err(EXIT_FAILURE, "asprintf(%s)", fmt); + + return ret; +} + + /** Get a color with a string * \param color Color string * \return Color pixel @@ -93,20 +171,6 @@ setwinstate(Window win, long state) return; } -/** My strdup. the strdup of string.h isn't ansi compatible.. - * Thanks linkdd. - * \param str char pointer -*/ -char* -_strdup(const char *str) -{ - char *ret = emalloc(strlen(str) + 1, sizeof(char)); - - strcpy(ret, str); - - return ret; -} - /* The following function are for configuration usage. {{{ */ @@ -176,9 +240,9 @@ alias_to_str(char *conf_choice) tmpchar = conf.alias[i].content; if(tmpchar) - return _strdup(tmpchar); + return xstrdup(tmpchar); else - return _strdup(conf_choice); + return xstrdup(conf_choice); return NULL; } @@ -327,7 +391,7 @@ clean_value(char *str) int i; char c, *p; - if(!str || !(p = _strdup(str))) + if(!str || !(p = xstrdup(str))) return NULL; /* Remove useless spaces */ diff --git a/src/viwmfs.c b/src/viwmfs.c index d47b247..41dd02e 100644 --- a/src/viwmfs.c +++ b/src/viwmfs.c @@ -116,7 +116,7 @@ viwmfs(int argc, char **argv) { for(i = 0; i < strlen(str); str[i] = str[i + 1], ++i); - cmd = _strdup(str); + cmd = xstrdup(str); for(i = 0; cmd[i] && cmd[i] != ' '; ++i); cmd[i] = '\0'; diff --git a/src/wmfs.c b/src/wmfs.c index 42f61e8..c41226a 100644 --- a/src/wmfs.c +++ b/src/wmfs.c @@ -422,7 +422,7 @@ main(int argc, char **argv) int i; char *ol = "csgVS"; - argv_global = _strdup(argv[0]); + argv_global = xstrdup(argv[0]); sprintf(conf.confpath, "%s/"DEF_CONF, getenv("HOME")); while((i = getopt(argc, argv, "hviSc:s:g:C:V:")) != -1) diff --git a/src/wmfs.h b/src/wmfs.h index 94afe36..d590d22 100644 --- a/src/wmfs.h +++ b/src/wmfs.h @@ -34,14 +34,22 @@ #define WMFS_H /* glibc stuff */ +#ifndef _BSD_SOURCE #define _BSD_SOURCE /* vsnprintf */ -#define _POSIX_SOURCE /* kill() */ +#endif +#ifndef _POSIX_SOURCE +#define _POSIX_SOURCE /* kill */ +#endif +#ifndef _GNU_SOURCE +#define _GNU_SOURCE /* asprintf */ +#endif /* Lib headers */ #include #include #include #include +#include #include #include #include @@ -61,7 +69,7 @@ #include /* Local headers */ -#include "parse/parse.h" +#include "parse.h" #include "structs.h" /* Optional dependencies */ @@ -281,11 +289,18 @@ void uicb_mouse_move(uicb_t); void uicb_mouse_resize(uicb_t); /* util.c */ +void *xmalloc(size_t, size_t); +void *xcalloc(size_t, size_t); +void *xrealloc(void *, size_t, size_t); +/* simples wrappers for allocating only one object */ +#define zmalloc(size) xmalloc(1, (size)) +#define zcalloc(size) xcalloc(1, (size)) +#define zrealloc(ptr, size) xrealloc((ptr), 1, (size)) +char *xstrdup(const char *); +int xasprintf(char **, const char *, ...); ulong color_enlight(ulong col); -void *emalloc(uint element, uint size); long getcolor(char *color); void setwinstate(Window win, long state); -char* _strdup(char const *str); /* Conf usage {{{ */ void* name_to_func(char *name, const func_name_list_t *l); ulong char_to_modkey(char *name, key_name_list_t key_l[]);