Re: [dev] dwm bit fields conversion

2013-07-08 Thread Kurt Van Dijck
Hello,

I just remembered this thing from an embedded 16bit CPU.

Why would you do
unsigned char XXX :1
and not
unsigned int XXX :1

I think the latter may benefit code size (a tiny bit)
because it avoids an integer promotion, at least on some platforms.

Kind regards,
Kurt

On Sun, Jul 07, 2013 at 11:07:58AM -0400, Jacob Todd wrote:
> this will be useful for running dwm on my pdp-11.
> 
> On Sun, Jul 7, 2013 at 10:49 AM, koneu  wrote:
> > In Xdefs.h, Bool is typedef'd as int (= at least 2 bytes, sometimes more, 
> > depending on the implementation), of which we set the last bit to 1 or 0.
> > In the Monitor and Client structures dwm uses, we can instead use char bit 
> > fields, storing up to 8 Bool values in 1 byte.
> >
> > diff --git a/dwm.c b/dwm.c
> > index 314adf4..83af1f6 100644
> > --- a/dwm.c
> > +++ b/dwm.c
> > @@ -91,7 +91,7 @@ struct Client {
> > int basew, baseh, incw, inch, maxw, maxh, minw, minh;
> > int bw, oldbw;
> > unsigned int tags;
> > -   Bool isfixed, isfloating, isurgent, neverfocus, oldstate, 
> > isfullscreen;
> > +   unsigned char isfixed :1, isfloating :1, isurgent :1, neverfocus 
> > :1, oldstate :1, isfullscreen :1, :2;
> > Client *next;
> > Client *snext;
> > Monitor *mon;
> > @@ -121,8 +121,7 @@ struct Monitor {
> > unsigned int seltags;
> > unsigned int sellt;
> > unsigned int tagset[2];
> > -   Bool showbar;
> > -   Bool topbar;
> > +   unsigned char showbar :1, topbar :1, :6;
> > Client *clients;
> > Client *sel;
> > Client *stack;
> >
> >
> 



Re: [dev] dwm bit fields conversion

2013-07-08 Thread Roberto E. Vargas Caballero

On Mon, Jul 08, 2013 at 09:47:10AM +0200, Kurt Van Dijck wrote:
> Hello,
> 
> I just remembered this thing from an embedded 16bit CPU.
> 
> Why would you do
>   unsigned char XXX :1
> and not
>   unsigned int XXX :1
> 
> I think the latter may benefit code size (a tiny bit)
> because it avoids an integer promotion, at least on some platforms.

unsigned char is directly incorrect and a gnu extension. Bitfields must be
integers o bool (since c99). You can see it with 'gcc -std=c99 -pedantic'.

Best regards,





[dev] [sbase] [patch v2] Add crypt.[ch] and update md5sum and sha1sum

2013-07-08 Thread sin
Hi,

An updated version of the previous patch based on the feedback.

Thanks,
stateless
>From b3ca3b5ca35f368ab81ae5f6ce3cfbfd7089ef49 Mon Sep 17 00:00:00 2001
From: sin 
Date: Sun, 7 Jul 2013 15:29:45 +0100
Subject: [PATCH v2] Add crypt.[ch] and update md5sum and sha1sum

Factor out the code from md5sum and sha1sum into a util function.

Use FILE * instead of a file descriptor.  This will make it a bit
easier/more consistent when we implement support for the -c option.
---
 Makefile |  1 +
 crypt.h  | 10 ++
 md5.h|  6 +++---
 md5sum.c | 49 +
 sha1.h   |  6 +++---
 sha1sum.c| 49 +
 util/crypt.c | 25 +
 util/md5.c   |  9 ++---
 util/sha1.c  | 10 +++---
 9 files changed, 97 insertions(+), 68 deletions(-)
 create mode 100644 crypt.h
 create mode 100644 util/crypt.c

diff --git a/Makefile b/Makefile
index f825ec4..c58fb86 100644
--- a/Makefile
+++ b/Makefile
@@ -10,6 +10,7 @@ LIB = \
util/apathmax.o  \
util/concat.o\
util/cp.o\
+   util/crypt.o \
util/enmasse.o   \
util/eprintf.o   \
util/estrtol.o   \
diff --git a/crypt.h b/crypt.h
new file mode 100644
index 000..e69dad5
--- /dev/null
+++ b/crypt.h
@@ -0,0 +1,10 @@
+struct crypt_ops {
+   void (*init)(void *);
+   void (*update)(void *, const void *, unsigned long);
+   void (*sum)(void *, uint8_t *);
+   void (*print)(void *, const char *, uint8_t *);
+   void *s;
+};
+
+int cryptsum(struct crypt_ops *ops, FILE *fp, const char *f,
+uint8_t *md);
diff --git a/md5.h b/md5.h
index 0895d3e..0b5005e 100644
--- a/md5.h
+++ b/md5.h
@@ -9,10 +9,10 @@ struct md5 {
 enum { MD5_DIGEST_LENGTH = 16 };
 
 /* reset state */
-void md5_init(struct md5 *s);
+void md5_init(void *ctx);
 /* process message */
-void md5_update(struct md5 *s, const void *m, unsigned long len);
+void md5_update(void *ctx, const void *m, unsigned long len);
 /* get message digest */
 /* state is ruined after sum, keep a copy if multiple sum is needed */
 /* part of the message might be left in s, zero it if secrecy is needed */
-void md5_sum(struct md5 *s, uint8_t md[MD5_DIGEST_LENGTH]);
+void md5_sum(void *ctx, uint8_t md[MD5_DIGEST_LENGTH]);
diff --git a/md5sum.c b/md5sum.c
index 722416f..8705b4b 100644
--- a/md5sum.c
+++ b/md5sum.c
@@ -1,15 +1,21 @@
 /* See LICENSE file for copyright and license details. */
-#include 
-#include 
-#include 
-#include 
 #include 
 #include 
 #include 
 #include "util.h"
+#include "crypt.h"
 #include "md5.h"
 
-static void md5sum(int fd, const char *f);
+static void md5_print(void *, const char *, uint8_t *);
+
+struct md5 s;
+struct crypt_ops md5_ops = {
+   md5_init,
+   md5_update,
+   md5_sum,
+   md5_print,
+   &s,
+};
 
 static void
 usage(void)
@@ -20,7 +26,8 @@ usage(void)
 int
 main(int argc, char *argv[])
 {
-   int fd;
+   FILE *fp;
+   uint8_t md[MD5_DIGEST_LENGTH];
 
ARGBEGIN {
default:
@@ -28,13 +35,13 @@ main(int argc, char *argv[])
} ARGEND;
 
if (argc == 0) {
-   md5sum(STDIN_FILENO, "");
+   cryptsum(&md5_ops, stdin, "", md);
} else {
for (; argc > 0; argc--) {
-   if ((fd = open(*argv, O_RDONLY)) < 0)
-   eprintf("open %s:", *argv);
-   md5sum(fd, *argv);
-   close(fd);
+   if ((fp = fopen(*argv, "r"))  == NULL)
+   eprintf("fopen %s:", *argv);
+   cryptsum(&md5_ops, fp, *argv, md);
+   fclose(fp);
argv++;
}
}
@@ -43,25 +50,11 @@ main(int argc, char *argv[])
 }
 
 static void
-md5sum(int fd, const char *f)
+md5_print(void *s, const char *f, uint8_t *md)
 {
-   unsigned char buf[BUFSIZ];
-   unsigned char digest[MD5_DIGEST_LENGTH];
-   struct md5 s;
-   ssize_t n;
int i;
 
-   md5_init(&s);
-   while ((n = read(fd, buf, sizeof buf)) > 0)
-   md5_update(&s, buf, n);
-   if (n < 0) {
-   eprintf("%s: read error:", f);
-   return;
-   }
-
-   md5_sum(&s, digest);
-
-   for (i = 0; i < sizeof(digest); i++)
-   printf("%02x", digest[i]);
+   for (i = 0; i < MD5_DIGEST_LENGTH; i++)
+   printf("%02x", md[i]);
printf("  %s\n", f);
 }
diff --git a/sha1.h b/sha1.h
index e11f49e..8631777 100644
--- a/sha1.h
+++ b/sha1.h
@@ -9,10 +9,10 @@ struct sha1 {
 enum { SHA1_DIGEST_LENGTH = 20 };
 
 /* reset state */
-void sha1_init(struct sha1 *s);
+void sha1_init(void *ctx);
 /* process message */
-void sha1_update(struct sha1 *s, const void *m, unsigned long len);
+void sha1_update(void *ctx, const void *m, unsigned long len);
 /* get message digest */
 /* 

[dev] [surf] [patch] Set cookiefile for new windows

2013-07-08 Thread Jack Murray
diff --git a/surf.c b/surf.c
index 53dda18..0f9b032 100644
--- a/surf.c
+++ b/surf.c
@@ -865,6 +865,8 @@ newwindow(Client *c, const Arg *arg, gboolean noembed) {
cmd[i++] = "-s";
if(showxid)
cmd[i++] = "-x";
+   cmd[i++] = "-c";
+   cmd[i++] = cookiefile;
cmd[i++] = "--";
uri = arg->v ? (char *)arg->v : c->linkhover;
if(uri)


Re: [dev] [surf] [patch] Set cookiefile for new windows

2013-07-08 Thread Jack Murray
Oops, that introduces a bug because I forgot to increase the buffer size.
Let's try that again:

diff --git a/surf.c b/surf.c
index 53dda18..939a06f 100644
--- a/surf.c
+++ b/surf.c
@@ -843,7 +843,7 @@ newclient(void) {
 static void
 newwindow(Client *c, const Arg *arg, gboolean noembed) {
guint i = 0;
-   const char *cmd[12], *uri;
+   const char *cmd[14], *uri;
const Arg a = { .v = (void *)cmd };
char tmp[64];

@@ -865,6 +865,8 @@ newwindow(Client *c, const Arg *arg, gboolean noembed) {
cmd[i++] = "-s";
if(showxid)
cmd[i++] = "-x";
+   cmd[i++] = "-c";
+   cmd[i++] = cookiefile;
cmd[i++] = "--";
uri = arg->v ? (char *)arg->v : c->linkhover;
if(uri)



On Tue, Jul 9, 2013 at 12:44 AM, Jack Murray  wrote:

> diff --git a/surf.c b/surf.c
> index 53dda18..0f9b032 100644
> --- a/surf.c
> +++ b/surf.c
> @@ -865,6 +865,8 @@ newwindow(Client *c, const Arg *arg, gboolean noembed)
> {
> cmd[i++] = "-s";
> if(showxid)
> cmd[i++] = "-x";
> +   cmd[i++] = "-c";
> +   cmd[i++] = cookiefile;
> cmd[i++] = "--";
> uri = arg->v ? (char *)arg->v : c->linkhover;
> if(uri)
>
>