On Fri, Aug 12, 2016 at 11:18:43AM +0200, Markus Teich wrote:
> Hiltjo Posthuma wrote:
> > I (and some others) think we should apply the behaviour change in die() to 
> > all
> > other suckless projects that use it for consistency.
> 
> Heyho,
> 
> yes, I proposed it to FRIGN yesterday as well. The consistency benefit is
> obvious, but there might be another point left to discuss: Should it print the
> newline always or never?
> 
> Pro always: Saves two characters in each call.
> Pro never: Same behaviour as the printf-family from libc.
> 
> I am pro always due to convenience.
> 
> --Markus
> 

Here is a patch that improve the error messages a bit more and a patch that 
checks
a memory allocation case.

The patches are attached.

-- 
Kind regards,
Hiltjo
>From 87aada1b352e6674e1f2d8b532db66bf75880642 Mon Sep 17 00:00:00 2001
From: Hiltjo Posthuma <[email protected]>
Date: Fri, 12 Aug 2016 10:57:44 +0200
Subject: [PATCH 1/2] improve some more error messages

- remove \n in die() messages in drw.c
- add a few more ':' suffix for functions that set errno.
- minor typo.
---
 drw.c  |  6 +++---
 sent.c | 14 ++++++++------
 2 files changed, 11 insertions(+), 9 deletions(-)

diff --git a/drw.c b/drw.c
index 95839c9..c1582e7 100644
--- a/drw.c
+++ b/drw.c
@@ -129,7 +129,7 @@ xfont_create(Drw *drw, const char *fontname, FcPattern 
*fontpattern)
                        return NULL;
                }
        } else {
-               die("no font specified.\n");
+               die("no font specified.");
        }
 
        font = ecalloc(1, sizeof(Fnt));
@@ -188,7 +188,7 @@ drw_clr_create(Drw *drw, Clr *dest, const char *clrname)
        if (!XftColorAllocName(drw->dpy, DefaultVisual(drw->dpy, drw->screen),
                               DefaultColormap(drw->dpy, drw->screen),
                               clrname, dest))
-               die("error, cannot allocate color '%s'\n", clrname);
+               die("error, cannot allocate color '%s'", clrname);
 }
 
 /* Wrapper to create color schemes. The caller has to call free(3) on the
@@ -331,7 +331,7 @@ drw_text(Drw *drw, int x, int y, unsigned int w, unsigned 
int h, unsigned int lp
 
                        if (!drw->fonts->pattern) {
                                /* Refer to the comment in xfont_create for 
more information. */
-                               die("the first font in the cache must be loaded 
from a font string.\n");
+                               die("the first font in the cache must be loaded 
from a font string.");
                        }
 
                        fcpattern = FcPatternDuplicate(drw->fonts->pattern);
diff --git a/sent.c b/sent.c
index b91c21a..df99ab4 100644
--- a/sent.c
+++ b/sent.c
@@ -206,8 +206,10 @@ ffload(Slide *s)
                die("sent: Unable to filter '%s':", filename);
        close(fdin);
 
-       if (read(fdout, hdr, 16) != 16 || memcmp("farbfeld", hdr, 8))
-               die("sent: Unable to filter '%s' into a valid farbfeld file", 
filename);
+       if (read(fdout, hdr, 16) != 16)
+               die("sent: Unable to read '%s' farbfeld file:", filename);
+       if (memcmp("farbfeld", hdr, 8))
+               die("sent: File '%s' has not a valid farbfeld header", 
filename);
 
        s->img = calloc(1, sizeof(Image));
        s->img->bufwidth = ntohl(*(uint32_t *)&hdr[8]);
@@ -217,13 +219,13 @@ ffload(Slide *s)
                free(s->img->buf);
        /* internally the image is stored in 888 format */
        if (!(s->img->buf = malloc(3 * s->img->bufwidth * s->img->bufheight)))
-               die("sent: Unable to allocate buffer for image");
+               die("sent: Unable to allocate buffer for image:");
 
        /* scratch buffer to read row by row */
        rowlen = s->img->bufwidth * 2 * strlen("RGBA");
        row = malloc(rowlen);
        if (!row)
-               die("sent: Unable to allocate buffer for image row");
+               die("sent: Unable to allocate buffer for image row:");
 
        /* extract window background color channels for transparency */
        bg_r = (sc[ColBg].pixel >> 16) % 256;
@@ -268,7 +270,7 @@ ffprepare(Image *img)
                height = img->bufheight * xw.uw / img->bufwidth;
 
        if (depth < 24)
-               die("sent: Display color depths <24 not supported");
+               die("sent: Display color depths < 24 not supported");
 
        if (!(img->ximg = XCreateImage(xw.dpy, CopyFromParent, depth, ZPixmap, 
0,
                                       NULL, width, height, 32, 0)))
@@ -585,7 +587,7 @@ xloadfonts()
 
        for (j = 0; j < LEN(fontfallbacks); j++) {
                if (!(fstrs[j] = malloc(MAXFONTSTRLEN)))
-                       die("sent: Unable to allocate fontstring");
+                       die("sent: Unable to allocate fontstring:");
        }
 
        for (i = 0; i < NUMFONTSCALES; i++) {
-- 
2.9.2

>From fe42ad084389cbcf22546afeca80a3a35e2cdafc Mon Sep 17 00:00:00 2001
From: Hiltjo Posthuma <[email protected]>
Date: Fri, 12 Aug 2016 13:09:36 +0200
Subject: [PATCH 2/2] check memory allocation for calloc, die on failure

---
 sent.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/sent.c b/sent.c
index df99ab4..b68b72e 100644
--- a/sent.c
+++ b/sent.c
@@ -211,7 +211,7 @@ ffload(Slide *s)
        if (memcmp("farbfeld", hdr, 8))
                die("sent: File '%s' has not a valid farbfeld header", 
filename);
 
-       s->img = calloc(1, sizeof(Image));
+       s->img = ecalloc(1, sizeof(Image));
        s->img->bufwidth = ntohl(*(uint32_t *)&hdr[8]);
        s->img->bufheight = ntohl(*(uint32_t *)&hdr[12]);
 
-- 
2.9.2

Reply via email to