aboutsummaryrefslogtreecommitdiffstats
path: root/src/path.c
blob: adf824bbb4fb2e3a6909ba65f941e011984bfebc (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
/* Paths as people write them.
 *
 * `~` is a shell thing: by the time a path reaches us from a config or layout
 * file no shell has touched it, so `cwd="~/dev/api"` was being handed to
 * chdir() literally, failing, and leaving the pane in whatever directory the
 * session happened to be in. Silently \u2014 which is the worst way for a path to
 * be wrong, because the pane still starts and still works.
 */
#include "sl0ppty.h"

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

const char *path_expand(const char *path, char *buf, size_t cap) {
  if (!path) return NULL;
  /* `~` and `~/...` only. `~user` needs the password database and is not
   * worth linking it in for; it is returned unchanged, which is exactly what
   * it did before, rather than being half-expanded into something wrong. */
  if (path[0] != '~' || (path[1] && path[1] != '/')) return path;
  const char *home = getenv("HOME");
  if (!home || !*home) return path;
  snprintf(buf, cap, "%s%s", home, path + 1);
  return buf;
}