aboutsummaryrefslogtreecommitdiffstats
path: root/src/mds-kbdc/paths.c
blob: 62a83084213dafeaa4f09810351fcc3582e98446 (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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
/**
 * mds — A micro-display server
 * Copyright © 2014, 2015, 2016  Mattias Andrée (maandree@member.fsf.org)
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
#include "paths.h"

#include <libmdsserver/macros.h>

#include <stddef.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>



/**
 * Get the current working directory
 * 
 * @return  The current working directory
 */
char* curpath(void)
{
  static size_t cwd_size = 4096;
  char* cwd = NULL;
  char* old = NULL;
  int saved_errno;
  
  /* glibc offers ways to do this in just one function call,
   * but we will not assume that glibc is used here. */
  for (;;)
    {
      fail_if (xxrealloc(old, cwd, cwd_size + 1, char));
      if (getcwd(cwd, cwd_size))
	break;
      else
	fail_if (errno != ERANGE);
      cwd_size <<= 1;
    }
  
  return cwd;
 fail:
  saved_errno = errno;
  free(old);
  free(cwd);
  errno = saved_errno;
  return NULL;
}


/**
 * Get the absolute path of a file
 * 
 * @param   path  The filename of the file
 * @return        The file's absolute path, `NULL` on error
 */
char* abspath(const char* path)
{
  char* cwd = NULL;
  char* buf = NULL;
  int saved_errno, slash = 1;
  size_t size, p;
  
  if (*path == '/')
    {
      fail_if (xstrdup(buf, path));
      return buf;
    }
  
  fail_if (cwd = curpath(), cwd == NULL);
  size = (p = strlen(cwd)) + strlen(path) + 2;
  fail_if (xmalloc(buf, size + 1, char));
  memcpy(buf, cwd, (p + 1) * sizeof(char));
  if (buf[p - 1] != '/')
    buf[p++] = '/';
  
  while (*path)
    if (slash && (path[0] == '/'))
      path += 1;
    else if (slash && (path[0] == '.') && (path[1] == '/'))
      path += 2;
    else if (slash && (path[0] == '.') && (path[1] == '.') && (path[2] == '/'))
      {
	path += 3;
	p--;
	while (p && (buf[--p] != '/'));
	p++;
      }
    else
      {
	buf[p++] = *path;
	slash = (*path++ == '/');
      }
  
  buf[p] = '\0';
  
  free(cwd);
  return buf;
 fail:
  saved_errno = errno;
  free(cwd);
  errno = saved_errno;
  return NULL;
}


/**
 * Get a relative path of a file
 * 
 * @param   path  The filename of the file
 * @param   base  The pathname of the base directory,
 *                `NULL` for the current working directroy
 * @return        The file's relative path, `NULL` on error
 */
char* relpath(const char* path, const char* base)
{
  char* abs = abspath(path);
  char* absbase = NULL;
  char* buf = NULL;
  char* old = NULL;
  int saved_errno;
  size_t p, slash = 1, back = 0;
  
  fail_if (abs == NULL);
  absbase = base ? abspath(base) : curpath();
  fail_if (absbase == NULL);
  
  if (absbase[strlen(absbase) - 1] != '/')
    /* Both `abspath` and `curpath` (and `relpath`) allocates one extra slot. */
    absbase[strlen(absbase) + 1] = '\0', absbase[strlen(absbase)] = '/';
  
  for (p = 1; abs[p] && absbase[p] && (abs[p] == absbase[p]); p++)
    if (abs[p] == '/')
      slash = p + 1;
  
  for (p = slash; absbase[p]; p++)
    if (absbase[p] == '/')
      back++;
  
  fail_if (xmalloc(buf, back * 3 + strlen(abs + slash) + 2, char));
  
  for (p = 0; back--;)
    buf[p++] = '.', buf[p++] = '.', buf[p++] = '/';
  memcpy(buf + p, abs + slash, (strlen(abs + slash) + 1) * sizeof(char));
  
  free(abs);
  free(absbase);
  return buf;
 fail:
  saved_errno = errno;
  free(abs);
  free(absbase);
  free(old);
  errno = saved_errno;
  return NULL;
}