Merged in changes from dflemstr (thanks!)
[pandora-libraries.git] / lib / pnd_utility.c
1
2 #include <stdio.h> /* for FILE etc */
3 #include <stdlib.h> /* for malloc */
4 #include <string.h> /* for making ftw.h happy */
5 #include <unistd.h> /* for fork exec */
6
7 #include "pnd_utility.h"
8
9 // a generalized variable-substitution routine might be nice; for now we need a quick tilde one,
10 // so here goes. Brute force ftw!
11 char *pnd_expand_tilde ( char *freeable_buffer ) {
12   char *p;
13   char *s = freeable_buffer;
14   char *home = getenv ( "HOME" );
15
16   if ( ! home ) {
17     return ( s ); // can't succeed
18   }
19
20   while ( ( p = strchr ( s, '~' ) ) ) {
21     char *temp = malloc ( strlen ( s ) + strlen ( home ) + 1 );
22     memset ( temp, '\0', strlen ( s ) + strlen ( home ) + 1 );
23     // copy in stuff prior to ~
24     strncpy ( temp, s, p - s );
25     // copy tilde in
26     strcat ( temp, home );
27     // copy stuff after tilde in
28     strcat ( temp, p + 1 );
29     // swap ptrs
30     free ( s );
31     s = temp;
32   } // while finding matches
33
34   return ( s );
35 }
36
37 void pnd_exec_no_wait_1 ( char *fullpath, char *arg1 ) {
38   int i;
39
40   if ( ( i = fork() ) < 0 ) {
41     printf ( "ERROR: Couldn't fork()\n" );
42     return;
43   }
44
45   if ( i ) {
46     return; // parent process, don't care
47   }
48
49   // child process, do something
50   if ( arg1 ) {
51     execl ( fullpath, fullpath, arg1, (char*) NULL );
52   } else {
53     execl ( fullpath, fullpath, (char*) NULL );
54   }
55
56   // getting here is an error
57   //printf ( "Error attempting to run %s\n", fullpath );
58
59   return;
60 }