summaryrefslogtreecommitdiff
path: root/dviware/kyocera/util.c
blob: 04504deeec2531680d88f90a4ed0634adab87c67 (plain)
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#include <stdio.h>
#include "util.h"
#include "dev.h"

char *outfname, *infname, *user, *host, *pgmnam, efn[256];
FILE *ef;
int olderrfd,debugging;

/* Utility procedures
 */

open_ef()
{
  if (!debugging) {
    sprintf(efn,"/usr/tmp/%s.err.XXXXXX",pgmnam);
    if (!(ef = fopen(mktemp(efn),"w+"))) croak("couldn't open %s",efn);
    unlink(efn);
    fflush(stderr); olderrfd = dup(2); close(2); dup(fileno(ef));
  }
}

close_ef()
{
  long fpos;

  if (ef) {
    /* Now put back the old stderr */
    (void) fflush(stderr);
    fpos = ftell(stderr);
    close(2); dup(olderrfd); close(olderrfd);
    /* And print out anything that was sent to stderr */
    if (fpos > 0) {
      rewind(ef);
      dev_print_log(ef);
    }
    (void) fclose(ef);
    ef = NULL;
  }
}

/*VARARGS1*/
debug(fmt,a,b,c,d,e)
     char *fmt;
     long a,b,c,d,e;
{
    if (debugging) fprintf(stderr,fmt,a,b,c,d,e);
}

/*VARARGS1*/
croak(fmt,a,b,c,d,e)
     char *fmt;
     long a,b,c,d,e;
{
  fprintf(stderr,"%s: ",pgmnam);
  fprintf(stderr,fmt,a,b,c,d,e);
  fprintf(stderr,"\n");
  if (debugging) {
    perror("last error");
    if (infname) fprintf(stderr,"input file %s\n",infname);
    if (outfname) fprintf(stderr,"output file %s\n",outfname);
    if (user && host) fprintf(stderr,"running for %s@%s\n",user,host);
  }
  close_ef();
  exit(0);
}

swallow(nchars,f)
     long nchars;
     FILE *f;
{
  while (nchars-- > 0) (void) getc(f);
}

unsigned long get2(f)
     FILE *f;
{
  register unsigned long x;

  x = getc(f);
  x = (x << 8) | getc(f);
  return(x);
}

unsigned long get3(f)
     FILE *f;
{
  register unsigned long x;

  x = getc(f);
  x = (x << 8) | getc(f);
  x = (x << 8) | getc(f);
  return(x);
}

unsigned long get4(f)
     FILE *f;
{
  register unsigned long x;

  x = getc(f);
  x = (x << 8) | getc(f);
  x = (x << 8) | getc(f);
  x = (x << 8) | getc(f);
  return(x);
}

long sget2(f)
     FILE *f;
{
  register long x;

  x = ((long) ((char) getc(f)));
  x = (x << 8) | getc(f);
  return(x);
}

long sget3(f)
     FILE *f;
{
  register long x;

  x = ((long) ((char) getc(f)));
  x = (x << 8) | getc(f);
  x = (x << 8) | getc(f);
  return(x);
}

long sget4(f)
     FILE *f;
{
  register long x;

  x = ((long) ((char) getc(f)));
  x = (x << 8) | getc(f);
  x = (x << 8) | getc(f);
  x = (x << 8) | getc(f);
  return(x);
}

put4(l,f)
     unsigned long l;
     FILE *f;
{
  putc((l >> 24) & 0377,f);
  putc((l >> 16) & 0377,f);
  putc((l >> 8) & 0377,f);
  putc(l & 0377,f);
}

put2(s,f)
     unsigned short s;
     FILE *f;
{
  putc((s >> 8) & 0377,f);
  putc(s & 0377,f);
}