No more password prompts
[kugel-rb.git] / uisimulator / x11 / io.c
blobd4d27140263693cbd621e4570ec6e9a39fb39f67
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2002 Daniel Stenberg
12 * All files in this archive are subject to the GNU General Public License.
13 * See the file COPYING in the source tree root for full license agreement.
15 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
16 * KIND, either express or implied.
18 ****************************************************************************/
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <sys/stat.h>
24 #ifdef __FreeBSD__
25 #include <sys/param.h>
26 #include <sys/mount.h>
27 #else
28 #include <sys/vfs.h>
29 #endif
30 #include <dirent.h>
31 #include <unistd.h>
33 #include <fcntl.h>
34 #include "debug.h"
36 #define DIRFUNCTIONS_DEFINED /* prevent those prototypes */
37 #define dirent x11_dirent
38 #include "../../firmware/include/dir.h"
39 #undef dirent
41 #define SIMULATOR_ARCHOS_ROOT "archos"
43 struct mydir {
44 DIR *dir;
45 char *name;
48 typedef struct mydir MYDIR;
50 MYDIR *x11_opendir(const char *name)
52 char buffer[256]; /* sufficiently big */
53 DIR *dir;
55 if(name[0] == '/') {
56 sprintf(buffer, "%s%s", SIMULATOR_ARCHOS_ROOT, name);
57 dir=(DIR *)opendir(buffer);
59 else
60 dir=(DIR *)opendir(name);
62 if(dir) {
63 MYDIR *my = (MYDIR *)malloc(sizeof(MYDIR));
64 my->dir = dir;
65 my->name = (char *)strdup(name);
67 return my;
69 /* failed open, return NULL */
70 return (MYDIR *)0;
73 struct x11_dirent *x11_readdir(MYDIR *dir)
75 char buffer[512]; /* sufficiently big */
76 static struct x11_dirent secret;
77 struct stat s;
78 struct dirent *x11 = (readdir)(dir->dir);
80 if(!x11)
81 return (struct x11_dirent *)0;
83 strcpy(secret.d_name, x11->d_name);
85 /* build file name */
86 sprintf(buffer, SIMULATOR_ARCHOS_ROOT "%s/%s",
87 dir->name, x11->d_name);
88 stat(buffer, &s); /* get info */
90 secret.attribute = S_ISDIR(s.st_mode)?ATTR_DIRECTORY:0;
91 secret.size = s.st_size;
93 return &secret;
96 void x11_closedir(MYDIR *dir)
98 free(dir->name);
99 (closedir)(dir->dir);
101 free(dir);
105 int x11_open(const char *name, int opts)
107 char buffer[256]; /* sufficiently big */
109 if(name[0] == '/') {
110 sprintf(buffer, "%s%s", SIMULATOR_ARCHOS_ROOT, name);
112 debugf("We open the real file '%s'\n", buffer);
113 return (open)(buffer, opts);
115 return (open)(name, opts);
118 int x11_close(int fd)
120 return (close)(fd);
123 int x11_creat(const char *name, mode_t mode)
125 char buffer[256]; /* sufficiently big */
126 (void)mode;
127 if(name[0] == '/') {
128 sprintf(buffer, "%s%s", SIMULATOR_ARCHOS_ROOT, name);
130 debugf("We create the real file '%s'\n", buffer);
131 return (creat)(buffer, 0666);
133 return (creat)(name, 0666);
136 int x11_mkdir(const char *name, mode_t mode)
138 char buffer[256]; /* sufficiently big */
139 (void)mode;
140 if(name[0] == '/') {
141 sprintf(buffer, "%s%s", SIMULATOR_ARCHOS_ROOT, name);
143 debugf("We create the real directory '%s'\n", buffer);
144 return (mkdir)(buffer, 0666);
146 return (mkdir)(name, 0666);
149 int x11_rmdir(const char *name)
151 char buffer[256]; /* sufficiently big */
152 if(name[0] == '/') {
153 sprintf(buffer, "%s%s", SIMULATOR_ARCHOS_ROOT, name);
155 debugf("We remove the real directory '%s'\n", buffer);
156 return (rmdir)(buffer);
158 return (rmdir)(name);
161 int x11_remove(char *name)
163 char buffer[256]; /* sufficiently big */
165 if(name[0] == '/') {
166 sprintf(buffer, "%s%s", SIMULATOR_ARCHOS_ROOT, name);
168 debugf("We remove the real file '%s'\n", buffer);
169 return (remove)(buffer);
171 return (remove)(name);
174 int x11_rename(char *oldpath, char* newpath)
176 char buffer1[256];
177 char buffer2[256];
179 if(oldpath[0] == '/') {
180 sprintf(buffer1, "%s%s", SIMULATOR_ARCHOS_ROOT, oldpath);
181 sprintf(buffer2, "%s%s", SIMULATOR_ARCHOS_ROOT, newpath);
183 debugf("We rename the real file '%s' to '%s'\n", buffer1, buffer2);
184 return (rename)(buffer1, buffer2);
186 return -1;
189 int x11_filesize(int fd)
191 int old = lseek(fd, 0, SEEK_CUR);
192 int size = lseek(fd, 0, SEEK_END);
193 lseek(fd, old, SEEK_SET);
195 return(size);
198 void fat_size(unsigned int* size, unsigned int* free)
200 struct statfs fs;
202 if (!statfs(".", &fs)) {
203 DEBUGF("statfs: bsize=%d blocks=%d free=%d\n",
204 fs.f_bsize, fs.f_blocks, fs.f_bfree);
205 if (size)
206 *size = fs.f_blocks * (fs.f_bsize / 1024);
207 if (free)
208 *free = fs.f_bfree * (fs.f_bsize / 1024);
210 else {
211 if (size)
212 *size = 0;
213 if (free)
214 *free = 0;