sites

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs

commit 28dbe0efac4bcf43f674c9920f95f6e7e90051c1
parent 097b82ba071d69ba37b2356c922495a81e0b9e38
Author: ComputerNerd <theprogrammernerd@gmail.com>
Date:   Sun, 14 Jun 2015 14:59:21 -0500

Improvements to barM.c:
* Made it more secure by using functions that are aware of the buffer
  size and by checking return values.
* Remove the need to spawn a process every second to run uname and get
  free ram.
* Remove the need for looping in bash -- there is no advantage of using
  a bash loop and running the process every second as opposed to making
  the program loop.
* Cache uname at the beginning of the program. There is no reason to
  update this every second

Diffstat:
Mdwm.suckless.org/dwmstatus/barM.c | 111++++++++++++++++++++++++++++++++++++++-----------------------------------------
1 file changed, 54 insertions(+), 57 deletions(-)

diff --git a/dwm.suckless.org/dwmstatus/barM.c b/dwm.suckless.org/dwmstatus/barM.c @@ -1,16 +1,16 @@ /* - * Copyright (C) 2014,2015 levi0x0 + * Copyright (C) 2014,2015 levi0x0 with enhancements by ProgrammerNerd * * barM (bar_monitor or BarMonitor) is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; either version 2 * of the License, or (at your option) any later version. * - * This is a new version of bar monitor, less lines of code more effective. + * This is a new version of bar monitor, even less lines of code more effective. * * Read main() to configure your new status Bar. * - * compile: gcc -o barM barM.c -lX11 + * compile: gcc -o barM barM.c -O2 -s -lX11 * * mv barM /usr/local/bin/ */ @@ -21,67 +21,78 @@ #include <string.h> #include <stdarg.h> #include <X11/Xlib.h> +#include <sys/utsname.h> +#include <sys/sysinfo.h> /* * Put this in your .xinitrc file: * - * while true;do - * $(barM) - * done & + * barM& * */ -#define VERSION "0.11" -#define TIME_FORMAT "(%H:%M) (%d-%m-%Y)" +#define VERSION "0.12" +#define TIME_FORMAT "%H:%M) (%d-%m-%Y" #define MAXSTR 1024 -static char status[MAXSTR]; - -/*append here your commands*/ -char *commands[] = {"uname", - "free -mh | awk 'NR==2{print $3\"/\"$2}'" +static const char * date(void); +static const char * getuname(void); +static const char * ram(void); +static void XSetRoot(const char *name); +/*Append here your functions.*/ +static const char*(*const functab[])(void)={ + ram,date }; -/* will append the buffer to status*/ -void sprint(char *format, ...) { - va_list li; - static char s[MAXSTR]; - va_start(li, format); - vsprintf(s, format, li); - va_end(li); - - strcat(status, s); +int main(void){ + char status[MAXSTR]; + /* It is foolish to repeatedly update uname. */ + int ret; + {struct utsname u; + if(uname(&u)){ + perror("uname failed"); + return 1; + } + ret=snprintf(status,sizeof(status),"(%s %s %s) ",u.sysname,u.nodename,u.release);} + char*off=status+ret; + if(off>=(status+MAXSTR)){ + XSetRoot(status); + return 1;/*This should not happen*/ + } + for(;;){ + int left=sizeof(status)-ret,i; + char*sta=off; + for(i = 0; i<sizeof(functab)/sizeof(functab[0]); ++i ) { + int ret=snprintf(sta,left,"(%s) ",functab[i]()); + sta+=ret; + left-=ret; + if(sta>=(status+MAXSTR))/*When snprintf has to resort to truncating a string it will return the length as if it were not truncated.*/ + break; + } + XSetRoot(status); + sleep(1); + } + return 0; } -/* returen the date*/ -char * date(void) { +/* Returns the date*/ +static const char * date(void){ static char date[MAXSTR]; - time_t now = time(0); strftime(date, MAXSTR, TIME_FORMAT, localtime(&now)); return date; } - - -/* open a pipe for a new coomand and return the output*/ -char * spawn(char *c) { - FILE *proc; - static char buffer[MAXSTR]; - - if ((proc = popen(c, "r")) == NULL ) { - fprintf(stderr, "[barM] Failed to execute the command.\n"); - exit(1);; - } - - fscanf(proc, "%[^\n]", buffer); - pclose(proc); - - return buffer; +/* Returns a string that contains the amount of free and available ram in megabytes*/ +static const char * ram(void){ + static char ram[MAXSTR]; + struct sysinfo s; + sysinfo(&s); + snprintf(ram,sizeof(ram),"%.1fM,%.1fM",((double)(s.totalram-s.freeram))/1048576.,((double)s.totalram)/1048576.); + return ram; } - -void XSetRoot(char *name) { +static void XSetRoot(const char *name){ Display *display; if (( display = XOpenDisplay(0x0)) == NULL ) { @@ -95,17 +106,3 @@ void XSetRoot(char *name) { XCloseDisplay(display); } -int main(int argc, char **argv) { - int i = 0; - for(i = 0; commands[i]; i++ ) { - sprint("(%s) ", spawn(commands[i])); - } - - sprint("%s", date()); - XSetRoot(status); - - /* sleep by default you dont need to add it to your bash*/ - sleep(1); - - return 0; -}