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 1b18832..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} @@ -60,9 +60,9 @@ install: all @echo installing xsession file to ${DESTDIR}${PREFIX}/share/xsessions mkdir -p ${DESTDIR}${PREFIX}/share/xsessions install -m 644 wmfs.desktop ${DESTDIR}${PREFIX}/share/xsessions/ - @echo installing default config file to ${DESTDIR}${XDG_CONFIG_DIR} - mkdir -p ${DESTDIR}${XDG_CONFIG_DIR} - install -m 444 wmfsrc ${DESTDIR}${XDG_CONFIG_DIR} + @echo installing default config file to ${DESTDIR}${XDG_CONFIG_DIR}/wmfs/ + mkdir -p ${DESTDIR}${XDG_CONFIG_DIR}/wmfs/ + install -m 444 wmfsrc ${DESTDIR}${XDG_CONFIG_DIR}/wmfs/ uninstall: @echo removing executable file from ${DESTDIR}${PREFIX}/bin @@ -71,8 +71,9 @@ uninstall: rm -f ${DESTDIR}${MANPREFIX}/man1/wmfs.1.gz @echo removing xsession file from ${DESTDIR}${PREFIX}/share/xsessions rm -f ${DESTDIR}${PREFIX}/share/xsessions/wmfs.desktop - @echo removing config file from ${DESTDIR}${XDG_CONFIG_DIR} - rm -f ${DESTDIR}${XDG_CONFIG_DIR}/wmfsrc + @echo removing config file from ${DESTDIR}${XDG_CONFIG_DIR}/wmfs/ + rm -f ${DESTDIR}${XDG_CONFIG_DIR}/wmfs/wmfsrc + rmdir ${DESTDIR}${XDG_CONFIG_DIR}/wmfs/ diff --git a/configure b/configure index a6184a9..27b83e0 100755 --- a/configure +++ b/configure @@ -7,7 +7,7 @@ USE_IMLIB2="imlib2" OS=`uname -s` PREFIX=/usr/local MANPREFIX="$PREFIX/man" -XDG_CONFIG_DIR="$PREFIX/etc/xdg/wmfs" +XDG_CONFIG_DIR="$PREFIX/etc/xdg" while true; do case "$1" in diff --git a/src/barwin.c b/src/barwin.c index b2170fe..13ce885 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; @@ -116,7 +116,7 @@ barwin_draw_text(BarWindow *bw, int x, int y, char *text) draw_rectangle(bw->dr, x - 4, 0, textw(text) + 8, bw->geo.height, bw->bg); /* Draw text */ - draw_text(bw->dr, x, y, bw->fg, 0, text); + draw_text(bw->dr, x, y, bw->fg, text); barwin_refresh(bw); @@ -136,7 +136,7 @@ barwin_draw_image_ofset_text(BarWindow *bw, int x, int y, char *text, int x_imag draw_rectangle(bw->dr, x - 4, 0, textw(text) + 8, bw->geo.height, bw->bg); /* Draw text */ - draw_image_ofset_text(bw->dr, x, y, bw->fg, 0, text, x_image_ofset, y_image_ofset); + draw_image_ofset_text(bw->dr, x, y, bw->fg, text, x_image_ofset, y_image_ofset); barwin_refresh(bw); diff --git a/src/client.c b/src/client.c index 23d7fbd..d3a9f75 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; @@ -1023,7 +1023,7 @@ client_set_rules(Client *c) if(XGetWindowProperty(dpy, c->win, ATOM("WM_WINDOW_ROLE"), 0L, 0x7FFFFFFFL, False, XA_STRING, &rf, &f, &n, &il, &data) == Success && data) { - strcpy(wwrole, (char*)data); + strncpy(wwrole, (char*)data, sizeof(wwrole)); XFree(data); } diff --git a/src/config.c b/src/config.c index 983e164..e912ecf 100644 --- a/src/config.c +++ b/src/config.c @@ -95,7 +95,8 @@ const func_name_list_t func_list[] = {"check_layout", uicb_checklayout }, {"clientlist", uicb_clientlist }, {"check_clist", uicb_checkclist }, - {"toggle_tagautohide", uicb_toggle_tagautohide } + {"toggle_tagautohide", uicb_toggle_tagautohide }, + {NULL, NULL} }; key_name_list_t key_list[] = @@ -197,7 +198,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); } @@ -228,7 +229,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); } @@ -250,7 +251,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); } @@ -290,7 +291,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); } @@ -318,7 +319,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); } @@ -329,7 +330,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; @@ -347,7 +348,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); } @@ -358,7 +359,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) { @@ -415,7 +416,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; } @@ -520,10 +521,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; @@ -533,7 +534,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++) { @@ -574,7 +575,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; } @@ -586,7 +587,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); } @@ -620,7 +621,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) { @@ -651,7 +652,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) { @@ -682,7 +683,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; @@ -710,7 +711,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) { @@ -736,7 +737,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..4fa3b3f 100644 --- a/src/draw.c +++ b/src/draw.c @@ -33,9 +33,9 @@ #include "wmfs.h" void -draw_text(Drawable d, int x, int y, char* fg, int pad, char *str) +draw_text(Drawable d, int x, int y, char* fg, char *str) { - draw_image_ofset_text(d, x, y, fg, pad, str, 0, 0); + draw_image_ofset_text(d, x, y, fg, str, 0, 0); } /** Draw a string in a Drawable @@ -47,22 +47,28 @@ draw_text(Drawable d, int x, int y, char* fg, int pad, char *str) * \param str String that will be draw */ void -draw_image_ofset_text(Drawable d, int x, int y, char* fg, int pad, char *str, int x_image_ofset, int y_image_ofset) +draw_image_ofset_text(Drawable d, int x, int y, char* fg, char *str, int x_image_ofset, int y_image_ofset) { XftColor xftcolor; XftDraw *xftd; - (void)pad; +#ifdef HAVE_IMLIB + char *ostr = NULL; + int i, ni, sw = 0; + ImageAttr im[128]; + size_t textlen; +#else + (void)x_image_ofset; + (void)y_image_ofset; +#endif /* HAVE_IMLIB */ if(!str) return; /* To draw image everywhere we can draw text */ #ifdef HAVE_IMLIB - char *ostr = NULL; - int i, ni, sw = 0; - ImageAttr im[128]; - ostr = _strdup(str); + ostr = xstrdup(str); + textlen = strlen(ostr); if(strstr(str, "i[")) { @@ -92,7 +98,7 @@ draw_image_ofset_text(Drawable d, int x, int y, char* fg, int pad, char *str, in #ifdef HAVE_IMLIB if(strstr(ostr, "i[")) - strcpy(str, ostr); + strncpy(str, ostr, textlen); IFREE(ostr); #endif /* HAVE_IMLIB */ @@ -195,16 +201,20 @@ ushort textw(char *text) { XGlyphInfo gl; +#ifdef HAVE_IMLIB + char *ostr = NULL; + ImageAttr im[128]; + size_t textlen; +#endif /* HAVE_IMLIB */ if(!text) return 0; + #ifdef HAVE_IMLIB - char *ostr = NULL; - ImageAttr im[128]; - - ostr = _strdup(text); + ostr = xstrdup(text); + textlen = strlen(ostr); if(strstr(text, "i[")) parse_image_block(im, text); @@ -214,7 +224,7 @@ textw(char *text) #ifdef HAVE_IMLIB if(strstr(ostr, "i[")) - strcpy(text, ostr); + strncpy(text, ostr, textlen); IFREE(ostr); #endif /* HAVE_IMLIB */ diff --git a/src/event.c b/src/event.c index 165005e..e1e0989 100644 --- a/src/event.c +++ b/src/event.c @@ -370,7 +370,6 @@ void enternotify(XCrossingEvent *ev) { Client *c; - Systray *s; int n; if((ev->mode != NotifyNormal @@ -379,7 +378,7 @@ enternotify(XCrossingEvent *ev) return; /* Don't handle EnterNotify event if it's about systray */ - if((s = systray_find(ev->window)) || ev->window == traywin) + if(systray_find(ev->window) || ev->window == traywin) return; if(conf.focus_fmouse) @@ -525,7 +524,6 @@ void maprequest(XMapRequestEvent *ev) { XWindowAttributes at; - Client *c; Systray *s; CHECK(XGetWindowAttributes(dpy, ev->window, &at)); @@ -536,7 +534,7 @@ maprequest(XMapRequestEvent *ev) ewmh_send_message(s->win, s->win, "_XEMBED", CurrentTime, XEMBED_WINDOW_ACTIVATE, 0, 0, 0); systray_update(); } - else if(!(c = client_gb_win(ev->window))) + else if(!client_gb_win(ev->window)) client_manage(ev->window, &at, True); return; 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..d89b8ef 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) @@ -220,7 +220,7 @@ infobar_draw_selbar(int sc) void infobar_draw_taglist(int sc) { - int i, x, j; + int i, x; Client *c = NULL; Bool is_occupied[MAXTAG + 1]; @@ -234,7 +234,7 @@ infobar_draw_taglist(int sc) if(c->screen == sc) is_occupied[c->tag] = True; - for(i = 1, x = j = 0; i < conf.ntag[sc] + 1; ++i) + for(i = 1, x = 0; i < conf.ntag[sc] + 1; ++i) { /* Autohide tag feature */ if(conf.tagautohide) 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..aab5566 100644 --- a/src/launcher.c +++ b/src/launcher.c @@ -156,7 +156,7 @@ launcher_execute(Launcher *launcher) else { tabhits = 1; - strcpy(tmpbuf, buf); + strncpy(tmpbuf, buf, sizeof(tmpbuf)); } @@ -169,7 +169,7 @@ launcher_execute(Launcher *launcher) if (complete) { - strcpy(buf, tmpbuf); + strncpy(buf, tmpbuf, sizeof(buf)); strncat(buf, complete, sizeof(buf)); found = True; free(complete); @@ -179,7 +179,7 @@ launcher_execute(Launcher *launcher) lastwastab = True; /* start a new round of tabbing */ - if (!found) + if (found == False) tabhits = 0; pos = strlen(buf); @@ -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); } } } @@ -283,19 +280,20 @@ complete_on_command(char *start, size_t hits) } dirname = strtok(NULL, ":"); } - qsort(namelist, n, sizeof(char *), qsort_string_compare); free(path); if(n > 0) { - ret = _strdup(namelist[((hits > 0) ? hits - 1 : 0) % n] + strlen(start)); + qsort(namelist, n, sizeof(char *), qsort_string_compare); + ret = xstrdup(namelist[((hits > 0) ? hits - 1 : 0) % n] + strlen(start)); for(i = 0; i < n; i++) - free(namelist[i]); + free(namelist[i]); + + free(namelist); } - free(namelist); return ret; } @@ -324,14 +322,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 +340,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 +365,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..6cf9d1f 100644 --- a/src/layout.c +++ b/src/layout.c @@ -325,7 +325,10 @@ multi_tile(int screen, Position type) { if(type == Top) mastergeo.y = (n <= nmaster) ? (uint)sg.y : sg.y + (sg.height - mwfact) - BORDH; - mastergeo.width = (sg.width / nmaster) - (BORDH * 4); + if (nmaster != 0) + mastergeo.width = (sg.width / nmaster) - (BORDH * 4); + else + mastergeo.width = sg.width - (BORDH * 4); mastergeo.height = (n <= nmaster) ? (uint)(sg.height - BORDH) : mwfact; } else @@ -333,7 +336,10 @@ multi_tile(int screen, Position type) if(type == Left) mastergeo.x = (n <= nmaster) ? (uint)sg.x : (sg.x + sg.width) - mwfact - (BORDH * 2); mastergeo.width = (n <= nmaster) ? (uint)(sg.width - (BORDH * 2)) : mwfact; - mastergeo.height = (sg.height / nmaster) - BORDH; + if (nmaster != 0) + mastergeo.height = sg.height - BORDH; + else + mastergeo.height = BORDH; } /* TILED SIZE */ @@ -823,7 +829,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..dcca36c 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; @@ -96,7 +100,7 @@ push_keyword(struct keyword *tail, enum keyword_t type, char *buf, size_t *offse if (*offset != 0) { buf[*offset] = '\0'; if (!strcmp(buf, INCLUDE_CMD)) - type = kw->type = INCLUDE; + kw->type = INCLUDE; else kw->name = strdup(buf); *offset = 0; @@ -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..4ac3205 100644 --- a/src/screen.c +++ b/src/screen.c @@ -91,6 +91,8 @@ screen_get_geo(int s) XFree(xsi); } +#else + (void)s; #endif /* HAVE_XINERAMA */ return geo; @@ -177,8 +179,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..ea78f17 100644 --- a/src/status.c +++ b/src/status.c @@ -158,11 +158,11 @@ statustext_normal(int sc, char *str) /* Draw normal text without any blocks */ draw_text(infobar[sc].bar->dr, (sgeo[sc].width - SHADH) - (textw(strwc) + sw), - FHINFOBAR, infobar[sc].bar->fg, 0, strwc); + FHINFOBAR, infobar[sc].bar->fg, strwc); if(n) { - strcpy(buf, strwc); + strncpy(buf, strwc, sizeof(buf)); for(i = k = 0; i < (int)strlen(str); ++i, ++k) if(str[i] == '\\' && str[i + 1] == '#' && str[i + 8] == '\\') @@ -177,9 +177,9 @@ statustext_normal(int sc, char *str) /* Draw text with its color */ draw_text(infobar[sc].bar->dr, (sgeo[sc].width - SHADH) - (textw(&buf[k]) + sw), - FHINFOBAR, col, 0, &buf[k]); + FHINFOBAR, col, &buf[k]); - strcpy(buf, strwc); + strncpy(buf, strwc, sizeof(buf)); ++i; } } @@ -195,7 +195,7 @@ void statustext_handle(int sc, char *str) { char *lastst; - int i, nr, ng, ns, len, sw = 0; + int i, nr, ng, ns, sw = 0; StatusRec r[128]; StatusGraph g[128]; StatusText s[128]; @@ -212,8 +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); - len = ((strlen(str) > MAXSTATUS) ? MAXSTATUS : strlen(str)); + infobar[sc].statustext = xstrdup(str); /* Store rectangles, located text & images properties. */ nr = statustext_rectangle(r, str); @@ -233,7 +232,7 @@ statustext_handle(int sc, char *str) /* Draw located text with stored properties. */ for(i = 0; i < ns; ++i) - draw_text(infobar[sc].bar->dr, s[i].x - sw, s[i].y, s[i].color, 0, s[i].text); + draw_text(infobar[sc].bar->dr, s[i].x - sw, s[i].y, s[i].color, s[i].text); barwin_refresh(infobar[sc].bar); 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 c001812..5b99fda 100644 --- a/src/tag.c +++ b/src/tag.c @@ -543,7 +543,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 @@ -656,11 +656,22 @@ void uicb_tag_rename(uicb_t cmd) { screen_get_sel(); + char *str; + size_t len; if(!cmd || !strlen(cmd)) return; - strcpy(tags[selscreen][seltag[selscreen]].name, cmd); + str = tags[selscreen][seltag[selscreen]].name; + len = strlen(str); + + /* TODO: if strlen(cmd) > len, the tag name + * will be truncated... + * We can't do a realloc because if the pointer change + * free() on paser will segfault.on free_conf()... + */ + strncpy(str, cmd, len); + infobar_update_taglist(selscreen); infobar_draw(selscreen); diff --git a/src/util.c b/src/util.c index a68baa6..f6527cf 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 */ @@ -353,7 +417,7 @@ patht(char *path) if(!path) return NULL; - strcpy(ret, path); + strncpy(ret, path, sizeof(ret)); if(strstr(path, "~/")) sprintf(ret, "%s/%s", getenv("HOME"), path + 2); diff --git a/src/viwmfs.c b/src/viwmfs.c index d47b247..a8fc7dd 100644 --- a/src/viwmfs.c +++ b/src/viwmfs.c @@ -103,7 +103,7 @@ viwmfs(int argc, char **argv) } } else - strcpy(str, argv[2]); + strncpy(str, argv[2], sizeof(str)); if(!strcmp(str, "help")) { @@ -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 d21fa2c..584086c 100644 --- a/src/wmfs.c +++ b/src/wmfs.c @@ -36,7 +36,6 @@ int errorhandler(Display *d, XErrorEvent *event) { char mess[256]; - Client *c; /* Check if there is another WM running */ if(BadAccess == event->error_code @@ -48,7 +47,7 @@ errorhandler(Display *d, XErrorEvent *event) * 42 = X_SetInputFocus * 28 = X_GrabButton */ - if((c = client_gb_win(event->resourceid))) + if(client_gb_win(event->resourceid)) if(event->error_code == BadWindow || event->request_code == 42 || event->request_code == 28) @@ -421,9 +420,8 @@ main(int argc, char **argv) int i; char *ol = "csgVS"; - argv_global = _strdup(argv[0]); + argv_global = xstrdup(argv[0]); all_argv = argv; - sprintf(conf.confpath, "%s/"DEF_CONF, getenv("HOME")); while((i = getopt(argc, argv, "hviSc:s:g:C:V:")) != -1) @@ -467,7 +465,7 @@ main(int argc, char **argv) break; case 'C': - strcpy(conf.confpath, optarg); + strncpy(conf.confpath, optarg, sizeof(conf.confpath)); break; case 'c': diff --git a/src/wmfs.h b/src/wmfs.h index 106d278..102a07b 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 */ @@ -136,8 +144,8 @@ void barwin_refresh_color(BarWindow *bw); void barwin_refresh(BarWindow *bw); /* draw.c */ -void draw_text(Drawable d, int x, int y, char* fg, int pad, char *str); -void draw_image_ofset_text(Drawable d, int x, int y, char* fg, int pad, char *str, int x_image_ofset, int y_image_ofset); +void draw_text(Drawable d, int x, int y, char* fg, char *str); +void draw_image_ofset_text(Drawable d, int x, int y, char* fg, char *str, int x_image_ofset, int y_image_ofset); void draw_rectangle(Drawable dr, int x, int y, uint w, uint h, uint color); void draw_graph(Drawable dr, int x, int y, uint w, uint h, uint color, char *data); @@ -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[]);