+typedef struct {
+ char *p_end; /* pointer to NULL at end of path */
+ char p_path[PATH_MAX + 1]; /* pointer to the start of a path */
+} PATH_T;
+
+
+static char *path_skip_heading(char *p)
+{
+ if (isdigit(p[0]) && p[1] == ':') {
+ p += 2;
+ } else {
+ char *q = p;
+ if (*q++ == '.') {
+ if (*q == '.')
+ ++q;
+ if (*q == '\0' || *q == '/')
+ p = q;
+ }
+ return p;
+ }
+ if (*p == '/')
+ ++p;
+
+ return p;
+}
+
+static void strip_trailing_slash(PATH_T *p)
+{
+ char *beg = path_skip_heading(p->p_path);
+ char *end = p->p_end;
+
+ while (end > beg && end[-1] == '/')
+ *--end = '\0';
+
+ p->p_end =end;
+}
+
+/*
+ * Move specified string into path. Convert "" to "." to handle BSD
+ * semantics for a null path. Strip trailing slashes.
+ */
+static PATH_T *path_setup(char *string)
+{
+ if (strlen(string) > PATH_MAX) {
+ cmd_error(0, 0, PSTR("'%s': Name too long"), string);
+ return NULL;
+ }
+
+ PATH_T *p = (PATH_T *) malloc(sizeof *p);
+ if (p == NULL) {
+ cmd_error(0, ENOMEM, NULL);
+ return NULL;
+ }
+
+ strcpy(p->p_path, string);
+ size_t len = strlen(string);
+ if (len > 1 && p->p_path[1] == ':' && p->p_path[2] != '/') {
+ if (len < PATH_MAX) {
+ memmove(p->p_path+3, p->p_path+2, len-1);
+ p->p_path[2] = '/';
+ len += 1;
+ } else {
+ cmd_error(0, ENOMEM, NULL);
+ return NULL;
+ }
+ }
+
+ p->p_end = p->p_path + len;
+ if (p->p_path == p->p_end) {
+ *p->p_end++ = '.';
+ *p->p_end = '\0';
+ }
+
+ strip_trailing_slash(p);
+ return p;
+}
+