comparison rvinterf/etmsync/fswrite.c @ 0:e7502631a0f9

initial import from freecalypso-sw rev 1033:5ab737ac3ad7
author Mychaela Falconia <falcon@freecalypso.org>
date Sat, 11 Jun 2016 00:13:35 +0000
parents
children 8136fb5eb292
comparison
equal deleted inserted replaced
-1:000000000000 0:e7502631a0f9
1 /*
2 * FFS write operation commands
3 */
4
5 #include <sys/types.h>
6 #include <ctype.h>
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <string.h>
10 #include <strings.h>
11 #include "etm.h"
12 #include "ffs.h"
13 #include "ffserr.h"
14 #include "tmffs2.h"
15 #include "limits.h"
16 #include "localtypes.h"
17 #include "localstruct.h"
18 #include "exitcodes.h"
19
20 extern u_char rvi_msg[];
21 extern int rvi_msg_len;
22
23 cmd_mkdir(argc, argv)
24 char **argv;
25 {
26 return do_mkdir_existok(argv[1]);
27 }
28
29 cmd_delete(argc, argv)
30 char **argv;
31 {
32 u_char cmdpkt[MAX_PKT_TO_TARGET], *dp;
33 int rc, slen;
34
35 slen = strlen(argv[1]);
36 if (slen >= TMFFS_STRING_SIZE) {
37 printf("error: pathname arg exceeds string length limit\n");
38 return(ERROR_USAGE);
39 }
40 dp = cmdpkt + 1;
41 *dp++ = ETM_FFS2;
42 *dp++ = TMFFS_REMOVE;
43 *dp++ = slen + 1;
44 strcpy(dp, argv[1]);
45 dp += slen + 1;
46 rc = etm_pkt_exch(cmdpkt, dp - cmdpkt - 1);
47 if (rc)
48 return(rc);
49 if (rvi_msg_len != 5) {
50 printf("error: TMFFS_REMOVE response has wrong length\n");
51 return(ERROR_TARGET);
52 }
53 if (rvi_msg[3]) {
54 report_ffs_err("ffs_remove", rvi_msg[3]);
55 return(ERROR_TARGET);
56 }
57 return(0);
58 }
59
60 hexdigit(c)
61 {
62 if (isdigit(c))
63 return(c - '0');
64 else if (isupper(c))
65 return(c - 'A' + 10);
66 else
67 return(c - 'a' + 10);
68 }
69
70 fwrite_hex_string(pathname, strarg)
71 char *pathname, *strarg;
72 {
73 u_char buf[256];
74 int maxlen, len;
75 char *cp;
76
77 maxlen = max_short_file_write(pathname);
78 for (cp = strarg, len = 0; ; cp += 2) {
79 while (isspace(*cp))
80 cp++;
81 if (!*cp)
82 break;
83 if (!isxdigit(cp[0]) || !isxdigit(cp[1])) {
84 fprintf(stderr, "error: invalid hex string argument\n");
85 return(ERROR_USAGE);
86 }
87 if (len >= maxlen) {
88 fprintf(stderr,
89 "error: hex string exceeds write packet limit\n");
90 return(ERROR_USAGE);
91 }
92 buf[len++] = hexdigit(cp[0]) << 4 | hexdigit(cp[1]);
93 }
94 return do_short_fwrite(pathname, buf, len);
95 }
96
97 fwrite_from_file(pathname, srcfile)
98 char *pathname, *srcfile;
99 {
100 u_char buf[240];
101 FILE *srcf;
102 int rc, cc, first, tfd;
103
104 srcf = fopen(srcfile, "r");
105 if (!srcf) {
106 perror(srcfile);
107 return(ERROR_UNIX);
108 }
109 for (first = 1; cc = fread(buf, 1, sizeof buf, srcf); first = 0) {
110 if (first) {
111 if (cc < sizeof buf &&
112 cc <= max_short_file_write(pathname)) {
113 fclose(srcf);
114 return do_short_fwrite(pathname, buf, cc);
115 }
116 rc = fd_open(pathname,
117 FFS_O_WRONLY | FFS_O_CREATE | FFS_O_TRUNC,
118 &tfd);
119 if (rc) {
120 fclose(srcf);
121 return(rc);
122 }
123 }
124 rc = fd_write(tfd, buf, cc);
125 if (rc) {
126 fclose(srcf);
127 fd_close(tfd);
128 return(rc);
129 }
130 }
131 fclose(srcf);
132 if (first) {
133 /* 0 length file: do an open-for-write to create it */
134 rc = fd_open(pathname,
135 FFS_O_WRONLY | FFS_O_CREATE | FFS_O_TRUNC,
136 &tfd);
137 if (rc)
138 return(rc);
139 }
140 return fd_close(tfd);
141 }
142
143 cmd_fwrite(argc, argv)
144 char **argv;
145 {
146 if (strlen(argv[1]) >= TMFFS_STRING_SIZE) {
147 fprintf(stderr,
148 "error: pathname arg exceeds string length limit\n");
149 return(ERROR_USAGE);
150 }
151 if (!strcmp(argv[2], "ascii"))
152 return do_short_fwrite(argv[1], argv[3], strlen(argv[3]));
153 else if (!strcmp(argv[2], "hex"))
154 return fwrite_hex_string(argv[1], argv[3]);
155 else if (!strcmp(argv[2], "file"))
156 return fwrite_from_file(argv[1], argv[3]);
157 else {
158 fprintf(stderr,
159 "error: middle argument to fwrite cmd must be \"ascii\", \"hex\" or \"file\"\n"
160 );
161 return(ERROR_USAGE);
162 }
163 }