r216: Check for copying a directory onto itself, as well as into itself.
[rox-filer.git] / ROX-Filer / src / main.c
blob8dccd8720f50cad6e784ecf5859a11b15dc89db2
1 /*
2 * $Id$
4 * ROX-Filer, filer for the ROX desktop project
5 * Copyright (C) 2000, Thomas Leonard, <tal197@ecs.soton.ac.uk>.
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the Free
9 * Software Foundation; either version 2 of the License, or (at your option)
10 * any later version.
12 * This program is distributed in the hope that it will be useful, but WITHOUT
13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15 * more details.
17 * You should have received a copy of the GNU General Public License along with
18 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
19 * Place, Suite 330, Boston, MA 02111-1307 USA
22 #include "config.h"
24 #include <stdlib.h>
25 #include <stdio.h>
26 #include <sys/types.h>
27 #include <signal.h>
28 #include <sys/wait.h>
29 #include <unistd.h>
30 #include <fcntl.h>
31 #include <pwd.h>
32 #include <grp.h>
34 #ifdef HAVE_GETOPT_LONG
35 # include <getopt.h>
36 #endif
38 #include <gtk/gtk.h>
39 #include "collection.h"
41 #include "main.h"
42 #include "support.h"
43 #include "gui_support.h"
44 #include "filer.h"
45 #include "mount.h"
46 #include "menu.h"
47 #include "dnd.h"
48 #include "options.h"
49 #include "choices.h"
50 #include "savebox.h"
51 #include "type.h"
52 #include "pixmaps.h"
53 #include "dir.h"
54 #include "action.h"
56 int number_of_windows = 0; /* Quit when this reaches 0 again... */
57 int to_error_log = -1; /* Write here to log errors */
59 uid_t euid;
60 gid_t egid;
61 int ngroups; /* Number of supplemental groups */
62 gid_t *supplemental_groups = NULL;
63 char *home_dir;
64 uid_t noaccess_uid; /* Used when stat() fails */
65 gid_t noaccess_gid; /* Used when stat() fails */
67 #define VERSION "ROX-Filer 0.1.16\n" \
68 "Copyright (C) 1999 Thomas Leonard.\n" \
69 "ROX-Filer comes with ABSOLUTELY NO WARRANTY,\n" \
70 "to the extent permitted by law.\n" \
71 "You may redistribute copies of ROX-Filer\n" \
72 "under the terms of the GNU General Public License.\n" \
73 "For more information about these matters, " \
74 "see the file named COPYING.\n"
76 #ifdef HAVE_GETOPT_LONG
77 # define USAGE "Try `ROX-Filer/AppRun --help' for more information.\n"
78 # define SHORT_ONLY_WARNING ""
79 #else
80 # define USAGE "Try `ROX-Filer/AppRun -h' for more information.\n"
81 # define SHORT_ONLY_WARNING \
82 "NOTE: Your system does not support long options - \n" \
83 "you must use the short versions instead.\n\n"
84 #endif
86 #define SHORT_OPS "t:b:ohv"
88 #define HELP "Usage: ROX-Filer/AppRun [OPTION]... [DIR]...\n" \
89 "Open filer windows showing each directory listed, or $HOME \n" \
90 "if no directories are given.\n\n" \
91 " -h, --help display this help and exit\n" \
92 " -v, --version display the version information and exit\n" \
93 " -t, --top [DIR] open DIR as a top-edge panel\n" \
94 " -b, --bottom [DIR] open DIR as a bottom-edge panel\n" \
95 " -o, --override override window manager control of panels\n" \
96 "\n" SHORT_ONLY_WARNING \
97 "Report bugs to <tal197@ecs.soton.ac.uk>.\n"
99 #ifdef HAVE_GETOPT_LONG
100 static struct option long_opts[] =
102 {"top", 1, NULL, 't'},
103 {"bottom", 1, NULL, 'b'},
104 {"override", 0, NULL, 'o'},
105 {"help", 0, NULL, 'h'},
106 {"version", 0, NULL, 'v'},
107 {NULL, 0, NULL, 0},
109 #endif
111 /* Take control of panels away from WM? */
112 gboolean override_redirect = FALSE;
114 static void child_died(int signum)
116 int status;
117 int child;
119 /* Find out which children exited and allow them to die */
122 child = waitpid(-1, &status, WNOHANG);
124 if (child == 0 || child == -1)
125 return;
127 /* fprintf(stderr, "Child %d exited\n", child); */
129 } while (1);
132 #define BUFLEN 40
133 void stderr_cb(gpointer data, gint source, GdkInputCondition condition)
135 char buf[BUFLEN];
136 static GtkWidget *log = NULL;
137 static GtkWidget *window = NULL;
138 ssize_t len;
140 if (!window)
142 window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
143 gtk_window_set_title(GTK_WINDOW(window), "ROX-Filer error log");
144 gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
145 gtk_window_set_default_size(GTK_WINDOW(window), 600, 300);
146 gtk_signal_connect_object(GTK_OBJECT(window), "delete_event",
147 gtk_widget_hide, GTK_OBJECT(window));
148 log = gtk_text_new(NULL, NULL);
149 gtk_container_add(GTK_CONTAINER(window), log);
152 if (!GTK_WIDGET_MAPPED(window))
153 gtk_widget_show_all(window);
155 len = read(source, buf, BUFLEN);
156 if (len > 0)
157 gtk_text_insert(GTK_TEXT(log), NULL, NULL, NULL, buf, len);
160 int main(int argc, char **argv)
162 int stderr_pipe[2];
163 struct sigaction act;
164 GList *panel_dirs = NULL;
165 GList *panel_sides = NULL;
166 struct passwd *pw;
167 struct group *gr;
169 home_dir = g_get_home_dir();
171 pw = getpwnam("noaccess");
172 noaccess_uid = pw? pw->pw_uid : 0;
174 gr = getgrnam("noaccess");
175 noaccess_gid = gr? gr->gr_gid : 0;
177 #ifdef HAVE_LIBVFS
178 mc_vfs_init();
179 #endif
180 gtk_init(&argc, &argv);
182 while (1)
184 int c;
185 #ifdef HAVE_GETOPT_LONG
186 int long_index;
187 c = getopt_long(argc, argv, SHORT_OPS,
188 long_opts, &long_index);
189 #else
190 c = getopt(argc, argv, SHORT_OPS);
191 #endif
193 if (c == EOF)
194 break; /* No more options */
196 switch (c)
198 case 'o':
199 override_redirect = TRUE;
200 break;
201 case 'v':
202 printf(VERSION);
203 return EXIT_SUCCESS;
204 case 'h':
205 printf(HELP);
206 return EXIT_SUCCESS;
207 case 't':
208 case 'b':
209 panel_sides = g_list_prepend(panel_sides,
210 (gpointer) c);
211 panel_dirs = g_list_prepend(panel_dirs,
212 *optarg == '=' ? optarg + 1
213 : optarg);
214 break;
215 default:
216 printf(USAGE);
217 return EXIT_FAILURE;
221 choices_init("ROX-Filer");
223 gui_support_init();
224 pixmaps_init();
225 dir_init();
226 menu_init();
227 dnd_init();
228 filer_init();
229 mount_init();
230 options_init();
231 savebox_init();
232 type_init();
233 action_init();
235 options_load();
237 /* Let child processes die */
238 act.sa_handler = child_died;
239 sigemptyset(&act.sa_mask);
240 act.sa_flags = SA_NOCLDSTOP;
241 sigaction(SIGCHLD, &act, NULL);
243 /* Ignore SIGPIPE - check for EPIPE errors instead */
244 act.sa_handler = SIG_IGN;
245 sigemptyset(&act.sa_mask);
246 act.sa_flags = 0;
247 sigaction(SIGPIPE, &act, NULL);
249 euid = geteuid();
250 egid = getegid();
251 ngroups = getgroups(0, NULL);
252 if (ngroups < 0)
253 ngroups = 0;
254 else if (ngroups > 0)
256 supplemental_groups = g_malloc(sizeof(gid_t) * ngroups);
257 getgroups(ngroups, supplemental_groups);
260 if (euid == 0)
262 if (get_choice("!!!DANGER!!!",
263 "Running ROX-Filer as root is VERY dangerous. If I "
264 "had a warranty (I don't) then doing this would "
265 "void it", 2,
266 "Don't click here", "Quit") != 0)
267 exit(EXIT_SUCCESS);
270 if (optind == argc && !panel_dirs)
271 filer_opendir(home_dir, PANEL_NO);
272 else
274 int i = optind;
275 GList *dir = panel_dirs;
276 GList *side = panel_sides;
278 while (dir)
280 int c = (int) side->data;
282 filer_opendir((char *) dir->data,
283 c == 't' ? PANEL_TOP : PANEL_BOTTOM);
284 dir = dir->next;
285 side = side->next;
288 g_list_free(dir);
289 g_list_free(side);
291 while (i < argc)
292 filer_opendir(argv[i++], PANEL_NO);
295 pipe(stderr_pipe);
296 close_on_exec(stderr_pipe[0], TRUE);
297 close_on_exec(stderr_pipe[1], TRUE);
298 gdk_input_add(stderr_pipe[0], GDK_INPUT_READ, stderr_cb, NULL);
299 to_error_log = stderr_pipe[1];
301 gtk_main();
303 return EXIT_SUCCESS;