Esempio n. 1
0
int main()
{
   int i,j,k;
   double x,y,a1,a2,a3,a;

   while(1) {
      for(i=0;i<2;i++) {
         scanf("%d",&n[i]);
         if(n[0]==0) goto done;
         for(j=0;j<n[i];j++) scanf("%d %d",&px[i][j],&py[i][j]);
         px[i][j]=px[i][0]; py[i][j]=py[i][0];
      }
      np=0;
      for(i=0;i<n[0];i++)
         for(j=0;j<n[1];j++) {
            /* check all lines in polygon 1 against all lines in polygon 2 for
             * intersections */
            k=linesintersect(px[0][i],py[0][i],px[0][i+1],py[0][i+1],
               px[1][j],py[1][j],px[1][j+1],py[1][j+1],&x,&y);
            if(k==1) {
               p[np].x=x; p[np++].y=y;
            } else if(k==2) {
               s[0].x=px[0][i]; s[0].y=py[0][i];
               s[1].x=px[0][i+1]; s[1].y=py[0][i+1];
               s[2].x=px[1][j]; s[2].y=py[1][j];
               s[3].x=px[1][j+1]; s[3].y=py[1][j+1];
               qsort(s,2,sizeof(s[0]),comppi);
               qsort(s+2,2,sizeof(s[0]),comppi);
               if(comppi(&s[1],&s[2])==1) {
                  if(comppi(&s[0],&s[2])==1) {
                     p[np].x=s[0].x; p[np++].y=s[0].y;                     
                  } else {
                     p[np].x=s[2].x; p[np++].y=s[2].y;
                  }
                  if(comppi(&s[1],&s[2])!=0) {
                     if(comppi(&s[1],&s[3])==-1) {
                        p[np].x=s[1].x; p[np++].y=s[1].y;
                     } else {
                        p[np].x=s[3].x; p[np++].y=s[3].y;
                     }
                  }
               }
            }
         }
      /* check if point from polygon 1 is within polygon 2 */
      for(i=0;i<n[0];i++)
         if(pnpoly(n[1],px[1],py[1],px[0][i],py[0][i])) {
            p[np].x=px[0][i]; p[np++].y=py[0][i];
         }
      /* check if point from polygon 2 is within polygon 1 */
      for(j=0;j<n[1];j++)
         if(pnpoly(n[0],px[0],py[0],px[1][j],py[1][j])) {
            p[np].x=px[1][j]; p[np++].y=py[1][j];
         }
/*      printf("%d: ",np);
      for(i=0;i<np;i++) printf("(%.1f %.1f) ",p[i].x,p[i].y);
      printf("\n");*/
      /* convex hull */
      if(np>=3) {
         nr=chainhull2d(p,np,r);
         r[nr]=r[0];
      } else 
         nr=0;
/*      printf("%d: ",nr);
      for(i=0;i<=nr;i++) printf("(%.1f %.1f) ",r[i].x,r[i].y);
      printf("\n");*/
      a1=fabs(areap(n[0],px[0],py[0]));
      a2=fabs(areap(n[1],px[1],py[1]));
      if(nr>=3) a3=fabs(aread(nr,r));
      else a3=0;
      a=(a1+a2)/2-a3;
      printf("%8.2f",a);
   }
done:
   printf("\n");
}
Esempio n. 2
0
/*
 * Process one message from message packet.
 *
 *  0   - no more messages
 *  1   - more messages
 *  2   - bad file
 *  3   - bad message header
 *  4   - unable to open temp file
 *  5   - unexpected end of packet
 *  >10 - import error
 */
