diff options
author | netblue30 <netblue30@yahoo.com> | 2017-09-16 08:49:05 -0400 |
---|---|---|
committer | netblue30 <netblue30@yahoo.com> | 2017-09-16 08:49:05 -0400 |
commit | 280f37eba89ebc211d0c02848d3d47d086458b25 (patch) | |
tree | 1398c5dfc53c4d286d7b6b528d5a3c1585a67325 /src/fbuilder/utils.c | |
parent | Merge pull request #1552 from SpotComms/mf (diff) | |
download | firejail-280f37eba89ebc211d0c02848d3d47d086458b25.tar.gz firejail-280f37eba89ebc211d0c02848d3d47d086458b25.tar.zst firejail-280f37eba89ebc211d0c02848d3d47d086458b25.zip |
--build
Diffstat (limited to 'src/fbuilder/utils.c')
-rw-r--r-- | src/fbuilder/utils.c | 72 |
1 files changed, 72 insertions, 0 deletions
diff --git a/src/fbuilder/utils.c b/src/fbuilder/utils.c new file mode 100644 index 000000000..902290899 --- /dev/null +++ b/src/fbuilder/utils.c | |||
@@ -0,0 +1,72 @@ | |||
1 | /* | ||
2 | * Copyright (C) 2014-2017 Firejail Authors | ||
3 | * | ||
4 | * This file is part of firejail project | ||
5 | * | ||
6 | * This program is free software; you can redistribute it and/or modify | ||
7 | * it under the terms of the GNU General Public License as published by | ||
8 | * the Free Software Foundation; either version 2 of the License, or | ||
9 | * (at your option) any later version. | ||
10 | * | ||
11 | * This program is distributed in the hope that it will be useful, | ||
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
14 | * GNU General Public License for more details. | ||
15 | * | ||
16 | * You should have received a copy of the GNU General Public License along | ||
17 | * with this program; if not, write to the Free Software Foundation, Inc., | ||
18 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. | ||
19 | */ | ||
20 | |||
21 | #include "fbuilder.h" | ||
22 | |||
23 | // todo: duplicated from src/firejail/util.c - remove dplication | ||
24 | // return 1 if the file is a directory | ||
25 | int is_dir(const char *fname) { | ||
26 | assert(fname); | ||
27 | if (*fname == '\0') | ||
28 | return 0; | ||
29 | |||
30 | // if fname doesn't end in '/', add one | ||
31 | int rv; | ||
32 | struct stat s; | ||
33 | if (fname[strlen(fname) - 1] == '/') | ||
34 | rv = stat(fname, &s); | ||
35 | else { | ||
36 | char *tmp; | ||
37 | if (asprintf(&tmp, "%s/", fname) == -1) { | ||
38 | fprintf(stderr, "Error: cannot allocate memory, %s:%d\n", __FILE__, __LINE__); | ||
39 | errExit("asprintf"); | ||
40 | } | ||
41 | rv = stat(tmp, &s); | ||
42 | free(tmp); | ||
43 | } | ||
44 | |||
45 | if (rv == -1) | ||
46 | return 0; | ||
47 | |||
48 | if (S_ISDIR(s.st_mode)) | ||
49 | return 1; | ||
50 | |||
51 | return 0; | ||
52 | } | ||
53 | |||
54 | // return NULL if fname is already a directory, or if no directory found | ||
55 | char *extract_dir(char *fname) { | ||
56 | assert(fname); | ||
57 | if (is_dir(fname)) | ||
58 | return NULL; | ||
59 | |||
60 | char *name = strdup(fname); | ||
61 | if (!name) | ||
62 | errExit("strdup"); | ||
63 | |||
64 | char *ptr = strrchr(name, '/'); | ||
65 | if (!ptr) { | ||
66 | free(name); | ||
67 | return NULL; | ||
68 | } | ||
69 | *ptr = '\0'; | ||
70 | |||
71 | return name; | ||
72 | } | ||