Back to index

courier  0.68.2
rfc2045_fromfd.c
Go to the documentation of this file.
00001 /*
00002 ** Copyright 1998 - 2008 Double Precision, Inc.  See COPYING for
00003 ** distribution information.
00004 */
00005 
00006 /*
00007 */
00008 #if    HAVE_CONFIG_H
00009 #include "rfc2045_config.h"
00010 #endif
00011 
00012 #include      <sys/types.h>
00013 
00014 #include      "rfc2045.h"
00015 #if    HAVE_UNISTD_H
00016 #include      <unistd.h>
00017 #endif
00018 
00019 /* Convert a message to the RFC2045 structure */
00020 
00021 struct rfc2045 *rfc2045_fromfd(int fd)
00022 {
00023 struct rfc2045       *rfc;
00024 char   buf[BUFSIZ];
00025 int    n;
00026 off_t  orig_pos;
00027 
00028        if ((orig_pos=lseek(fd, 0L, SEEK_CUR)) == (off_t)-1) return (NULL);
00029        if (lseek(fd, (off_t)0, SEEK_SET) == (off_t)-1)  return (NULL);
00030        if ((rfc=rfc2045_alloc()) == 0)    return (NULL);
00031 
00032        while ((n=read(fd, buf, sizeof(buf))) > 0)
00033               rfc2045_parse(rfc, buf, n);
00034        rfc2045_parse_partial(rfc);
00035 
00036        if (lseek(fd, orig_pos, SEEK_SET) == (off_t)-1)
00037        {
00038               rfc2045_free(rfc);
00039               rfc=0;
00040        }
00041        return (rfc);
00042 }
00043 
00044 /* Convert a message to the RFC2045 structure, halting after header */
00045 
00046 struct rfc2045 *rfc2045header_fromfd(int fd)
00047 {
00048 struct        rfc2045        *rfc;
00049 char        buf[BUFSIZ];
00050 int        n;
00051 off_t        orig_pos;
00052 
00053         if ((orig_pos=lseek(fd, 0L, SEEK_CUR)) == (off_t)-1) return (NULL);
00054         if (lseek(fd, (off_t)0, SEEK_SET) == (off_t)-1)        return (NULL);
00055         if ((rfc=rfc2045_alloc()) == 0)        return (NULL);
00056 
00057         while (rfc->workinheader && (n=read(fd, buf, sizeof(buf))) > 0)
00058                 rfc2045_parse(rfc, buf, n);
00059         if (lseek(fd, orig_pos, SEEK_SET) == (off_t)-1)
00060         {
00061                 rfc2045_free(rfc);
00062                 rfc=0;
00063         }
00064         return (rfc);
00065 }