1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
|
/* $Header$ */
/*
* Copyright © 1988-2004 Keith Packard and Bart Massey.
* All Rights Reserved. See the file COPYING in this directory
* for licensing information.
*/
/*
* util.c
*
* general purpose utilities
*/
#include "nickle.h"
#ifdef notdef
double
dist (x0, y0, x1, y1)
double x0, y0, x1, y1;
{
register double tx, ty;
tx = x0 - x1;
ty = y0 - y1;
return sqrt (tx*tx + ty*ty);
}
#endif
DataType TempType = { 0, 0, "TempType" };
void *
AllocateTemp (int size)
{
DataType **b;
b = ALLOCATE (&TempType, sizeof (DataType *) + size);
return b + 1;
}
#include <stdarg.h>
#include <stdio.h>
#ifdef HAVE_VPRINTF
#include <sys/poll.h>
#include <errno.h>
/*
* Currently vfprintf() is required. It would
* be easy to do a _doprnt() version if necessary,
* and it would certainly be possible to develop
* non-varargs versions of these. Contributed code welcome.
*/
static int
wait_write (int fd, char *buf, int len)
{
int n;
int w = 0;
while (len)
{
n = write (fd, buf, len);
if (n < 0)
{
if (errno == EINTR)
{
struct pollfd f;
f.fd = fd;
f.events = POLLOUT;
(void) poll (&f, 1, -1);
}
else
{
if (w)
return w;
return -1;
}
}
else
{
w += n;
buf += n;
len -= n;
}
}
return w;
}
void
debug (char *format, ...)
{
va_list ap;
char buf[4096];
int len;
va_start (ap, format);
len = vsnprintf (buf, sizeof (buf), format, ap);
va_end (ap);
wait_write (2, buf, len);
}
void
panic (char *format, ...)
{
va_list ap;
char buf[4096];
int len;
va_start (ap, format);
len = vsnprintf (buf, sizeof (buf), format, ap);
va_end (ap);
wait_write (2, buf, len);
abort ();
}
#endif
|