From 8b3aa75c5407d4917c6c54484f07c5472894def5 Mon Sep 17 00:00:00 2001 From: Jan Lipovský Date: Thu, 30 Dec 2010 10:59:31 +0100 Subject: ncpmount wrapper --- ncpwrapper.c | 138 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 138 insertions(+) create mode 100644 ncpwrapper.c (limited to 'ncpwrapper.c') diff --git a/ncpwrapper.c b/ncpwrapper.c new file mode 100644 index 0000000..cdec0c5 --- /dev/null +++ b/ncpwrapper.c @@ -0,0 +1,138 @@ +#include + +#include "globals.h" +#include "ncpwrapper.h" +#include "dialogs.h" + + +static void +cb_child_watch( GPid pid, + gint status) +{ + /* Close pid */ + g_spawn_close_pid( pid ); +} + + +static gboolean +cb_out_watch( GIOChannel *channel, + GIOCondition cond) +{ + GString *string; + gchar *tmp; + gsize size; + + if( cond == G_IO_HUP ) + { + g_io_channel_unref( channel ); + return( FALSE ); + } + + string = g_string_new(""); + + while (g_io_channel_read_line( channel, &tmp, &size, NULL, NULL) == G_IO_STATUS_NORMAL) + { + if(tmp != NULL) + string = g_string_append (string, tmp); + + g_free( tmp ); + } + + + show_message( GTK_MESSAGE_INFO,"%s", "ncpmount output","%s",string->str); + g_string_free(string, TRUE); + + return( TRUE ); +} + + +static gboolean +cb_err_watch( GIOChannel *channel, + GIOCondition cond) +{ + GString *string; + gchar *tmp; + gsize size; + + if( cond == G_IO_HUP ) + { + g_io_channel_unref( channel ); + return( FALSE ); + } +/* + g_io_channel_read_line( channel, &string, &size, NULL, NULL ); + show_message( GTK_MESSAGE_ERROR,"ERROR: %s", string,"%s",string); + puts ("err"); + puts (string); + g_free( string );*/ + + string = g_string_new(""); + + while (g_io_channel_read_line( channel, &tmp, &size, NULL, NULL) == G_IO_STATUS_NORMAL) + { + if(tmp != NULL) + string = g_string_append (string, tmp); + + g_free( tmp ); + } + + + show_message( GTK_MESSAGE_ERROR,"%s", "ncpmount error","%s",string->str); + g_string_free(string, TRUE); + + return( TRUE ); +} + +/* + TODO - jak odchytit uspesne mountnuti??? +*/ + +void run_ncpmount() +{ + + GIOChannel *out_ch, *err_ch; + GError *gerror = NULL; + GPid pid; + gint out, err; + gboolean ret; + const gchar *command = get_ncpmount_command(); + + gchar **argv = g_strsplit(command, " ", 0); + + puts (command); + + g_free(argv[0]); + argv[0] = g_strdup("/usr/bin/ncpmount"); + + /* gchar *argv[] = {"/usr/bin/ncpmount", "-v", NULL};*/ + + /* Spawn child process */ + ret = g_spawn_async_with_pipes( NULL, argv, NULL, + G_SPAWN_DO_NOT_REAP_CHILD, NULL, + NULL, &pid, NULL, &out, &err, &gerror); + if( !ret ) + { + g_warning( "SPAWN FAILED" ); + show_message( GTK_MESSAGE_ERROR,"ERROR: %s", "Spawn failed!","%s",gerror->message); + + g_error_free(gerror); + return; + } + + /* Add watch function to catch termination of the process. This function + * will clean any remnants of process. */ + g_child_watch_add( pid, (GChildWatchFunc)cb_child_watch, NULL); + + /* Create channels that will be used to read data from pipes. */ +#ifdef G_OS_WIN32 + out_ch = g_io_channel_win32_new_fd( out ); + err_ch = g_io_channel_win32_new_fd( err ); +#else + out_ch = g_io_channel_unix_new( out ); + err_ch = g_io_channel_unix_new( err ); +#endif + + /* Add watches to channels */ + g_io_add_watch( out_ch, G_IO_IN | G_IO_HUP, (GIOFunc)cb_out_watch, NULL ); + g_io_add_watch( err_ch, G_IO_IN | G_IO_HUP, (GIOFunc)cb_err_watch, NULL ); +} -- cgit