btrfs-progs pretty/quiet build
[platform/upstream/btrfs-progs.git] / common.c
1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public
4  * License v2 as published by the Free Software Foundation.
5  *
6  * This program is distributed in the hope that it will be useful,
7  * but WITHOUT ANY WARRANTY; without even the implied warranty of
8  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
9  * General Public License for more details.
10  *
11  * You should have received a copy of the GNU General Public
12  * License along with this program; if not, write to the
13  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
14  * Boston, MA 021110-1307, USA.
15  */
16
17 #include <sys/types.h>
18 #include <sys/stat.h>
19 #include <dirent.h>
20 #include <fcntl.h>
21
22 int open_file_or_dir(const char *fname)
23 {
24         int ret;
25         struct stat st;
26         DIR *dirstream;
27         int fd;
28
29         ret = stat(fname, &st);
30         if (ret < 0) {
31                 return -1;
32         }
33         if (S_ISDIR(st.st_mode)) {
34                 dirstream = opendir(fname);
35                 if (!dirstream) {
36                         return -2;
37                 }
38                 fd = dirfd(dirstream);
39         } else {
40                 fd = open(fname, O_RDWR);
41         }
42         if (fd < 0) {
43                 return -3;
44         }
45         return fd;
46 }