File: get.c

package info (click to toggle)
moxftp 2.2-18.1
  • links: PTS
  • area: main
  • in suites: woody
  • size: 4,160 kB
  • ctags: 3,837
  • sloc: ansic: 43,420; makefile: 353; perl: 262; sh: 192
file content (269 lines) | stat: -rw-r--r-- 6,766 bytes parent folder | download | duplicates (4)
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
/*
 * Copyright (c) 1992 The Regents of the University of Texas System.
 * Copyright (c) 1993 The Regents of the University of Texas System.
 * Copyright (c) 1994 The Regents of the University of Texas System.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms are permitted provided
 * that the above copyright notice and this paragraph are duplicated in all
 * such forms and that any documentation, advertising materials,  and other
 * materials  related to such  distribution  and use  acknowledge  that the
 * software  was  developed  by the  University of Texas.  The  name of the
 * University may not be  used to endorse or promote  products derived from
 * this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
 * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
 * MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
 */

#ifndef lint
static char copyright[] =
"@(#) Copyright (c) 1993 The Regents of the University of Texas System.\n\
 All rights reserved.\n";
static char rcsid[] =
"$Id: get.c,v 1.1 1994/03/14 18:55:53 jones Exp $\n";
static char rcspath[] =
"$Source: /mintaka/u0/xx/ss/jones/jones.old/mftp.src/moxftp-2.0/RCS/get.c,v $\n";
#endif

/* $Log: get.c,v $
 * Revision 1.1  1994/03/14  18:55:53  jones
 * Initial revision
 *
 */

#include "machine.h"
#include "defs.h"

static char *remote = NULL;
static char *local   = NULL;
static int rate = 0;
static int size = 0;
static int bytes = 0;

extern XtAppContext App_context;
static XtIntervalId timeout;
static char *view_file_name = NULL;
static void percent();

static void 
finish_get(output, tag, data)
struct _output *output;
int   tag;
DATA  data;
{
    int code;
    char *msg = NULL;

    if(timeout) {
	if (timeout) XtRemoveTimeOut(timeout);
	timeout = 0;
        Update_Percent(100, size);
        Update_Percent(0, 0);
    }
    Unset_Busy();
    Clear_Noop(NOOP_GET);
    code =  ftp_status(output);

    if (ftp_response[code].status & FTP_ERROR) {
 	Set_Status_Error("get failed - ftp error:",  last_response);
    } else { 
        msg = concat(msg, "Finished transfer of remote file ");
        msg = concat(msg,  remote);
        Set_Status_Short(msg);
        XtFree(msg);

        Set_Status(" ");
    }
    XtFree(local);
    XtFree(remote);
    local  = NULL;
    remote = NULL;
    do_callbacks(code, (CB*)data);
}

static void
status(command, tag)
char *command;
int   tag;
{
    char *cp = command;
    char *junk = NULL;
    char *status = NULL;

    Animate(Top);
    status = concat(status, "Fetching remote file ");
    status = concat(status, remote);
    if (local && *local && (strcmp(local, remote) != 0)) {
    	status = concat(status, " to local file ");
        status = concat(status, local);
    }
    Set_Status(status);
    XtFree(junk);
    XtFree(status);
    if (bytes) {
        timeout =  XtAppAddTimeOut(App_context, (unsigned long)1000,
                                   (XtTimerCallbackProc)percent,
                                   (XtPointer)NULL);
    }
  
}


static void 
percent(client_data, id)
caddr_t client_data;
XtIntervalId *id;
{
    int p;
    
    size = Get_Chars();
    p = (100*size)/bytes;
    Update_Percent(p, size);
    timeout =  XtAppAddTimeOut(App_context, (unsigned long)1000, 
		               (XtTimerCallbackProc)percent, 
			       (XtPointer)NULL);
}
   
void
get_peek_150(code)
char *code;
{
    char *cp;
    int  atoi();
    
    if (!remote || timeout) return;
    if (strncmp(code, "150 O", sizeof("150 O") - 1) != 0) return;
    if (!(cp = bsdstrstr(code,"byte"))) return;
    cp = RINDEX(code, '(');
    if (!cp) return;
    cp++;
    bytes = atoi(cp);
    if (bytes == 0) return;
    timeout =  XtAppAddTimeOut(App_context, (unsigned long)1000, 
			           (XtTimerCallbackProc)percent, 
				   (XtPointer)NULL);
}


void 
Start_Get(echo, remote_file, local_file, size, callback, data, cb)
int    echo;
char  *local_file;
char  *remote_file;
int    size;
void (*callback)();
DATA   data;
CB    *cb;
{
    char *command = NULL;
    CB *cbp;

    command = concat(command,"RETR ");
    if (INDEX(remote_file,' ') != NULL) {
	command = concat(command, "\"");
        command = concat(command, remote_file);
	command = concat(command, "\"");
    } else {
    	command = concat(command, remote_file);
    }
    remote = XtNewString(remote_file);

    if (local_file && *local_file) {
         local = XtNewString(local_file);
    } else {
         local = XtNewString(remote_file);
    }

    if (size) bytes = size;
    else      bytes = 0;

    cbp = link_callback(NULL, callback, data, cb);
    Start_Listen(command, status, finish_get, 0, echo,
                 0, cbp, local, 0);
    XtFree(command);
    Set_Busy(BUSY_GET);
    Set_Noop(NOOP_GET);
}



static void 
finish_view(output, data, cb)
struct _output *output;
DATA  data;
CB    *cb;
{
    int code;
    char *msg = NULL;

    if(timeout) {
	if (timeout) XtRemoveTimeOut(timeout);
	timeout = 0;
        Update_Percent(100, size);
        Update_Percent(0, 0);
    }
    Unset_Busy();
    Clear_Noop(NOOP_GET);
    code =  ftp_status(output);

    if (ftp_response[code].status & FTP_ERROR) {
 	Set_Status_Error("view failed - ftp error:",  last_response);
    } else { 
        msg = concat(msg, "Finished transfer of remote file ");
        msg = concat(msg,  remote);
        Set_Status_Short(msg);
        XtFree(msg);
        Set_Status(" ");
        View_File(local, view_file_name);
    }
    XtFree(local);
    XtFree(remote);
    if(view_file_name) XtFree(view_file_name);
    local  = NULL;
    remote = NULL;
    view_file_name = NULL;
    do_callbacks(code, cb);
}

static int new = 0;

void 
Start_View(echo, remote_file, local_file, size, callback, data, cb)
int    echo;
char  *local_file;
char  *remote_file;
int    size;
void (*callback)();
DATA   data;
CB     *cb;
{
    char *command = NULL;
    CB   *cbp;

    local = XtMalloc(sizeof("/tmp/view%d%d%s") + sizeof(local_file) + 10 + 
		     10+1);
    sprintf(local, "/tmp/view%d%d%s",getpid(), new, local_file);
    view_file_name = XtNewString(local_file);
    new++;
    command = concat(command,"RETR ");
    if (INDEX(remote_file,' ') != NULL) {
        command = concat(command, "\"");
        command = concat(command, remote_file);
        command = concat(command, "\"");
    } else {
        command = concat(command, remote_file);
    }
    remote = XtNewString(remote_file);

    if (size) bytes = size;
    else      bytes = 0;
    cbp = link_callback(NULL, callback, data, cb);
    Start_Listen(command, status, finish_view, 0, echo, 0, cbp, local, 0);
    XtFree(command);
    Set_Busy(BUSY_GET);
    Set_Noop(NOOP_GET);
}