0
|
1 /* popsbr.c - POP client subroutines */
|
|
2 #ifndef lint
|
|
3 static char ident[] = "@(#)$Id$";
|
|
4 #endif /* lint */
|
|
5
|
|
6 #if defined(NNTP) && !defined(PSHSBR)
|
|
7 #undef NNTP
|
|
8 #endif
|
|
9
|
|
10 /* LINTLIBRARY */
|
|
11
|
|
12 #include "../h/strings.h"
|
|
13 #ifdef NNTP /* building pshsbr.o from popsbr.c */
|
|
14 #include "../h/nntp.h"
|
|
15 #endif /* NNTP */
|
|
16 #include <stdio.h>
|
|
17 #include <signal.h>
|
|
18
|
|
19 #ifndef POPSERVICE
|
|
20 #define POPSERVICE "pop"
|
|
21 #endif
|
|
22
|
|
23 #define NOTOK (-1)
|
|
24 #define OK 0
|
|
25 #define DONE 1
|
|
26
|
|
27 #define TRM "."
|
|
28 #define TRMLEN (sizeof TRM - 1)
|
|
29
|
|
30 #ifdef __CYGWIN32__
|
|
31 #include <errno.h>
|
|
32 #endif
|
|
33 extern int errno;
|
|
34 #if !defined(BSD44) && !defined(__GNU_LIBRARY__)
|
|
35 extern int sys_nerr;
|
|
36 extern char *sys_errlist[];
|
|
37 #endif
|
|
38 #ifdef __NetBSD__ /* XXX */
|
|
39 #include <errno.h>
|
|
40 #endif
|
|
41
|
|
42 static int poprint = 0;
|
|
43 static int pophack = 0;
|
|
44
|
|
45 char response[BUFSIZ];
|
|
46
|
|
47 static FILE *input;
|
|
48 static FILE *output;
|
|
49
|
|
50 #ifdef __STDC__
|
|
51 static int traverse (int (*)(), const char*, char *, char *, char *, char *);
|
|
52 #define targ_t char *
|
|
53 #else
|
|
54 static int traverse();
|
|
55 #define targ_t int
|
|
56 #endif
|
|
57
|
|
58 #if !defined(NNTP) && defined(MPOP)
|
|
59 #define command pop_command
|
|
60 #define multiline pop_multiline
|
|
61 int command(), multiline();
|
|
62 #else
|
|
63 static int command(), multiline();
|
|
64 #endif
|
|
65
|
|
66 static int getline();
|
|
67 static putline();
|
|
68
|
|
69 #ifdef NNTP
|
|
70 static int xtnd_last = -1,
|
|
71 xtnd_first = 0;
|
|
72 static char xtnd_name[512]; /* INCREDIBLE HACK!! */
|
|
73 #endif /* NNTP */
|
|
74
|
|
75 /* */
|
|
76
|
|
77 #ifndef NNTP
|
|
78 #ifdef APOP
|
|
79 #include "md5.c"
|
|
80
|
|
81 static char *pop_auth (user, pass)
|
|
82 char *user,
|
|
83 *pass;
|
|
84 {
|
|
85 register char *cp,
|
|
86 *lp;
|
|
87 register unsigned char *dp;
|
|
88 unsigned char *ep,
|
|
89 digest[16];
|
|
90 MD5_CTX mdContext;
|
|
91 static char buffer[BUFSIZ * 2];
|
|
92
|
|
93 if ((cp = index (response, '<')) == NULL
|
|
94 || (lp = index (cp, '>')) == NULL) {
|
|
95 (void) sprintf (buffer, "APOP not available: %s", response);
|
|
96 buffer[sizeof(response) - 1] = '\0';
|
|
97 (void) strcpy (response, buffer);
|
|
98 return NULL;
|
|
99 }
|
|
100
|
|
101 *++lp = '\0';
|
|
102 (void) sprintf (buffer, "%s%s", cp, pass);
|
|
103
|
|
104 MD5Init (&mdContext);
|
|
105 MD5Update (&mdContext, (unsigned char *) buffer,
|
|
106 (unsigned int) strlen (buffer));
|
|
107 MD5Final (digest, &mdContext);
|
|
108
|
|
109 (void) sprintf (cp = buffer, "%s ", user);
|
|
110 cp += strlen (cp);
|
|
111 for (ep = (dp = digest) + sizeof digest / sizeof digest[0];
|
|
112 dp < ep;
|
|
113 cp += 2)
|
|
114 (void) sprintf (cp, "%02x", *dp++ & 0xff);
|
|
115 *cp = '\0';
|
|
116
|
|
117 return buffer;
|
|
118 }
|
|
119 #endif /* APOP */
|
|
120 #endif /* !NNTP */
|
|
121
|
|
122 /* */
|
|
123
|
|
124 #if defined(RPOP) || defined(APOP)
|
|
125 int pop_init (host, user, pass, snoop, rpop)
|
|
126 int rpop;
|
|
127 #else
|
|
128 int pop_init (host, user, pass, snoop)
|
|
129 #endif
|
|
130 char *host,
|
|
131 *user,
|
|
132 *pass;
|
|
133 int snoop;
|
|
134 {
|
|
135 #ifdef APOP
|
|
136 int apop;
|
|
137 #else
|
|
138 #ifndef RPOP /* !APOP && !RPOP */
|
|
139 int rpop = 0;
|
|
140 #endif
|
|
141 #endif
|
|
142 int fd1,
|
|
143 fd2;
|
|
144 char buffer[BUFSIZ];
|
|
145
|
|
146 #ifdef APOP
|
|
147 if ((apop = rpop) < 0)
|
|
148 rpop = 0;
|
|
149 #endif /* APOP */
|
|
150
|
|
151 #ifndef NNTP
|
|
152 #ifndef KPOP
|
|
153 if ((fd1 = client (host, "tcp", POPSERVICE, rpop, response)) == NOTOK)
|
|
154 #else /* KPOP */
|
|
155 (void) sprintf (buffer, "%s/%s", POPSERVICE, "kpop");
|
|
156 if ((fd1 = client (host, "tcp", buffer, rpop, response)) == NOTOK)
|
|
157 #endif
|
|
158 #else /* NNTP */
|
|
159 if ((fd1 = client (host, "tcp", "nntp", rpop, response)) == NOTOK)
|
|
160 #endif
|
|
161 return NOTOK;
|
|
162
|
|
163 if ((fd2 = dup (fd1)) == NOTOK) {
|
|
164 (void) sprintf (response, "unable to dup connection descriptor: %s",
|
|
165 errno > 0 && errno < sys_nerr ? sys_errlist[errno]
|
|
166 : "unknown error");
|
|
167 (void) close (fd1);
|
|
168 return NOTOK;
|
|
169 }
|
|
170 #ifndef NNTP
|
|
171 if (pop_set (fd1, fd2, snoop) == NOTOK)
|
|
172 #else /* NNTP */
|
|
173 if (pop_set (fd1, fd2, snoop, (char *)0) == NOTOK)
|
|
174 #endif /* NNTP */
|
|
175 return NOTOK;
|
|
176
|
|
177 (void) signal (SIGPIPE, SIG_IGN);
|
|
178
|
|
179 switch (getline (response, sizeof response, input)) {
|
|
180 case OK:
|
|
181 if (poprint)
|
|
182 fprintf (stderr, "<--- %s\n", response);
|
|
183 #ifndef NNTP
|
|
184 if (*response == '+') {
|
|
185 #ifndef KPOP
|
|
186 #ifdef APOP
|
|
187 if (apop < 0) {
|
|
188 char *cp = pop_auth (user, pass);
|
|
189
|
|
190 if (cp && command ("APOP %s", cp) != NOTOK)
|
|
191 return OK;
|
|
192 }
|
|
193 else
|
|
194 #endif /* apop */
|
|
195 if (command ("USER %s", user) != NOTOK
|
|
196 && command ("%s %s", rpop ? "RPOP" : (pophack++, "PASS"),
|
|
197 pass) != NOTOK)
|
|
198 return OK;
|
|
199 #else /* KPOP */
|
|
200 if (command ("USER %s", user) != NOTOK
|
|
201 && command ("PASS %s", pass) != NOTOK)
|
|
202 return OK;
|
|
203 #endif
|
|
204 }
|
|
205 #else /* NNTP */
|
|
206 if (*response < CHAR_ERR) {
|
|
207 (void) command ("MODE READER");
|
|
208 #ifdef NEWSAUTH
|
|
209 if (getenv("NEWSAUTH")) /* special for IDENT protocol */
|
|
210 command("MODE IDENTIFY");
|
|
211 #endif
|
|
212 return OK;
|
|
213 }
|
|
214 #endif /* NNTP */
|
|
215 (void) strcpy (buffer, response);
|
|
216 (void) command ("QUIT");
|
|
217 (void) strcpy (response, buffer);
|
|
218 /* and fall */
|
|
219
|
|
220 case NOTOK:
|
|
221 case DONE:
|
|
222 if (poprint)
|
|
223 fprintf (stderr, "%s\n", response);
|
|
224 (void) fclose (input);
|
|
225 (void) fclose (output);
|
|
226 return NOTOK;
|
|
227 }
|
|
228 /* NOTREACHED */
|
|
229 }
|
|
230 /* */
|
|
231
|
|
232 #ifndef NNTP
|
|
233 int pop_set (in, out, snoop)
|
|
234 #else /* NNTP */
|
|
235 int pop_set (in, out, snoop, myname)
|
|
236 char *myname;
|
|
237 #endif /* NNTP */
|
|
238 int in,
|
|
239 out,
|
|
240 snoop;
|
|
241 {
|
|
242 #ifdef NNTP
|
|
243 if (myname && *myname)
|
|
244 strcpy (xtnd_name, myname); /* interface from bbc to msh */
|
|
245
|
|
246 #endif /* NNTP */
|
|
247 if ((input = fdopen (in, "r")) == NULL
|
|
248 || (output = fdopen (out, "w")) == NULL) {
|
|
249 (void) strcpy (response, "fdopen failed on connection descriptor");
|
|
250 if (input)
|
|
251 (void) fclose (input);
|
|
252 else
|
|
253 (void) close (in);
|
|
254 (void) close (out);
|
|
255 return NOTOK;
|
|
256 }
|
|
257
|
|
258 poprint = snoop;
|
|
259
|
|
260 return OK;
|
|
261 }
|
|
262
|
|
263
|
|
264 int pop_fd (in, out)
|
|
265 char *in,
|
|
266 *out;
|
|
267 {
|
|
268 (void) sprintf (in, "%d", fileno (input));
|
|
269 (void) sprintf (out, "%d", fileno (output));
|
|
270 return OK;
|
|
271 }
|
|
272
|
|
273 /* */
|
|
274
|
|
275 int pop_stat (nmsgs, nbytes)
|
|
276 int *nmsgs,
|
|
277 *nbytes;
|
|
278 {
|
|
279 #ifdef NNTP
|
|
280 char **ap;
|
|
281 extern char **brkstring();
|
|
282 #endif /* NNTP */
|
|
283
|
|
284 #ifndef NNTP
|
|
285 if (command ("STAT") == NOTOK)
|
|
286 return NOTOK;
|
|
287
|
|
288 *nmsgs = *nbytes = 0;
|
|
289 (void) sscanf (response, "+OK %d %d", nmsgs, nbytes);
|
|
290
|
|
291 #else /* NNTP */
|
|
292 if (xtnd_last < 0) { /* in msh, xtnd_name is set from myname */
|
|
293 if (command("GROUP %s", xtnd_name) == NOTOK)
|
|
294 return NOTOK;
|
|
295
|
|
296 ap = brkstring (response, " ", "\n"); /* "211 nart first last ggg" */
|
|
297 xtnd_first = atoi (ap[2]);
|
|
298 xtnd_last = atoi (ap[3]);
|
|
299 }
|
|
300
|
|
301 /* nmsgs is not the real nart, but an incredible simuation */
|
|
302 if (xtnd_last > 0)
|
|
303 *nmsgs = xtnd_last - xtnd_first + 1; /* because of holes... */
|
|
304 else
|
|
305 *nmsgs = 0;
|
|
306 *nbytes = xtnd_first; /* for subtracting offset in msh() */
|
|
307 #endif /* NNTP */
|
|
308
|
|
309 return OK;
|
|
310 }
|
|
311
|
|
312 #ifdef NNTP
|
|
313 int pop_exists (action)
|
|
314 int (*action) ();
|
|
315 {
|
|
316 int msgno,
|
|
317 arg1,
|
|
318 arg2,
|
|
319 status;
|
|
320
|
|
321 #ifdef XMSGS /* hacked into NNTP 1.5 */
|
|
322 if (traverse (action, "XMSGS %d-%d",
|
|
323 (targ_t)xtnd_first, (targ_t)xtnd_last, 0, 0) == OK)
|
|
324 return OK;
|
|
325 #endif
|
|
326 if (traverse (action, "LISTGROUP", /* provided by INN 1.4 */
|
|
327 0, 0, 0, 0) == OK)
|
|
328 return OK;
|
|
329 if (traverse (action, "XHDR NONAME %d-%d",
|
|
330 (targ_t)xtnd_first, (targ_t)xtnd_last, 0, 0) == OK)
|
|
331 return OK;
|
|
332
|
|
333 status = NOTOK;
|
|
334 for (msgno = xtnd_first; msgno <= xtnd_last; msgno++)
|
|
335 if (pop_list(msgno, (int *) 0, &arg1, &arg2, (int *) 0) == OK) {
|
|
336 char *cp;
|
|
337 if ((cp = index(response, ' ')) == NULL)
|
|
338 continue;
|
|
339 (*action) (++cp);
|
|
340 status = OK;
|
|
341 }
|
|
342 return status;
|
|
343 }
|
|
344 #endif /* NNTP */
|
|
345
|
|
346 #if !defined(BPOP) && !defined(NNTP)
|
|
347 int pop_list (msgno, nmsgs, msgs, bytes)
|
|
348 #else /* !BPOP && !NNTP */
|
|
349 int pop_list (msgno, nmsgs, msgs, bytes, ids)
|
|
350 int *ids;
|
|
351 #endif /* !BPOP && !NNTP */
|
|
352 int msgno,
|
|
353 *nmsgs,
|
|
354 *msgs,
|
|
355 *bytes;
|
|
356 {
|
|
357 int i;
|
|
358 #if !defined(BPOP) && !defined(NNTP)
|
|
359 int *ids = NULL;
|
|
360 #endif
|
|
361
|
|
362 if (msgno) {
|
|
363 #ifndef NNTP
|
|
364 if (command ("LIST %d", msgno) == NOTOK)
|
|
365 return NOTOK;
|
|
366 *msgs = *bytes = 0;
|
|
367 if (ids) {
|
|
368 *ids = 0;
|
|
369 (void) sscanf (response, "+OK %d %d %d", msgs, bytes, ids);
|
|
370 }
|
|
371 else
|
|
372 (void) sscanf (response, "+OK %d %d", msgs, bytes);
|
|
373 #else /* NNTP */
|
|
374 *msgs = *bytes = 0;
|
|
375 if (command ("STAT %d", msgno) == NOTOK)
|
|
376 return NOTOK;
|
|
377 if (ids) {
|
|
378 *ids = msgno;
|
|
379 }
|
|
380 #endif /* NNTP */
|
|
381 return OK;
|
|
382 }
|
|
383
|
|
384 #ifndef NNTP
|
|
385 if (command ("LIST") == NOTOK)
|
|
386 return NOTOK;
|
|
387
|
|
388 for (i = 0; i < *nmsgs; i++)
|
|
389 switch (multiline ()) {
|
|
390 case NOTOK:
|
|
391 return NOTOK;
|
|
392 case DONE:
|
|
393 *nmsgs = ++i;
|
|
394 return OK;
|
|
395 case OK:
|
|
396 *msgs = *bytes = 0;
|
|
397 if (ids) {
|
|
398 *ids = 0;
|
|
399 (void) sscanf (response, "%d %d %d",
|
|
400 msgs++, bytes++, ids++);
|
|
401 }
|
|
402 else
|
|
403 (void) sscanf (response, "%d %d", msgs++, bytes++);
|
|
404 break;
|
|
405 }
|
|
406 for (;;)
|
|
407 switch (multiline ()) {
|
|
408 case NOTOK:
|
|
409 return NOTOK;
|
|
410 case DONE:
|
|
411 return OK;
|
|
412 case OK:
|
|
413 break;
|
|
414 }
|
|
415 #else /* NNTP */
|
|
416 return NOTOK;
|
|
417 #endif /* NNTP */
|
|
418 }
|
|
419
|
|
420 /* */
|
|
421
|
|
422 int pop_retr (msgno, action)
|
|
423 int msgno,
|
|
424 (*action) ();
|
|
425 {
|
|
426 #ifndef NNTP
|
|
427 return traverse (action, "RETR %d", (targ_t)msgno, 0, 0, 0);
|
|
428 #else /* NNTP */
|
|
429 return traverse (action, "ARTICLE %d", (targ_t)msgno, 0, 0, 0);
|
|
430 #endif /* NNTP */
|
|
431 }
|
|
432
|
|
433
|
|
434 /* VARARGS2 */
|
|
435
|
|
436 static int traverse (action, fmt, a, b, c, d)
|
|
437 int (*action) ();
|
|
438 #ifdef __STDC__
|
|
439 const char *fmt;
|
|
440 #else
|
|
441 char *fmt;
|
|
442 #endif
|
|
443 char *a,
|
|
444 *b,
|
|
445 *c,
|
|
446 *d;
|
|
447 {
|
|
448 char buffer[sizeof response];
|
|
449
|
|
450 if (command (fmt, a, b, c, d) == NOTOK)
|
|
451 return NOTOK;
|
|
452 (void) strcpy (buffer, response);
|
|
453
|
|
454 for (;;)
|
|
455 switch (multiline ()) {
|
|
456 case NOTOK:
|
|
457 return NOTOK;
|
|
458
|
|
459 case DONE:
|
|
460 (void) strcpy (response, buffer);
|
|
461 return OK;
|
|
462
|
|
463 case OK:
|
|
464 (*action) (response);
|
|
465 break;
|
|
466 }
|
|
467 }
|
|
468
|
|
469 /* */
|
|
470
|
|
471 int pop_dele (msgno)
|
|
472 int msgno;
|
|
473 {
|
|
474 return command ("DELE %d", msgno);
|
|
475 }
|
|
476
|
|
477
|
|
478 int pop_noop () {
|
|
479 return command ("NOOP");
|
|
480 }
|
|
481
|
|
482
|
|
483 #ifndef NNTP
|
|
484 #ifdef MPOP
|
|
485 int pop_last () {
|
|
486 return command ("LAST");
|
|
487 }
|
|
488 #endif /* MPOP */
|
|
489 #endif /* !NNTP */
|
|
490
|
|
491 int pop_rset () {
|
|
492 return command ("RSET");
|
|
493 }
|
|
494
|
|
495 /* */
|
|
496
|
|
497 int pop_top (msgno, lines, action)
|
|
498 int msgno,
|
|
499 lines,
|
|
500 (*action) ();
|
|
501 {
|
|
502 #ifndef NNTP
|
|
503 return traverse (action, "TOP %d %d", (targ_t)msgno, (targ_t)lines, 0, 0);
|
|
504 #else /* NNTP */
|
|
505 return traverse (action, "HEAD %d", (targ_t)msgno, 0, 0, 0);
|
|
506 #endif /* NNTP */
|
|
507 }
|
|
508
|
|
509
|
|
510 #if defined(BPOP) || defined(NNTP)
|
|
511 int pop_xtnd (action, fmt, a, b, c, d)
|
|
512 int (*action) ();
|
|
513 char *fmt,
|
|
514 *a,
|
|
515 *b,
|
|
516 *c,
|
|
517 *d;
|
|
518 {
|
|
519 char buffer[BUFSIZ];
|
|
520 #ifdef NNTP
|
|
521 extern char **brkstring();
|
|
522 char **ap;
|
|
523 #endif /* NNTP */
|
|
524
|
|
525 #ifndef NNTP
|
|
526 (void) sprintf (buffer, "XTND %s", fmt);
|
|
527 return traverse (action, buffer, a, b, c, d);
|
|
528 #else /* NNTP */
|
|
529 sprintf (buffer, fmt, a, b, c, d);
|
|
530 ap = brkstring (buffer, " ", "\n"); /* a hack, i know... */
|
|
531
|
|
532 if (uleq(ap[0], "x-bboards")) { /* XTND "X-BBOARDS group */
|
|
533 /* most of these parameters are meaningless under NNTP.
|
|
534 * bbc.c was modified to set AKA and LEADERS as appropriate,
|
|
535 * the rest are left blank.
|
|
536 */
|
|
537 return OK;
|
|
538 }
|
|
539 if (uleq (ap[0], "archive") && ap[1]) {
|
|
540 sprintf (xtnd_name, "%s", ap[1]); /* save the name */
|
|
541 xtnd_last = 0;
|
|
542 xtnd_first = 1; /* setup to fail in pop_stat */
|
|
543 return OK;
|
|
544 }
|
|
545 if (uleq (ap[0], "bboards")) {
|
|
546
|
|
547 if (ap[1]) { /* XTND "BBOARDS group" */
|
|
548 sprintf (xtnd_name, "%s", ap[1]); /* save the name */
|
|
549 if (command("GROUP %s", xtnd_name) == NOTOK)
|
|
550 return NOTOK;
|
|
551
|
|
552 strcpy (buffer, response); /* action must ignore extra args */
|
|
553 ap = brkstring (response, " ", "\n");/* "211 nart first last g" */
|
|
554 xtnd_first = atoi (ap[2]);
|
|
555 xtnd_last = atoi (ap[3]);
|
|
556
|
|
557 (*action) (buffer);
|
|
558 return OK;
|
|
559
|
|
560 } else { /* XTND "BBOARDS" */
|
|
561 return traverse (action, "LIST", a, b, c, d);
|
|
562 }
|
|
563 }
|
|
564 return NOTOK; /* unknown XTND command */
|
|
565 #endif /* NNTP */
|
|
566 }
|
|
567 #endif /* BPOP || NNTP */
|
|
568
|
|
569 /* */
|
|
570
|
|
571 int pop_quit () {
|
|
572 int i;
|
|
573
|
|
574 i = command ("QUIT");
|
|
575 (void) pop_done ();
|
|
576
|
|
577 return i;
|
|
578 }
|
|
579
|
|
580
|
|
581 int pop_done () {
|
|
582 (void) fclose (input);
|
|
583 (void) fclose (output);
|
|
584
|
|
585 return OK;
|
|
586 }
|
|
587
|
|
588 /* */
|
|
589
|
|
590 /* VARARGS1 */
|
|
591
|
|
592 #if defined(MPOP) && !defined(NNTP)
|
|
593 int command (fmt, a, b, c, d)
|
|
594 #else
|
|
595 static int command (fmt, a, b, c, d)
|
|
596 #endif
|
|
597 char *fmt,
|
|
598 *a,
|
|
599 *b,
|
|
600 *c,
|
|
601 *d;
|
|
602 {
|
|
603 char *cp,
|
|
604 buffer[BUFSIZ];
|
|
605
|
|
606 (void) sprintf (buffer, fmt, a, b, c, d);
|
|
607 if (poprint)
|
|
608 if (pophack) {
|
|
609 if (cp = index (buffer, ' '))
|
|
610 *cp = 0;
|
|
611 fprintf (stderr, "---> %s ********\n", buffer);
|
|
612 if (cp)
|
|
613 *cp = ' ';
|
|
614 pophack = 0;
|
|
615 }
|
|
616 else
|
|
617 fprintf (stderr, "---> %s\n", buffer);
|
|
618
|
|
619 if (putline (buffer, output) == NOTOK)
|
|
620 return NOTOK;
|
|
621
|
|
622 switch (getline (response, sizeof response, input)) {
|
|
623 case OK:
|
|
624 if (poprint)
|
|
625 fprintf (stderr, "<--- %s\n", response);
|
|
626 #ifndef NNTP
|
|
627 return (*response == '+' ? OK : NOTOK);
|
|
628 #else /* NNTP */
|
|
629 return (*response < CHAR_ERR ? OK : NOTOK);
|
|
630 #endif /* NNTP */
|
|
631
|
|
632 case NOTOK:
|
|
633 case DONE:
|
|
634 if (poprint)
|
|
635 fprintf (stderr, "%s\n", response);
|
|
636 return NOTOK;
|
|
637 }
|
|
638 /* NOTREACHED */
|
|
639 }
|
|
640
|
|
641 #if defined(MPOP) && !defined(NNTP)
|
|
642 int multiline () {
|
|
643 #else
|
|
644 static int multiline () {
|
|
645 #endif
|
|
646 char buffer[BUFSIZ + TRMLEN];
|
|
647
|
|
648 if (getline (buffer, sizeof buffer, input) != OK)
|
|
649 return NOTOK;
|
|
650 #ifdef DEBUG
|
|
651 if (poprint)
|
|
652 fprintf (stderr, "<--- %s\n", response);
|
|
653 #endif /* DEBUG */
|
|
654 if (strncmp (buffer, TRM, TRMLEN) == 0) {
|
|
655 if (buffer[TRMLEN] == 0)
|
|
656 return DONE;
|
|
657 else
|
|
658 (void) strcpy (response, buffer + TRMLEN);
|
|
659 }
|
|
660 else
|
|
661 (void) strcpy (response, buffer);
|
|
662
|
|
663 return OK;
|
|
664 }
|
|
665
|
|
666 /* */
|
|
667
|
|
668 static int getline (s, n, iop)
|
|
669 char *s;
|
|
670 int n;
|
|
671 FILE * iop;
|
|
672 {
|
|
673 int c;
|
|
674 char *p;
|
|
675
|
|
676 p = s;
|
|
677 while (--n > 0 && (c = fgetc (iop)) != EOF)
|
|
678 if ((*p++ = c) == '\n')
|
|
679 break;
|
|
680 if (ferror (iop) && c != EOF) {
|
|
681 (void) strcpy (response, "error on connection");
|
|
682 return NOTOK;
|
|
683 }
|
|
684 if (c == EOF && p == s) {
|
|
685 (void) strcpy (response, "connection closed by foreign host");
|
|
686 return DONE;
|
|
687 }
|
|
688 *p = 0;
|
|
689 if (*--p == '\n')
|
|
690 *p = 0;
|
|
691 if (*--p == '\r')
|
|
692 *p = 0;
|
|
693
|
|
694 return OK;
|
|
695 }
|
|
696
|
|
697
|
|
698 static putline (s, iop)
|
|
699 char *s;
|
|
700 FILE * iop;
|
|
701 {
|
|
702 (void) fprintf (iop, "%s\r\n", s);
|
|
703 (void) fflush (iop);
|
|
704 if (ferror (iop)) {
|
|
705 (void) strcpy (response, "lost connection");
|
|
706 return NOTOK;
|
|
707 }
|
|
708
|
|
709 return OK;
|
|
710 }
|