]> git.proxmox.com Git - mirror_lxc.git/blame - src/lxc/pam/utils.c
utils: split into {file,string}_utils.{c,h}
[mirror_lxc.git] / src / lxc / pam / utils.c
CommitLineData
60534f79
SG
1/*
2 * lxc: linux Container library
3 *
4 * (C) Copyright IBM Corp. 2007, 2008
5 *
6 * Authors:
7 * Daniel Lezcano <daniel.lezcano at free.fr>
8 *
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
13 *
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
18 *
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 */
23
24
25#include <sys/types.h>
26#include <sys/stat.h>
27#include <unistd.h>
28#include <stdlib.h>
29#include <stdbool.h>
30#include <string.h>
31#include <sys/vfs.h>
32#include <stdarg.h>
33
34#include "utils.h"
35
bd583214
DJ
36#ifndef HAVE_STRLCAT
37#include "include/strlcat.h"
38#endif
39
60534f79
SG
40bool file_exists(const char *f)
41{
42 struct stat statbuf;
43
44 return stat(f, &statbuf) == 0;
45}
46
47void *must_realloc(void *orig, size_t sz)
48{
49 void *ret;
50
51 do {
52 ret = realloc(orig, sz);
53 } while (!ret);
54
55 return ret;
56}
57
58char *must_copy_string(const char *entry)
59{
60 char *ret;
61
62 if (!entry)
63 return NULL;
64 do {
65 ret = strdup(entry);
66 } while (!ret);
67
68 return ret;
69}
70
71char *must_make_path(const char *first, ...)
72{
73 va_list args;
74 char *cur, *dest;
75 size_t full_len = strlen(first);
bd583214 76 size_t buf_len;
60534f79
SG
77
78 dest = must_copy_string(first);
79
80 va_start(args, first);
81 while ((cur = va_arg(args, char *)) != NULL) {
82 full_len += strlen(cur);
83 if (cur[0] != '/')
84 full_len++;
efed99a4 85
bd583214
DJ
86 buf_len = full_len + 1;
87 dest = must_realloc(dest, buf_len);
efed99a4 88
60534f79 89 if (cur[0] != '/')
bd583214
DJ
90 (void)strlcat(dest, "/", buf_len);
91 (void)strlcat(dest, cur, buf_len);
60534f79
SG
92 }
93 va_end(args);
94
95 return dest;
96}
97
98bool is_fs_type(const struct statfs *fs, fs_type_magic magic_val)
99{
100 return (fs->f_type == (fs_type_magic)magic_val);
101}
102
103bool has_fs_type(const char *path, fs_type_magic magic_val)
104{
105 bool has_type;
106 int ret;
107 struct statfs sb;
108
109 ret = statfs(path, &sb);
110 if (ret < 0)
111 return false;
112
113 has_type = is_fs_type(&sb, magic_val);
114
115 return has_type;
116}