1 /* sysstuff.c
2  * Copyright (C) 2002-2004 Pascal Eberhard <pascal.ebo@netcourrier.com>
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License as
6  * published by the Free Software Foundation; either version 2 of the
7  * License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public
15  * License along with this program; if not, write to the
16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, USA.
18  */
19 
20 
21 #include "log.h"
22 #include <stdlib.h>
23 #include <errno.h>
24 #include <gtk/gtk.h>
25 
26 #include <stdio.h>      // FILENAME_MAX
27 #include <string.h>
28 #include <sys/types.h>
29 #include <sys/stat.h>
30 #define  __USE_GNU 1
31 #include <unistd.h>     // group_member, chdir
32 
33 // ----------------------------------------------------------------------------
sys_spawn(gchar * cmdline,gchar * path)34 gboolean sys_spawn(gchar *cmdline, gchar *path)
35 {
36   int          cc;
37   gboolean     spawned;
38   GError      *err = NULL;
39   gchar       *oldpwd = NULL;
40 
41   g_assert(cmdline != NULL);
42 
43   // switch to desired PATH
44   if ((path != NULL) && (strcmp(path, "") != 0))
45   {
46     cc = chdir(path);
47     WARN("chdir:%d, errno:%d, path:%s", cc, errno, path);
48     oldpwd = g_get_current_dir();
49   }
50 
51   spawned = g_spawn_command_line_async(cmdline, &err/*GError*/);
52 
53   if (oldpwd != NULL)
54   {
55     cc = chdir(oldpwd);
56     if (cc != 0)
57       WARN("chdir:%d, errno:%d, oldpwd:%s", cc, errno, oldpwd);
58     g_free(oldpwd);
59   }
60 
61   if (!spawned)
62   {
63     if (err != NULL && err->message != NULL)
64     {
65       WARN("g_spawn_command_line_async, cmdline:%s, errcode:%d, msg:%s",
66            cmdline, err->code, err->message);
67       g_error_free(err);
68     }
69     else
70     {
71       WARN("g_spawn_command_line_async, cmdline:%s", cmdline);
72     }
73   }
74   return spawned;
75 }
76 // ----------------------------------------------------------------------------
77 // remove all not necessary '/' in the path
make_path_uniq(char ** path)78 void make_path_uniq(char **path)
79 {
80   int i, len, dbl_slash_cnt, dbl_slash_cnt_old;
81   char *new_path;
82   len = strlen(*path);
83   dbl_slash_cnt = 0;
84   for (i=0; i<len; i++)
85   {
86     if ( (*path)[i] == '/' )
87     {
88       if ( (i != 0) && ((*path)[i-1] == '/') )
89         dbl_slash_cnt++;
90       if ( ( i != 0) && (i == (len-1)) )
91         dbl_slash_cnt++;
92     }
93   }
94   if (dbl_slash_cnt == 0)
95     return;
96 
97   new_path = (char*) g_malloc((len - dbl_slash_cnt + 1) * sizeof(char));
98   dbl_slash_cnt = 0;
99   dbl_slash_cnt_old = 0;
100   for (i=0; i<len; i++)
101   {
102     if ( (*path)[i] == '/' )
103     {
104       if ( (i != 0) && ((*path)[i-1] == '/') )
105         dbl_slash_cnt++;
106       if ( (i != 0) && (i == (len-1)) )
107         dbl_slash_cnt++;
108     }
109     if (dbl_slash_cnt_old == dbl_slash_cnt)
110       new_path[i - dbl_slash_cnt] = (*path)[i];
111     dbl_slash_cnt_old = dbl_slash_cnt;
112   }
113   new_path[len - dbl_slash_cnt] = '\0';
114   g_free(*path);
115   *path = new_path;
116 }
117 // ----------------------------------------------------------------------------
118 // ----------------------------------------------------------------------------
119