VALGRIND option added. Old version sdk-tools support.
[framework/appfw/debug-launchpad.git] / src / fileutils.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <unistd.h>
5 #include <sys/stat.h>
6 #include <limits.h>
7
8 static int recurse(const char *path, mode_t mode, int (*fn)(const char *,mode_t, int)) {
9     struct stat st;
10     char dir[PATH_MAX];
11
12     if (path == NULL) {
13         return -1;
14     }
15     if (lstat (path, &st) == -1) {
16         return -1;
17     }
18     if (strrchr(path, '/') != NULL) {
19         int n = strlen(path)-strlen(strrchr(path, '/'));
20         if (n >= PATH_MAX) {
21             return -1;
22         }
23         strncpy(dir, path, n);
24         dir[n] = '\0';
25         fn(dir, mode,1);
26         return 1;
27     }
28     return -1;
29 }
30
31 int dlp_chmod(const char *path, mode_t mode, int recursive) {
32 #ifdef HAVE_WIN32_PROC
33     fprintf(stderr, "error: dlp_chmod not implemented on Win32 (%s)\n", path);
34     return -1;
35 #else
36     struct stat st;
37
38     if (stat (path, &st) == -1)
39         return -1;
40
41     if (chmod (path, mode) == -1) {
42         return -1;
43     }
44     if (recursive) {
45         return recurse(path, mode, dlp_chmod);
46     }
47     return 1;
48 #endif
49 }