summaryrefslogtreecommitdiffstats
path: root/ncpwrapper.c
blob: ac24f917c895ba8785837ce6b919b803bfdfa6b4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#include <gtk/gtk.h>

#include "ncpwrapper.h"

gboolean mounted = TRUE;

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 );
    }

    /* Receved some message - not mounted */
    mounted = FALSE;

    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 );
        /* hangup signal - if mounted then show message */
        if(mounted)
            show_message( GTK_MESSAGE_INFO,"Mounting succesfull", "","Mounted to \"%s\" directory.",cmd_params.mount_point);
        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 );
    }


    /* Receved some message - not mounted */
    mounted = FALSE;

    show_message( GTK_MESSAGE_ERROR,"%s", "ncpmount error","%s",string->str);
    g_string_free(string, TRUE);

    return( TRUE );
}

void run_ncpmount()
{

    GIOChannel  *out_ch, *err_ch;
    GError      *gerror = NULL;
    GPid        pid;
    gint        out, err;
    gboolean    ret;
    gchar *command = get_ncpmount_command(NULL);
    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};*/

    mounted = TRUE;
    /* 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( err_ch, G_IO_IN | G_IO_HUP, (GIOFunc)cb_err_watch, NULL );
    g_io_add_watch( out_ch, G_IO_IN | G_IO_HUP, (GIOFunc)cb_out_watch, NULL );

    g_strfreev(argv);
    g_free(command);
}