sandbox: Add a function to load a relative file path

At present this implementation is specific to loading the test FDT. We
plan to load others, so create a generic function to handle this.

The path is now limited to 256 characters, to simplify the code.

When there is an empty argv[0] (which should not happen), the function now
just uses the path as is, with no prefix.

Signed-off-by: Simon Glass <sjg@chromium.org>
diff --git a/arch/sandbox/cpu/state.c b/arch/sandbox/cpu/state.c
index e0d0112..787e021 100644
--- a/arch/sandbox/cpu/state.c
+++ b/arch/sandbox/cpu/state.c
@@ -396,6 +396,28 @@
 	return old_val;
 }
 
+int state_get_rel_filename(const char *rel_path, char *buf, int size)
+{
+	struct sandbox_state *state = state_get_current();
+	int rel_len, prog_len;
+	char *p;
+	int len;
+
+	rel_len = strlen(rel_path);
+	p = strrchr(state->argv[0], '/');
+	prog_len = p ? p - state->argv[0] : 0;
+
+	/* allow space for a / and a terminator */
+	len = prog_len + 1 + rel_len + 1;
+	if (len > size)
+		return -ENOSPC;
+	strncpy(buf, state->argv[0], prog_len);
+	buf[prog_len] = '/';
+	strcpy(buf + prog_len + 1, rel_path);
+
+	return len;
+}
+
 int state_init(void)
 {
 	state = &main_state;