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