Fix typecast format warnings when printing the 9p debug information
[spfs.git] / libspfs / fmt.c
blobe4f9b23efb91b3809982d0578719e0a8eec9fb96
1 /*
2 * Copyright (C) 2006 by Latchesar Ionkov <lucho@ionkov.net>
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * LATCHESAR IONKOV AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
19 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
20 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
23 #include <stdlib.h>
24 #include <stdio.h>
25 #include <string.h>
26 #include <errno.h>
27 #include "spfs.h"
28 #include "spfsimpl.h"
30 static int
31 sp_printperm(FILE *f, int perm)
33 int n;
34 char b[10];
36 n = 0;
37 if (perm & Dmdir)
38 b[n++] = 'd';
39 if (perm & Dmappend)
40 b[n++] = 'a';
41 if (perm & Dmauth)
42 b[n++] = 'A';
43 if (perm & Dmexcl)
44 b[n++] = 'l';
45 if (perm & Dmtmp)
46 b[n++] = 't';
47 if (perm & Dmdevice)
48 b[n++] = 'D';
49 if (perm & Dmsocket)
50 b[n++] = 'S';
51 if (perm & Dmnamedpipe)
52 b[n++] = 'P';
53 if (perm & Dmsymlink)
54 b[n++] = 'L';
55 b[n] = '\0';
57 return fprintf(f, "%s%03o", b, perm&0777);
60 static int
61 sp_printqid(FILE *f, Spqid *q)
63 int n;
64 char buf[10];
66 n = 0;
67 if (q->type & Qtdir)
68 buf[n++] = 'd';
69 if (q->type & Qtappend)
70 buf[n++] = 'a';
71 if (q->type & Qtauth)
72 buf[n++] = 'A';
73 if (q->type & Qtexcl)
74 buf[n++] = 'l';
75 if (q->type & Qttmp)
76 buf[n++] = 't';
77 if (q->type & Qtsymlink)
78 buf[n++] = 'L';
79 buf[n] = '\0';
81 return fprintf(f, " (%.16llx %x '%s')", (unsigned long long)q->path, q->version, buf);
84 int
85 sp_printstat(FILE *f, Spstat *st, int dotu)
87 int n;
89 n = fprintf(f, "'%.*s' '%.*s' '%.*s' '%.*s' q ",
90 st->name.len, st->name.str, st->uid.len, st->uid.str,
91 st->gid.len, st->gid.str, st->muid.len, st->muid.str);
93 n += sp_printqid(f, &st->qid);
94 n += fprintf(f, " m ");
95 n += sp_printperm(f, st->mode);
96 n += fprintf(f, " at %d mt %d l %llu t %d d %d",
97 st->atime, st->mtime, (unsigned long long)st->length, st->type, st->dev);
98 if (dotu)
99 n += fprintf(f, " ext '%.*s'", st->extension.len,
100 st->extension.str);
102 return n;
106 sp_dump(FILE *f, u8 *data, int datalen)
108 int i, n;
110 i = n = 0;
111 while (i < datalen) {
112 n += fprintf(f, "%02x", data[i]);
113 if (i%4 == 3)
114 n += fprintf(f, " ");
115 if (i%32 == 31)
116 n += fprintf(f, "\n");
118 i++;
120 n += fprintf(f, "\n");
122 return n;
125 static int
126 sp_printdata(FILE *f, u8 *buf, int buflen)
128 return sp_dump(f, buf, buflen<64?buflen:64);
132 sp_dumpdata(u8 *buf, int buflen)
134 return sp_dump(stderr, buf, buflen);
138 sp_printfcall(FILE *f, Spfcall *fc, int dotu)
140 int i, ret, type, fid, tag;
142 if (!fc)
143 return fprintf(f, "NULL");
145 type = fc->type;
146 fid = fc->fid;
147 tag = fc->tag;
149 ret = 0;
150 switch (type) {
151 case Tversion:
152 ret += fprintf(f, "Tversion tag %u msize %u version '%.*s'",
153 tag, fc->msize, fc->version.len, fc->version.str);
154 break;
156 case Rversion:
157 ret += fprintf(f, "Rversion tag %u msize %u version '%.*s'",
158 tag, fc->msize, fc->version.len, fc->version.str);
159 break;
161 case Tauth:
162 ret += fprintf(f, "Tauth tag %u afid %d uname %.*s aname %.*s",
163 tag, fc->afid, fc->uname.len, fc->uname.str,
164 fc->aname.len, fc->aname.str);
165 if (dotu)
166 ret += fprintf(f, " nuname %d", fc->n_uname);
167 break;
169 case Rauth:
170 ret += fprintf(f, "Rauth tag %u qid ", tag);
171 sp_printqid(f, &fc->qid);
172 break;
174 case Tattach:
175 ret += fprintf(f, "Tattach tag %u fid %d afid %d uname %.*s aname %.*s",
176 tag, fid, fc->afid, fc->uname.len, fc->uname.str,
177 fc->aname.len, fc->aname.str);
178 if (dotu)
179 ret += fprintf(f, " nuname %d", fc->n_uname);
180 break;
182 case Rattach:
183 ret += fprintf(f, "Rattach tag %u qid ", tag);
184 sp_printqid(f, &fc->qid);
185 break;
187 case Rerror:
188 ret += fprintf(f, "Rerror tag %u ename %.*s", tag,
189 fc->ename.len, fc->ename.str);
190 if (dotu)
191 ret += fprintf(f, " ecode %d", fc->ecode);
192 break;
194 case Tflush:
195 ret += fprintf(f, "Tflush tag %u oldtag %u", tag, fc->oldtag);
196 break;
198 case Rflush:
199 ret += fprintf(f, "Rflush tag %u", tag);
200 break;
202 case Twalk:
203 ret += fprintf(f, "Twalk tag %u fid %d newfid %d nwname %d",
204 tag, fid, fc->newfid, fc->nwname);
205 for(i = 0; i < fc->nwname; i++)
206 ret += fprintf(f, " '%.*s'", fc->wnames[i].len,
207 fc->wnames[i].str);
208 break;
210 case Rwalk:
211 ret += fprintf(f, "Rwalk tag %u nwqid %d", tag, fc->nwqid);
212 for(i = 0; i < fc->nwqid; i++)
213 ret += sp_printqid(f, &fc->wqids[i]);
214 break;
216 case Topen:
217 ret += fprintf(f, "Topen tag %u fid %d mode %d", tag, fid,
218 fc->mode);
219 break;
221 case Ropen:
222 ret += fprintf(f, "Ropen tag %u", tag);
223 ret += sp_printqid(f, &fc->qid);
224 ret += fprintf(f, " iounit %d", fc->iounit);
225 break;
227 case Tcreate:
228 ret += fprintf(f, "Tcreate tag %u fid %d name %.*s perm ",
229 tag, fid, fc->name.len, fc->name.str);
230 ret += sp_printperm(f, fc->perm);
231 ret += fprintf(f, " mode %d", fc->mode);
232 if (dotu)
233 ret += fprintf(f, " ext %.*s", fc->extension.len,
234 fc->extension.str);
235 break;
237 case Rcreate:
238 ret += fprintf(f, "Rcreate tag %u", tag);
239 ret += sp_printqid(f, &fc->qid);
240 ret += fprintf(f, " iounit %d", fc->iounit);
241 break;
243 case Tread:
244 ret += fprintf(f, "Tread tag %u fid %d offset %llu count %u",
245 tag, fid, (unsigned long long)fc->offset, fc->count);
246 break;
248 case Rread:
249 ret += fprintf(f, "Rread tag %u count %u data ", tag, fc->count);
250 ret += sp_printdata(f, fc->data, fc->count);
251 break;
253 case Twrite:
254 ret += fprintf(f, "Twrite tag %u fid %d offset %llu count %u data ",
255 tag, fid, (unsigned long long)fc->offset, fc->count);
256 ret += sp_printdata(f, fc->data, fc->count);
257 break;
259 case Rwrite:
260 ret += fprintf(f, "Rwrite tag %u count %u", tag, fc->count);
261 break;
263 case Tclunk:
264 ret += fprintf(f, "Tclunk tag %u fid %d", tag, fid);
265 break;
267 case Rclunk:
268 ret += fprintf(f, "Rclunk tag %u", tag);
269 break;
271 case Tremove:
272 ret += fprintf(f, "Tremove tag %u fid %d", tag, fid);
273 break;
275 case Rremove:
276 ret += fprintf(f, "Rremove tag %u", tag);
277 break;
279 case Tstat:
280 ret += fprintf(f, "Tstat tag %u fid %d", tag, fid);
281 break;
283 case Rstat:
284 ret += fprintf(f, "Rstat tag %u ", tag);
285 ret += sp_printstat(f, &fc->stat, dotu);
286 break;
288 case Twstat:
289 ret += fprintf(f, "Twstat tag %u fid %d ", tag, fid);
290 ret += sp_printstat(f, &fc->stat, dotu);
291 break;
293 case Rwstat:
294 ret += fprintf(f, "Rwstat tag %u", tag);
295 break;
297 default:
298 ret += fprintf(f, "unknown type %d", type);
299 break;
302 return ret;