3 /*******************************************************************************/
5 /* gEDA Suite Project Manager */
7 /* Copyright (C) 2002 Piotr Miarecki, sp9rve@eter.ariadna.pl */
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. */
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. */
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. */
22 /*******************************************************************************/
28 #define GTK_ENABLE_BROKEN
36 #include <sys/types.h>
49 /* PID of last thread created by FileExec() */
50 static pid_t Pid
= -1;
58 int FileIsExisting(const char *szFileName
)
62 fp
= fopen(szFileName
, "r");
77 int FileCopy(const char *szSource
, const char *szDest
)
81 Source
= fopen(szSource
, "r");
84 /* TODO: error handling */
88 Dest
= fopen(szDest
, "w");
91 /* TODO: error handling */
96 fputc(fgetc(Source
), Dest
);
107 get file name, extension and directory from full path
110 char *FileGetName(const char *szFilename
)
112 static char szName
[TEXTLEN
];
115 /* extract only filename with extension */
116 for (i
= strlen(szFilename
) - 1; i
>= 0 && szFilename
[i
] != G_DIR_SEPARATOR
; i
--)
120 if (szFilename
[i
] == G_DIR_SEPARATOR
)
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
--)
136 char *FileGetExt(const char *szFilename
)
138 static char szExt
[TEXTLEN
];
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);
154 char *FileGetDir(const char *szFilename
)
156 static char szDir
[TEXTLEN
];
159 strncpy(szDir
, szFilename
, TEXTLEN
- 1);
160 for (i
= strlen(szDir
) - 1; i
>= 0 && szDir
[i
] != '/'; i
--)
162 if (szDir
[i
] == '/' && i
< TEXTLEN
)
173 char *FileGetRel(const char *szFilename
)
176 static char szRel
[TEXTLEN
];
177 char szDirectory
[TEXTLEN
], *pResult
;
179 /* get current directory */
180 pResult
= getcwd(szDirectory
, TEXTLEN
- 1);
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 */
203 for (i
= 0; i
< strlen(szFilename
); i
++)
204 if (strncmp(szFilename
, szDirectory
, i
))
209 for (j
= i
, k
= 1; j
< strlen(szDirectory
); j
++)
210 if (szDirectory
[j
] == '/')
214 for (j
= 0; j
< k
; j
++)
215 strcat(szRel
, "../");
216 strcat(szRel
, szFilename
+ i
);
224 int FileExec(const char *szCommand
)
227 FILE *hStdOut
, *hStdErr
;
228 char szValue
[TEXTLEN
], szText
[2000];
231 /* create a new process to execute external shell commands) */
238 /* run the command in the child process */
243 /* redirecting stdout */
244 sprintf(szValue
, "/%s/%s-stdout-%ld", GM_TMPDIR
, GM_TMPNAME
, (long) Pid
);
245 hStdOut
= freopen(szValue
, "w", stdout
);
251 /* redirecting stderr */
252 sprintf(szValue
, "/%s/%s-stderr-%ld", GM_TMPDIR
, GM_TMPNAME
, (long) Pid
);
253 hStdErr
= freopen(szValue
, "w", stderr
);
259 /* execute command */
260 execl("/bin/sh", "sh", "-c", szCommand
, NULL
);
262 /* end child process */
266 /* wait for death of child in the parent process */
267 while (waitpid(Pid
, NULL
, WNOHANG
) == 0)
270 while (g_main_iteration(FALSE
));
273 /* copy stderr to sterr window */
274 pText
= GTK_TEXT(lookup_widget(GTK_WIDGET(pWindowMain
), "StatusText"));
277 /* TODO: error handling */
282 sprintf(szValue
, "/%s/%s-stderr-%ld", GM_TMPDIR
, GM_TMPNAME
, (long) Pid
);
283 hStdErr
= fopen(szValue
, "r");
291 while (!feof(hStdErr
))
297 szText
[j
++] = (char) '*';
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
));
310 /* remove last stdout file */
311 sprintf(szValue
, "/%s/%s-stdout-%ld", GM_TMPDIR
, GM_TMPNAME
, (long) Pid
);
312 iResult
= remove(szValue
);
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
);
323 /* TODO: error handling */
326 /* TODO: in this manner the function never return failure, fix it */