comparison rvinterf/etmsync/fileio.c @ 284:7b4d4e3e610a

fc-fsio: functions written for file descriptor open/read/close
author Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
date Fri, 28 Feb 2014 05:50:01 +0000
parents
children bb28ba9e82c5
comparison
equal deleted inserted replaced
283:88eed3327682 284:7b4d4e3e610a
1 /*
2 * FFS2 file descriptor I/O operations
3 */
4
5 #include <sys/types.h>
6 #include <stdio.h>
7 #include <stdlib.h>
8 #include <string.h>
9 #include <strings.h>
10 #include "etm.h"
11 #include "ffs.h"
12 #include "tmffs2.h"
13 #include "limits.h"
14 #include "localtypes.h"
15 #include "localstruct.h"
16 #include "exitcodes.h"
17
18 extern u_char rvi_msg[];
19 extern int rvi_msg_len;
20
21 fd_open(pathname, flags, fdrtn)
22 char *pathname;
23 int flags, *fdrtn;
24 {
25 u_char cmdpkt[MAX_PKT_TO_TARGET], *dp;
26 int rc, slen;
27
28 slen = strlen(pathname);
29 if (slen >= TMFFS_STRING_SIZE) {
30 printf("error: pathname arg exceeds string length limit\n");
31 return(ERROR_USAGE);
32 }
33 dp = cmdpkt + 1;
34 *dp++ = ETM_FFS2;
35 *dp++ = TMFFS_OPEN;
36 *dp++ = slen + 1;
37 strcpy(dp, pathname);
38 dp += slen + 1;
39 *dp++ = flags;
40 rc = etm_pkt_exch(cmdpkt, dp - cmdpkt - 1);
41 if (rc)
42 return(rc);
43 if (rvi_msg[3]) {
44 printf("open fd: FFS error %d\n", rvi_msg[3]);
45 return(ERROR_TARGET);
46 }
47 if (rvi_msg_len != 6) {
48 printf("error: open fd response has wrong length\n");
49 return(ERROR_TARGET);
50 }
51 *fdrtn = rvi_msg[4];
52 return(0);
53 }
54
55 fd_read(fd, databuf, rdsize, rdret)
56 u_char *databuf;
57 int fd, rdsize, *rdret;
58 {
59 u_char cmdpkt[6];
60 int rc, sz;
61
62 if (rdsize > MAX_READ_DATA) {
63 fprintf(stderr,
64 "BUG: attempt to read more than possible per TMFFS2 protocol\n");
65 exit(ERROR_BUG);
66 }
67 cmdpkt[1] = ETM_FFS2;
68 cmdpkt[2] = TMFFS_READ;
69 cmdpkt[3] = fd;
70 cmdpkt[4] = rdsize;
71 rc = etm_pkt_exch(cmdpkt, 4);
72 if (rc)
73 return(rc);
74 if (rvi_msg[3]) {
75 printf("read fd: FFS error %d\n", rvi_msg[3]);
76 return(ERROR_TARGET);
77 }
78 if (rvi_msg_len < 6) {
79 *rdret = 0;
80 return(0);
81 }
82 sz = rvi_msg[4];
83 if (rvi_msg_len != sz + 6 || sz > rdsize) {
84 printf("error: read fd response has wrong length\n");
85 return(ERROR_TARGET);
86 }
87 bcopy(rvi_msg + 5, databuf, sz);
88 *rdret = sz;
89 return(0);
90 }
91
92 fd_close(fd)
93 {
94 u_char cmdpkt[5];
95 int rc;
96
97 cmdpkt[1] = ETM_FFS2;
98 cmdpkt[2] = TMFFS_CLOSE;
99 cmdpkt[3] = fd;
100 rc = etm_pkt_exch(cmdpkt, 3);
101 if (rc)
102 return(rc);
103 if (rvi_msg[3]) {
104 printf("close fd: FFS error %d\n", rvi_msg[3]);
105 return(ERROR_TARGET);
106 }
107 if (rvi_msg_len != 5) {
108 printf("error: close fd response has wrong length\n");
109 return(ERROR_TARGET);
110 }
111 return(0);
112 }