Krolyxon
Commit 9a7e97c

Branches

Refs →

nsxiv9a7e97c

Use XSI realpath(3)

Author
Bert Münnich
Date
2015-10-28 21:52 +0100
Parent
66c3c55
Commit
9a7e97cd89b307ecc0cd2f304919b576cb5704d1

+0 −85

3 files changed

  1. M main.c +0 −15
  2. M util.c +0 −68
  3. M util.h +0 −2
M main.c View
@@ -131,27 +131,12 @@ void check_add_file(char *filename, bool given)
131131 memset(&files[filecnt/2], 0, filecnt/2 * sizeof(*files));
132132 }
133133
134−#if defined _BSD_SOURCE || defined _XOPEN_SOURCE && \
135− ((_XOPEN_SOURCE - 0) >= 500 || defined _XOPEN_SOURCE_EXTENDED)
136−
137134 if ((files[fileidx].path = realpath(filename, NULL)) == NULL) {
138135 warn("could not get real path of file: %s\n", filename);
139136 return;
140137 }
141−#else
142− if (*filename != '/') {
143− if ((files[fileidx].path = absolute_path(filename)) == NULL) {
144− warn("could not get absolute path of file: %s\n", filename);
145− return;
146− }
147− } else {
148− files[fileidx].path = NULL;
149− }
150−#endif
151138
152139 files[fileidx].name = s_strdup(filename);
153− if (files[fileidx].path == NULL)
154− files[fileidx].path = files[fileidx].name;
155140 if ((bn = strrchr(files[fileidx].name , '/')) != NULL && bn[1] != '\0')
156141 files[fileidx].base = ++bn;
157142 else
M util.c View
@@ -106,74 +106,6 @@ void size_readable(float *size, const char **unit)
106106 *unit = units[MIN(i, ARRLEN(units) - 1)];
107107 }
108108
109−char* absolute_path(const char *filename)
110−{
111− size_t len;
112− const char *basename;
113− char *dir, *dirname = NULL, *path = NULL, *s;
114− char *cwd = NULL, *twd = NULL;
115−
116− if (*filename == '\0' || *filename == '/')
117− return NULL;
118−
119− len = FNAME_LEN;
120− cwd = (char*) s_malloc(len);
121− while ((s = getcwd(cwd, len)) == NULL && errno == ERANGE) {
122− len *= 2;
123− cwd = (char*) s_realloc(cwd, len);
124− }
125− if (s == NULL)
126− goto error;
127−
128− s = strrchr(filename, '/');
129− if (s != NULL) {
130− len = s - filename;
131− dirname = (char*) s_malloc(len + 1);
132− strncpy(dirname, filename, len);
133− dirname[len] = '\0';
134− basename = s + 1;
135−
136− if (chdir(cwd) < 0)
137− /* we're not able to come back afterwards */
138− goto error;
139− if (chdir(dirname) < 0)
140− goto error;
141−
142− len = FNAME_LEN;
143− twd = (char*) s_malloc(len);
144− while ((s = getcwd(twd, len)) == NULL && errno == ERANGE) {
145− len *= 2;
146− twd = (char*) s_realloc(twd, len);
147− }
148− if (chdir(cwd) < 0)
149− die("could not revert to prior working directory");
150− if (s == NULL)
151− goto error;
152− dir = twd;
153− } else {
154− /* only a single filename given */
155− basename = filename;
156− dir = cwd;
157− }
158−
159− len = strlen(dir) + strlen(basename) + 2;
160− path = (char*) s_malloc(len);
161− snprintf(path, len, "%s/%s", dir, basename);
162−
163− goto end;
164−
165−error:
166− free(path);
167− path = NULL;
168−
169−end:
170− free(dirname);
171− free(cwd);
172− free(twd);
173−
174− return path;
175−}
176−
177109 int r_opendir(r_dir_t *rdir, const char *dirname)
178110 {
179111 if (*dirname == '\0')
M util.h View
@@ -70,8 +70,6 @@ void die(const char*, ...);
7070
7171 void size_readable(float*, const char**);
7272
73−char* absolute_path(const char*);
74−
7573 int r_opendir(r_dir_t*, const char*);
7674 int r_closedir(r_dir_t*);
7775 char* r_readdir(r_dir_t*);