Removed all files using cvs remove since setup is quite old and hasn't been
[geda-gaf/whiteaudio.git] / geda / graphman / filetool.c
blobe385d6a609a028b3f260d5ffb3da1d4903ca9204
1 /* $Id$ */
3 /*******************************************************************************/
4 /* */
5 /* gEDA Suite Project Manager */
6 /* */
7 /* Copyright (C) 2002 Piotr Miarecki, sp9rve@eter.ariadna.pl */
8 /* */
9 /* This program is free software; you can redistribute it and/or */
10 /* modify it under the terms of the GNU General Public License */
11 /* as published by the Free Software Foundation version 2. */
12 /* */
13 /* This program is distributed in the hope that it will be useful, */
14 /* but WITHOUT ANY WARRANTY; without even the implied warranty of */
15 /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
16 /* GNU General Public License for more details. */
17 /* */
18 /* You should have received a copy of the GNU General Public License */
19 /* along with this program; if not, write to the Free Software */
20 /* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
21 /* */
22 /*******************************************************************************/
24 #ifdef HAVE_CONFIG_H
25 #include "config.h"
26 #endif
28 #define GTK_ENABLE_BROKEN
29 #include <gtk/gtk.h>
30 #include <stdio.h>
32 #ifdef HAVE_STRING_H
33 #include <string.h>
34 #endif
36 #include <sys/types.h>
37 #include <sys/wait.h>
39 #ifdef HAVE_UNISTD_H
40 #include <unistd.h>
41 #endif
43 #include "filetool.h"
44 #include "global.h"
45 #include "support.h"
49 /* PID of last thread created by FileExec() */
50 static pid_t Pid = -1;
54 /*
55 check if file exists
58 int FileIsExisting(const char *szFileName)
60 FILE *fp;
62 fp = fopen(szFileName, "r");
63 if (fp == NULL)
64 return FAILURE;
66 fclose(fp);
68 return SUCCESS;
73 /*
74 copy file
77 int FileCopy(const char *szSource, const char *szDest)
79 FILE *Source, *Dest;
81 Source = fopen(szSource, "r");
82 if (Source == NULL)
84 /* TODO: error handling */
85 return FAILURE;
88 Dest = fopen(szDest, "w");
89 if (Dest == NULL)
91 /* TODO: error handling */
92 return FAILURE;
95 while (!feof(Source))
96 fputc(fgetc(Source), Dest);
98 fclose(Dest);
99 fclose(Source);
101 return SUCCESS;
107 get file name, extension and directory from full path
110 char *FileGetName(const char *szFilename)
112 static char szName[TEXTLEN];
113 int i, j;
115 /* extract only filename with extension */
116 for (i = strlen(szFilename) - 1; i >= 0 && szFilename[i] != G_DIR_SEPARATOR; i --)
118 if (i < 0)
119 i = 0;
120 if (szFilename[i] == G_DIR_SEPARATOR)
121 i ++;
122 if (i > strlen(szFilename) - 1)
123 i = strlen(szFilename) - 1;
124 strcpy(szName, szFilename + i);
126 /* remove extension */
127 for (j = strlen(szName) - 1; j >= 0 && szName[j] != '.'; j --)
129 if (j >= 0)
130 szName[j] = 0;
132 return szName;
136 char *FileGetExt(const char *szFilename)
138 static char szExt[TEXTLEN];
139 int i;
141 szExt[0] = 0;
142 for (i = strlen(szFilename) - 1; i >= 0 && szFilename[i] != '/' && szFilename[i] != '.'; i --)
144 if (szFilename[i] == '.')
146 if (strlen(szFilename + i + 1) < TEXTLEN)
147 strcpy(szExt, szFilename + i + 1);
150 return szExt;
154 char *FileGetDir(const char *szFilename)
156 static char szDir[TEXTLEN];
157 int i;
159 strncpy(szDir, szFilename, TEXTLEN - 1);
160 for (i = strlen(szDir) - 1; i >= 0 && szDir[i] != '/'; i --)
162 if (szDir[i] == '/' && i < TEXTLEN)
163 szDir[i] = 0;
164 else
165 szDir[0] = 0;
167 return szDir;
173 char *FileGetRel(const char *szFilename)
175 int i, j, k;
176 static char szRel[TEXTLEN];
177 char szDirectory[TEXTLEN], *pResult;
179 /* get current directory */
180 pResult = getcwd(szDirectory, TEXTLEN - 1);
181 if (pResult == NULL)
183 strcpy(szRel, "");
184 return szRel;
188 /* if file name is a relative one */
189 if (szFilename[0] != '/')
191 strcpy(szRel, szFilename);
194 /* if the file exists in a subdirectory */
195 else if (!strncmp(szFilename, szDirectory, strlen(szDirectory)))
197 strcpy(szRel, szFilename + strlen(szDirectory) + 1);
200 /* if the file exists in a different directory tree */
201 else
203 for (i = 0; i < strlen(szFilename); i ++)
204 if (strncmp(szFilename, szDirectory, i))
205 break;
206 if (i > 0)
207 i --;
209 for (j = i, k = 1; j < strlen(szDirectory); j ++)
210 if (szDirectory[j] == '/')
211 k ++;
213 strcpy(szRel, "");
214 for (j = 0; j < k; j ++)
215 strcat(szRel, "../");
216 strcat(szRel, szFilename + i);
219 return szRel;
224 int FileExec(const char *szCommand)
226 GtkText *pText;
227 FILE *hStdOut, *hStdErr;
228 char szValue[TEXTLEN], szText[2000];
229 int iResult, i, j;
231 /* create a new process to execute external shell commands) */
232 Pid = fork();
233 if (Pid < 0)
235 return FAILURE;
238 /* run the command in the child process */
239 if (Pid == 0)
241 Pid = getpid();
243 /* redirecting stdout */
244 sprintf(szValue, "/%s/%s-stdout-%ld", GM_TMPDIR, GM_TMPNAME, (long) Pid);
245 hStdOut = freopen(szValue, "w", stdout);
246 if (hStdOut == NULL)
248 _exit(0);
251 /* redirecting stderr */
252 sprintf(szValue, "/%s/%s-stderr-%ld", GM_TMPDIR, GM_TMPNAME, (long) Pid);
253 hStdErr = freopen(szValue, "w", stderr);
254 if (hStdErr == NULL)
256 _exit(0);
259 /* execute command */
260 execl("/bin/sh", "sh", "-c", szCommand, NULL);
262 /* end child process */
263 _exit(0);
266 /* wait for death of child in the parent process */
267 while (waitpid(Pid, NULL, WNOHANG) == 0)
269 sleep(1);
270 while (g_main_iteration(FALSE));
273 /* copy stderr to sterr window */
274 pText = GTK_TEXT(lookup_widget(GTK_WIDGET(pWindowMain), "StatusText"));
275 if (pText == NULL)
277 /* TODO: error handling */
279 else
281 /* print stderr */
282 sprintf(szValue, "/%s/%s-stderr-%ld", GM_TMPDIR, GM_TMPNAME, (long) Pid);
283 hStdErr = fopen(szValue, "r");
284 if (hStdErr == NULL)
286 /* TODO */
288 else
290 j = 0;
291 while (!feof(hStdErr))
293 i = fgetc(hStdErr);
294 if (i < 0)
295 break;
297 szText[j++] = (char) '*';
300 szText[j] = 0;
301 gtk_text_set_point(pText, 0);
302 gtk_text_forward_delete(pText, gtk_text_get_length(pText));
303 gtk_text_insert(pText, NULL, NULL, NULL, szText, strlen(szText));
304 gtk_widget_show(GTK_WIDGET(pText));
305 while (g_main_iteration(FALSE));
306 fclose(hStdErr);
310 /* remove last stdout file */
311 sprintf(szValue, "/%s/%s-stdout-%ld", GM_TMPDIR, GM_TMPNAME, (long) Pid);
312 iResult = remove(szValue);
313 if (iResult != 0)
315 /* TODO: error handling */
318 /* remove last stderr file */
319 sprintf(szValue, "/%s/%s-stderr-%ld", GM_TMPDIR, GM_TMPNAME, (long) Pid);
320 iResult = remove(szValue);
321 if (iResult != 0)
323 /* TODO: error handling */
326 /* TODO: in this manner the function never return failure, fix it */
328 return SUCCESS;