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