xref: /minix/minix/lib/libvboxfs/path.c (revision 83133719)
1 /* Part of libvboxfs - (c) 2012, D.C. van Moolenbroek */
2 
3 #include "inc.h"
4 
5 /*
6  * Store a local path name in the given path object, performing any necessary
7  * conversions.  The path object is expected to be used read-only, so the size
8  * of the path object is set as small as possible.  If 'name' is NULL, the path
9  * will be initialized to the empty string.
10  */
11 int
12 vboxfs_set_path(vboxfs_path_t *path, char *name)
13 {
14 	size_t len;
15 
16 	len = strlen(name);
17 
18 	/* FIXME: missing UTF-8 conversion */
19 
20 	if (len >= sizeof(path->data))
21 		return ENAMETOOLONG;
22 
23 	strcpy(path->data, name);
24 
25 	path->len = len;
26 	path->size = len + 1;
27 
28 	return OK;
29 }
30 
31 /*
32  * Retrieve the path name from the given path object.  Make sure the name fits
33  * in the given name buffer first.  The given size must include room for a
34  * terminating null character.
35  */
36 int
37 vboxfs_get_path(vboxfs_path_t *path, char *name, size_t size)
38 {
39 
40 	/* FIXME: missing UTF-8 conversion */
41 
42 	if (path->len >= size)
43 		return ENAMETOOLONG;
44 
45 	assert(path->data[path->len] == 0);
46 
47 	strcpy(name, path->data);
48 
49 	return OK;
50 }
51 
52 /*
53  * Return the byte size of a previously initialized path object.
54  */
55 size_t
56 vboxfs_get_path_size(vboxfs_path_t *path)
57 {
58 
59 	return offsetof(vboxfs_path_t, data) + path->size;
60 }
61