From be4cffef39c4973ef3e0b286d3ef2e07d905e949 Mon Sep 17 00:00:00 2001 From: Aaron Marcher Date: Thu, 10 Aug 2017 20:30:52 +0200 Subject: [PATCH 01/16] Removed .gitignore from repository A file outside the repository (.git/info/exclude) can replace this and suits better for this usecase. --- .gitignore | 3 --- 1 file changed, 3 deletions(-) delete mode 100644 .gitignore diff --git a/.gitignore b/.gitignore deleted file mode 100644 index 484c6c0..0000000 --- a/.gitignore +++ /dev/null @@ -1,3 +0,0 @@ -slstatus -slstatus.o -config.h -- 2.20.1 From dad6dc63004b8bece431349011c7bc33eeefbd07 Mon Sep 17 00:00:00 2001 From: Aaron Marcher Date: Thu, 10 Aug 2017 20:32:14 +0200 Subject: [PATCH 02/16] Copyright sign (C) is not required in LICENSE According to https://opensource.org/licenses/ISC --- LICENSE | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/LICENSE b/LICENSE index 05d1377..2b04cde 100644 --- a/LICENSE +++ b/LICENSE @@ -1,7 +1,7 @@ ISC License -Copyright (C) 2016-2017 Aaron Marcher -Copyright (C) 2017 Laslo Hunhold +Copyright 2016-2017 Aaron Marcher +Copyright 2017 Laslo Hunhold Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted, provided that the above copyright notice -- 2.20.1 From b3eed601e5ae15528f78d2f115074fc71d434b31 Mon Sep 17 00:00:00 2001 From: Aaron Marcher Date: Thu, 10 Aug 2017 21:06:48 +0200 Subject: [PATCH 03/16] Moved contributors to LICENSE --- CONTRIBUTORS.md | 11 ----------- LICENSE | 10 ++++++++++ 2 files changed, 10 insertions(+), 11 deletions(-) delete mode 100644 CONTRIBUTORS.md diff --git a/CONTRIBUTORS.md b/CONTRIBUTORS.md deleted file mode 100644 index 610d4bf..0000000 --- a/CONTRIBUTORS.md +++ /dev/null @@ -1,11 +0,0 @@ -Contributors -============ - -Thanks you very much for your great help! - -- [Vlaix](https://github.com/Vlaix) -- [pfannkuckengesicht](https://github.com/pfannkuchengesicht) -- [sahne](https://github.com/sahne) -- [Ali H. Fardan](http://hexeract.info) -- [Quentin Rameau](https://fifth.space) -- [Mike Coddington](https://coddington.us) diff --git a/LICENSE b/LICENSE index 2b04cde..3068bd0 100644 --- a/LICENSE +++ b/LICENSE @@ -1,6 +1,16 @@ ISC License Copyright 2016-2017 Aaron Marcher + +Copyright 2016 Roy Freytag +Copyright 2016 Vincent Loupmon +Copyright 2016 Daniel Walter +Copyright 2016 Jody Leonard +Copyright 2016 Mike Coddington +Copyright 2016 Ali H. Fardan +Copyright 2016 parazyd +Copyright 2016-2017 Quentin Rameau +Copyright 2017 Tobias Stoeckmann Copyright 2017 Laslo Hunhold Permission to use, copy, modify, and/or distribute this software for any purpose -- 2.20.1 From 5134a480b5d51bcc5db57ecbf6348beafe130862 Mon Sep 17 00:00:00 2001 From: Aaron Marcher Date: Thu, 10 Aug 2017 21:08:53 +0200 Subject: [PATCH 04/16] Removed CONTRIBUTING.md --- CONTRIBUTING.md | 6 ------ 1 file changed, 6 deletions(-) delete mode 100644 CONTRIBUTING.md diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md deleted file mode 100644 index 1da8c0b..0000000 --- a/CONTRIBUTING.md +++ /dev/null @@ -1,6 +0,0 @@ -Contributing -============ - -If you want to contribute, please use [the suckless coding style](http://suckless.org/coding_style). - -Feel free to add yourself to [CONTRIBUTORS.md](CONTRIBUTORS.md) afterwards if you want. -- 2.20.1 From b6fb77c7ac1a2531f1fecaaf20a2f405a61a4928 Mon Sep 17 00:00:00 2001 From: Aaron Marcher Date: Thu, 10 Aug 2017 21:10:17 +0200 Subject: [PATCH 05/16] Removed TODO.md --- TODO.md | 0 1 file changed, 0 insertions(+), 0 deletions(-) delete mode 100644 TODO.md diff --git a/TODO.md b/TODO.md deleted file mode 100644 index e69de29..0000000 -- 2.20.1 From d35e518e394b706cb791e30d4fcf7bd0498217ee Mon Sep 17 00:00:00 2001 From: Aaron Marcher Date: Thu, 10 Aug 2017 21:49:37 +0200 Subject: [PATCH 06/16] New README in plain text --- README | 56 ++++++++++++++++++++++++++++++++++++++++ README.md | 76 ------------------------------------------------------- 2 files changed, 56 insertions(+), 76 deletions(-) create mode 100644 README delete mode 100644 README.md diff --git a/README b/README new file mode 100644 index 0000000..77659b4 --- /dev/null +++ b/README @@ -0,0 +1,56 @@ +slstatus - suckless status +========================== +slstatus is a suckless status monitor for window managers that use WM_NAME +(e.g. dwm) or stdin to fill the status bar. + + +Features +-------- +- Battery percentage/power/state +- CPU usage +- CPU frequency +- Custom shell commands +- Date and time +- Disk status (free storage, percentage, total storage and used storage) +- Available entropy +- Username/GID/UID +- Hostname +- IP address +- Kernel version +- Keyboard indicators +- Load average +- Number of files in a directory (hint: Maildir) +- Memory status (free memory, percentage, total memory and used memory) +- Swap status (free swap, percentage, total swap and used swap) +- Temperature +- Uptime +- Volume percentage (OSS/ALSA) +- WiFi signal percentage and ESSID + + +Requirements +------------ +In order to build slstatus you need the Xlib header files and optionally ALSA +for volume percentage. PulseAudio is not supported for various reasons. + + +Installation +------------ +Edit config.mk to match your local setup (slstatus is installed into the +/usr/local namespace by default). + +Afterwards enter the following command to build and install dmenu (if necessary +as root): + + make clean install + + +Running slstatus +---------------- +See the man page for details. + + +Configuration +------------- +slstatus can be customized by creating a custom config.h and (re)compiling the +source code. This keeps it fast, secure and simple. diff --git a/README.md b/README.md deleted file mode 100644 index 408477e..0000000 --- a/README.md +++ /dev/null @@ -1,76 +0,0 @@ -![slstatus](slstatus.png) - -**slstatus** is a suckless and lightweight status monitor for window managers that use WM_NAME as statusbar (e.g. DWM) or any other status bars if they support reading/piping from slstatus. It is written in pure C without any extern programs being executed and only reads from files most of the time. slstatus is meant to be a better alternative to Bash scripts (inefficient) and Conky (bloated and written in C++). - -If you write a bash script that shows system information in WM_NAME (or any other status bar), it executes a huge amount of external commands (top, free etc.) every few seconds. This results in high system resource usage. slstatus solves this problem by only using C libraries and/or reading from files in sysfs/procfs most of the time. - -Looking at the LOC (lines of code) of the [Conky project](https://github.com/brndnmtthws/conky), it is very interesting: *28346 lines C++, 219 lines Python and 110 lines Lua*. slstatus currently has about **800 lines of clean documented C code** and even includes additional possibilities as it can be customized and extended very easily. Configuring it by customizing the config.h (C header) file is very secure and fast as no config files are parsed at runtime. - -The following information is included: - -- Battery percentage/power/state -- CPU usage (in percent) -- CPU frequency (in MHz) -- Custom shell commands -- Date and time -- Disk[s] status (free storage, percentage, total storage and used storage) -- Available entropy -- Username/GID/UID -- Hostname -- IP addresses -- Kernel version -- Keyboard indicators -- Load averages -- Number of files in a directory (hint: Maildir) -- Memory status (free memory, percentage, total memory and used memory) -- Swap status (free swap, percentage, total swap and used swap) -- Temperature -- Uptime -- Volume percentage (OSS/ALSA) -- WiFi signal percentage and ESSID - -Multiple entries per function (e.g. multiple batteries) are supported and everything can be reordered and customized via a C header file (similar to other suckless programs). - -## Usage - -### Installation - -Be sure you satisfy the dependencies: X11 and optionally ALSA (for volume percentage, I will *not* support PulseAudio). -Also you should have basic development tools like a C compiler and GNU make installed. -Then copy config.def.h to config.h and customize it to your needs. -(Re)Compile (and install) it (after modifications): - - $ make clean all - # make install - -### Starting - -If you use DWM or any other window manager that uses WM_NAME, write the following code to ~/.xinitrc (or any other initialization script) to start slstatus automatically: - - slstatus -d - -If you use any other status bar or window manager you will have to figure it out yourself. Something like this could fit your requirements: - - slstatus -o | other_status_bar & - -### Specific function quirks - -- Volume percentage - -If there is no `/dev/mixer` on your system and you use ALSA, it means you have to load the OSS compatibility module by issuing: - -``` -# modprobe snd-pcm-oss -``` - -## Contributing - -Hunt FIXME's in the code or do WTF you want! If it is useful, I will merge. - -People who contributed are listed in [CONTRIBUTORS.md](CONTRIBUTORS.md). Please add yourself to this list afterwards. - -For detailed information about coding style and restrictions see [CONTRIBUTING.md](CONTRIBUTING.md). - -## License - -See [LICENSE](LICENSE). -- 2.20.1 From 8dad4910bfc75aa1085a20de95ceee951588deae Mon Sep 17 00:00:00 2001 From: Laslo Hunhold Date: Thu, 10 Aug 2017 21:32:10 +0200 Subject: [PATCH 07/16] Refactor build system --- Makefile | 22 +++++++++++----------- config.mk | 22 ++++++++++++---------- 2 files changed, 23 insertions(+), 21 deletions(-) diff --git a/Makefile b/Makefile index 9e1d0d5..dbe636e 100644 --- a/Makefile +++ b/Makefile @@ -1,11 +1,13 @@ # See LICENSE file for copyright and license details. +# slstatus - suckless status monitor +.POSIX: include config.mk all: slstatus slstatus: slstatus.c config.h config.mk - ${CC} ${CFLAGS} -o $@ slstatus.c ${LDFLAGS} + $(CC) -o $@ $(CFLAGS) $(CPPFLAGS) $(LDFLAGS) slstatus.c $(LDLIBS) config.h: cp config.def.h $@ @@ -14,15 +16,13 @@ clean: rm -f slstatus install: all - mkdir -p ${DESTDIR}${PREFIX}/bin - cp -f slstatus ${DESTDIR}${PREFIX}/bin - chmod 755 ${DESTDIR}${PREFIX}/bin/slstatus - mkdir -p ${DESTDIR}${MANPREFIX}/man1 - cp -f slstatus.1 ${DESTDIR}${MANPREFIX}/man1 - chmod 644 ${DESTDIR}${MANPREFIX}/man1/slstatus.1 + mkdir -p "$(DESTDIR)$(PREFIX)/bin" + cp -f slstatus "$(DESTDIR)$(PREFIX)/bin" + chmod 755 "$(DESTDIR)$(PREFIX)/bin/slstatus" + mkdir -p "$(DESTDIR)$(MANPREFIX)/man1" + cp -f slstatus.1 "$(DESTDIR)$(MANPREFIX)/man1" + chmod 644 "$(DESTDIR)$(MANPREFIX)/man1/slstatus.1" uninstall: - rm -f ${DESTDIR}${PREFIX}/bin/slstatus - rm -f ${DESTDIR}${MANPREFIX}/man1/slstatus.1 - -.PHONY: all clean install uninstall + rm -f "$(DESTDIR)$(PREFIX)/bin/slstatus" + rm -f "$(DESTDIR)$(MANPREFIX)/man1/slstatus.1" diff --git a/config.mk b/config.mk index 67fd628..dad0684 100644 --- a/config.mk +++ b/config.mk @@ -1,18 +1,20 @@ -# See LICENSE file for copyright and license details. +# slstatus version +VERSION = 0 +# Customize below to fit your system + +#paths PREFIX = /usr/local -MANPREFIX = ${PREFIX}/share/man +MANPREFIX = ${PREFIX}/man X11INC = /usr/X11R6/include X11LIB = /usr/X11R6/lib -INCS = -I/usr/include -I${X11INC} -LIBS = -L/usr/lib -lc -L${X11LIB} -lX11 - -CPPFLAGS = -D_GNU_SOURCE -# -Wno-unused-function for routines not activated by user -CFLAGS = -std=c99 -pedantic -Wno-unused-function -Wall -Wextra -Os -fstack-protector-strong -fstack-check -fPIE ${INCS} ${CPPFLAGS} -LDFLAGS = ${LIBS} +# flags +CPPFLAGS = -I$(X11INC) -DVERSION=\"$(VERSION)\" -D_DEFAULT_SOURCE +CFLAGS = -std=c99 -pedantic -Wall -Wextra -Wno-unused -Os +LDFLAGS = -L$(X11LIB) -s +LDLIBS = -lX11 +# compiler and linker CC = cc -LD = ld -- 2.20.1 From 958c34052a13b964f5fd1e748c18796d155af7db Mon Sep 17 00:00:00 2001 From: Laslo Hunhold Date: Thu, 10 Aug 2017 21:36:29 +0200 Subject: [PATCH 08/16] Convert slstatus.1 to mandoc, simplify it and remove -h We also change the semantics of -v to only return the version information. There is now no need for usage() to exit with anything other than 1. --- slstatus.1 | 68 +++++++++++++++++++++++++++++------------------------- slstatus.c | 14 +++++------ 2 files changed, 42 insertions(+), 40 deletions(-) diff --git a/slstatus.1 b/slstatus.1 index de5d0e8..bc27271 100644 --- a/slstatus.1 +++ b/slstatus.1 @@ -1,34 +1,38 @@ -.TH SLSTATUS 1 slstatus -.SH NAME -slstatus \- suckless status -.SH SYNOPSIS -.B slstatus -.RB [ \-dhov ] -.SH DESCRIPTION -slstatus is a suckless and lightweight status monitor for window managers that use WM_NAME as statusbar (e.g. DWM) or any other status bars as long as they support reading from stdin. It is written in pure C without any extern programs being executed and only reads from files most of the time. slstatus is meant to be a better alternative to Bash scripts (inefficient) and Conky (bloated and written in C++). -.SH OPTIONS -.TP -.B \-d -run slstatus as daemon in background -.TP -.B \-h -display help -.TP -.B \-o -output continuously to console instead of WM_NAME -.TP -.B \-n -output to console once and exit -.TP -.B \-v -display version and copyright details -.SH CUSTOMIZATION -.B slstatus +.Dd 2017-08-10 +.Dt SLSTATUS 1 +.Os suckless.org +.Sh NAME +.Nm slstatus +.Nd suckless status monitor +.Sh SYNOPSIS +.Nm +.Oo +.Op Fl v +| +.Oo +.Op Fl d +.Op Fl o | n +.Oc +.Oc +.Sh DESCRIPTION +.Nm +is a suckless status monitor for window managers that use WM_NAME (e.g. dwm) or +stdin to fill the status bar. +By default, +.Nm +outputs to WM_NAME. +.Sh OPTIONS +.Bl -tag -width Ds +.It Fl d +Daemonize. +.It Fl n +Write to stdout once and exit. +.It Fl o +Write to stdout continuously. +.It Fl v +Write version information to stdout and exit. +.El +.Sh CUSTOMIZATION +.Nm can be customized by creating a custom config.h and (re)compiling the source code. This keeps it fast, secure and simple. -.SH AUTHORS -See the LICENSE file for the authors. -.SH LICENSE -See the LICENSE file for the terms of redistribution. -.SH BUGS -See the FIXME's in the code. diff --git a/slstatus.c b/slstatus.c index 5564771..9a4831d 100644 --- a/slstatus.c +++ b/slstatus.c @@ -70,7 +70,7 @@ static const char *vol_perc(const char *card); static const char *wifi_perc(const char *iface); static const char *wifi_essid(const char *iface); static void sighandler(const int signo); -static void usage(const int eval); +static void usage(void); char *argv0; static unsigned short int delay = 0; @@ -838,10 +838,10 @@ sighandler(const int signo) } static void -usage(const int eval) +usage(void) { fprintf(stderr, "usage: %s [-d] [-o] [-n] [-v] [-h]\n", argv0); - exit(eval); + exit(1); } int @@ -865,16 +865,14 @@ main(int argc, char *argv[]) nflag = 1; break; case 'v': - printf("slstatus (C) 2016-2017 slstatus engineers\n"); + printf("slstatus-"VERSION"\n"); return 0; - case 'h': - usage(0); default: - usage(1); + usage(); } ARGEND if ((dflag && oflag) || (dflag && nflag) || (oflag && nflag)) { - usage(1); + usage(); } if (dflag && daemon(1, 1) < 0) { err(1, "daemon"); -- 2.20.1 From 4d33c360141952f6071d19c34c0395ad2ff668a1 Mon Sep 17 00:00:00 2001 From: Laslo Hunhold Date: Thu, 10 Aug 2017 21:56:06 +0200 Subject: [PATCH 09/16] Remove d- and v-flags d-flag: There's no need for that, use the &-operator or fork+exec in a non-shell-context. In the latter case you get the PID for free. v-flag: If you want to find out which version of a package is installed, consult your package manager. That's his job. --- config.mk | 2 +- slstatus.1 | 11 ----------- slstatus.c | 15 +++------------ 3 files changed, 4 insertions(+), 24 deletions(-) diff --git a/config.mk b/config.mk index dad0684..f27a0a9 100644 --- a/config.mk +++ b/config.mk @@ -11,7 +11,7 @@ X11INC = /usr/X11R6/include X11LIB = /usr/X11R6/lib # flags -CPPFLAGS = -I$(X11INC) -DVERSION=\"$(VERSION)\" -D_DEFAULT_SOURCE +CPPFLAGS = -I$(X11INC) -D_DEFAULT_SOURCE CFLAGS = -std=c99 -pedantic -Wall -Wextra -Wno-unused -Os LDFLAGS = -L$(X11LIB) -s LDLIBS = -lX11 diff --git a/slstatus.1 b/slstatus.1 index bc27271..d29f197 100644 --- a/slstatus.1 +++ b/slstatus.1 @@ -6,14 +6,7 @@ .Nd suckless status monitor .Sh SYNOPSIS .Nm -.Oo -.Op Fl v -| -.Oo -.Op Fl d .Op Fl o | n -.Oc -.Oc .Sh DESCRIPTION .Nm is a suckless status monitor for window managers that use WM_NAME (e.g. dwm) or @@ -23,14 +16,10 @@ By default, outputs to WM_NAME. .Sh OPTIONS .Bl -tag -width Ds -.It Fl d -Daemonize. .It Fl n Write to stdout once and exit. .It Fl o Write to stdout continuously. -.It Fl v -Write version information to stdout and exit. .El .Sh CUSTOMIZATION .Nm diff --git a/slstatus.c b/slstatus.c index 9a4831d..b31f21d 100644 --- a/slstatus.c +++ b/slstatus.c @@ -75,7 +75,7 @@ static void usage(void); char *argv0; static unsigned short int delay = 0; static unsigned short int done; -static unsigned short int dflag, oflag, nflag; +static unsigned short int oflag, nflag; static Display *dpy; #include "config.h" @@ -840,7 +840,7 @@ sighandler(const int signo) static void usage(void) { - fprintf(stderr, "usage: %s [-d] [-o] [-n] [-v] [-h]\n", argv0); + fprintf(stderr, "usage: %s [-o | -n]\n", argv0); exit(1); } @@ -855,28 +855,19 @@ main(int argc, char *argv[]) size_t len; ARGBEGIN { - case 'd': - dflag = 1; - break; case 'o': oflag = 1; break; case 'n': nflag = 1; break; - case 'v': - printf("slstatus-"VERSION"\n"); - return 0; default: usage(); } ARGEND - if ((dflag && oflag) || (dflag && nflag) || (oflag && nflag)) { + if (oflag && nflag) { usage(); } - if (dflag && daemon(1, 1) < 0) { - err(1, "daemon"); - } memset(&act, 0, sizeof(act)); act.sa_handler = sighandler; -- 2.20.1 From c9d47405f4ab3cf99d7d2116fbb7622c02a27e5e Mon Sep 17 00:00:00 2001 From: Laslo Hunhold Date: Thu, 10 Aug 2017 22:22:39 +0200 Subject: [PATCH 10/16] Reduce -o | -n to -s What we really want is to either output to WM_NAME or stdout. If we want just one single line, we do slstatus | head -n 1. --- slstatus.1 | 8 +++----- slstatus.c | 31 ++++++++++--------------------- 2 files changed, 13 insertions(+), 26 deletions(-) diff --git a/slstatus.1 b/slstatus.1 index d29f197..f9ef3ad 100644 --- a/slstatus.1 +++ b/slstatus.1 @@ -6,7 +6,7 @@ .Nd suckless status monitor .Sh SYNOPSIS .Nm -.Op Fl o | n +.Op Fl s .Sh DESCRIPTION .Nm is a suckless status monitor for window managers that use WM_NAME (e.g. dwm) or @@ -16,10 +16,8 @@ By default, outputs to WM_NAME. .Sh OPTIONS .Bl -tag -width Ds -.It Fl n -Write to stdout once and exit. -.It Fl o -Write to stdout continuously. +.It Fl s +Write to stdout instead of WM_NAME. .El .Sh CUSTOMIZATION .Nm diff --git a/slstatus.c b/slstatus.c index b31f21d..4b6770b 100644 --- a/slstatus.c +++ b/slstatus.c @@ -75,7 +75,6 @@ static void usage(void); char *argv0; static unsigned short int delay = 0; static unsigned short int done; -static unsigned short int oflag, nflag; static Display *dpy; #include "config.h" @@ -840,41 +839,34 @@ sighandler(const int signo) static void usage(void) { - fprintf(stderr, "usage: %s [-o | -n]\n", argv0); + fprintf(stderr, "usage: %s [-s]\n", argv0); exit(1); } int main(int argc, char *argv[]) { - unsigned short int i; - char status_string[MAXLEN]; - char *element; struct arg argument; struct sigaction act; - size_t len; + size_t i, len; + int sflag = 0; + char status_string[MAXLEN]; + char *element; ARGBEGIN { - case 'o': - oflag = 1; - break; - case 'n': - nflag = 1; + case 's': + sflag = 1; break; default: usage(); } ARGEND - if (oflag && nflag) { - usage(); - } - memset(&act, 0, sizeof(act)); act.sa_handler = sighandler; sigaction(SIGINT, &act, 0); sigaction(SIGTERM, &act, 0); - if (!oflag) { + if (!sflag) { dpy = XOpenDisplay(NULL); } @@ -896,11 +888,8 @@ main(int argc, char *argv[]) } } - if (oflag) { - printf("%s\n", status_string); - } else if (nflag) { + if (sflag) { printf("%s\n", status_string); - done = 1; } else { XStoreName(dpy, DefaultRootWindow(dpy), status_string); XSync(dpy, False); @@ -915,7 +904,7 @@ main(int argc, char *argv[]) } } - if (!oflag) { + if (!sflag) { XStoreName(dpy, DefaultRootWindow(dpy), NULL); XCloseDisplay(dpy); } -- 2.20.1 From 00ce7a746ab0630e7d3b84288307435c6d62610a Mon Sep 17 00:00:00 2001 From: Laslo Hunhold Date: Thu, 10 Aug 2017 22:23:55 +0200 Subject: [PATCH 11/16] Print usage() when we are left with arguments --- slstatus.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/slstatus.c b/slstatus.c index 4b6770b..052af87 100644 --- a/slstatus.c +++ b/slstatus.c @@ -861,6 +861,10 @@ main(int argc, char *argv[]) usage(); } ARGEND + if (argc) { + usage(); + } + memset(&act, 0, sizeof(act)); act.sa_handler = sighandler; sigaction(SIGINT, &act, 0); -- 2.20.1 From 6b8384ef2f7328580425ac2cc75e0c65a04aa942 Mon Sep 17 00:00:00 2001 From: Aaron Marcher Date: Thu, 10 Aug 2017 22:43:56 +0200 Subject: [PATCH 12/16] Reformatted LICENSE to fit 75 character width --- LICENSE | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/LICENSE b/LICENSE index 3068bd0..4ac0402 100644 --- a/LICENSE +++ b/LICENSE @@ -13,14 +13,14 @@ Copyright 2016-2017 Quentin Rameau Copyright 2017 Tobias Stoeckmann Copyright 2017 Laslo Hunhold -Permission to use, copy, modify, and/or distribute this software for any purpose -with or without fee is hereby granted, provided that the above copyright notice -and this permission notice appear in all copies. +Permission to use, copy, modify, and/or distribute this software for any +purpose with or without fee is hereby granted, provided that the above +copyright notice and this permission notice appear in all copies. -THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH -REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND -FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, -INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS -OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER -TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF -THIS SOFTWARE. +THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES +WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF +MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR +ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES +WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN +ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF +OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. -- 2.20.1 From 178c23e3d2b75a99010987bf3e17a82ff7b83b6a Mon Sep 17 00:00:00 2001 From: Laslo Hunhold Date: Fri, 11 Aug 2017 00:17:49 +0200 Subject: [PATCH 13/16] Refactor battery_state() --- slstatus.c | 29 +++++++++++++++++------------ 1 file changed, 17 insertions(+), 12 deletions(-) diff --git a/slstatus.c b/slstatus.c index 052af87..c4a9e92 100644 --- a/slstatus.c +++ b/slstatus.c @@ -142,10 +142,19 @@ battery_power(const char *bat) static const char * battery_state(const char *bat) { - char path[PATH_MAX]; - char state[12]; FILE *fp; + struct { + char *state; + char *symbol; + } map[] = { + { "Charging", "+" }, + { "Discharging", "-" }, + { "Full", "=" }, + { "Unknown", "/" }, + }; + size_t i; int n; + char path[PATH_MAX], state[12]; snprintf(path, sizeof(path), "%s%s%s", "/sys/class/power_supply/", bat, "/status"); fp = fopen(path, "r"); @@ -158,17 +167,13 @@ battery_state(const char *bat) if (n != 1) return UNKNOWN_STR; - if (strcmp(state, "Charging") == 0) { - return "+"; - } else if (strcmp(state, "Discharging") == 0) { - return "-"; - } else if (strcmp(state, "Full") == 0) { - return "="; - } else if (strcmp(state, "Unknown") == 0) { - return "/"; - } else { - return "?"; + for (i = 0; i < sizeof(map) / sizeof(*map); i++) { + if (!strcmp(map[i].state, state)) { + break; + } } + + return (i == sizeof(map) / sizeof(*map)) ? "?" : map[i].symbol; } static const char * -- 2.20.1 From 1814061396ef5855e2d1815665fc32e0902ccbe9 Mon Sep 17 00:00:00 2001 From: Laslo Hunhold Date: Fri, 11 Aug 2017 13:39:19 +0200 Subject: [PATCH 14/16] Add and use LEN() macro --- slstatus.c | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/slstatus.c b/slstatus.c index c4a9e92..596d6a6 100644 --- a/slstatus.c +++ b/slstatus.c @@ -28,6 +28,8 @@ #include "arg.h" +#define LEN(x) (sizeof (x) / sizeof *(x)) + struct arg { const char *(*func)(); const char *fmt; @@ -167,13 +169,13 @@ battery_state(const char *bat) if (n != 1) return UNKNOWN_STR; - for (i = 0; i < sizeof(map) / sizeof(*map); i++) { + for (i = 0; i < LEN(map); i++) { if (!strcmp(map[i].state, state)) { break; } } - return (i == sizeof(map) / sizeof(*map)) ? "?" : map[i].symbol; + return (i == LEN(map)) ? "?" : map[i].symbol; } static const char * @@ -745,7 +747,7 @@ vol_perc(const char *card) close(afd); return UNKNOWN_STR; } - for (i = 0; i < (sizeof(vnames) / sizeof((vnames[0]))); i++) { + for (i = 0; i < LEN(vnames); i++) { if (devmask & (1 << i) && !strcmp("vol", vnames[i])) { if (ioctl(afd, MIXER_READ(i), &v) == -1) { warn("vol_perc: ioctl"); @@ -884,8 +886,7 @@ main(int argc, char *argv[]) while (!done) { status_string[0] = '\0'; - for (element = status_string, i = len = 0; - i < sizeof(args) / sizeof(args[0]); + for (element = status_string, i = len = 0; i < LEN(args); ++i, element += len) { argument = args[i]; len = snprintf(element, sizeof(status_string)-1 - len, -- 2.20.1 From 6f011743921db04e5513df45c5ac4b2c752d52de Mon Sep 17 00:00:00 2001 From: Aaron Marcher Date: Fri, 11 Aug 2017 14:33:02 +0200 Subject: [PATCH 15/16] Fixed possible NULL-deref and removed unnecessary XOpenDisplay() - Added a check for the return value of XOpenDisplay() in main(). This fixes a possible NULL-deref. - Removed unnsecessary XOpenDisplay and XCloseDisplay from keyboard_indicators(). The ones in main() are sufficent. --- slstatus.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/slstatus.c b/slstatus.c index 596d6a6..47bd4c9 100644 --- a/slstatus.c +++ b/slstatus.c @@ -380,10 +380,9 @@ kernel_release(void) static const char * keyboard_indicators(void) { - Display *dpy = XOpenDisplay(NULL); XKeyboardState state; + XGetKeyboardControl(dpy, &state); - XCloseDisplay(dpy); switch (state.led_mask) { case 1: @@ -879,6 +878,10 @@ main(int argc, char *argv[]) if (!sflag) { dpy = XOpenDisplay(NULL); + if (!dpy) { + fprintf(stderr, "slstatus: cannot open display"); + exit(1); + } } setlocale(LC_ALL, ""); -- 2.20.1 From 2c114ec2b506e32d54586110c710c78ef156e066 Mon Sep 17 00:00:00 2001 From: "Ali H. Fardan" Date: Sat, 12 Aug 2017 07:01:13 +0300 Subject: [PATCH 16/16] keyboard_indicators: fix segfault when -s is specified --- slstatus.c | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/slstatus.c b/slstatus.c index 47bd4c9..aa05cd1 100644 --- a/slstatus.c +++ b/slstatus.c @@ -380,9 +380,15 @@ kernel_release(void) static const char * keyboard_indicators(void) { + Display *dpy = XOpenDisplay(NULL); XKeyboardState state; + if (dpy == NULL) { + warnx("XOpenDisplay failed"); + return UNKNOWN_STR; + } XGetKeyboardControl(dpy, &state); + XCloseDisplay(dpy); switch (state.led_mask) { case 1: -- 2.20.1