1a0e47ec4ed97b1601cbc3498126a760b5e02895
2 * Copyright (C) 2012 - David Goulet <dgoulet@efficios.com>
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License, version 2 only, as
6 * published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful, but WITHOUT
9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 * You should have received a copy of the GNU General Public License along with
14 * this program; if not, write to the Free Software Foundation, Inc., 51
15 * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
25 #include <common/common.h>
30 * Return the realpath(3) of the path even if the last directory token does not
31 * exist. For example, with /tmp/test1/test2, if test2/ does not exist but the
32 * /tmp/test1 does, the real path is returned. In normal time, realpath(3)
33 * fails if the end point directory does not exist.
35 __attribute__((visibility("hidden")))
36 char *utils_expand_path(const char *path
)
38 const char *end_path
= path
;
39 char *next
, *cut_path
= NULL
, *expanded_path
= NULL
;
46 /* Find last token delimited by '/' */
47 while ((next
= strpbrk(end_path
+ 1, "/"))) {
51 /* Cut last token from original path */
52 cut_path
= strndup(path
, end_path
- path
);
54 expanded_path
= zmalloc(PATH_MAX
);
55 if (expanded_path
== NULL
) {
56 PERROR("zmalloc expand path");
60 expanded_path
= realpath((char *)cut_path
, expanded_path
);
61 if (expanded_path
== NULL
) {
64 ERR("%s: No such file or directory", cut_path
);
67 PERROR("realpath utils expand path");
73 /* Add end part to expanded path */
74 strncat(expanded_path
, end_path
, PATH_MAX
- strlen(expanded_path
) - 1);
86 * Create a pipe in dst.
88 __attribute__((visibility("hidden")))
89 int utils_create_pipe(int *dst
)
99 PERROR("create pipe");
106 * Create pipe and set CLOEXEC flag to both fd.
108 * Make sure the pipe opened by this function are closed at some point. Use
109 * utils_close_pipe().
111 __attribute__((visibility("hidden")))
112 int utils_create_pipe_cloexec(int *dst
)
120 ret
= utils_create_pipe(dst
);
125 for (i
= 0; i
< 2; i
++) {
126 ret
= fcntl(dst
[i
], F_SETFD
, FD_CLOEXEC
);
128 PERROR("fcntl pipe cloexec");
138 * Close both read and write side of the pipe.
140 __attribute__((visibility("hidden")))
141 void utils_close_pipe(int *src
)
149 for (i
= 0; i
< 2; i
++) {
157 PERROR("close pipe");
163 * Create a new string using two strings range.
165 __attribute__((visibility("hidden")))
166 char *utils_strdupdelim(const char *begin
, const char *end
)
170 str
= zmalloc(end
- begin
+ 1);
172 PERROR("zmalloc strdupdelim");
176 memcpy(str
, begin
, end
- begin
);
177 str
[end
- begin
] = '\0';
184 * Set CLOEXEC flag to the give file descriptor.
186 __attribute__((visibility("hidden")))
187 int utils_set_fd_cloexec(int fd
)
196 ret
= fcntl(fd
, F_SETFD
, FD_CLOEXEC
);
198 PERROR("fcntl cloexec");
This page took 0.035909 seconds and 5 git commands to generate.