static int sandbox_cmdline_cb_test_fdt(struct sandbox_state *state,
const char *arg)
{
- const char *fmt = "/arch/sandbox/dts/test.dtb";
- char *p;
+ char buf[256];
char *fname;
int len;
- len = strlen(state->argv[0]) + strlen(fmt) + 1;
+ len = state_get_rel_filename("arch/sandbox/dts/test.dtb", buf,
+ sizeof(buf));
+ if (len < 0)
+ return len;
+
fname = os_malloc(len);
if (!fname)
return -ENOMEM;
- strcpy(fname, state->argv[0]);
- p = strrchr(fname, '/');
- if (!p)
- p = fname + strlen(fname);
- len -= p - fname;
- snprintf(p, len, fmt);
+ strcpy(fname, buf);
state->fdt_fname = fname;
return 0;
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;
*/
void state_show(struct sandbox_state *state);
+/**
+ * state_get_rel_filename() - Get a filename relative to the executable
+ *
+ * This uses argv[0] to obtain a filename path
+ *
+ * @rel_path: Relative path to build, e.g. "arch/sandbox/dts/test.dtb". Must not
+ * have a trailing /
+ * @buf: Buffer to use to return the filename
+ * @size: Size of buffer
+ * @return length of filename (including terminator), -ENOSPC if @size is too
+ * small
+ */
+int state_get_rel_filename(const char *rel_path, char *buf, int size);
+
/**
* Initialize the test system state
*/