1 /*
2 * Copyright (c) 2000 Satoru Takabayashi <satoru@namazu.org>
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 */
33
34 #include <assert.h>
35 #include <errno.h>
36 #include <stdio.h>
37 #include <stdlib.h>
38 #include <string.h>
39 #include <unistd.h>
40
41 #include "ttyrec.h"
42
43 #define SWAP_ENDIAN(val) ((unsigned int) ( \
44 (((unsigned int) (val) & (unsigned int) 0x000000ffU) << 24) | \
45 (((unsigned int) (val) & (unsigned int) 0x0000ff00U) << 8) | \
46 (((unsigned int) (val) & (unsigned int) 0x00ff0000U) >> 8) | \
47 (((unsigned int) (val) & (unsigned int) 0xff000000U) >> 24)))
48
49 static int
is_little_endian()50 is_little_endian ()
51 {
52 static int retval = -1;
53
54 if (retval == -1) {
55 int n = 1;
56 char *p = (char *)&n;
57 char x[] = {1, 0, 0, 0};
58
59 assert(sizeof(int) == 4);
60
61 if (memcmp(p, x, 4) == 0) {
62 retval = 1;
63 } else {
64 retval = 0;
65 }
66 }
67
68 return retval;
69 }
70
71 static int
convert_to_little_endian(int x)72 convert_to_little_endian (int x)
73 {
74 if (is_little_endian()) {
75 return x;
76 } else {
77 return SWAP_ENDIAN(x);
78 }
79 }
80
81 int
read_header(FILE * fp,Header * h)82 read_header (FILE *fp, Header *h)
83 {
84 int buf[3];
85
86 if (fread(buf, sizeof(int), 3, fp) == 0) {
87 return 0;
88 }
89
90 h->tv.tv_sec = convert_to_little_endian(buf[0]);
91 h->tv.tv_usec = convert_to_little_endian(buf[1]);
92 h->len = convert_to_little_endian(buf[2]);
93
94 return 1;
95 }
96
97 int
write_header(FILE * fp,Header * h)98 write_header (FILE *fp, Header *h)
99 {
100 int buf[3];
101
102 buf[0] = convert_to_little_endian(h->tv.tv_sec);
103 buf[1] = convert_to_little_endian(h->tv.tv_usec);
104 buf[2] = convert_to_little_endian(h->len);
105
106 if (fwrite(buf, sizeof(int), 3, fp) == 0) {
107 return 0;
108 }
109
110 return 1;
111 }
112
113 static char *progname = "";
114 void
set_progname(const char * name)115 set_progname (const char *name)
116 {
117 progname = strdup(name);
118 }
119
120 FILE *
efopen(const char * path,const char * mode)121 efopen (const char *path, const char *mode)
122 {
123 FILE *fp = fopen(path, mode);
124 if (fp == NULL) {
125 fprintf(stderr, "%s: %s: %s\n", progname, path, strerror(errno));
126 exit(EXIT_FAILURE);
127 }
128 return fp;
129 }
130
131 int
edup(int oldfd)132 edup (int oldfd)
133 {
134 int fd = dup(oldfd);
135 if (fd == -1) {
136 fprintf(stderr, "%s: dup failed: %s\n", progname, strerror(errno));
137 exit(EXIT_FAILURE);
138 }
139 return fd;
140 }
141
142 int
edup2(int oldfd,int newfd)143 edup2 (int oldfd, int newfd)
144 {
145 int fd = dup2(oldfd, newfd);
146 if (fd == -1) {
147 fprintf(stderr, "%s: dup2 failed: %s\n", progname, strerror(errno));
148 exit(EXIT_FAILURE);
149 }
150 return fd;
151 }
152
153 FILE *
efdopen(int fd,const char * mode)154 efdopen (int fd, const char *mode)
155 {
156 FILE *fp = fdopen(fd, mode);
157 if (fp == NULL) {
158 fprintf(stderr, "%s: fdopen failed: %s\n", progname, strerror(errno));
159 exit(EXIT_FAILURE);
160 }
161 }
162