From 777155a9987fb983a4f0df22d69db1fa5e6c1118 Mon Sep 17 00:00:00 2001 From: skeezix Date: Thu, 5 Mar 2009 23:35:28 -0500 Subject: [PATCH] Added support for "~" expansion in the dotdesktop-path (and only in that place, in pndnotifyd) --- Makefile | 2 +- apps/pndnotifyd.c | 9 +++++++++ include/pnd_utility.h | 17 +++++++++++++++++ lib/pnd_utility.c | 34 ++++++++++++++++++++++++++++++++++ test/discotest.c | 6 ++++++ testdata/conf/desktop | 3 ++- 6 files changed, 69 insertions(+), 2 deletions(-) create mode 100644 include/pnd_utility.h create mode 100644 lib/pnd_utility.c diff --git a/Makefile b/Makefile index 6260563..77a13ce 100644 --- a/Makefile +++ b/Makefile @@ -20,7 +20,7 @@ LIB = libpnd.a SOLIB = libpnd.so.1 # canonicle name SOLIB1 = libpnd.so.1.0.1 # versioned name XMLOBJ = lib/tinyxml/tinystr.o lib/tinyxml/tinyxml.o lib/tinyxml/tinyxmlerror.o lib/tinyxml/tinyxmlparser.o -ALLOBJ = pnd_conf.o pnd_container.o pnd_discovery.o pnd_pxml.o pnd_notify.o pnd_locate.o pnd_tinyxml.o pnd_pndfiles.o pnd_apps.o +ALLOBJ = pnd_conf.o pnd_container.o pnd_discovery.o pnd_pxml.o pnd_notify.o pnd_locate.o pnd_tinyxml.o pnd_pndfiles.o pnd_apps.o pnd_utility.o all: ${SOLIB} ${LIB} conftest discotest notifytest locatetest pndnotifyd diff --git a/apps/pndnotifyd.c b/apps/pndnotifyd.c index 45b204f..6e44c2b 100644 --- a/apps/pndnotifyd.c +++ b/apps/pndnotifyd.c @@ -27,6 +27,7 @@ #include "../lib/pnd_pathiter.h" #include "pnd_discovery.h" #include "pnd_locate.h" +#include "pnd_utility.h" static unsigned char g_daemon_mode = 0; @@ -177,6 +178,14 @@ int main ( int argc, char *argv[] ) { if ( pndrun ) printf ( "Default pndrun is %s\n", pndrun ); } + /* handle globbing or variable substitution + */ + dotdesktoppath = pnd_expand_tilde ( strdup ( dotdesktoppath ) ); + + /* validate paths + */ + mkdir ( dotdesktoppath, 0777 ); + /* startup */ diff --git a/include/pnd_utility.h b/include/pnd_utility.h new file mode 100644 index 0000000..257bfb7 --- /dev/null +++ b/include/pnd_utility.h @@ -0,0 +1,17 @@ + +#ifndef h_pnd_utility_h +#define h_pnd_utility_h + +#ifdef __cplusplus +extern "C" { +#endif + +// given a malloc'd pointer to a string, expand ~ to $HOME as often as it is found, returning a +// new string; the old string is destroyed in the process, or returned as-is. +char *pnd_expand_tilde ( char *freeable_buffer ); + +#ifdef __cplusplus +} /* "C" */ +#endif + +#endif diff --git a/lib/pnd_utility.c b/lib/pnd_utility.c new file mode 100644 index 0000000..11fbc97 --- /dev/null +++ b/lib/pnd_utility.c @@ -0,0 +1,34 @@ + +#include /* for FILE etc */ +#include /* for malloc */ +#include /* for making ftw.h happy */ + +#include "pnd_utility.h" + +// a generalized variable-substitution routine might be nice; for now we need a quick tilde one, +// so here goes. Brute force ftw! +char *pnd_expand_tilde ( char *freeable_buffer ) { + char *p; + char *s = freeable_buffer; + char *home = getenv ( "HOME" ); + + if ( ! home ) { + return ( s ); // can't succeed + } + + while ( ( p = strchr ( s, '~' ) ) ) { + char *temp = malloc ( strlen ( s ) + strlen ( home ) + 1 ); + memset ( temp, '\0', strlen ( s ) + strlen ( home ) + 1 ); + // copy in stuff prior to ~ + strncpy ( temp, s, p - s ); + // copy tilde in + strcat ( temp, home ); + // copy stuff after tilde in + strcat ( temp, s + 1 ); + // swap ptrs + free ( s ); + s = temp; + } // while finding matches + + return ( s ); +} diff --git a/test/discotest.c b/test/discotest.c index 5bc7030..364af15 100644 --- a/test/discotest.c +++ b/test/discotest.c @@ -9,6 +9,7 @@ #include "pnd_pxml.h" #include "pnd_discovery.h" #include "pnd_locate.h" +#include "pnd_utility.h" int main ( int argc, char *argv[] ) { char *configpath; @@ -213,5 +214,10 @@ int main ( int argc, char *argv[] ) { pnd_box_delete ( apph ); } + // extra testing - tilde-substitution + char *p = strdup ( "~/.applications" ); + printf ( "Tilde substitution: in '%s'\n", p ); + printf ( " out '%s'\n", pnd_expand_tilde ( p ) ); + return ( 0 ); } diff --git a/testdata/conf/desktop b/testdata/conf/desktop index 7e9ff76..c84db94 100644 --- a/testdata/conf/desktop +++ b/testdata/conf/desktop @@ -3,4 +3,5 @@ # Desktop configuration [dotfiles] -dotdesktoppath ./testdata/dotdesktop # path for pndnotifyd to spit .desktop files into +#dotdesktoppath ./testdata/dotdesktop # path for pndnotifyd to spit .desktop files into +dotdesktoppath ~/.applications # path for pndnotifyd to spit .desktop files into -- 2.39.5