util.c
author Anselm R. Garbe <arg@suckless.org>
Wed, 30 May 2007 12:10:01 +0200
changeset 909 8a8b7a413e66
parent 906 e04a79845936
child 910 c13cb8c6b7a5
permissions -rw-r--r--
Jukka also belongs to Copyright holders after all he has contributed and done for dwm, updated -v as well
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
858
b797071ebbd4 yet another fix of copyright compactisition
Anselm R. Garbe <arg@suckless.org>
parents: 857
diff changeset
     1
/* © 2006-2007 Anselm R. Garbe <garbeam at gmail dot com>
b797071ebbd4 yet another fix of copyright compactisition
Anselm R. Garbe <arg@suckless.org>
parents: 857
diff changeset
     2
 * © 2006-2007 Sander van Dijk <a dot h dot vandijk at gmail dot com>
909
8a8b7a413e66 Jukka also belongs to Copyright holders after all he has contributed and done for dwm, updated -v as well
Anselm R. Garbe <arg@suckless.org>
parents: 906
diff changeset
     3
 * © 2006-2007 Jukka Salmi <jukka at salmi dot ch>
905
ca352bd2f8f7 added anydot to Copyright holders, because he contributed a lot recently
Anselm R. Garbe <arg@suckless.org>
parents: 858
diff changeset
     4
 * © 2007 Premysl Hruby <dfenze at gmail dot com>
906
e04a79845936 added nsz to copyright holders as well, because he did a lot recently
Anselm R. Garbe <arg@suckless.org>
parents: 905
diff changeset
     5
 * © 2007 Szabolcs Nagy <nszabolcs at gmail dot com>
857
453788ae925c making Copyright notices more compact
Anselm R. Garbe <arg@suckless.org>
parents: 823
diff changeset
     6
 * See LICENSE file for license details. */
76
4bd49f404f10 proceeded with cleaning up, sorting functions, etc
Anselm R. Garbe <garbeam@wmii.de>
parents: 75
diff changeset
     7
#include "dwm.h"
2
a79188fe4a40 added new stuff
Anselm R. Garbe <garbeam@wmii.de>
parents:
diff changeset
     8
#include <stdarg.h>
a79188fe4a40 added new stuff
Anselm R. Garbe <garbeam@wmii.de>
parents:
diff changeset
     9
#include <stdio.h>
a79188fe4a40 added new stuff
Anselm R. Garbe <garbeam@wmii.de>
parents:
diff changeset
    10
#include <stdlib.h>
5
e5018cae273f added several other stuff
Anselm R. Garbe <garbeam@wmii.de>
parents: 3
diff changeset
    11
#include <sys/wait.h>
e5018cae273f added several other stuff
Anselm R. Garbe <garbeam@wmii.de>
parents: 3
diff changeset
    12
#include <unistd.h>
e5018cae273f added several other stuff
Anselm R. Garbe <garbeam@wmii.de>
parents: 3
diff changeset
    13
84
052fe7498930 ordered variables in structs and source files alphabetically
Anselm R. Garbe <garbeam@wmii.de>
parents: 76
diff changeset
    14
/* extern */
76
4bd49f404f10 proceeded with cleaning up, sorting functions, etc
Anselm R. Garbe <garbeam@wmii.de>
parents: 75
diff changeset
    15
3
e969f3575b7a several new changes, made gridmenu working
Anselm R. Garbe <garbeam@wmii.de>
parents: 2
diff changeset
    16
