292f9317 |
1 | /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * |
2 | * Mupen64plus-core - osal_files_unix.c * |
3 | * Mupen64Plus homepage: http://code.google.com/p/mupen64plus/ * |
4 | * Copyright (C) 2009 Richard Goedeken * |
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 * |
17 | * along with this program; if not, write to the * |
18 | * Free Software Foundation, Inc., * |
19 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * |
20 | * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ |
21 | |
22 | /* This file contains the definitions for the unix-specific file handling |
23 | * functions |
24 | */ |
25 | |
26 | #include <sys/types.h> |
27 | #include <sys/stat.h> |
28 | #include <unistd.h> |
29 | #include <stdlib.h> |
30 | #include <string.h> |
31 | #include <stdio.h> |
32 | #include <dirent.h> |
33 | |
34 | #include "osal_files.h" |
35 | |
36 | /* global functions */ |
37 | |
38 | int osal_is_directory(const char* name) |
39 | { |
40 | DIR* dir; |
41 | dir = opendir(name); |
42 | if(dir != NULL) |
43 | { |
44 | closedir(dir); |
45 | return 1; |
46 | } |
47 | return 0; |
48 | } |
49 | |
50 | int osal_mkdirp(const char *dirpath, int mode) |
51 | { |
52 | struct stat fileinfo; |
53 | int dirpathlen = strlen(dirpath); |
54 | char *currpath = strdup(dirpath); |
55 | |
56 | /* first, break the path into pieces by replacing all of the slashes wil NULL chars */ |
57 | while (strlen(currpath) > 1) |
58 | { |
59 | char *lastslash = strrchr(currpath, '/'); |
60 | if (lastslash == NULL) |
61 | break; |
62 | *lastslash = 0; |
63 | } |
64 | |
65 | /* then re-assemble the path from left to right until we get to a directory that doesn't exist */ |
66 | while (strlen(currpath) < dirpathlen) |
67 | { |
68 | if (strlen(currpath) > 0 && stat(currpath, &fileinfo) != 0) |
69 | break; |
70 | currpath[strlen(currpath)] = '/'; |
71 | } |
72 | |
73 | /* then walk up the path chain, creating directories along the way */ |
74 | do |
75 | { |
76 | if (stat(currpath, &fileinfo) != 0) |
77 | { |
78 | if (mkdir(currpath, mode) != 0) |
79 | { |
80 | free(currpath); |
81 | return 1; /* mkdir failed */ |
82 | } |
83 | } |
84 | if (strlen(currpath) == dirpathlen) |
85 | break; |
86 | else |
87 | currpath[strlen(currpath)] = '/'; |
88 | } while (1); |
89 | |
90 | free(currpath); |
91 | return 0; |
92 | } |
93 | |
94 | void * osal_search_dir_open(const char *pathname) |
95 | { |
96 | DIR *dir; |
97 | dir = opendir(pathname); |
98 | return dir; |
99 | } |
100 | |
101 | const char *osal_search_dir_read_next(void * dir_handle) |
102 | { |
103 | DIR *dir = (DIR *) dir_handle; |
104 | struct dirent *entry; |
105 | |
106 | entry = readdir(dir); |
107 | if (entry == NULL) |
108 | return NULL; |
109 | return entry->d_name; |
110 | } |
111 | |
112 | void osal_search_dir_close(void * dir_handle) |
113 | { |
114 | closedir((DIR *) dir_handle); |
115 | } |
116 | |