imapext-2007

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

imap-2007e
author yuuji@gentei.org
date Mon, 14 Sep 2009 15:17:45 +0900
parents
children
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/c-client/netmsg.c	Mon Sep 14 15:17:45 2009 +0900
     1.3 @@ -0,0 +1,104 @@
     1.4 +/* ========================================================================
     1.5 + * Copyright 1988-2006 University of Washington
     1.6 + *
     1.7 + * Licensed under the Apache License, Version 2.0 (the "License");
     1.8 + * you may not use this file except in compliance with the License.
     1.9 + * You may obtain a copy of the License at
    1.10 + *
    1.11 + *     http://www.apache.org/licenses/LICENSE-2.0
    1.12 + *
    1.13 + * 
    1.14 + * ========================================================================
    1.15 + */
    1.16 +
    1.17 +/*
    1.18 + * Program:	Network message (SMTP/NNTP/POP2/POP3) routines
    1.19 + *
    1.20 + * Author:	Mark Crispin
    1.21 + *		Networks and Distributed Computing
    1.22 + *		Computing & Communications
    1.23 + *		University of Washington
    1.24 + *		Administration Building, AG-44
    1.25 + *		Seattle, WA  98195
    1.26 + *		Internet: MRC@CAC.Washington.EDU
    1.27 + *
    1.28 + * Date:	8 June 1995
    1.29 + * Last Edited:	6 December 2006
    1.30 + */
    1.31 +
    1.32 +
    1.33 +#include <stdio.h>
    1.34 +#include <errno.h>
    1.35 +extern int errno;		/* just in case */
    1.36 +#include "c-client.h"
    1.37 +#include "netmsg.h"
    1.38 +#include "flstring.h"
    1.39 +
    1.40 +/* Network message read
    1.41 + * Accepts: file
    1.42 + *	    number of bytes to read
    1.43 + *	    buffer address
    1.44 + * Returns: T if success, NIL otherwise
    1.45 + */
    1.46 +
    1.47 +long netmsg_read (void *stream,unsigned long count,char *buffer)
    1.48 +{
    1.49 +  return (fread (buffer,(size_t) 1,(size_t) count,(FILE *) stream) == count) ?
    1.50 +    T : NIL;
    1.51 +}
    1.52 +
    1.53 +/* Slurp dot-terminated text from NET
    1.54 + * Accepts: NET stream
    1.55 + *	    place to return size
    1.56 + *	    place to return header size
    1.57 + * Returns: file descriptor
    1.58 + */
    1.59 +
    1.60 +FILE *netmsg_slurp (NETSTREAM *stream,unsigned long *size,unsigned long *hsiz)
    1.61 +{
    1.62 +  unsigned long i;
    1.63 +  char *s,*t,tmp[MAILTMPLEN];
    1.64 +  FILE *f = tmpfile ();
    1.65 +  if (!f) {
    1.66 +    sprintf (tmp,".%lx.%lx",(unsigned long) time (0),(unsigned long)getpid ());
    1.67 +    if (f = fopen (tmp,"wb+")) unlink (tmp);
    1.68 +    else {
    1.69 +      sprintf (tmp,"Unable to create scratch file: %.80s",strerror (errno));
    1.70 +      MM_LOG (tmp,ERROR);
    1.71 +      return NIL;
    1.72 +    }
    1.73 +  }
    1.74 +  *size = 0;			/* initially emtpy */
    1.75 +  if (hsiz) *hsiz = 0;
    1.76 +  while (s = net_getline (stream)) {
    1.77 +    if (*s == '.') {		/* possible end of text? */
    1.78 +      if (s[1]) t = s + 1;	/* pointer to true start of line */
    1.79 +      else {
    1.80 +	fs_give ((void **) &s);	/* free the line */
    1.81 +	break;			/* end of data */
    1.82 +      }
    1.83 +    }
    1.84 +    else t = s;			/* want the entire line */
    1.85 +    if (f) {			/* copy it to the file */
    1.86 +      i = strlen (t);		/* size of line */
    1.87 +      if ((fwrite (t,(size_t) 1,(size_t) i,f) == i) &&
    1.88 +	  (fwrite ("\015\012",(size_t) 1,(size_t) 2,f) == 2)) {
    1.89 +	*size += i + 2;		/* tally up size of data */
    1.90 +				/* note header position */
    1.91 +	if (!i && hsiz && !*hsiz) *hsiz = *size;
    1.92 +      }
    1.93 +      else {
    1.94 +	sprintf (tmp,"Error writing scratch file at byte %lu",*size);
    1.95 +	MM_LOG (tmp,ERROR);
    1.96 +	fclose (f);		/* forget it */
    1.97 +	f = NIL;		/* failure now */
    1.98 +      }
    1.99 +    }
   1.100 +    fs_give ((void **) &s);	/* free the line */
   1.101 +  }
   1.102 +				/* if making a file, rewind to start of file */
   1.103 +  if (f) fseek (f,(unsigned long) 0,SEEK_SET);
   1.104 +				/* header consumes entire message */
   1.105 +  if (hsiz && !*hsiz) *hsiz = *size;
   1.106 +  return f;			/* return the file descriptor */
   1.107 +}

UW-IMAP'd extensions by yuuji