void *
461
9d23330a5268 removed a bunch of lines through making function signatures more consistent with my style ( { does not belong to a new line, if function args are single-lined)
Anselm R. Garbe <arg@10kloc.org>
parents: 373
diff changeset
    17
emallocz(unsigned int size) {
3
e969f3575b7a several new changes, made gridmenu working
Anselm R. Garbe <garbeam@wmii.de>
parents: 2
diff changeset
    18
	void *res = calloc(1, size);
123
61490330e90a cleaned up code
arg@10ksloc.org
parents: 92
diff changeset
    19
3
e969f3575b7a several new changes, made gridmenu working
Anselm R. Garbe <garbeam@wmii.de>
parents: 2
diff changeset
    20
	if(!res)
325
58c09c533d3f removed badmalloc (thx for the pointer to Uriel)
Anselm R. Garbe <arg@10kloc.org>
parents: 317
diff changeset
    21
		eprint("fatal: could not malloc() %u bytes\n", size);
3
e969f3575b7a several new changes, made gridmenu working
Anselm R. Garbe <garbeam@wmii.de>
parents: 2
diff changeset
    22
	return res;
e969f3575b7a several new changes, made gridmenu working
Anselm R. Garbe <garbeam@wmii.de>
parents: 2
diff changeset
    23
}
e969f3575b7a several new changes, made gridmenu working
Anselm R. Garbe <garbeam@wmii.de>
parents: 2
diff changeset
    24
e969f3575b7a several new changes, made gridmenu working
Anselm R. Garbe <garbeam@wmii.de>
parents: 2
diff changeset
    25
void
461
9d23330a5268 removed a bunch of lines through making function signatures more consistent with my style ( { does not belong to a new line, if function args are single-lined)
Anselm R. Garbe <arg@10kloc.org>
parents: 373
diff changeset
    26
eprint(const char *errstr, ...) {
76
4bd49f404f10 proceeded with cleaning up, sorting functions, etc
Anselm R. Garbe <garbeam@wmii.de>
parents: 75
diff changeset
    27
	va_list ap;
123
61490330e90a cleaned up code
arg@10ksloc.org
parents: 92
diff changeset
    28
76
4bd49f404f10 proceeded with cleaning up, sorting functions, etc
Anselm R. Garbe <garbeam@wmii.de>
parents: 75
diff changeset
    29
	va_start(ap, errstr);
4bd49f404f10 proceeded with cleaning up, sorting functions, etc
Anselm R. Garbe <garbeam@wmii.de>
parents: 75
diff changeset
    30
	vfprintf(stderr, errstr, ap);
4bd49f404f10 proceeded with cleaning up, sorting functions, etc
Anselm R. Garbe <garbeam@wmii.de>
parents: 75
diff changeset
    31
	va_end(ap);
92
4bee0aa5b286 using EXIT_stuff in exit() now
Anselm R. Garbe <garbeam@wmii.de>
parents: 84
diff changeset
    32
	exit(EXIT_FAILURE);
76
4bd49f404f10 proceeded with cleaning up, sorting functions, etc
Anselm R. Garbe <garbeam@wmii.de>
parents: 75
diff changeset
    33
}
4bd49f404f10 proceeded with cleaning up, sorting functions, etc
Anselm R. Garbe <garbeam@wmii.de>
parents: 75
diff changeset
    34
4bd49f404f10 proceeded with cleaning up, sorting functions, etc
Anselm R. Garbe <garbeam@wmii.de>
parents: 75
diff changeset
    35
void
823
fb5cbf0bd923 replaced Arg union with const char *arg, seems cleaner to me, even if we need atoi() in some places
Anselm R. Garbe <arg@suckless.org>
parents: 813
diff changeset
    36
spawn(const char *arg) {
189
523df4a3c1c4 using execl now, argv changed, using cmd and const char defs directly in the KEYS struct
arg@10ksloc.org
parents: 187
diff changeset
    37
	static char *shell = NULL;
123
61490330e90a cleaned up code
arg@10ksloc.org
parents: 92
diff changeset
    38
189
523df4a3c1c4 using execl now, argv changed, using cmd and const char defs directly in the KEYS struct
arg@10ksloc.org
parents: 187
diff changeset
    39
	if(!shell && !(shell = getenv("SHELL")))
523df4a3c1c4 using execl now, argv changed, using cmd and const char defs directly in the KEYS struct
arg@10ksloc.org
parents: 187
diff changeset
    40
		shell = "/bin/sh";
823
fb5cbf0bd923 replaced Arg union with const char *arg, seems cleaner to me, even if we need atoi() in some places
Anselm R. Garbe <arg@suckless.org>
parents: 813
diff changeset
    41
	if(!arg)
5
e5018cae273f added several other stuff
Anselm R. Garbe <garbeam@wmii.de>
parents: 3
diff changeset
    42
		return;
471
740c4bfc3124 reviewed util.c
arg@mmvi
parents: 461
diff changeset
    43
	/* The double-fork construct avoids zombie processes and keeps the code
740c4bfc3124 reviewed util.c
arg@mmvi
parents: 461
diff changeset
    44
	 * clean from stupid signal handlers. */
5
e5018cae273f added several other stuff
Anselm R. Garbe <garbeam@wmii.de>
parents: 3
diff changeset
    45
	if(fork() == 0) {
e5018cae273f added several other stuff
Anselm R. Garbe <garbeam@wmii.de>
parents: 3
diff changeset
    46
		if(fork() == 0) {
e5018cae273f added several other stuff
Anselm R. Garbe <garbeam@wmii.de>
parents: 3
diff changeset
    47
			if(dpy)
e5018cae273f added several other stuff
Anselm R. Garbe <garbeam@wmii.de>
parents: 3
diff changeset
    48
				close(ConnectionNumber(dpy));
9
d567f430a81d fixed several stuff (gridwm gets better and better)
Anselm R. Garbe <garbeam@wmii.de>
parents: 6
diff changeset
    49
			setsid();
823
fb5cbf0bd923 replaced Arg union with const char *arg, seems cleaner to me, even if we need atoi() in some places
Anselm R. Garbe <arg@suckless.org>
parents: 813
diff changeset
    50
			execl(shell, shell, "-c", arg, (char *)NULL);
fb5cbf0bd923 replaced Arg union with const char *arg, seems cleaner to me, even if we need atoi() in some places
Anselm R. Garbe <arg@suckless.org>
parents: 813
diff changeset
    51
			fprintf(stderr, "dwm: execl '%s -c %s'", shell, arg);
5
e5018cae273f added several other stuff
Anselm R. Garbe <garbeam@wmii.de>
parents: 3
diff changeset
    52
			perror(" failed");
e5018cae273f added several other stuff
Anselm R. Garbe <garbeam@wmii.de>
parents: 3
diff changeset
    53
		}
138
c1185dc7a36e some cleanups/fixes inspired by Jukka Salmi's feedback
arg@10ksloc.org
parents: 123
diff changeset
    54
		exit(0);
5
e5018cae273f added several other stuff
Anselm R. Garbe <garbeam@wmii.de>
parents: 3
diff changeset
    55
	}
e5018cae273f added several other stuff
Anselm R. Garbe <garbeam@wmii.de>
parents: 3
diff changeset
    56
	wait(0);
e5018cae273f added several other stuff
Anselm R. Garbe <garbeam@wmii.de>
parents: 3
diff changeset
    57
}