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 char *utils_expand_path(const char *path
)
37 const char *end_path
= path
;
38 char *next
, *cut_path
= NULL
, *expanded_path
= NULL
;
45 /* Find last token delimited by '/' */
46 while ((next
= strpbrk(end_path
+ 1, "/"))) {
50 /* Cut last token from original path */
51 cut_path
= strndup(path
, end_path
- path
);
53 expanded_path
= zmalloc(PATH_MAX
);
54 if (expanded_path
== NULL
) {
55 PERROR("zmalloc expand path");
59 expanded_path
= realpath((char *)cut_path
, expanded_path
);
60 if (expanded_path
== NULL
) {
63 ERR("%s: No such file or directory", cut_path
);
66 PERROR("realpath utils expand path");
72 /* Add end part to expanded path */
73 strncat(expanded_path
, end_path
, PATH_MAX
);
85 * Create a pipe in dst.
87 int utils_create_pipe(int *dst
)
97 PERROR("create pipe");
104 * Create pipe and set CLOEXEC flag to both fd.
106 * Make sure the pipe opened by this function are closed at some point. Use
107 * utils_close_pipe().
109 int utils_create_pipe_cloexec(int *dst
)
117 ret
= utils_create_pipe(dst
);
122 for (i
= 0; i
< 2; i
++) {
123 ret
= fcntl(dst
[i
], F_SETFD
, FD_CLOEXEC
);
125 PERROR("fcntl pipe cloexec");
135 * Close both read and write side of the pipe.
137 void utils_close_pipe(int *src
)
145 for (i
= 0; i
< 2; i
++) {
153 PERROR("close pipe");
This page took 0.053155 seconds and 4 git commands to generate.