comparison mcabber/mcabber/commands.c @ 1675:c73b31124fa6

Use glob() in source command
author Myhailo Danylenko <isbear@ukrpost.net>
date Mon, 18 Jan 2010 16:36:55 +0200
parents 41c26b7d2890
children 96b5484423af
comparison
equal deleted inserted replaced
1674:f02e7076ccec 1675:c73b31124fa6
22 #include <string.h> 22 #include <string.h>
23 #include <stdlib.h> 23 #include <stdlib.h>
24 #include <sys/types.h> 24 #include <sys/types.h>
25 #include <sys/stat.h> 25 #include <sys/stat.h>
26 #include <unistd.h> 26 #include <unistd.h>
27 #include <errno.h>
28 #include <glob.h>
27 29
28 #include "commands.h" 30 #include "commands.h"
29 #include "help.h" 31 #include "help.h"
30 #include "roster.h" 32 #include "roster.h"
31 #include "screen.h" 33 #include "screen.h"
3780 show_roster = 0; 3782 show_roster = 0;
3781 3783
3782 readline_disable_chat_mode(show_roster); 3784 readline_disable_chat_mode(show_roster);
3783 } 3785 }
3784 3786
3787 static int source_print_error(const char *path, int eerrno)
3788 {
3789 scr_LogPrint(LPRINT_DEBUG, "Source: glob (%s) error: %s.", path, strerror(eerrno));
3790 return 0;
3791 }
3792
3785 static void do_source(char *arg) 3793 static void do_source(char *arg)
3786 { 3794 {
3787 static int recur_level; 3795 static int recur_level;
3788 gchar *filename, *expfname; 3796 gchar *filename, *expfname;
3797 glob_t flist;
3789 if (!*arg) { 3798 if (!*arg) {
3790 scr_LogPrint(LPRINT_NORMAL, "Missing filename."); 3799 scr_LogPrint(LPRINT_NORMAL, "Missing filename.");
3791 return; 3800 return;
3792 } 3801 }
3793 if (recur_level > 20) { 3802 if (recur_level > 20) {
3795 return; 3804 return;
3796 } 3805 }
3797 filename = g_strdup(arg); 3806 filename = g_strdup(arg);
3798 strip_arg_special_chars(filename); 3807 strip_arg_special_chars(filename);
3799 expfname = expand_filename(filename); 3808 expfname = expand_filename(filename);
3800 recur_level++;
3801 cfg_read_file(expfname, FALSE);
3802 recur_level--;
3803 g_free(filename); 3809 g_free(filename);
3810 // match
3811 flist.gl_offs = 0;
3812 if (glob(expfname, 0, source_print_error, &flist)) {
3813 scr_LogPrint(LPRINT_LOGNORM, "Source: error: %s.", strerror (errno));
3814 } else {
3815 int i;
3816 // sort list
3817 for (i = 1; i < flist.gl_pathc; ++i) {
3818 int j;
3819 for (j = i-1; j > 0; --j) {
3820 char *a = flist.gl_pathv[j+1];
3821 char *b = flist.gl_pathv[j];
3822 if (strcmp(a, b) < 0) {
3823 flist.gl_pathv[j] = a;
3824 flist.gl_pathv[j+1] = b;
3825 } else
3826 break;
3827 }
3828 }
3829 // source files in list
3830 for (i=0; i < flist.gl_pathc; ++i) {
3831 recur_level++;
3832 cfg_read_file(flist.gl_pathv[i], FALSE);
3833 recur_level--;
3834 }
3835 // free
3836 globfree(&flist);
3837 }
3804 g_free(expfname); 3838 g_free(expfname);
3805 } 3839 }
3806 3840
3807 static void do_connect(char *arg) 3841 static void do_connect(char *arg)
3808 { 3842 {