imapext-2007

annotate src/c-client/netmsg.c @ 0:ada5e610ab86

imap-2007e
author yuuji@gentei.org
date Mon, 14 Sep 2009 15:17:45 +0900
parents
children
rev   line source
yuuji@0 1 /* ========================================================================
yuuji@0 2 * Copyright 1988-2006 University of Washington
yuuji@0 3 *
yuuji@0 4 * Licensed under the Apache License, Version 2.0 (the "License");
yuuji@0 5 * you may not use this file except in compliance with the License.
yuuji@0 6 * You may obtain a copy of the License at
yuuji@0 7 *
yuuji@0 8 * http://www.apache.org/licenses/LICENSE-2.0
yuuji@0 9 *
yuuji@0 10 *
yuuji@0 11 * ========================================================================
yuuji@0 12 */
yuuji@0 13
yuuji@0 14 /*
yuuji@0 15 * Program: Network message (SMTP/NNTP/POP2/POP3) routines
yuuji@0 16 *
yuuji@0 17 * Author: Mark Crispin
yuuji@0 18 * Networks and Distributed Computing
yuuji@0 19 * Computing & Communications
yuuji@0 20 * University of Washington
yuuji@0 21 * Administration Building, AG-44
yuuji@0 22 * Seattle, WA 98195
yuuji@0 23 * Internet: MRC@CAC.Washington.EDU
yuuji@0 24 *
yuuji@0 25 * Date: 8 June 1995
yuuji@0 26 * Last Edited: 6 December 2006
yuuji@0 27 */
yuuji@0 28
yuuji@0 29
yuuji@0 30 #include <stdio.h>
yuuji@0 31 #include <errno.h>
yuuji@0 32 extern int errno; /* just in case */
yuuji@0 33 #include "c-client.h"
yuuji@0 34 #include "netmsg.h"
yuuji@0 35 #include "flstring.h"
yuuji@0 36
yuuji@0 37 /* Network message read
yuuji@0 38 * Accepts: file
yuuji@0 39 * number of bytes to read
yuuji@0 40 * buffer address
yuuji@0 41 * Returns: T if success, NIL otherwise
yuuji@0 42 */
yuuji@0 43
yuuji@0 44 long netmsg_read (void *stream,unsigned long count,char *buffer)
yuuji@0 45 {
yuuji@0 46 return (fread (buffer,(size_t) 1,(size_t) count,(FILE *) stream) == count) ?
yuuji@0 47 T : NIL;
yuuji@0 48 }
yuuji@0 49
yuuji@0 50 /* Slurp dot-terminated text from NET
yuuji@0 51 * Accepts: NET stream
yuuji@0 52 * place to return size
yuuji@0 53 * place to return header size
yuuji@0 54 * Returns: file descriptor
yuuji@0 55 */
yuuji@0 56
yuuji@0 57 FILE *netmsg_slurp (NETSTREAM *stream,unsigned long *size,unsigned long *hsiz)
yuuji@0 58 {
yuuji@0 59 unsigned long i;
yuuji@0 60 char *s,*t,tmp[MAILTMPLEN];
yuuji@0 61 FILE *f = tmpfile ();
yuuji@0 62 if (!f) {
yuuji@0 63 sprintf (tmp,".%lx.%lx",(unsigned long) time (0),(unsigned long)getpid ());
yuuji@0 64 if (f = fopen (tmp,"wb+")) unlink (tmp);
yuuji@0 65 else {
yuuji@0 66 sprintf (tmp,"Unable to create scratch file: %.80s",strerror (errno));
yuuji@0 67 MM_LOG (tmp,ERROR);
yuuji@0 68 return NIL;
yuuji@0 69 }
yuuji@0 70 }
yuuji@0 71 *size = 0; /* initially emtpy */
yuuji@0 72 if (hsiz) *hsiz = 0;
yuuji@0 73 while (s = net_getline (stream)) {
yuuji@0 74 if (*s == '.') { /* possible end of text? */
yuuji@0 75 if (s[1]) t = s + 1; /* pointer to true start of line */
yuuji@0 76 else {
yuuji@0 77 fs_give ((void **) &s); /* free the line */
yuuji@0 78 break; /* end of data */
yuuji@0 79 }
yuuji@0 80 }
yuuji@0 81 else t = s; /* want the entire line */
yuuji@0 82 if (f) { /* copy it to the file */
yuuji@0 83 i = strlen (t); /* size of line */
yuuji@0 84 if ((fwrite (t,(size_t) 1,(size_t) i,f) == i) &&
yuuji@0 85 (fwrite ("\015\012",(size_t) 1,(size_t) 2,f) == 2)) {
yuuji@0 86 *size += i + 2; /* tally up size of data */
yuuji@0 87 /* note header position */
yuuji@0 88 if (!i && hsiz && !*hsiz) *hsiz = *size;
yuuji@0 89 }
yuuji@0 90 else {
yuuji@0 91 sprintf (tmp,"Error writing scratch file at byte %lu",*size);
yuuji@0 92 MM_LOG (tmp,ERROR);
yuuji@0 93 fclose (f); /* forget it */
yuuji@0 94 f = NIL; /* failure now */
yuuji@0 95 }
yuuji@0 96 }
yuuji@0 97 fs_give ((void **) &s); /* free the line */
yuuji@0 98 }
yuuji@0 99 /* if making a file, rewind to start of file */
yuuji@0 100 if (f) fseek (f,(unsigned long) 0,SEEK_SET);
yuuji@0 101 /* header consumes entire message */
yuuji@0 102 if (hsiz && !*hsiz) *hsiz = *size;
yuuji@0 103 return f; /* return the file descriptor */
yuuji@0 104 }

UW-IMAP'd extensions by yuuji