Fix: Remove useless NULL and zero assignment
[lttng-tools.git] / src / common / utils.c
CommitLineData
81b86775
DG
1/*
2 * Copyright (C) 2012 - David Goulet <dgoulet@efficios.com>
3 *
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.
7 *
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
11 * more details.
12 *
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.
16 */
17
18#define _GNU_SOURCE
19#include <ctype.h>
20#include <fcntl.h>
21#include <limits.h>
22#include <stdlib.h>
23#include <string.h>
24
25#include <common/common.h>
26
27#include "utils.h"
28
29/*
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.
34 */
32dd26fb 35__attribute__((visibility("hidden")))
81b86775
DG
36char *utils_expand_path(const char *path)
37{
38 const char *end_path = path;
39 char *next, *cut_path = NULL, *expanded_path = NULL;
40
41 /* Safety net */
42 if (path == NULL) {
43 goto error;
44 }
45
46 /* Find last token delimited by '/' */
47 while ((next = strpbrk(end_path + 1, "/"))) {
48 end_path = next;
49 }
50
51 /* Cut last token from original path */
52 cut_path = strndup(path, end_path - path);
53
54 expanded_path = zmalloc(PATH_MAX);
55 if (expanded_path == NULL) {
56 PERROR("zmalloc expand path");
57 goto error;
58 }
59
60 expanded_path = realpath((char *)cut_path, expanded_path);
61 if (expanded_path == NULL) {
62 switch (errno) {
63 case ENOENT:
64 ERR("%s: No such file or directory", cut_path);
65 break;
66 default:
67 PERROR("realpath utils expand path");
68 break;
69 }
70 goto error;
71 }
72
73 /* Add end part to expanded path */
c30ce0b3 74 strncat(expanded_path, end_path, PATH_MAX - strlen(expanded_path) - 1);
81b86775
DG
75
76 free(cut_path);
77 return expanded_path;
78
79error:
80 free(expanded_path);
81 free(cut_path);
82 return NULL;
83}
84
85/*
86 * Create a pipe in dst.
87 */
32dd26fb 88__attribute__((visibility("hidden")))
81b86775
DG
89int utils_create_pipe(int *dst)
90{
91 int ret;
92
93 if (dst == NULL) {
94 return -1;
95 }
96
97 ret = pipe(dst);
98 if (ret < 0) {
99 PERROR("create pipe");
100 }
101
102 return ret;
103}
104
105/*
106 * Create pipe and set CLOEXEC flag to both fd.
107 *
108 * Make sure the pipe opened by this function are closed at some point. Use
109 * utils_close_pipe().
110 */
32dd26fb 111__attribute__((visibility("hidden")))
81b86775
DG
112int utils_create_pipe_cloexec(int *dst)
113{
114 int ret, i;
115
116 if (dst == NULL) {
117 return -1;
118 }
119
120 ret = utils_create_pipe(dst);
121 if (ret < 0) {
122 goto error;
123 }
124
125 for (i = 0; i < 2; i++) {
126 ret = fcntl(dst[i], F_SETFD, FD_CLOEXEC);
127 if (ret < 0) {
128 PERROR("fcntl pipe cloexec");
129 goto error;
130 }
131 }
132
133error:
134 return ret;
135}
136
137/*
138 * Close both read and write side of the pipe.
139 */
32dd26fb 140__attribute__((visibility("hidden")))
81b86775
DG
141void utils_close_pipe(int *src)
142{
143 int i, ret;
144
145 if (src == NULL) {
146 return;
147 }
148
149 for (i = 0; i < 2; i++) {
150 /* Safety check */
151 if (src[i] < 0) {
152 continue;
153 }
154
155 ret = close(src[i]);
156 if (ret) {
157 PERROR("close pipe");
158 }
159 }
160}
a4b92340
DG
161
162/*
163 * Create a new string using two strings range.
164 */
32dd26fb 165__attribute__((visibility("hidden")))
a4b92340
DG
166char *utils_strdupdelim(const char *begin, const char *end)
167{
168 char *str;
169
170 str = zmalloc(end - begin + 1);
171 if (str == NULL) {
172 PERROR("zmalloc strdupdelim");
173 goto error;
174 }
175
176 memcpy(str, begin, end - begin);
177 str[end - begin] = '\0';
178
179error:
180 return str;
181}
This page took 0.02982 seconds and 4 git commands to generate.