add license file for Tizen SDK
[framework/appfw/debug-launchpad.git] / src / fileutils.c
1 /*
2  *  debug-launchpad
3  *
4  * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact: Jungmin Cho <chivalry.cho@samsung.com>, Gwangho Hwang <gwang.hwang@samsung.com>
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  *
20  */
21
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <unistd.h>
26 #include <sys/stat.h>
27 #include <limits.h>
28
29 static int recurse(const char *path, mode_t mode, int (*fn)(const char *,mode_t, int)) {
30     struct stat st;
31     char dir[PATH_MAX];
32
33     if (path == NULL) {
34         return -1;
35     }
36     if (lstat (path, &st) == -1) {
37         return -1;
38     }
39     if (strrchr(path, '/') != NULL) {
40         int n = strlen(path)-strlen(strrchr(path, '/'));
41         if (n >= PATH_MAX) {
42             return -1;
43         }
44         strncpy(dir, path, n);
45         dir[n] = '\0';
46         fn(dir, mode,1);
47         return 0;
48     }
49     return -1;
50 }
51
52 int dlp_chmod(const char *path, mode_t mode, int recursive) {
53 #ifdef HAVE_WIN32_PROC
54     fprintf(stderr, "error: dlp_chmod not implemented on Win32 (%s)\n", path);
55     return -1;
56 #else
57     struct stat st;
58
59     if (stat (path, &st) == -1)
60         return -1;
61
62     if (chmod (path, mode) == -1) {
63         return -1;
64     }
65     if (recursive) {
66         return recurse(path, mode, dlp_chmod);
67     }
68     return 0;
69 #endif
70 }