[tpwd] Fix segfault when exactly one argument given
[tinyapps.git] / xgetclass.c
blobdc95d7505c97b986f847bf40000db8f8271bb050
1 /*
2 * Prints window's ClassHint
3 * Copyright 2005,2006 by Michal Nazarewicz <mina86@mina86.com>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 3 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, see <http://www.gnu.org/licenses/>.
18 * This is part of Tiny Applications Collection
19 * -> http://tinyapps.sourceforge.net/
23 * Compilation:
24 * <CC> -O2 $CFLAGS -I<X11_INC_DIR> -L<X11_LIB_DIR> -lX11 \
25 * -o xgetclass xgetclass.c
26 * Where:
27 * <CC> - C Compiler (eg. gcc)
28 * <X11_INC_DIR> - Path to X11's header files (eg. /usr/X11R6/include)
29 * <X11_LIB_DIR> - Path to X11's libraries (eg. /usr/X11R6/lib)
33 * Invocation: xgetclass <wnd-id>
34 * Result: Prints aplication name and class (in separate lines) of
35 * window with ID given as an argument.
39 #include <stdio.h>
40 #include <stdlib.h>
41 #include <X11/Xlib.h>
42 #include <X11/Xutil.h>
45 int main(int argc, char **argv) {
46 char *c;
47 unsigned long wnd;
48 Display *display;
49 XClassHint class;
51 /* Get executable name */
52 for (c = *argv; *c; ++c) {
53 if (*c=='/' && c[1]) {
54 *argv = ++c;
58 /* Invalid number of arguments */
59 if (argc!=2) {
60 fprintf(stderr, "usage: %s <wnd-id>\n", *argv);
61 return 1;
64 /* Parse win id */
65 wnd = strtoul(argv[1], &c, 0);
66 if (*c) {
67 fprintf(stderr, "usage: %s <wnd-id>\n", *argv);
68 return 1;
71 /* Open display */
72 c = XDisplayName(0);
73 if (!(display = XOpenDisplay(c))) {
74 fprintf(stderr, "%s: can't open display %s\n", *argv, c);
75 return 1;
78 /* Get class */
79 XGetClassHint(display, wnd, &class);
81 /* Print */
82 printf("%s\n%s\n", class.res_name, class.res_class);
83 XFree(class.res_name);
84 XFree(class.res_class);
85 return 0;