chiark / gitweb /
undo broken deletion
[trains.git] / parport / lib.c
1 /**/
2
3 #include <errno.h>
4 #include <stdio.h>
5 #include <string.h>
6 #include <sys/time.h>
7 #include <time.h>
8 #include <stdlib.h>
9
10 #include <sys/ioctl.h>
11
12 #include "lib.h"
13
14 int fd;
15
16 void doioctl(int ioctlnum, void *vp, unsigned long vpv) {
17   int r;
18   errno=0;
19   r= ioctl(fd, ioctlnum, vp);
20   if (r) {
21     fprintf(stderr,"ioctl #%d 0x%lx gave %d %s\n",
22             ioctlnum, vpv, r, strerror(errno));
23     exit(127);
24   }
25 }
26
27 unsigned long number(const char *a) {
28   unsigned long v;
29   char *ep;
30
31   v= strtoul(a,&ep,0);
32   if (*ep || ep==a) {
33     fprintf(stderr,"bad number `%s'\n",a);
34     exit(127);
35   }
36   return v;
37 }
38
39 static void gettod(struct timeval *now_r) {
40   int r;
41   r= gettimeofday(now_r,0);
42   if (r) { perror("gettimeofday failed"); exit(127); }
43 }  
44
45 void usleep_gettod(unsigned long us) {
46   struct timeval start, end, now;
47   ldiv_t ld;
48
49   gettod(&start);
50
51   ld= ldiv(start.tv_usec + us, 1000000);
52   end.tv_sec= start.tv_sec + ld.quot;
53   end.tv_usec= ld.rem;
54
55   do {
56     gettod(&now);
57   } while (timercmp(&now,&end,<));
58 }