File: cmain.c

package info (click to toggle)
libcompface 1989.11.11-23
  • links: PTS
  • area: main
  • in suites: woody
  • size: 212 kB
  • ctags: 156
  • sloc: ansic: 1,510; makefile: 203; sh: 40
file content (162 lines) | stat: -rw-r--r-- 3,303 bytes parent folder | download | duplicates (2)
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
158
159
160
161
162

/*  @(#)cmain.c 1.7 91/10/24
 *
 *  Compface - 48x48x1 image compression.
 *
 *  Copyright (c) James Ashton - Sydney University - June 1990.
 *
 *  Written 11th November 1889.
 *
 *  Permission is given to distribute these sources, as long as the
 *  copyright messages are not removed, and no monies are exchanged. 
 *
 *  No responsibility is taken for any errors on inaccuracies inherent
 *  either to the comments or the code of this program, but if reported
 *  to me, then an attempt will be made to fix them.
 */

#include <fcntl.h>
#include "compface.h"

/* the buffer is longer than needed to handle sparse input formats */
#define FACEBUFLEN 2048
char fbuf[FACEBUFLEN];

/* IO file descriptors and their names */
int infile    = 0;
char *inname  = "<stdin>";
int outfile   = 1;
char *outname = "<stdout>";

/* basename of executable */
char *cmdname;

/* error handling definitions follow */

extern void exit P((int)) ;

#define ERR strerror(errno)
#define INITERR(s) {(void)strcpy(fbuf, cmdname); (void)strcat(fbuf, ": ");\
					(void)strcat(fbuf, (s));}
#define ADDERR(s) (void)strcat(fbuf, (s));
#define ERROR {(void)strcat(fbuf, "\n");\
				(void)write(2, fbuf, strlen(fbuf)); exit(1);}
#define INITWARN(s) {(void)strcpy(fbuf, cmdname);\
					(void)strcat(fbuf, ": (warning) ");\
					(void)strcat(fbuf, (s));}
#define ADDWARN(s) (void)strcat(fbuf, (s));
#define WARN {(void)strcat(fbuf, "\n"); (void)write(2, fbuf, strlen(fbuf));}

int
main(argc, argv)
int argc;
char *argv[];
{
  cmdname = *argv;
  while (**argv)
    if (*((*argv)++) == '/')
      cmdname = *argv;               /* find the command's basename */

  if (argc > 3)
    {
      INITERR("usage: ")
      ADDERR(cmdname)
      ADDERR(" [infile [outfile]]")
      ERROR
    }

  if ((argc > 1) && strcmp(*++argv, "-"))
    {
      inname = *argv;
      if ((infile = open(inname, O_RDONLY)) == -1)
        {
          INITERR(inname)
          ADDERR(": ")
          ADDERR(ERR)
          ERROR
        }
    }

  if (argc > 2)
    {
      outname = *++argv;
      if ((outfile = open(outname, O_WRONLY | O_CREAT | O_TRUNC, 0644)) == -1)
        {
          INITERR(outname)
          ADDERR(": ")
          ADDERR(ERR)
          ERROR
        }
    }

  (void) ReadBuf();
  switch (compface(fbuf))
    {
      case -2 : INITERR("internal error")
                ERROR
      case -1 : INITERR(inname)
                ADDERR(": insufficient or invalid data")
                ERROR
      case  1 : INITWARN(inname)
                ADDWARN(": excess data ignored")
                WARN
      default : ;
    }
  (void) WriteBuf();
  exit(0);
/*NOTREACHED*/
}


int
WriteBuf()
{
	register char *s, *t;
	register int len;

	s = fbuf;
	t = s + strlen(s);
	while (s < t)
	{
		if ((len = write(outfile, s, t - s)) == -1)
		{
			INITERR(outname)
			ADDERR(": ")
			ADDERR(ERR)
			ERROR
		}
		s += len;
	}
	return 0;
}


int
ReadBuf()
{
	register int count, len;
	register char *t;

	count = 0;
	t = fbuf;
	while (len = read(infile, t, FACEBUFLEN - count))
	{
		if (len == -1)
		{
			INITERR(inname)
			ADDERR(": ")
			ADDERR(ERR)
			ERROR
		}
		t += len;
		if ((count += len) >= FACEBUFLEN)
		{
			INITWARN(inname)
			ADDWARN(" exceeds internal buffer size.  Data may be lost")
			WARN
			break;
		}
	}
	*t = '\0';
	return count;
}