1 /* rsraw.c
2 * UNIX 4.3 BSD on SunOS 4.x
3 * Modifyed by $Id: rsraw.c,v 1.1 1995/02/07 06:59:45 tani Exp tani $
4 */
5
6 char rsraw_writers[] = "Mai";
7 char rsraw_id_string[] = "For Any POSIX";
8
9 #include <stdio.h>
10
11 #include <fcntl.h>
12 #include <termios.h>
13 #include <time.h>
14 #ifdef HAVE_UTIME_H
15 #include <utime.h>
16 #endif
17 #include <sys/types.h>
18 #include <sys/stat.h>
19
20
21 /*--------------------------------------------------------------------*/
second(void)22 int second(void)
23 {
24 long t;
25
26 t = time(NULL);
27 return (int) (t % 60);
28 }
29
30 /*--------------------------------------------------------------------*/
31 /* returns hour, minute, second
32 */
hms(char * result)33 void hms(char *result)
34 {
35 time_t t;
36 struct tm *now;
37
38 t = time(NULL);
39 now = localtime(&t);
40 *result++ = (char) now->tm_hour;
41 *result++ = (char) now->tm_min;
42 *result = (char) now->tm_sec;
43 }
44
45 /*====================================================================*/
46 static struct termios dev_io_status;
47 static struct termios dev_io_modify;
48 static int dev_io_fd;
49
50 static struct termios tty_in_status;
51 static struct termios tty_in_modify;
52 static int tty_in_fd;
53
54 /*--------------------------------------------------------------------*/
com_send(unsigned char ch)55 int com_send(unsigned char ch)
56 {
57 #if 0
58 printf("[S%c(%02x)]", (ch >= ' ' && ch < 0x7f) ? ch : '?', ch);
59 fflush(stdout);
60 #endif
61 return write(dev_io_fd, (char *) &ch, 1);
62 }
63
64 /*--------------------------------------------------------------------*/
65 #define MY_RS_BUFFER_SIZE 4096
66 #define RS_READ_SIZE 256
67 static char rs_buffer[MY_RS_BUFFER_SIZE];
68 static char *rs_readp;
69 static char *rs_writep;
70 static char *rs_limitp;
71
com_length(void)72 int com_length(void)
73 {
74 int size;
75
76 size = read(dev_io_fd, rs_writep, RS_READ_SIZE);
77
78 if (size > 0) {
79 rs_writep += size;
80 if (rs_writep >= rs_buffer + MY_RS_BUFFER_SIZE - RS_READ_SIZE) {
81 rs_limitp = rs_writep;
82 rs_writep = rs_buffer;
83 }
84 }
85
86 if (rs_readp <= rs_writep) {
87 size = rs_writep - rs_readp;
88 } else {
89 size = (rs_limitp - rs_readp) + (rs_writep - rs_buffer);
90 }
91
92 return size;
93 }
94
95 /*--------------------------------------------------------------------*/
com_recv()96 int com_recv()
97 {
98 int return_value;
99
100 (void) com_length();
101
102 if (rs_readp != rs_writep) {
103 return_value = (int) (*rs_readp++) & 0xff;
104 if (rs_readp == rs_limitp)
105 rs_readp = rs_buffer;
106 } else {
107 return_value = -1;
108 }
109
110 #if 0
111 printf("<%c:%02x>", (return_value >= ' ' && return_value < 0x7f) ? return_value : '?', return_value);
112 fflush(stdout);
113 #endif
114 return return_value;
115 }
116
117 /*--------------------------------------------------------------------*/
118 /* communication port, tty �̐ݒ�
119 */
120 static char *devicename[] = {
121 "/dev/ttya",
122 "/dev/ttyb",
123 "/dev/cua",
124 "/dev/cub"
125 };
126
com_setup(char * buf,unsigned int dummy)127 int com_setup(char *buf, unsigned int dummy)
128 {
129 rs_readp = rs_writep = rs_limitp = rs_buffer;
130
131 if (buf[0] < 0 || buf[0] > 3)
132 return -1;
133 #if 0
134 dev_io_fd = open(devicename[buf[0]], O_RDWR|O_NDELAY, 0777);
135 #else
136 dev_io_fd = fileno(stdout);
137 #endif
138 if (dev_io_fd < 0) {
139 return dev_io_fd;
140 }
141 tcgetattr(dev_io_fd, &dev_io_status);
142 dev_io_modify = dev_io_status;
143
144 /* c_iflag �̃Z�b�g
145 * 8bit through (~ISTRIP)
146 * NL-CR �̕ϊ��Ȃ��̏�� (~INLCR, ~ICRNL)
147 */
148 dev_io_modify.c_iflag = IGNPAR;
149
150 /* c_oflag �̃Z�b�g
151 * NL-CR �̕ϊ��Ȃ��̏�� (~ONLCR, ~OCRNL)
152 */
153 dev_io_modify.c_oflag = 0;
154
155 /* c_cflag �̃Z�b�g
156 * �p���e�B�Ȃ� (~PARENB)
157 * bit ���A���x����U�N���A���čĐݒ肷��
158 */
159 dev_io_modify.c_cflag = ((int) buf[3])|CS8|CREAD;
160
161 /* c_lflag �̃Z�b�g
162 * local echo �Ȃ� (~ECHO)
163 * raw mode (~ICANON)
164 */
165 dev_io_modify.c_lflag = 0;
166
167 /* c_cc �̃Z�b�g
168 * wait �Ȃ��̏�Ԃɂ���
169 */
170 dev_io_modify.c_cc[VMIN] = 0;
171 dev_io_modify.c_cc[VTIME] = 0;
172 tcsetattr(dev_io_fd, TCSANOW, &dev_io_modify);
173
174 /* �L�[���͂̂��߂̐ݒ� */
175 tty_in_fd = open("/dev/tty", O_RDONLY|O_NDELAY, 0777);
176 if (tty_in_fd < 0) {
177 close(dev_io_fd);
178 return tty_in_fd;
179 }
180 tcgetattr(tty_in_fd, &tty_in_status);
181 tty_in_modify = tty_in_status;
182 tty_in_modify.c_lflag &= ~(ECHO|ICANON);
183 tty_in_modify.c_cc[VMIN] = 0;
184 tty_in_modify.c_cc[VTIME] = 0;
185 tcsetattr(tty_in_fd, TCSANOW, &tty_in_modify);
186
187 return 0;
188 }
189
190 /*--------------------------------------------------------------------*/
191 /* RS-232C �� RTS �� off �ɂ��� */
com_rts_off(void)192 void com_rts_off(void)
193 {
194 }
195
196 /*--------------------------------------------------------------------*/
197 /* RS-232C �� RTS �� on �ɂ��� */
com_rts_on(void)198 void com_rts_on(void)
199 {
200 }
201
202 /*--------------------------------------------------------------------*/
com_close(void)203 void com_close(void)
204 {
205 tcsetattr(dev_io_fd, TCSANOW, &dev_io_status);
206 tcsetattr(tty_in_fd, TCSANOW, &tty_in_status);
207 (void) close(tty_in_fd);
208 (void) close(dev_io_fd);
209 }
210
211 /*--------------------------------------------------------------------*/
kbd_check(void)212 int kbd_check(void)
213 {
214 char c;
215 int i;
216
217 i = read(tty_in_fd, &c, 1);
218 if (i > 0)
219 i = (int) c & 0xff;
220 else
221 i = -1;
222 return i;
223 }
224
225 /*====================================================================*/
get_file_information(char * filename,long * info)226 int get_file_information(char *filename, long *info)
227 {
228 struct stat buf;
229 int sts;
230
231 if (stat(filename, &buf) == 0) {
232 struct tm *lt;
233
234 info[0] = buf.st_size;
235 info[1] = (long) -540;
236 lt = localtime((time_t*) &(buf.st_mtime));
237
238 info[2] = info[4] =
239 (long) lt->tm_year * 10000 + (lt->tm_mon + 1) * 100 + lt->tm_mday;
240 info[3] = info[5] =
241 (long) lt->tm_hour * 3600 + lt->tm_min * 60 + lt->tm_sec;
242 sts = 0;
243 } else {
244 sts = -1;
245 }
246 return sts;
247 }
248
249 /*--------------------------------------------------------------------*/
dt_to_tm(long date,long time)250 static time_t dt_to_tm(long date, long time)
251 {
252 struct tm t;
253
254 t.tm_sec = (int) (time % 60);
255 t.tm_min = (int) ((time / 60) % 60);
256 t.tm_hour = (int) (time / 3600);
257 t.tm_mday = date % 100;
258 t.tm_mon = (date / 100) % 100 - 1;
259 t.tm_year = (date / 10000);
260 #ifdef USE_MKTIME
261 return mktime(&t);
262 #else
263 return timelocal(&t);
264 #endif
265 }
266
267 /*--------------------------------------------------------------------*/
set_file_time(char * file,long * info)268 int set_file_time(char *file, long *info)
269 {
270 #ifdef HAVE_UTIME_H
271 struct utimbuf timep;
272
273 timep.actime = dt_to_tm(info[2], info[3]);
274 timep.modtime = dt_to_tm(info[4], info[5]);
275 return utime(file, &timep);
276 #else
277 time_t timep[2];
278
279 timep[0] = dt_to_tm(info[2], info[3]);
280 timep[1] = dt_to_tm(info[4], info[5]);
281 return utime(file, timep);
282 #endif
283 }
284
285 /* ---- end of rsraw.c ---- */
286