1 /* GTK - The GIMP Toolkit
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library 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 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
20 #include <sys/types.h>
22 #include <sys/param.h>
31 #include "gdk/gdkkeysyms.h"
32 #include "gtkbutton.h"
34 #include "gtkfilesel.h"
39 #include "gtklistitem.h"
41 #include "gtkscrolledwindow.h"
42 #include "gtksignal.h"
45 #include "gtkmenuitem.h"
46 #include "gtkoptionmenu.h"
48 #include "gtkdialog.h"
50 #define DIR_LIST_WIDTH 180
51 #define DIR_LIST_HEIGHT 180
52 #define FILE_LIST_WIDTH 180
53 #define FILE_LIST_HEIGHT 180
55 /* I've put this here so it doesn't get confused with the
56 * file completion interface */
57 typedef struct _HistoryCallbackArg HistoryCallbackArg;
59 struct _HistoryCallbackArg
66 typedef struct _CompletionState CompletionState;
67 typedef struct _CompletionDir CompletionDir;
68 typedef struct _CompletionDirSent CompletionDirSent;
69 typedef struct _CompletionDirEntry CompletionDirEntry;
70 typedef struct _CompletionUserDir CompletionUserDir;
71 typedef struct _PossibleCompletion PossibleCompletion;
73 /* Non-external file completion decls and structures */
75 /* A contant telling PRCS how many directories to cache. Its actually
76 * kept in a list, so the geometry isn't important. */
77 #define CMPL_DIRECTORY_CACHE_SIZE 10
79 /* A constant used to determine whether a substring was an exact
80 * match by first_diff_index()
82 #define PATTERN_MATCH -1
83 /* The arguments used by all fnmatch() calls below
85 #define FNMATCH_FLAGS (FNM_PATHNAME | FNM_PERIOD)
87 #define CMPL_ERRNO_TOO_LONG ((1<<16)-1)
89 /* This structure contains all the useful information about a directory
90 * for the purposes of filename completion. These structures are cached
91 * in the CompletionState struct. CompletionDir's are reference counted.
93 struct _CompletionDirSent
100 gchar *name_buffer; /* memory segment containing names of all entries */
102 struct _CompletionDirEntry *entries;
105 struct _CompletionDir
107 CompletionDirSent *sent;
112 struct _CompletionDir *cmpl_parent;
117 /* This structure contains pairs of directory entry names with a flag saying
118 * whether or not they are a valid directory. NOTE: This information is used
119 * to provide the caller with information about whether to update its completions
120 * or try to open a file. Since directories are cached by the directory mtime,
121 * a symlink which points to an invalid file (which will not be a directory),
122 * will not be reevaluated if that file is created, unless the containing
123 * directory is touched. I consider this case to be worth ignoring (josh).
125 struct _CompletionDirEntry
131 struct _CompletionUserDir
137 struct _PossibleCompletion
139 /* accessible fields, all are accessed externally by functions
143 gint is_a_completion;
151 struct _CompletionState
153 gint last_valid_char;
155 gint updated_text_len;
156 gint updated_text_alloc;
159 gchar *user_dir_name_buffer;
160 gint user_directories_len;
161 gchar *user_home_dir;
163 gchar *last_completion_text;
165 gint user_completion_index; /* if >= 0, currently completing ~user */
167 struct _CompletionDir *completion_dir; /* directory completing from */
168 struct _CompletionDir *active_completion_dir;
170 struct _PossibleCompletion the_completion;
172 struct _CompletionDir *reference_dir; /* initial directory */
174 GList* directory_storage;
175 GList* directory_sent_storage;
177 struct _CompletionUserDir *user_directories;
181 /* File completion functions which would be external, were they used
182 * outside of this file.
185 static CompletionState* cmpl_init_state (void);
186 static void cmpl_free_state (CompletionState *cmpl_state);
187 static gint cmpl_state_okay (CompletionState* cmpl_state);
188 static gchar* cmpl_strerror (gint);
190 static PossibleCompletion* cmpl_completion_matches(gchar *text_to_complete,
191 gchar **remaining_text,
192 CompletionState *cmpl_state);
194 /* Returns a name for consideration, possibly a completion, this name
195 * will be invalid after the next call to cmpl_next_completion.
197 static char* cmpl_this_completion (PossibleCompletion*);
199 /* True if this completion matches the given text. Otherwise, this
200 * output can be used to have a list of non-completions.
202 static gint cmpl_is_a_completion (PossibleCompletion*);
204 /* True if the completion is a directory
206 static gint cmpl_is_directory (PossibleCompletion*);
208 /* Obtains the next completion, or NULL
210 static PossibleCompletion* cmpl_next_completion (CompletionState*);
212 /* Updating completions: the return value of cmpl_updated_text() will
213 * be text_to_complete completed as much as possible after the most
214 * recent call to cmpl_completion_matches. For the present
215 * application, this is the suggested replacement for the user's input
216 * string. You must CALL THIS AFTER ALL cmpl_text_completions have
219 static gchar* cmpl_updated_text (CompletionState* cmpl_state);
221 /* After updating, to see if the completion was a directory, call
222 * this. If it was, you should consider re-calling completion_matches.
224 static gint cmpl_updated_dir (CompletionState* cmpl_state);
226 /* Current location: if using file completion, return the current
227 * directory, from which file completion begins. More specifically,
228 * the cwd concatenated with all exact completions up to the last
229 * directory delimiter('/').
231 static gchar* cmpl_reference_position (CompletionState* cmpl_state);
233 /* backing up: if cmpl_completion_matches returns NULL, you may query
234 * the index of the last completable character into cmpl_updated_text.
236 static gint cmpl_last_valid_char (CompletionState* cmpl_state);
238 /* When the user selects a non-directory, call cmpl_completion_fullname
239 * to get the full name of the selected file.
241 static gchar* cmpl_completion_fullname (gchar*, CompletionState* cmpl_state);
244 /* Directory operations. */
245 static CompletionDir* open_ref_dir (gchar* text_to_complete,
246 gchar** remaining_text,
247 CompletionState* cmpl_state);
248 static gboolean check_dir (gchar *dir_name,
250 gboolean *stat_subdirs);
251 static CompletionDir* open_dir (gchar* dir_name,
252 CompletionState* cmpl_state);
253 static CompletionDir* open_user_dir (gchar* text_to_complete,
254 CompletionState *cmpl_state);
255 static CompletionDir* open_relative_dir (gchar* dir_name, CompletionDir* dir,
256 CompletionState *cmpl_state);
257 static CompletionDirSent* open_new_dir (gchar* dir_name,
259 gboolean stat_subdirs);
260 static gint correct_dir_fullname (CompletionDir* cmpl_dir);
261 static gint correct_parent (CompletionDir* cmpl_dir,
263 static gchar* find_parent_dir_fullname (gchar* dirname);
264 static CompletionDir* attach_dir (CompletionDirSent* sent,
266 CompletionState *cmpl_state);
267 static void free_dir_sent (CompletionDirSent* sent);
268 static void free_dir (CompletionDir *dir);
269 static void prune_memory_usage(CompletionState *cmpl_state);
271 /* Completion operations */
272 static PossibleCompletion* attempt_homedir_completion(gchar* text_to_complete,
273 CompletionState *cmpl_state);
274 static PossibleCompletion* attempt_file_completion(CompletionState *cmpl_state);
275 static CompletionDir* find_completion_dir(gchar* text_to_complete,
276 gchar** remaining_text,
277 CompletionState* cmpl_state);
278 static PossibleCompletion* append_completion_text(gchar* text,
279 CompletionState* cmpl_state);
280 static gint get_pwdb(CompletionState* cmpl_state);
281 static gint first_diff_index(gchar* pat, gchar* text);
282 static gint compare_user_dir(const void* a, const void* b);
283 static gint compare_cmpl_dir(const void* a, const void* b);
284 static void update_cmpl(PossibleCompletion* poss,
285 CompletionState* cmpl_state);
287 static void gtk_file_selection_class_init (GtkFileSelectionClass *klass);
288 static void gtk_file_selection_init (GtkFileSelection *filesel);
289 static void gtk_file_selection_destroy (GtkObject *object);
290 static gint gtk_file_selection_key_press (GtkWidget *widget,
294 static void gtk_file_selection_file_button (GtkWidget *widget,
297 GdkEventButton *bevent,
300 static void gtk_file_selection_dir_button (GtkWidget *widget,
303 GdkEventButton *bevent,
306 static void gtk_file_selection_populate (GtkFileSelection *fs,
309 static void gtk_file_selection_abort (GtkFileSelection *fs);
311 static void gtk_file_selection_update_history_menu (GtkFileSelection *fs,
314 static void gtk_file_selection_create_dir (GtkWidget *widget, gpointer data);
315 static void gtk_file_selection_delete_file (GtkWidget *widget, gpointer data);
316 static void gtk_file_selection_rename_file (GtkWidget *widget, gpointer data);
320 static GtkWindowClass *parent_class = NULL;
322 /* Saves errno when something cmpl does fails. */
323 static gint cmpl_errno;
326 gtk_file_selection_get_type (void)
328 static GtkType file_selection_type = 0;
330 if (!file_selection_type)
332 static const GtkTypeInfo filesel_info =
335 sizeof (GtkFileSelection),
336 sizeof (GtkFileSelectionClass),
337 (GtkClassInitFunc) gtk_file_selection_class_init,
338 (GtkObjectInitFunc) gtk_file_selection_init,
339 /* reserved_1 */ NULL,
340 /* reserved_2 */ NULL,
341 (GtkClassInitFunc) NULL,
344 file_selection_type = gtk_type_unique (GTK_TYPE_WINDOW, &filesel_info);
347 return file_selection_type;
351 gtk_file_selection_class_init (GtkFileSelectionClass *class)
353 GtkObjectClass *object_class;
355 object_class = (GtkObjectClass*) class;
357 parent_class = gtk_type_class (GTK_TYPE_WINDOW);
359 object_class->destroy = gtk_file_selection_destroy;
363 gtk_file_selection_init (GtkFileSelection *filesel)
365 GtkWidget *entry_vbox;
367 GtkWidget *list_hbox;
368 GtkWidget *confirm_area;
369 GtkWidget *pulldown_hbox;
370 GtkWidget *scrolled_win;
372 static const char *dir_title [] = { "Directories", NULL };
373 static const char *file_title [] = { "Files", NULL };
375 filesel->cmpl_state = cmpl_init_state ();
377 /* The dialog-sized vertical box */
378 filesel->main_vbox = gtk_vbox_new (FALSE, 10);
379 gtk_container_set_border_width (GTK_CONTAINER (filesel), 10);
380 gtk_container_add (GTK_CONTAINER (filesel), filesel->main_vbox);
381 gtk_widget_show (filesel->main_vbox);
383 /* The horizontal box containing create, rename etc. buttons */
384 filesel->button_area = gtk_hbutton_box_new ();
385 gtk_button_box_set_layout(GTK_BUTTON_BOX(filesel->button_area), GTK_BUTTONBOX_START);
386 gtk_button_box_set_spacing(GTK_BUTTON_BOX(filesel->button_area), 0);
387 gtk_box_pack_start (GTK_BOX (filesel->main_vbox), filesel->button_area,
389 gtk_widget_show (filesel->button_area);
391 gtk_file_selection_show_fileop_buttons(filesel);
393 /* hbox for pulldown menu */
394 pulldown_hbox = gtk_hbox_new (TRUE, 5);
395 gtk_box_pack_start (GTK_BOX (filesel->main_vbox), pulldown_hbox, FALSE, FALSE, 0);
396 gtk_widget_show (pulldown_hbox);
399 filesel->history_pulldown = gtk_option_menu_new ();
400 gtk_widget_show (filesel->history_pulldown);
401 gtk_box_pack_start (GTK_BOX (pulldown_hbox), filesel->history_pulldown,
404 /* The horizontal box containing the directory and file listboxes */
405 list_hbox = gtk_hbox_new (FALSE, 5);
406 gtk_box_pack_start (GTK_BOX (filesel->main_vbox), list_hbox, TRUE, TRUE, 0);
407 gtk_widget_show (list_hbox);
409 /* The directories clist */
410 filesel->dir_list = gtk_clist_new_with_titles (1, dir_title);
411 gtk_widget_set_usize (filesel->dir_list, DIR_LIST_WIDTH, DIR_LIST_HEIGHT);
412 gtk_signal_connect (GTK_OBJECT (filesel->dir_list), "select_row",
413 (GtkSignalFunc) gtk_file_selection_dir_button,
415 gtk_clist_column_titles_passive (GTK_CLIST (filesel->dir_list));
417 scrolled_win = gtk_scrolled_window_new (NULL, NULL);
418 gtk_container_add (GTK_CONTAINER (scrolled_win), filesel->dir_list);
419 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled_win),
420 GTK_POLICY_AUTOMATIC, GTK_POLICY_ALWAYS);
421 gtk_container_set_border_width (GTK_CONTAINER (scrolled_win), 5);
422 gtk_box_pack_start (GTK_BOX (list_hbox), scrolled_win, TRUE, TRUE, 0);
423 gtk_widget_show (filesel->dir_list);
424 gtk_widget_show (scrolled_win);
426 /* The files clist */
427 filesel->file_list = gtk_clist_new_with_titles (1, file_title);
428 gtk_widget_set_usize (filesel->file_list, FILE_LIST_WIDTH, FILE_LIST_HEIGHT);
429 gtk_signal_connect (GTK_OBJECT (filesel->file_list), "select_row",
430 (GtkSignalFunc) gtk_file_selection_file_button,
432 gtk_clist_column_titles_passive (GTK_CLIST (filesel->file_list));
434 scrolled_win = gtk_scrolled_window_new (NULL, NULL);
435 gtk_container_add (GTK_CONTAINER (scrolled_win), filesel->file_list);
436 gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled_win),
437 GTK_POLICY_AUTOMATIC, GTK_POLICY_ALWAYS);
438 gtk_container_set_border_width (GTK_CONTAINER (scrolled_win), 5);
439 gtk_box_pack_start (GTK_BOX (list_hbox), scrolled_win, TRUE, TRUE, 0);
440 gtk_widget_show (filesel->file_list);
441 gtk_widget_show (scrolled_win);
443 /* action area for packing buttons into. */
444 filesel->action_area = gtk_hbox_new (TRUE, 0);
445 gtk_box_pack_start (GTK_BOX (filesel->main_vbox), filesel->action_area,
447 gtk_widget_show (filesel->action_area);
449 /* The OK/Cancel button area */
450 confirm_area = gtk_hbutton_box_new ();
451 gtk_button_box_set_layout(GTK_BUTTON_BOX(confirm_area), GTK_BUTTONBOX_END);
452 gtk_button_box_set_spacing(GTK_BUTTON_BOX(confirm_area), 5);
453 gtk_box_pack_end (GTK_BOX (filesel->main_vbox), confirm_area, FALSE, FALSE, 0);
454 gtk_widget_show (confirm_area);
457 filesel->ok_button = gtk_button_new_with_label ("OK");
458 GTK_WIDGET_SET_FLAGS (filesel->ok_button, GTK_CAN_DEFAULT);
459 gtk_box_pack_start (GTK_BOX (confirm_area), filesel->ok_button, TRUE, TRUE, 0);
460 gtk_widget_grab_default (filesel->ok_button);
461 gtk_widget_show (filesel->ok_button);
463 /* The Cancel button */
464 filesel->cancel_button = gtk_button_new_with_label ("Cancel");
465 GTK_WIDGET_SET_FLAGS (filesel->cancel_button, GTK_CAN_DEFAULT);
466 gtk_box_pack_start (GTK_BOX (confirm_area), filesel->cancel_button, TRUE, TRUE, 0);
467 gtk_widget_show (filesel->cancel_button);
469 /* The selection entry widget */
470 entry_vbox = gtk_vbox_new (FALSE, 2);
471 gtk_box_pack_end (GTK_BOX (filesel->main_vbox), entry_vbox, FALSE, FALSE, 0);
472 gtk_widget_show (entry_vbox);
474 filesel->selection_text = label = gtk_label_new ("");
475 gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.5);
476 gtk_box_pack_start (GTK_BOX (entry_vbox), label, FALSE, FALSE, 0);
477 gtk_widget_show (label);
479 filesel->selection_entry = gtk_entry_new ();
480 gtk_signal_connect (GTK_OBJECT (filesel->selection_entry), "key_press_event",
481 (GtkSignalFunc) gtk_file_selection_key_press, filesel);
482 gtk_signal_connect_object (GTK_OBJECT (filesel->selection_entry), "focus_in_event",
483 (GtkSignalFunc) gtk_widget_grab_default,
484 GTK_OBJECT (filesel->ok_button));
485 gtk_signal_connect_object (GTK_OBJECT (filesel->selection_entry), "activate",
486 (GtkSignalFunc) gtk_button_clicked,
487 GTK_OBJECT (filesel->ok_button));
488 gtk_box_pack_start (GTK_BOX (entry_vbox), filesel->selection_entry, TRUE, TRUE, 0);
489 gtk_widget_show (filesel->selection_entry);
491 if (!cmpl_state_okay (filesel->cmpl_state))
495 sprintf (err_buf, "Directory unreadable: %s", cmpl_strerror (cmpl_errno));
497 gtk_label_set (GTK_LABEL (filesel->selection_text), err_buf);
501 gtk_file_selection_populate (filesel, "", FALSE);
504 gtk_widget_grab_focus (filesel->selection_entry);
508 gtk_file_selection_new (const gchar *title)
510 GtkFileSelection *filesel;
512 filesel = gtk_type_new (GTK_TYPE_FILE_SELECTION);
513 gtk_window_set_title (GTK_WINDOW (filesel), title);
515 return GTK_WIDGET (filesel);
519 gtk_file_selection_show_fileop_buttons (GtkFileSelection *filesel)
521 g_return_if_fail (filesel != NULL);
522 g_return_if_fail (GTK_IS_FILE_SELECTION (filesel));
524 /* delete, create directory, and rename */
525 if (!filesel->fileop_c_dir)
527 filesel->fileop_c_dir = gtk_button_new_with_label ("Create Dir");
528 gtk_signal_connect (GTK_OBJECT (filesel->fileop_c_dir), "clicked",
529 (GtkSignalFunc) gtk_file_selection_create_dir,
531 gtk_box_pack_start (GTK_BOX (filesel->button_area),
532 filesel->fileop_c_dir, TRUE, TRUE, 0);
533 gtk_widget_show (filesel->fileop_c_dir);
536 if (!filesel->fileop_del_file)
538 filesel->fileop_del_file = gtk_button_new_with_label ("Delete File");
539 gtk_signal_connect (GTK_OBJECT (filesel->fileop_del_file), "clicked",
540 (GtkSignalFunc) gtk_file_selection_delete_file,
542 gtk_box_pack_start (GTK_BOX (filesel->button_area),
543 filesel->fileop_del_file, TRUE, TRUE, 0);
544 gtk_widget_show (filesel->fileop_del_file);
547 if (!filesel->fileop_ren_file)
549 filesel->fileop_ren_file = gtk_button_new_with_label ("Rename File");
550 gtk_signal_connect (GTK_OBJECT (filesel->fileop_ren_file), "clicked",
551 (GtkSignalFunc) gtk_file_selection_rename_file,
553 gtk_box_pack_start (GTK_BOX (filesel->button_area),
554 filesel->fileop_ren_file, TRUE, TRUE, 0);
555 gtk_widget_show (filesel->fileop_ren_file);
558 gtk_widget_queue_resize(GTK_WIDGET(filesel));
562 gtk_file_selection_hide_fileop_buttons (GtkFileSelection *filesel)
564 g_return_if_fail (filesel != NULL);
565 g_return_if_fail (GTK_IS_FILE_SELECTION (filesel));
567 if (filesel->fileop_ren_file)
569 gtk_widget_destroy (filesel->fileop_ren_file);
570 filesel->fileop_ren_file = NULL;
573 if (filesel->fileop_del_file)
575 gtk_widget_destroy (filesel->fileop_del_file);
576 filesel->fileop_del_file = NULL;
579 if (filesel->fileop_c_dir)
581 gtk_widget_destroy (filesel->fileop_c_dir);
582 filesel->fileop_c_dir = NULL;
589 gtk_file_selection_set_filename (GtkFileSelection *filesel,
590 const gchar *filename)
592 char buf[MAXPATHLEN];
593 const char *name, *last_slash;
595 g_return_if_fail (filesel != NULL);
596 g_return_if_fail (GTK_IS_FILE_SELECTION (filesel));
597 g_return_if_fail (filename != NULL);
599 last_slash = strrchr (filename, '/');
608 gint len = MIN (MAXPATHLEN - 1, last_slash - filename + 1);
610 strncpy (buf, filename, len);
613 name = last_slash + 1;
616 gtk_file_selection_populate (filesel, buf, FALSE);
618 if (filesel->selection_entry)
619 gtk_entry_set_text (GTK_ENTRY (filesel->selection_entry), name);
623 gtk_file_selection_get_filename (GtkFileSelection *filesel)
625 static char nothing[2] = "";
629 g_return_val_if_fail (filesel != NULL, nothing);
630 g_return_val_if_fail (GTK_IS_FILE_SELECTION (filesel), nothing);
632 text = gtk_entry_get_text (GTK_ENTRY (filesel->selection_entry));
635 filename = cmpl_completion_fullname (text, filesel->cmpl_state);
643 gtk_file_selection_complete (GtkFileSelection *filesel,
644 const gchar *pattern)
646 g_return_if_fail (filesel != NULL);
647 g_return_if_fail (GTK_IS_FILE_SELECTION (filesel));
648 g_return_if_fail (pattern != NULL);
650 if (filesel->selection_entry)
651 gtk_entry_set_text (GTK_ENTRY (filesel->selection_entry), pattern);
652 gtk_file_selection_populate (filesel, (gchar*) pattern, TRUE);
656 gtk_file_selection_destroy (GtkObject *object)
658 GtkFileSelection *filesel;
660 HistoryCallbackArg *callback_arg;
662 g_return_if_fail (object != NULL);
663 g_return_if_fail (GTK_IS_FILE_SELECTION (object));
665 filesel = GTK_FILE_SELECTION (object);
667 if (filesel->fileop_dialog)
668 gtk_widget_destroy (filesel->fileop_dialog);
670 if (filesel->history_list)
672 list = filesel->history_list;
675 callback_arg = list->data;
676 g_free (callback_arg->directory);
677 g_free (callback_arg);
680 g_list_free (filesel->history_list);
681 filesel->history_list = NULL;
684 cmpl_free_state (filesel->cmpl_state);
685 filesel->cmpl_state = NULL;
687 if (GTK_OBJECT_CLASS (parent_class)->destroy)
688 (* GTK_OBJECT_CLASS (parent_class)->destroy) (object);
691 /* Begin file operations callbacks */
694 gtk_file_selection_fileop_error (gchar *error_message)
701 g_return_if_fail (error_message != NULL);
704 dialog = gtk_dialog_new ();
706 gtk_signal_connect (GTK_OBJECT (dialog), "destroy",
707 (GtkSignalFunc) gtk_file_selection_fileop_destroy,
710 gtk_window_set_title (GTK_WINDOW (dialog), "Error");
711 gtk_window_set_position (GTK_WINDOW (dialog), GTK_WIN_POS_MOUSE);
713 vbox = gtk_vbox_new(FALSE, 0);
714 gtk_container_set_border_width(GTK_CONTAINER(vbox), 8);
715 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->vbox), vbox,
717 gtk_widget_show(vbox);
719 label = gtk_label_new(error_message);
720 gtk_misc_set_alignment(GTK_MISC(label), 0.0, 0.0);
721 gtk_box_pack_start(GTK_BOX(vbox), label, FALSE, FALSE, 5);
722 gtk_widget_show(label);
724 /* yes, we free it */
725 g_free (error_message);
728 button = gtk_button_new_with_label ("Close");
729 gtk_signal_connect_object (GTK_OBJECT (button), "clicked",
730 (GtkSignalFunc) gtk_widget_destroy,
732 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->action_area),
733 button, TRUE, TRUE, 0);
734 GTK_WIDGET_SET_FLAGS(button, GTK_CAN_DEFAULT);
735 gtk_widget_grab_default(button);
736 gtk_widget_show (button);
738 gtk_widget_show (dialog);
742 gtk_file_selection_fileop_destroy (GtkWidget *widget, gpointer data)
744 GtkFileSelection *fs = data;
746 g_return_if_fail (fs != NULL);
747 g_return_if_fail (GTK_IS_FILE_SELECTION (fs));
749 fs->fileop_dialog = NULL;
754 gtk_file_selection_create_dir_confirmed (GtkWidget *widget, gpointer data)
756 GtkFileSelection *fs = data;
761 CompletionState *cmpl_state;
763 g_return_if_fail (fs != NULL);
764 g_return_if_fail (GTK_IS_FILE_SELECTION (fs));
766 dirname = gtk_entry_get_text (GTK_ENTRY (fs->fileop_entry));
767 cmpl_state = (CompletionState*) fs->cmpl_state;
768 path = cmpl_reference_position (cmpl_state);
770 full_path = g_strconcat (path, "/", dirname, NULL);
771 if ( (mkdir (full_path, 0755) < 0) )
773 buf = g_strconcat ("Error creating directory \"", dirname, "\": ",
774 g_strerror(errno), NULL);
775 gtk_file_selection_fileop_error (buf);
779 gtk_widget_destroy (fs->fileop_dialog);
780 gtk_file_selection_populate (fs, "", FALSE);
784 gtk_file_selection_create_dir (GtkWidget *widget, gpointer data)
786 GtkFileSelection *fs = data;
792 g_return_if_fail (fs != NULL);
793 g_return_if_fail (GTK_IS_FILE_SELECTION (fs));
795 if (fs->fileop_dialog)
799 fs->fileop_dialog = dialog = gtk_dialog_new ();
800 gtk_signal_connect (GTK_OBJECT (dialog), "destroy",
801 (GtkSignalFunc) gtk_file_selection_fileop_destroy,
803 gtk_window_set_title (GTK_WINDOW (dialog), "Create Directory");
804 gtk_window_set_position (GTK_WINDOW (dialog), GTK_WIN_POS_MOUSE);
806 /* If file dialog is grabbed, grab option dialog */
807 /* When option dialog is closed, file dialog will be grabbed again */
808 if (GTK_WINDOW(fs)->modal)
809 gtk_window_set_modal (GTK_WINDOW(dialog), TRUE);
811 vbox = gtk_vbox_new(FALSE, 0);
812 gtk_container_set_border_width(GTK_CONTAINER(vbox), 8);
813 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->vbox), vbox,
815 gtk_widget_show(vbox);
817 label = gtk_label_new("Directory name:");
818 gtk_misc_set_alignment(GTK_MISC(label), 0.0, 0.0);
819 gtk_box_pack_start(GTK_BOX(vbox), label, FALSE, FALSE, 5);
820 gtk_widget_show(label);
822 /* The directory entry widget */
823 fs->fileop_entry = gtk_entry_new ();
824 gtk_box_pack_start (GTK_BOX (vbox), fs->fileop_entry,
826 GTK_WIDGET_SET_FLAGS(fs->fileop_entry, GTK_CAN_DEFAULT);
827 gtk_widget_show (fs->fileop_entry);
830 button = gtk_button_new_with_label ("Create");
831 gtk_signal_connect (GTK_OBJECT (button), "clicked",
832 (GtkSignalFunc) gtk_file_selection_create_dir_confirmed,
834 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->action_area),
835 button, TRUE, TRUE, 0);
836 GTK_WIDGET_SET_FLAGS(button, GTK_CAN_DEFAULT);
837 gtk_widget_show(button);
839 button = gtk_button_new_with_label ("Cancel");
840 gtk_signal_connect_object (GTK_OBJECT (button), "clicked",
841 (GtkSignalFunc) gtk_widget_destroy,
843 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->action_area),
844 button, TRUE, TRUE, 0);
845 GTK_WIDGET_SET_FLAGS(button, GTK_CAN_DEFAULT);
846 gtk_widget_grab_default(button);
847 gtk_widget_show (button);
849 gtk_widget_show (dialog);
853 gtk_file_selection_delete_file_confirmed (GtkWidget *widget, gpointer data)
855 GtkFileSelection *fs = data;
856 CompletionState *cmpl_state;
861 g_return_if_fail (fs != NULL);
862 g_return_if_fail (GTK_IS_FILE_SELECTION (fs));
864 cmpl_state = (CompletionState*) fs->cmpl_state;
865 path = cmpl_reference_position (cmpl_state);
867 full_path = g_strconcat (path, "/", fs->fileop_file, NULL);
868 if ( (unlink (full_path) < 0) )
870 buf = g_strconcat ("Error deleting file \"", fs->fileop_file, "\": ",
871 g_strerror(errno), NULL);
872 gtk_file_selection_fileop_error (buf);
876 gtk_widget_destroy (fs->fileop_dialog);
877 gtk_file_selection_populate (fs, "", FALSE);
881 gtk_file_selection_delete_file (GtkWidget *widget, gpointer data)
883 GtkFileSelection *fs = data;
891 g_return_if_fail (fs != NULL);
892 g_return_if_fail (GTK_IS_FILE_SELECTION (fs));
894 if (fs->fileop_dialog)
897 filename = gtk_entry_get_text (GTK_ENTRY (fs->selection_entry));
898 if (strlen(filename) < 1)
901 fs->fileop_file = filename;
904 fs->fileop_dialog = dialog = gtk_dialog_new ();
905 gtk_signal_connect (GTK_OBJECT (dialog), "destroy",
906 (GtkSignalFunc) gtk_file_selection_fileop_destroy,
908 gtk_window_set_title (GTK_WINDOW (dialog), "Delete File");
909 gtk_window_set_position (GTK_WINDOW (dialog), GTK_WIN_POS_MOUSE);
911 /* If file dialog is grabbed, grab option dialog */
912 /* When option dialog is closed, file dialog will be grabbed again */
913 if (GTK_WINDOW(fs)->modal)
914 gtk_window_set_modal (GTK_WINDOW(dialog), TRUE);
916 vbox = gtk_vbox_new(FALSE, 0);
917 gtk_container_set_border_width(GTK_CONTAINER(vbox), 8);
918 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->vbox), vbox,
920 gtk_widget_show(vbox);
922 buf = g_strconcat ("Really delete file \"", filename, "\" ?", NULL);
923 label = gtk_label_new(buf);
924 gtk_misc_set_alignment(GTK_MISC(label), 0.0, 0.0);
925 gtk_box_pack_start(GTK_BOX(vbox), label, FALSE, FALSE, 5);
926 gtk_widget_show(label);
930 button = gtk_button_new_with_label ("Delete");
931 gtk_signal_connect (GTK_OBJECT (button), "clicked",
932 (GtkSignalFunc) gtk_file_selection_delete_file_confirmed,
934 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->action_area),
935 button, TRUE, TRUE, 0);
936 GTK_WIDGET_SET_FLAGS(button, GTK_CAN_DEFAULT);
937 gtk_widget_show(button);
939 button = gtk_button_new_with_label ("Cancel");
940 gtk_signal_connect_object (GTK_OBJECT (button), "clicked",
941 (GtkSignalFunc) gtk_widget_destroy,
943 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->action_area),
944 button, TRUE, TRUE, 0);
945 GTK_WIDGET_SET_FLAGS(button, GTK_CAN_DEFAULT);
946 gtk_widget_grab_default(button);
947 gtk_widget_show (button);
949 gtk_widget_show (dialog);
954 gtk_file_selection_rename_file_confirmed (GtkWidget *widget, gpointer data)
956 GtkFileSelection *fs = data;
962 CompletionState *cmpl_state;
964 g_return_if_fail (fs != NULL);
965 g_return_if_fail (GTK_IS_FILE_SELECTION (fs));
967 file = gtk_entry_get_text (GTK_ENTRY (fs->fileop_entry));
968 cmpl_state = (CompletionState*) fs->cmpl_state;
969 path = cmpl_reference_position (cmpl_state);
971 new_filename = g_strconcat (path, "/", file, NULL);
972 old_filename = g_strconcat (path, "/", fs->fileop_file, NULL);
974 if ( (rename (old_filename, new_filename)) < 0)
976 buf = g_strconcat ("Error renaming file \"", file, "\": ",
977 g_strerror(errno), NULL);
978 gtk_file_selection_fileop_error (buf);
980 g_free (new_filename);
981 g_free (old_filename);
983 gtk_widget_destroy (fs->fileop_dialog);
984 gtk_file_selection_populate (fs, "", FALSE);
988 gtk_file_selection_rename_file (GtkWidget *widget, gpointer data)
990 GtkFileSelection *fs = data;
997 g_return_if_fail (fs != NULL);
998 g_return_if_fail (GTK_IS_FILE_SELECTION (fs));
1000 if (fs->fileop_dialog)
1003 fs->fileop_file = gtk_entry_get_text (GTK_ENTRY (fs->selection_entry));
1004 if (strlen(fs->fileop_file) < 1)
1008 fs->fileop_dialog = dialog = gtk_dialog_new ();
1009 gtk_signal_connect (GTK_OBJECT (dialog), "destroy",
1010 (GtkSignalFunc) gtk_file_selection_fileop_destroy,
1012 gtk_window_set_title (GTK_WINDOW (dialog), "Rename File");
1013 gtk_window_set_position (GTK_WINDOW (dialog), GTK_WIN_POS_MOUSE);
1015 /* If file dialog is grabbed, grab option dialog */
1016 /* When option dialog closed, file dialog will be grabbed again */
1017 if (GTK_WINDOW(fs)->modal)
1018 gtk_window_set_modal (GTK_WINDOW(dialog), TRUE);
1020 vbox = gtk_vbox_new(FALSE, 0);
1021 gtk_container_set_border_width (GTK_CONTAINER(vbox), 8);
1022 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->vbox), vbox,
1024 gtk_widget_show(vbox);
1026 buf = g_strconcat ("Rename file \"", fs->fileop_file, "\" to:", NULL);
1027 label = gtk_label_new(buf);
1028 gtk_misc_set_alignment(GTK_MISC(label), 0.0, 0.0);
1029 gtk_box_pack_start(GTK_BOX(vbox), label, FALSE, FALSE, 5);
1030 gtk_widget_show(label);
1033 /* New filename entry */
1034 fs->fileop_entry = gtk_entry_new ();
1035 gtk_box_pack_start (GTK_BOX (vbox), fs->fileop_entry,
1037 GTK_WIDGET_SET_FLAGS(fs->fileop_entry, GTK_CAN_DEFAULT);
1038 gtk_widget_show (fs->fileop_entry);
1040 gtk_entry_set_text (GTK_ENTRY (fs->fileop_entry), fs->fileop_file);
1041 gtk_editable_select_region (GTK_EDITABLE (fs->fileop_entry),
1042 0, strlen (fs->fileop_file));
1045 button = gtk_button_new_with_label ("Rename");
1046 gtk_signal_connect (GTK_OBJECT (button), "clicked",
1047 (GtkSignalFunc) gtk_file_selection_rename_file_confirmed,
1049 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->action_area),
1050 button, TRUE, TRUE, 0);
1051 GTK_WIDGET_SET_FLAGS(button, GTK_CAN_DEFAULT);
1052 gtk_widget_show(button);
1054 button = gtk_button_new_with_label ("Cancel");
1055 gtk_signal_connect_object (GTK_OBJECT (button), "clicked",
1056 (GtkSignalFunc) gtk_widget_destroy,
1058 gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog)->action_area),
1059 button, TRUE, TRUE, 0);
1060 GTK_WIDGET_SET_FLAGS(button, GTK_CAN_DEFAULT);
1061 gtk_widget_grab_default(button);
1062 gtk_widget_show (button);
1064 gtk_widget_show (dialog);
1069 gtk_file_selection_key_press (GtkWidget *widget,
1073 GtkFileSelection *fs;
1076 g_return_val_if_fail (widget != NULL, FALSE);
1077 g_return_val_if_fail (event != NULL, FALSE);
1079 if (event->keyval == GDK_Tab)
1083 fs = GTK_FILE_SELECTION (user_data);
1084 text = gtk_entry_get_text (GTK_ENTRY (fs->selection_entry));
1086 intercept = text && *text;
1088 text = g_strdup (text);
1090 gtk_file_selection_populate (fs, text, TRUE);
1095 gtk_signal_emit_stop_by_name (GTK_OBJECT (widget), "key_press_event");
1105 gtk_file_selection_history_callback (GtkWidget *widget, gpointer data)
1107 GtkFileSelection *fs = data;
1108 HistoryCallbackArg *callback_arg;
1111 g_return_if_fail (fs != NULL);
1112 g_return_if_fail (GTK_IS_FILE_SELECTION (fs));
1114 list = fs->history_list;
1117 callback_arg = list->data;
1119 if (callback_arg->menu_item == widget)
1121 gtk_file_selection_populate (fs, callback_arg->directory, FALSE);
1130 gtk_file_selection_update_history_menu (GtkFileSelection *fs,
1131 gchar *current_directory)
1133 HistoryCallbackArg *callback_arg;
1134 GtkWidget *menu_item;
1140 g_return_if_fail (fs != NULL);
1141 g_return_if_fail (GTK_IS_FILE_SELECTION (fs));
1142 g_return_if_fail (current_directory != NULL);
1144 list = fs->history_list;
1146 if (fs->history_menu)
1149 callback_arg = list->data;
1150 g_free (callback_arg->directory);
1151 g_free (callback_arg);
1154 g_list_free (fs->history_list);
1155 fs->history_list = NULL;
1157 gtk_widget_destroy (fs->history_menu);
1160 fs->history_menu = gtk_menu_new();
1162 current_dir = g_strdup (current_directory);
1164 dir_len = strlen (current_dir);
1166 for (i = dir_len; i >= 0; i--)
1168 /* the i == dir_len is to catch the full path for the first
1170 if ( (current_dir[i] == '/') || (i == dir_len))
1172 /* another small hack to catch the full path */
1174 current_dir[i + 1] = '\0';
1175 menu_item = gtk_menu_item_new_with_label (current_dir);
1177 callback_arg = g_new (HistoryCallbackArg, 1);
1178 callback_arg->menu_item = menu_item;
1180 /* since the autocompletion gets confused if you don't
1181 * supply a trailing '/' on a dir entry, set the full
1182 * (current) path to "" which just refreshes the filesel */
1184 callback_arg->directory = g_strdup ("");
1186 callback_arg->directory = g_strdup (current_dir);
1189 fs->history_list = g_list_append (fs->history_list, callback_arg);
1191 gtk_signal_connect (GTK_OBJECT (menu_item), "activate",
1192 (GtkSignalFunc) gtk_file_selection_history_callback,
1194 gtk_menu_append (GTK_MENU (fs->history_menu), menu_item);
1195 gtk_widget_show (menu_item);
1199 gtk_option_menu_set_menu (GTK_OPTION_MENU (fs->history_pulldown),
1201 g_free (current_dir);
1205 gtk_file_selection_file_button (GtkWidget *widget,
1208 GdkEventButton *bevent,
1211 GtkFileSelection *fs = NULL;
1212 gchar *filename, *temp = NULL;
1214 g_return_if_fail (GTK_IS_CLIST (widget));
1217 g_return_if_fail (fs != NULL);
1218 g_return_if_fail (GTK_IS_FILE_SELECTION (fs));
1220 gtk_clist_get_text (GTK_CLIST (fs->file_list), row, 0, &temp);
1221 filename = g_strdup (temp);
1226 switch (bevent->type)
1228 case GDK_2BUTTON_PRESS:
1229 gtk_button_clicked (GTK_BUTTON (fs->ok_button));
1233 gtk_entry_set_text (GTK_ENTRY (fs->selection_entry), filename);
1237 gtk_entry_set_text (GTK_ENTRY (fs->selection_entry), filename);
1244 gtk_file_selection_dir_button (GtkWidget *widget,
1247 GdkEventButton *bevent,
1250 GtkFileSelection *fs = NULL;
1251 gchar *filename, *temp = NULL;
1253 g_return_if_fail (GTK_IS_CLIST (widget));
1255 fs = GTK_FILE_SELECTION (user_data);
1256 g_return_if_fail (fs != NULL);
1257 g_return_if_fail (GTK_IS_FILE_SELECTION (fs));
1259 gtk_clist_get_text (GTK_CLIST (fs->dir_list), row, 0, &temp);
1260 filename = g_strdup (temp);
1265 switch (bevent->type)
1267 case GDK_2BUTTON_PRESS:
1268 gtk_file_selection_populate (fs, filename, FALSE);
1272 gtk_entry_set_text (GTK_ENTRY (fs->selection_entry), filename);
1276 gtk_entry_set_text (GTK_ENTRY (fs->selection_entry), filename);
1283 gtk_file_selection_populate (GtkFileSelection *fs,
1287 CompletionState *cmpl_state;
1288 PossibleCompletion* poss;
1291 gchar* rem_path = rel_path;
1294 gint did_recurse = FALSE;
1295 gint possible_count = 0;
1296 gint selection_index = -1;
1297 gint file_list_width;
1298 gint dir_list_width;
1300 g_return_if_fail (fs != NULL);
1301 g_return_if_fail (GTK_IS_FILE_SELECTION (fs));
1303 cmpl_state = (CompletionState*) fs->cmpl_state;
1304 poss = cmpl_completion_matches (rel_path, &rem_path, cmpl_state);
1306 if (!cmpl_state_okay (cmpl_state))
1308 /* Something went wrong. */
1309 gtk_file_selection_abort (fs);
1313 g_assert (cmpl_state->reference_dir);
1315 gtk_clist_freeze (GTK_CLIST (fs->dir_list));
1316 gtk_clist_clear (GTK_CLIST (fs->dir_list));
1317 gtk_clist_freeze (GTK_CLIST (fs->file_list));
1318 gtk_clist_clear (GTK_CLIST (fs->file_list));
1320 /* Set the dir_list to include ./ and ../ */
1323 row = gtk_clist_append (GTK_CLIST (fs->dir_list), text);
1326 row = gtk_clist_append (GTK_CLIST (fs->dir_list), text);
1328 /*reset the max widths of the lists*/
1329 dir_list_width = gdk_string_width(fs->dir_list->style->font,"../");
1330 gtk_clist_set_column_width(GTK_CLIST(fs->dir_list),0,dir_list_width);
1331 file_list_width = 1;
1332 gtk_clist_set_column_width(GTK_CLIST(fs->file_list),0,file_list_width);
1336 if (cmpl_is_a_completion (poss))
1338 possible_count += 1;
1340 filename = cmpl_this_completion (poss);
1344 if (cmpl_is_directory (poss))
1346 if (strcmp (filename, "./") != 0 &&
1347 strcmp (filename, "../") != 0)
1349 int width = gdk_string_width(fs->dir_list->style->font,
1351 row = gtk_clist_append (GTK_CLIST (fs->dir_list), text);
1352 if(width > dir_list_width)
1354 dir_list_width = width;
1355 gtk_clist_set_column_width(GTK_CLIST(fs->dir_list),0,
1362 int width = gdk_string_width(fs->file_list->style->font,
1364 row = gtk_clist_append (GTK_CLIST (fs->file_list), text);
1365 if(width > file_list_width)
1367 file_list_width = width;
1368 gtk_clist_set_column_width(GTK_CLIST(fs->file_list),0,
1374 poss = cmpl_next_completion (cmpl_state);
1377 gtk_clist_thaw (GTK_CLIST (fs->dir_list));
1378 gtk_clist_thaw (GTK_CLIST (fs->file_list));
1380 /* File lists are set. */
1382 g_assert (cmpl_state->reference_dir);
1387 /* User is trying to complete filenames, so advance the user's input
1388 * string to the updated_text, which is the common leading substring
1389 * of all possible completions, and if its a directory attempt
1390 * attempt completions in it. */
1392 if (cmpl_updated_text (cmpl_state)[0])
1395 if (cmpl_updated_dir (cmpl_state))
1397 gchar* dir_name = g_strdup (cmpl_updated_text (cmpl_state));
1401 gtk_file_selection_populate (fs, dir_name, TRUE);
1407 if (fs->selection_entry)
1408 gtk_entry_set_text (GTK_ENTRY (fs->selection_entry),
1409 cmpl_updated_text (cmpl_state));
1414 selection_index = cmpl_last_valid_char (cmpl_state) -
1415 (strlen (rel_path) - strlen (rem_path));
1416 if (fs->selection_entry)
1417 gtk_entry_set_text (GTK_ENTRY (fs->selection_entry), rem_path);
1422 if (fs->selection_entry)
1423 gtk_entry_set_text (GTK_ENTRY (fs->selection_entry), "");
1428 if (fs->selection_entry)
1429 gtk_entry_set_position (GTK_ENTRY (fs->selection_entry), selection_index);
1431 if (fs->selection_entry)
1433 sel_text = g_new (char, strlen (cmpl_reference_position (cmpl_state)) +
1434 sizeof ("Selection: "));
1435 strcpy (sel_text, "Selection: ");
1436 strcat (sel_text, cmpl_reference_position (cmpl_state));
1438 gtk_label_set (GTK_LABEL (fs->selection_text), sel_text);
1442 if (fs->history_pulldown)
1444 gtk_file_selection_update_history_menu (fs, cmpl_reference_position (cmpl_state));
1451 gtk_file_selection_abort (GtkFileSelection *fs)
1455 sprintf (err_buf, "Directory unreadable: %s", cmpl_strerror (cmpl_errno));
1457 /* BEEP gdk_beep(); */
1459 if (fs->selection_entry)
1460 gtk_label_set (GTK_LABEL (fs->selection_text), err_buf);
1463 /**********************************************************************/
1464 /* External Interface */
1465 /**********************************************************************/
1467 /* The four completion state selectors
1470 cmpl_updated_text (CompletionState* cmpl_state)
1472 return cmpl_state->updated_text;
1476 cmpl_updated_dir (CompletionState* cmpl_state)
1478 return cmpl_state->re_complete;
1482 cmpl_reference_position (CompletionState* cmpl_state)
1484 return cmpl_state->reference_dir->fullname;
1488 cmpl_last_valid_char (CompletionState* cmpl_state)
1490 return cmpl_state->last_valid_char;
1494 cmpl_completion_fullname (gchar* text, CompletionState* cmpl_state)
1496 static char nothing[2] = "";
1498 if (!cmpl_state_okay (cmpl_state))
1502 else if (text[0] == '/')
1504 strcpy (cmpl_state->updated_text, text);
1506 else if (text[0] == '~')
1511 dir = open_user_dir (text, cmpl_state);
1515 /* spencer says just return ~something, so
1516 * for now just do it. */
1517 strcpy (cmpl_state->updated_text, text);
1522 strcpy (cmpl_state->updated_text, dir->fullname);
1524 slash = strchr (text, '/');
1527 strcat (cmpl_state->updated_text, slash);
1532 strcpy (cmpl_state->updated_text, cmpl_state->reference_dir->fullname);
1533 strcat (cmpl_state->updated_text, "/");
1534 strcat (cmpl_state->updated_text, text);
1537 return cmpl_state->updated_text;
1540 /* The three completion selectors
1543 cmpl_this_completion (PossibleCompletion* pc)
1549 cmpl_is_directory (PossibleCompletion* pc)
1551 return pc->is_directory;
1555 cmpl_is_a_completion (PossibleCompletion* pc)
1557 return pc->is_a_completion;
1560 /**********************************************************************/
1561 /* Construction, deletion */
1562 /**********************************************************************/
1564 static CompletionState*
1565 cmpl_init_state (void)
1567 gchar getcwd_buf[2*MAXPATHLEN];
1568 CompletionState *new_state;
1570 new_state = g_new (CompletionState, 1);
1572 /* We don't use getcwd() on SUNOS, because, it does a popen("pwd")
1573 * and, if that wasn't bad enough, hangs in doing so.
1575 #if defined(sun) && !defined(__SVR4)
1576 if (!getwd (getcwd_buf))
1578 if (!getcwd (getcwd_buf, MAXPATHLEN))
1581 /* Oh joy, we can't get the current directory. Um..., we should have
1582 * a root directory, right? Right? (Probably not portable to non-Unix)
1584 strcpy (getcwd_buf, "/");
1589 new_state->reference_dir = NULL;
1590 new_state->completion_dir = NULL;
1591 new_state->active_completion_dir = NULL;
1593 if ((new_state->user_home_dir = getenv("HOME")) != NULL)
1595 /* if this fails, get_pwdb will fill it in. */
1596 new_state->user_home_dir = g_strdup(new_state->user_home_dir);
1599 new_state->directory_storage = NULL;
1600 new_state->directory_sent_storage = NULL;
1601 new_state->last_valid_char = 0;
1602 new_state->updated_text = g_new (gchar, MAXPATHLEN);
1603 new_state->updated_text_alloc = MAXPATHLEN;
1604 new_state->the_completion.text = g_new (gchar, MAXPATHLEN);
1605 new_state->the_completion.text_alloc = MAXPATHLEN;
1606 new_state->user_dir_name_buffer = NULL;
1607 new_state->user_directories = NULL;
1609 new_state->reference_dir = open_dir (getcwd_buf, new_state);
1611 if (!new_state->reference_dir)
1613 /* Directories changing from underneath us, grumble */
1614 strcpy (getcwd_buf, "/");
1622 cmpl_free_dir_list(GList* dp0)
1627 free_dir (dp->data);
1635 cmpl_free_dir_sent_list(GList* dp0)
1640 free_dir_sent (dp->data);
1648 cmpl_free_state (CompletionState* cmpl_state)
1650 cmpl_free_dir_list (cmpl_state->directory_storage);
1651 cmpl_free_dir_sent_list (cmpl_state->directory_sent_storage);
1653 if (cmpl_state->user_dir_name_buffer)
1654 g_free (cmpl_state->user_dir_name_buffer);
1655 if (cmpl_state->user_home_dir)
1656 g_free (cmpl_state->user_home_dir);
1657 if (cmpl_state->user_directories)
1658 g_free (cmpl_state->user_directories);
1659 if (cmpl_state->the_completion.text)
1660 g_free (cmpl_state->the_completion.text);
1661 if (cmpl_state->updated_text)
1662 g_free (cmpl_state->updated_text);
1664 g_free (cmpl_state);
1668 free_dir(CompletionDir* dir)
1670 g_free(dir->fullname);
1675 free_dir_sent(CompletionDirSent* sent)
1677 g_free(sent->name_buffer);
1678 g_free(sent->entries);
1683 prune_memory_usage(CompletionState *cmpl_state)
1685 GList* cdsl = cmpl_state->directory_sent_storage;
1686 GList* cdl = cmpl_state->directory_storage;
1690 for(; cdsl && len < CMPL_DIRECTORY_CACHE_SIZE; len += 1)
1694 cmpl_free_dir_sent_list(cdsl->next);
1698 cmpl_state->directory_storage = NULL;
1700 if (cdl->data == cmpl_state->reference_dir)
1701 cmpl_state->directory_storage = g_list_prepend(NULL, cdl->data);
1703 free_dir (cdl->data);
1710 /**********************************************************************/
1711 /* The main entrances. */
1712 /**********************************************************************/
1714 static PossibleCompletion*
1715 cmpl_completion_matches (gchar* text_to_complete,
1716 gchar** remaining_text,
1717 CompletionState* cmpl_state)
1720 PossibleCompletion *poss;
1722 prune_memory_usage(cmpl_state);
1724 g_assert (text_to_complete != NULL);
1726 cmpl_state->user_completion_index = -1;
1727 cmpl_state->last_completion_text = text_to_complete;
1728 cmpl_state->the_completion.text[0] = 0;
1729 cmpl_state->last_valid_char = 0;
1730 cmpl_state->updated_text_len = -1;
1731 cmpl_state->updated_text[0] = 0;
1732 cmpl_state->re_complete = FALSE;
1734 first_slash = strchr (text_to_complete, '/');
1736 if (text_to_complete[0] == '~' && !first_slash)
1738 /* Text starts with ~ and there is no slash, show all the
1739 * home directory completions.
1741 poss = attempt_homedir_completion (text_to_complete, cmpl_state);
1743 update_cmpl(poss, cmpl_state);
1748 cmpl_state->reference_dir =
1749 open_ref_dir (text_to_complete, remaining_text, cmpl_state);
1751 if(!cmpl_state->reference_dir)
1754 cmpl_state->completion_dir =
1755 find_completion_dir (*remaining_text, remaining_text, cmpl_state);
1757 cmpl_state->last_valid_char = *remaining_text - text_to_complete;
1759 if(!cmpl_state->completion_dir)
1762 cmpl_state->completion_dir->cmpl_index = -1;
1763 cmpl_state->completion_dir->cmpl_parent = NULL;
1764 cmpl_state->completion_dir->cmpl_text = *remaining_text;
1766 cmpl_state->active_completion_dir = cmpl_state->completion_dir;
1768 cmpl_state->reference_dir = cmpl_state->completion_dir;
1770 poss = attempt_file_completion(cmpl_state);
1772 update_cmpl(poss, cmpl_state);
1777 static PossibleCompletion*
1778 cmpl_next_completion (CompletionState* cmpl_state)
1780 PossibleCompletion* poss = NULL;
1782 cmpl_state->the_completion.text[0] = 0;
1784 if(cmpl_state->user_completion_index >= 0)
1785 poss = attempt_homedir_completion(cmpl_state->last_completion_text, cmpl_state);
1787 poss = attempt_file_completion(cmpl_state);
1789 update_cmpl(poss, cmpl_state);
1794 /**********************************************************************/
1795 /* Directory Operations */
1796 /**********************************************************************/
1798 /* Open the directory where completion will begin from, if possible. */
1799 static CompletionDir*
1800 open_ref_dir(gchar* text_to_complete,
1801 gchar** remaining_text,
1802 CompletionState* cmpl_state)
1805 CompletionDir *new_dir;
1807 first_slash = strchr(text_to_complete, '/');
1809 if (text_to_complete[0] == '/' || !cmpl_state->reference_dir)
1811 new_dir = open_dir("/", cmpl_state);
1814 *remaining_text = text_to_complete + 1;
1816 else if (text_to_complete[0] == '~')
1818 new_dir = open_user_dir(text_to_complete, cmpl_state);
1823 *remaining_text = first_slash + 1;
1825 *remaining_text = text_to_complete + strlen(text_to_complete);
1834 *remaining_text = text_to_complete;
1836 new_dir = open_dir(cmpl_state->reference_dir->fullname, cmpl_state);
1841 new_dir->cmpl_index = -1;
1842 new_dir->cmpl_parent = NULL;
1848 /* open a directory by user name */
1849 static CompletionDir*
1850 open_user_dir(gchar* text_to_complete,
1851 CompletionState *cmpl_state)
1856 g_assert(text_to_complete && text_to_complete[0] == '~');
1858 first_slash = strchr(text_to_complete, '/');
1861 cmp_len = first_slash - text_to_complete - 1;
1863 cmp_len = strlen(text_to_complete + 1);
1868 if (!cmpl_state->user_home_dir &&
1869 !get_pwdb(cmpl_state))
1871 return open_dir(cmpl_state->user_home_dir, cmpl_state);
1876 char* copy = g_new(char, cmp_len + 1);
1878 strncpy(copy, text_to_complete + 1, cmp_len);
1880 pwd = getpwnam(copy);
1888 return open_dir(pwd->pw_dir, cmpl_state);
1892 /* open a directory relative the the current relative directory */
1893 static CompletionDir*
1894 open_relative_dir(gchar* dir_name,
1896 CompletionState *cmpl_state)
1898 gchar path_buf[2*MAXPATHLEN];
1900 if(dir->fullname_len + strlen(dir_name) + 2 >= MAXPATHLEN)
1902 cmpl_errno = CMPL_ERRNO_TOO_LONG;
1906 strcpy(path_buf, dir->fullname);
1908 if(dir->fullname_len > 1)
1910 path_buf[dir->fullname_len] = '/';
1911 strcpy(path_buf + dir->fullname_len + 1, dir_name);
1915 strcpy(path_buf + dir->fullname_len, dir_name);
1918 return open_dir(path_buf, cmpl_state);
1921 /* after the cache lookup fails, really open a new directory */
1922 static CompletionDirSent*
1923 open_new_dir(gchar* dir_name, struct stat* sbuf, gboolean stat_subdirs)
1925 CompletionDirSent* sent;
1928 struct dirent *dirent_ptr;
1929 gint buffer_size = 0;
1930 gint entry_count = 0;
1932 struct stat ent_sbuf;
1933 char path_buf[MAXPATHLEN*2];
1936 sent = g_new(CompletionDirSent, 1);
1937 sent->mtime = sbuf->st_mtime;
1938 sent->inode = sbuf->st_ino;
1939 sent->device = sbuf->st_dev;
1941 path_buf_len = strlen(dir_name);
1943 if (path_buf_len > MAXPATHLEN)
1945 cmpl_errno = CMPL_ERRNO_TOO_LONG;
1949 strcpy(path_buf, dir_name);
1951 directory = opendir(dir_name);
1959 while((dirent_ptr = readdir(directory)) != NULL)
1961 int entry_len = strlen(dirent_ptr->d_name);
1962 buffer_size += entry_len + 1;
1965 if(path_buf_len + entry_len + 2 >= MAXPATHLEN)
1967 cmpl_errno = CMPL_ERRNO_TOO_LONG;
1968 closedir(directory);
1973 sent->name_buffer = g_new(gchar, buffer_size);
1974 sent->entries = g_new(CompletionDirEntry, entry_count);
1975 sent->entry_count = entry_count;
1977 buffer_ptr = sent->name_buffer;
1979 rewinddir(directory);
1981 for(i = 0; i < entry_count; i += 1)
1983 dirent_ptr = readdir(directory);
1988 closedir(directory);
1992 strcpy(buffer_ptr, dirent_ptr->d_name);
1993 sent->entries[i].entry_name = buffer_ptr;
1994 buffer_ptr += strlen(dirent_ptr->d_name);
1998 path_buf[path_buf_len] = '/';
1999 strcpy(path_buf + path_buf_len + 1, dirent_ptr->d_name);
2003 if(stat(path_buf, &ent_sbuf) >= 0 && S_ISDIR(ent_sbuf.st_mode))
2004 sent->entries[i].is_dir = 1;
2006 /* stat may fail, and we don't mind, since it could be a
2007 * dangling symlink. */
2008 sent->entries[i].is_dir = 0;
2011 sent->entries[i].is_dir = 1;
2014 qsort(sent->entries, sent->entry_count, sizeof(CompletionDirEntry), compare_cmpl_dir);
2016 closedir(directory);
2022 check_dir(gchar *dir_name, struct stat *result, gboolean *stat_subdirs)
2024 /* A list of directories that we know only contain other directories.
2025 * Trying to stat every file in these directories would be very
2029 static const struct {
2032 struct stat statbuf;
2033 } no_stat_dirs[] = {
2034 { "/afs", FALSE, { 0 } },
2035 { "/net", FALSE, { 0 } }
2038 static const gint n_no_stat_dirs = sizeof(no_stat_dirs) / sizeof(no_stat_dirs[0]);
2039 static gboolean initialized = FALSE;
2046 for (i=0; i<n_no_stat_dirs; i++)
2048 if (stat(no_stat_dirs[i].name, &no_stat_dirs[i].statbuf) == 0)
2049 no_stat_dirs[i].present = TRUE;
2053 if(stat(dir_name, result) < 0)
2059 *stat_subdirs = TRUE;
2060 for (i=0; i<n_no_stat_dirs; i++)
2062 if (no_stat_dirs[i].present &&
2063 (no_stat_dirs[i].statbuf.st_dev == result->st_dev) &&
2064 (no_stat_dirs[i].statbuf.st_ino == result->st_ino))
2066 *stat_subdirs = FALSE;
2074 /* open a directory by absolute pathname */
2075 static CompletionDir*
2076 open_dir(gchar* dir_name, CompletionState* cmpl_state)
2079 gboolean stat_subdirs;
2080 CompletionDirSent *sent;
2083 if (!check_dir (dir_name, &sbuf, &stat_subdirs))
2086 cdsl = cmpl_state->directory_sent_storage;
2092 if(sent->inode == sbuf.st_ino &&
2093 sent->mtime == sbuf.st_mtime &&
2094 sent->device == sbuf.st_dev)
2095 return attach_dir(sent, dir_name, cmpl_state);
2100 sent = open_new_dir(dir_name, &sbuf, stat_subdirs);
2103 cmpl_state->directory_sent_storage =
2104 g_list_prepend(cmpl_state->directory_sent_storage, sent);
2106 return attach_dir(sent, dir_name, cmpl_state);
2112 static CompletionDir*
2113 attach_dir(CompletionDirSent* sent, gchar* dir_name, CompletionState *cmpl_state)
2115 CompletionDir* new_dir;
2117 new_dir = g_new(CompletionDir, 1);
2119 cmpl_state->directory_storage =
2120 g_list_prepend(cmpl_state->directory_storage, new_dir);
2122 new_dir->sent = sent;
2123 new_dir->fullname = g_strdup(dir_name);
2124 new_dir->fullname_len = strlen(dir_name);
2130 correct_dir_fullname(CompletionDir* cmpl_dir)
2132 gint length = strlen(cmpl_dir->fullname);
2135 if (strcmp(cmpl_dir->fullname + length - 2, "/.") == 0)
2139 strcpy(cmpl_dir->fullname, "/");
2140 cmpl_dir->fullname_len = 1;
2143 cmpl_dir->fullname[length - 2] = 0;
2146 else if (strcmp(cmpl_dir->fullname + length - 3, "/./") == 0)
2147 cmpl_dir->fullname[length - 2] = 0;
2148 else if (strcmp(cmpl_dir->fullname + length - 3, "/..") == 0)
2152 strcpy(cmpl_dir->fullname, "/");
2153 cmpl_dir->fullname_len = 1;
2157 if(stat(cmpl_dir->fullname, &sbuf) < 0)
2163 cmpl_dir->fullname[length - 2] = 0;
2165 if(!correct_parent(cmpl_dir, &sbuf))
2168 else if (strcmp(cmpl_dir->fullname + length - 4, "/../") == 0)
2172 strcpy(cmpl_dir->fullname, "/");
2173 cmpl_dir->fullname_len = 1;
2177 if(stat(cmpl_dir->fullname, &sbuf) < 0)
2183 cmpl_dir->fullname[length - 3] = 0;
2185 if(!correct_parent(cmpl_dir, &sbuf))
2189 cmpl_dir->fullname_len = strlen(cmpl_dir->fullname);
2195 correct_parent(CompletionDir* cmpl_dir, struct stat *sbuf)
2202 last_slash = strrchr(cmpl_dir->fullname, '/');
2204 g_assert(last_slash);
2206 if(last_slash != cmpl_dir->fullname)
2207 { /* last_slash[0] = 0; */ }
2214 if (stat(cmpl_dir->fullname, &parbuf) < 0)
2220 if (parbuf.st_ino == sbuf->st_ino && parbuf.st_dev == sbuf->st_dev)
2221 /* it wasn't a link */
2227 last_slash[0] = '/'; */
2229 /* it was a link, have to figure it out the hard way */
2231 new_name = find_parent_dir_fullname(cmpl_dir->fullname);
2236 g_free(cmpl_dir->fullname);
2238 cmpl_dir->fullname = new_name;
2244 find_parent_dir_fullname(gchar* dirname)
2246 gchar buffer[MAXPATHLEN];
2247 gchar buffer2[MAXPATHLEN];
2249 #if defined(sun) && !defined(__SVR4)
2252 if(!getcwd(buffer, MAXPATHLEN))
2259 if(chdir(dirname) != 0 || chdir("..") != 0)
2265 #if defined(sun) && !defined(__SVR4)
2268 if(!getcwd(buffer2, MAXPATHLEN))
2277 if(chdir(buffer) != 0)
2283 return g_strdup(buffer2);
2286 /**********************************************************************/
2287 /* Completion Operations */
2288 /**********************************************************************/
2290 static PossibleCompletion*
2291 attempt_homedir_completion(gchar* text_to_complete,
2292 CompletionState *cmpl_state)
2296 if (!cmpl_state->user_dir_name_buffer &&
2297 !get_pwdb(cmpl_state))
2299 length = strlen(text_to_complete) - 1;
2301 cmpl_state->user_completion_index += 1;
2303 while(cmpl_state->user_completion_index < cmpl_state->user_directories_len)
2305 index = first_diff_index(text_to_complete + 1,
2306 cmpl_state->user_directories
2307 [cmpl_state->user_completion_index].login);
2314 if(cmpl_state->last_valid_char < (index + 1))
2315 cmpl_state->last_valid_char = index + 1;
2316 cmpl_state->user_completion_index += 1;
2320 cmpl_state->the_completion.is_a_completion = 1;
2321 cmpl_state->the_completion.is_directory = 1;
2323 append_completion_text("~", cmpl_state);
2325 append_completion_text(cmpl_state->
2326 user_directories[cmpl_state->user_completion_index].login,
2329 return append_completion_text("/", cmpl_state);
2332 if(text_to_complete[1] ||
2333 cmpl_state->user_completion_index > cmpl_state->user_directories_len)
2335 cmpl_state->user_completion_index = -1;
2340 cmpl_state->user_completion_index += 1;
2341 cmpl_state->the_completion.is_a_completion = 1;
2342 cmpl_state->the_completion.is_directory = 1;
2344 return append_completion_text("~/", cmpl_state);
2348 /* returns the index (>= 0) of the first differing character,
2349 * PATTERN_MATCH if the completion matches */
2351 first_diff_index(gchar* pat, gchar* text)
2355 while(*pat && *text && *text == *pat)
2365 return PATTERN_MATCH;
2368 static PossibleCompletion*
2369 append_completion_text(gchar* text, CompletionState* cmpl_state)
2373 if(!cmpl_state->the_completion.text)
2376 len = strlen(text) + strlen(cmpl_state->the_completion.text) + 1;
2378 if(cmpl_state->the_completion.text_alloc > len)
2380 strcat(cmpl_state->the_completion.text, text);
2381 return &cmpl_state->the_completion;
2384 while(i < len) { i <<= 1; }
2386 cmpl_state->the_completion.text_alloc = i;
2388 cmpl_state->the_completion.text = (gchar*)g_realloc(cmpl_state->the_completion.text, i);
2390 if(!cmpl_state->the_completion.text)
2394 strcat(cmpl_state->the_completion.text, text);
2395 return &cmpl_state->the_completion;
2399 static CompletionDir*
2400 find_completion_dir(gchar* text_to_complete,
2401 gchar** remaining_text,
2402 CompletionState* cmpl_state)
2404 gchar* first_slash = strchr(text_to_complete, '/');
2405 CompletionDir* dir = cmpl_state->reference_dir;
2406 CompletionDir* next;
2407 *remaining_text = text_to_complete;
2411 gint len = first_slash - *remaining_text;
2413 gchar *found_name = NULL; /* Quiet gcc */
2415 gchar* pat_buf = g_new (gchar, len + 1);
2417 strncpy(pat_buf, *remaining_text, len);
2420 for(i = 0; i < dir->sent->entry_count; i += 1)
2422 if(dir->sent->entries[i].is_dir &&
2423 fnmatch(pat_buf, dir->sent->entries[i].entry_name,
2424 FNMATCH_FLAGS)!= FNM_NOMATCH)
2434 found_name = dir->sent->entries[i].entry_name;
2441 /* Perhaps we are trying to open an automount directory */
2442 found_name = pat_buf;
2445 next = open_relative_dir(found_name, dir, cmpl_state);
2453 next->cmpl_parent = dir;
2457 if(!correct_dir_fullname(dir))
2463 *remaining_text = first_slash + 1;
2464 first_slash = strchr(*remaining_text, '/');
2473 update_cmpl(PossibleCompletion* poss, CompletionState* cmpl_state)
2477 if(!poss || !cmpl_is_a_completion(poss))
2480 cmpl_len = strlen(cmpl_this_completion(poss));
2482 if(cmpl_state->updated_text_alloc < cmpl_len + 1)
2484 cmpl_state->updated_text =
2485 (gchar*)g_realloc(cmpl_state->updated_text,
2486 cmpl_state->updated_text_alloc);
2487 cmpl_state->updated_text_alloc = 2*cmpl_len;
2490 if(cmpl_state->updated_text_len < 0)
2492 strcpy(cmpl_state->updated_text, cmpl_this_completion(poss));
2493 cmpl_state->updated_text_len = cmpl_len;
2494 cmpl_state->re_complete = cmpl_is_directory(poss);
2496 else if(cmpl_state->updated_text_len == 0)
2498 cmpl_state->re_complete = FALSE;
2503 first_diff_index(cmpl_state->updated_text,
2504 cmpl_this_completion(poss));
2506 cmpl_state->re_complete = FALSE;
2508 if(first_diff == PATTERN_MATCH)
2511 if(first_diff > cmpl_state->updated_text_len)
2512 strcpy(cmpl_state->updated_text, cmpl_this_completion(poss));
2514 cmpl_state->updated_text_len = first_diff;
2515 cmpl_state->updated_text[first_diff] = 0;
2519 static PossibleCompletion*
2520 attempt_file_completion(CompletionState *cmpl_state)
2522 gchar *pat_buf, *first_slash;
2523 CompletionDir *dir = cmpl_state->active_completion_dir;
2525 dir->cmpl_index += 1;
2527 if(dir->cmpl_index == dir->sent->entry_count)
2529 if(dir->cmpl_parent == NULL)
2531 cmpl_state->active_completion_dir = NULL;
2537 cmpl_state->active_completion_dir = dir->cmpl_parent;
2539 return attempt_file_completion(cmpl_state);
2543 g_assert(dir->cmpl_text);
2545 first_slash = strchr(dir->cmpl_text, '/');
2549 gint len = first_slash - dir->cmpl_text;
2551 pat_buf = g_new (gchar, len + 1);
2552 strncpy(pat_buf, dir->cmpl_text, len);
2557 gint len = strlen(dir->cmpl_text);
2559 pat_buf = g_new (gchar, len + 2);
2560 strcpy(pat_buf, dir->cmpl_text);
2561 strcpy(pat_buf + len, "*");
2566 if(dir->sent->entries[dir->cmpl_index].is_dir)
2568 if(fnmatch(pat_buf, dir->sent->entries[dir->cmpl_index].entry_name,
2569 FNMATCH_FLAGS) != FNM_NOMATCH)
2571 CompletionDir* new_dir;
2573 new_dir = open_relative_dir(dir->sent->entries[dir->cmpl_index].entry_name,
2582 new_dir->cmpl_parent = dir;
2584 new_dir->cmpl_index = -1;
2585 new_dir->cmpl_text = first_slash + 1;
2587 cmpl_state->active_completion_dir = new_dir;
2590 return attempt_file_completion(cmpl_state);
2595 return attempt_file_completion(cmpl_state);
2601 return attempt_file_completion(cmpl_state);
2606 if(dir->cmpl_parent != NULL)
2608 append_completion_text(dir->fullname +
2609 strlen(cmpl_state->completion_dir->fullname) + 1,
2611 append_completion_text("/", cmpl_state);
2614 append_completion_text(dir->sent->entries[dir->cmpl_index].entry_name, cmpl_state);
2616 cmpl_state->the_completion.is_a_completion =
2617 (fnmatch(pat_buf, dir->sent->entries[dir->cmpl_index].entry_name,
2618 FNMATCH_FLAGS) != FNM_NOMATCH);
2620 cmpl_state->the_completion.is_directory = dir->sent->entries[dir->cmpl_index].is_dir;
2621 if(dir->sent->entries[dir->cmpl_index].is_dir)
2622 append_completion_text("/", cmpl_state);
2625 return &cmpl_state->the_completion;
2631 get_pwdb(CompletionState* cmpl_state)
2633 struct passwd *pwd_ptr;
2635 gint len = 0, i, count = 0;
2637 if(cmpl_state->user_dir_name_buffer)
2641 while ((pwd_ptr = getpwent()) != NULL)
2643 len += strlen(pwd_ptr->pw_name);
2644 len += strlen(pwd_ptr->pw_dir);
2649 if (!cmpl_state->user_home_dir)
2651 /* the loser doesn't have $HOME set */
2654 pwd_ptr = getpwuid(getuid());
2660 /* Allocate this separately, since it might be filled in elsewhere */
2661 cmpl_state->user_home_dir = g_strdup (pwd_ptr->pw_dir);
2666 cmpl_state->user_dir_name_buffer = g_new(gchar, len);
2667 cmpl_state->user_directories = g_new(CompletionUserDir, count);
2668 cmpl_state->user_directories_len = count;
2670 buf_ptr = cmpl_state->user_dir_name_buffer;
2672 for(i = 0; i < count; i += 1)
2674 pwd_ptr = getpwent();
2681 strcpy(buf_ptr, pwd_ptr->pw_name);
2682 cmpl_state->user_directories[i].login = buf_ptr;
2683 buf_ptr += strlen(buf_ptr);
2685 strcpy(buf_ptr, pwd_ptr->pw_dir);
2686 cmpl_state->user_directories[i].homedir = buf_ptr;
2687 buf_ptr += strlen(buf_ptr);
2691 qsort(cmpl_state->user_directories,
2692 cmpl_state->user_directories_len,
2693 sizeof(CompletionUserDir),
2702 if(cmpl_state->user_dir_name_buffer)
2703 g_free(cmpl_state->user_dir_name_buffer);
2704 if(cmpl_state->user_directories)
2705 g_free(cmpl_state->user_directories);
2707 cmpl_state->user_dir_name_buffer = NULL;
2708 cmpl_state->user_directories = NULL;
2714 compare_user_dir(const void* a, const void* b)
2716 return strcmp((((CompletionUserDir*)a))->login,
2717 (((CompletionUserDir*)b))->login);
2721 compare_cmpl_dir(const void* a, const void* b)
2723 return strcmp((((CompletionDirEntry*)a))->entry_name,
2724 (((CompletionDirEntry*)b))->entry_name);
2728 cmpl_state_okay(CompletionState* cmpl_state)
2730 return cmpl_state && cmpl_state->reference_dir;
2734 cmpl_strerror(gint err)
2736 if(err == CMPL_ERRNO_TOO_LONG)
2737 return "Name too long";
2739 return g_strerror (err);