Back to index

courier  0.68.2
pcpaddcommon.c
Go to the documentation of this file.
00001 /*
00002 ** Copyright 2001 Double Precision, Inc.  See COPYING for
00003 ** distribution information.
00004 */
00005 
00006 
00007 #include "config.h"
00008 #include <stdio.h>
00009 #include <stdlib.h>
00010 #include <string.h>
00011 #include <errno.h>
00012 #include <signal.h>
00013 #include <fcntl.h>
00014 #include <unistd.h>
00015 #include <sys/stat.h>
00016 #include "pcp.h"
00017 
00018 static int compar_times(const void *a, const void *b)
00019 {
00020        const struct PCP_event_time *aa=*(const struct PCP_event_time **)a;
00021        const struct PCP_event_time *bb=*(const struct PCP_event_time **)b;
00022 
00023        return (aa->start < bb->start ? -1:
00024               aa->start > bb->start ? 1:
00025               aa->end < bb->end ? -1:
00026               aa->end > bb->end ? 1:0);
00027 }
00028 
00029 const struct PCP_event_time **
00030 pcp_add_sort_times(const struct PCP_event_time *t,
00031                  unsigned n)
00032 {
00033        const struct PCP_event_time **ptr;
00034        unsigned i;
00035 
00036        if (n == 0)
00037        {
00038               errno=EINVAL;
00039               return (NULL);
00040        }
00041 
00042        ptr=malloc(n*sizeof(const struct PCP_event_time *));
00043        if (!ptr)
00044               return (NULL);
00045 
00046        for (i=0; i<n; i++)
00047               ptr[i]=t+i;
00048 
00049        if (n)
00050               qsort(ptr, n, sizeof(*ptr),
00051                     compar_times);
00052 
00053        for (i=0; i<n; i++)
00054        {
00055               if (ptr[i]->start > ptr[i]->end)
00056               {
00057                      free(ptr);
00058                      errno=EINVAL;
00059                      return (NULL);
00060               }
00061 
00062               if (i > 0 && ptr[i-1]->end > ptr[i]->start)
00063               {
00064                      free(ptr);
00065                      errno=EINVAL;
00066                      return (NULL);
00067               }
00068        }
00069 
00070        return (ptr);
00071 }
00072 
00073 int pcp_read_saveevent(struct PCP_save_event *ae,
00074                      char *buf, int bufsize)
00075 {
00076        int n;
00077 
00078        if (ae->write_event_func)
00079               return ( ((*ae->write_event_func)
00080                        (buf, bufsize, ae->write_event_func_misc_ptr)));
00081 
00082        if (!ae->write_event_buf && ae->write_event_fd >= 0)
00083               return ( read(ae->write_event_fd, buf, bufsize));
00084 
00085        if (ae->write_event_buf == 0)
00086        {
00087               errno=EIO;
00088               return (-1);
00089        }
00090 
00091        for (n=0; n<bufsize && *ae->write_event_buf; n++)
00092               buf[n]= *ae->write_event_buf++;
00093 
00094        return (n);
00095 }