From d56480c377ac042979ea47525982220f36e91dee Mon Sep 17 00:00:00 2001 From: Panagiotis Dimopoulos Date: Thu, 5 Apr 2018 12:46:04 +0300 Subject: [PATCH] Fix bugs. --- wmArchUp/CHANGES | 6 ++++ wmArchUp/Makefile | 2 +- wmArchUp/wmarchup.c | 68 +++++++++++++++++++++++++++++++++++++++++---- 3 files changed, 69 insertions(+), 7 deletions(-) diff --git a/wmArchUp/CHANGES b/wmArchUp/CHANGES index dbb71d1..4877bc9 100644 --- a/wmArchUp/CHANGES +++ b/wmArchUp/CHANGES @@ -1,3 +1,9 @@ +2018-04-04 Panagiotis A. Dimopoulos + + - Release 1.2 + - Fix a bug when left click is not working in some cases + - Fix a bug leaving zombie process when checking for new updates + 2018-04-02 Panagiotis A. Dimopoulos - Release 1.1 diff --git a/wmArchUp/Makefile b/wmArchUp/Makefile index 803e267..818fa3c 100644 --- a/wmArchUp/Makefile +++ b/wmArchUp/Makefile @@ -1,7 +1,7 @@ CC=gcc INSTALL=install PREFIX=/usr/local -CFLAGS+=-Wall -O3 `pkg-config --cflags dockapp` +CFLAGS+=-Wall -Wextra -O3 `pkg-config --cflags dockapp` LIBS=`pkg-config --libs dockapp` wmarchup: wmarchup.o diff --git a/wmArchUp/wmarchup.c b/wmArchUp/wmarchup.c index 71b38cc..69c8045 100644 --- a/wmArchUp/wmarchup.c +++ b/wmArchUp/wmarchup.c @@ -1,6 +1,10 @@ /* Also includes Xlib, Xresources, XPM, stdlib and stdio */ #include #include +#include +#include +#include +#include /* Include the pixmap to use */ #include "archlinux.xpm" @@ -10,8 +14,9 @@ #define TRUE 1 #define FALSE 0 #define MAX 256 +#define MAXPATHLEN 4096 #define CHK_INTERVAL 600 -#define WMARCHUP_VER "1.1" +#define WMARCHUP_VER "1.2" #define VERSION "wmArchUp version " WMARCHUP_VER /* @@ -21,6 +26,7 @@ void destroy(void); void check_for_updates(); void buttonrelease(int button, int state, int x, int y); void update(); +char *get_update_script(); /* * Global variables @@ -29,6 +35,7 @@ Pixmap arch, arch_mask, arch_bw, arch_bw_mask, checking, checking_mask; unsigned short height, width; unsigned int check_interval = CHK_INTERVAL; int updates_available = FALSE; +char *script; /* * M A I N @@ -37,6 +44,9 @@ int updates_available = FALSE; int main(int argc, char **argv) { + /* Find bash update script */ + script = get_update_script(); + /* Set callbacks for events */ DACallbacks eventCallbacks = { destroy, /* destroy */ @@ -126,8 +136,7 @@ update() if (updates_available == TRUE) { XSelectInput(DAGetDisplay(NULL), DAGetWindow(), NoEventMask); - char *update_script = "./arch_update.sh"; - int ret = system(update_script); + int ret = system(script); if (WEXITSTATUS(ret) == 0) { updates_available = FALSE; @@ -144,7 +153,6 @@ void check_for_updates() { XSelectInput(DAGetDisplay(NULL), DAGetWindow(), NoEventMask); - FILE *fp; char res[MAX]; @@ -152,9 +160,8 @@ check_for_updates() DASetPixmap(checking); /* Read output from command */ - fp = popen("checkupdates", "r"); + FILE *fp = popen("checkupdates", "r"); if (fgets(res, MAX, fp) != NULL) { - fclose(fp); updates_available = TRUE; DASetShape(arch_mask); DASetPixmap(arch); @@ -164,6 +171,10 @@ check_for_updates() DASetPixmap(arch_bw); } + if (pclose(fp) != 0) { + fprintf(stderr, " Error: Failed to close command stream \n"); + } + XSelectInput(DAGetDisplay(NULL), DAGetWindow(), ButtonPressMask | ButtonReleaseMask); } @@ -185,3 +196,48 @@ buttonrelease(int button, int state, int x, int y) check_for_updates(); } } + +char * +get_update_script() +{ + int length; + char *p; + char *script_name = "arch_update.sh"; + + char *fullpath = malloc(MAXPATHLEN + strlen(script_name)); + if (fullpath == NULL) { + perror("Can't allocate memory."); + exit(1); + } + + /* + * /proc/self is a symbolic link to the process-ID subdir of /proc, e.g. + * /proc/4323 when the pid of the process of this program is 4323. + * Inside /proc/ there is a symbolic link to the executable that is + * running as this . This symbolic link is called "exe". So if we + * read the path where the symlink /proc/self/exe points to we have the + * full path of the executable. + */ + + length = readlink("/proc/self/exe", fullpath, MAXPATHLEN); + + /* + * Catch some errors: + */ + if (length < 0) { + perror("resolving symlink /proc/self/exe."); + exit(1); + } + if (length >= MAXPATHLEN) { + fprintf(stderr, "Path too long.\n"); + exit(1); + } + + fullpath[length] = '\0'; + if ((p = strrchr(fullpath, '/'))) { + *(p + 1) = '\0'; + } + strcat(fullpath, script_name); + + return fullpath; +}