int getmessage(FILE *pkt, faddr *p_from, faddr *p_to)
{
    char	    buf[MAX_LINE_LENGTH +1], *orig = NULL, *p, *l, *r, *subj = NULL;
    int		    tmp, rc, maxrc = 0, result, flags, cost;
    static faddr    f, t;
    faddr	    *o;
    time_t	    mdate = 0L;
    FILE	    *fp;
    unsigned char   buffer[0x0e];

    Nopper();

    result = fread(&buffer, 1, sizeof(buffer), pkt);
    if (result == 0) {
	Syslog('m', "Zero bytes message, assume end of pkt");
	return 0;
    }

    switch (tmp = (buffer[0x01] << 8) + buffer[0x00]) {
	case 0:	if (result == 2)
		    return 0;
		else {
		    Syslog('!', "Junk after logical end of packet, skipped");
		    return 5;

		}
	case 2:	break;

	default:Syslog('!', "bad message type: 0x%04x",tmp);
		return 2;
    }

    if (result != 14) {
	Syslog('!', "Unexpected end of packet");
	return 5;
    }

    memset(&f, 0, sizeof(f));
    memset(&t, 0, sizeof(t));
    f.node = (buffer[0x03] << 8) + buffer[0x02];
    t.node = (buffer[0x05] << 8) + buffer[0x04];
    f.net  = (buffer[0x07] << 8) + buffer[0x06];
    t.net  = (buffer[0x09] << 8) + buffer[0x08];
    flags  = (buffer[0x0b] << 8) + buffer[0x0a];
    cost   = (buffer[0x0d] << 8) + buffer[0x0c];

    /*
     * Read the DateTime, toUserName, fromUserName and subject fields
     * from the packed message. The stringlength is +1 for the right
     * check. This is different then in ifmail's original code.
     */
    if (aread(buf, sizeof(buf)-1, pkt)) {
	if (strlen(buf) > 20)
	    Syslog('!', "date too long (%d) \"%s\"", strlen(buf), printable(buf, 0));
	mdate = parsefdate(buf, NULL);
	if (aread(buf, sizeof(buf)-1, pkt)) {
	    Syslog('!', "date not null-terminated: \"%s\"",buf);
	    return 3;
	}
    }

    if (aread(buf, sizeof(buf)-1, pkt)) {
	if (strlen(buf) > 36)
	    Syslog('!', "to name too long (%d) \"%s\"", strlen(buf), printable(buf, 0));
	t.name = xstrcpy(buf);
	if (aread(buf, sizeof(buf)-1, pkt)) {
	    if (*(p=t.name+strlen(t.name)-1) == '\n')
		*p = '\0';
	    Syslog('!', "to name not null-terminated: \"%s\"",buf);
	    return 3;
	}
    }

    if (aread(buf, sizeof(buf)-1, pkt)) {
	if (strlen(buf) > 36)
	    Syslog('!', "from name too long (%d) \"%s\"", strlen(buf), printable(buf, 0));
	f.name = xstrcpy(buf);
	if (aread(buf, sizeof(buf)-1, pkt)) {
	    if (*(p=f.name+strlen(f.name)-1) == '\n') 
		*p = '\0';
	    Syslog('!', "from name not null-terminated: \"%s\"",buf);
	    return 3;
	}
    }
	
    if (aread(buf, sizeof(buf)-1, pkt)) {
	if (strlen(buf) > 72)
	    Syslog('!', "subject too long (%d) \"%s\"", strlen(buf), printable(buf, 0));
	subj = xstrcpy(buf);
	if (aread(buf, sizeof(buf)-1, pkt)) {
	    if (*(p=subj+strlen(subj)-1) == '\n') 
		*p = '\0';
	    subj = xstrcat(subj,(char *)"\\n");
	    subj = xstrcat(subj,buf);
	    Syslog('!', "subj not null-terminated: \"%s\"",buf);
	    return 3;
	}
    }

    if (feof(pkt) || ferror(pkt)) {
	Syslog('!', "Could not read message header, aborting");
	return 3;
    }

    if ((fp = tmpfile()) == NULL) {
	WriteError("$unable to open temporary file");
	return 4;
    }

    /*
     * Read the text from the .pkt file
     */
    while (aread(buf,sizeof(buf)-1,pkt)) {

	fputs(buf, fp);

	/*
	 * Extract info from Origin line if found.
	 */
	if (!strncmp(buf," * Origin:",10)) {
	    p=buf+10;
	    while (*p == ' ') 
		p++;
	    if ((l=strrchr(p,'(')) && (r=strrchr(p,')')) && (l < r)) {
		*l = '\0';
		*r = '\0';
		l++;
		if ((o = parsefnode(l))) {
		    f.point = o->point;
		    f.node = o->node;
		    f.net = o->net;
		    f.zone = o->zone;
		    if (o->domain) 
			f.domain=o->domain;
		    o->domain=NULL;
		    tidy_faddr(o);
		}
	    } else
		if (*(l=p+strlen(p)-1) == '\n')
		    *l='\0';
		for (l=p+strlen(p)-1;*l == ' ';l--) 
		    *l='\0'; 
		orig = xstrcpy(p);
	}
    }

    rc = importmsg(p_from, &f, &t, orig, subj, mdate, flags, cost, fp, p_to->zone);
    if (rc)
	rc+=10;
    if (rc > maxrc) 
	maxrc = rc;

    fclose(fp);

    if(f.name) 
	free(f.name); 
    f.name=NULL;

    if(t.name) 
	free(t.name); 
    t.name=NULL;

    if(f.domain) 
	free(f.domain); 
    f.domain=NULL;

    if(t.domain) 
	free(t.domain); 
    t.domain=NULL;

    if (subj)
	free(subj);
    subj = NULL;

    if (orig)
	free(orig);
    orig = NULL;

    if (feof(pkt) || ferror(pkt)) {
	WriteError("Unexpected end of packet");
	return 5;
    }
    return 1;
}
Esempio n. 3
0
int
main(int argc, char **argv)
{
	long		elaps[2];
	ulong_t		etime, stime;
	unsigned long	mem;
	ulong_t		expand();
	int 		ver;	/* version of acct struct */
	int 		aread();

	if ((ur = (struct urec *) calloc(a_usize,
		sizeof (struct urec))) == NULL) {
		fprintf(stderr, "acctpr1: Cannot allocate memory\n");
		exit(3);
	}

	urlast = ur;
	if ((sr = (struct srec *) calloc(a_ssize,
		sizeof (struct srec))) == NULL) {
		fprintf(stderr, "acctpr1: Cannot allocate memory\n");
		exit(3);
	}

	while (--argc > 0) {
		if (**++argv == '-')
			switch(*++*argv) {
			}
		else {
			readctmp(*argv);
		}
	}


	if (fread((char *)&ab, sizeof(struct acct), 1, stdin) != 1)
		exit(1);
	else if (ab.ac_flag & AEXPND)
		ver = 2;	/* 4.0 acct structure */
	else 
		ver = 1;	/* 3.x acct structure */

	rewind(stdin);

	while (aread(ver) == 1) {
		if (!MYKIND(ab.ac_flag))
			continue;
		pb.pt_uid = ab.ac_uid;
		CPYN(pb.pt_name, getname(ab.ac_uid, ab.ac_tty, ab.ac_btime));
		/*
		 * approximate cpu P/NP split as same as elapsed time
		 */
		if ((etime = SECS(expand(ab.ac_etime))) == 0)
			etime = 1;
		stime = expand(ab.ac_stime) + expand(ab.ac_utime);
		mem = expand(ab.ac_mem);
		if(pnpsplit(ab.ac_btime, etime, elaps) == 0) {
			fprintf(stderr, "acctprc1: could not calculate prime/non-prime hours\n");

			exit(1);
		}
		pb.pt_cpu[0] = (double)stime * (double)elaps[0] / etime;
		pb.pt_cpu[1] = (stime > pb.pt_cpu[0])? stime - pb.pt_cpu[0] : 0;
		pb.pt_cpu[1] = stime - pb.pt_cpu[0];
		if (stime)
			pb.pt_mem = (mem + stime - 1) / stime;
		else
			pb.pt_mem = 0;	/* unlikely */
		printf("%ld\t%.*s\t%lu\t%lu\t%u\n",
		    pb.pt_uid,
		    OUTPUT_NSZ,
		    pb.pt_name,
		    pb.pt_cpu[0], pb.pt_cpu[1],
		    pb.pt_mem);
	}
	
	exit(0);
}