some coverity fixes.
[minix.git] / lib / libhgfs / path.c
blobe10f817b782082fc7b66d8e6e9a48f4ade87ee54
1 /* Part of libhgfs - (c) 2009, D.C. van Moolenbroek */
3 #include "inc.h"
5 #include <limits.h>
7 /*===========================================================================*
8 * path_put *
9 *===========================================================================*/
10 void path_put(path)
11 char *path;
13 /* Append the given path name in HGFS format to the RPC buffer. Truncate it
14 * if it is longer than PATH_MAX bytes.
16 char *p, buf[PATH_MAX];
17 int len;
19 /* No leading slashes are allowed. */
20 for (p = path; *p == '/'; p++);
22 /* No double or tailing slashes, either. */
23 for (len = 0; *p && len < sizeof(buf) - 1; len++) {
24 if (*p == '/') {
25 for (p++; *p == '/'; p++);
27 if (!*p) break;
29 buf[len] = 0;
31 else buf[len] = *p++;
34 RPC_NEXT32 = len;
36 memcpy(RPC_PTR, buf, len);
37 RPC_ADVANCE(len);
39 RPC_NEXT8 = 0;
42 /*===========================================================================*
43 * path_get *
44 *===========================================================================*/
45 int path_get(path, max)
46 char *path;
47 int max;
49 /* Retrieve a HGFS formatted path name from the RPC buffer. Returns EINVAL if
50 * the path name is invalid. Returns ENAMETOOLONG if the path name is too
51 * long. Returns OK on success.
53 char *p, *q;
54 int n, len;
56 n = len = RPC_NEXT32;
58 if (len >= max) return ENAMETOOLONG;
60 for (p = path, q = RPC_PTR; n--; p++, q++) {
61 /* We can not deal with a slash in a path component. */
62 if (*q == '/') return EINVAL;
64 if (*q == 0) *p = '/';
65 else *p = *q;
68 RPC_ADVANCE(len);
70 *p = 0;
72 return (RPC_NEXT8 != 0) ? EINVAL : OK;