source: trunk/include/stdlib.h @ 524

Revision 524, 8.7 KB checked in by reyalp, 5 years ago (diff)
  • made ubasic eat up to 100 labels or REMs in one call to ubasic_run, rather than taking 10ms each
  • made script buffer dynamic, allocated when script is loaded
  • made some other script related memory dynamic

see http://chdk.setepontos.com/index.php/topic,688.msg21646.html#msg21646 for details

  • Property svn:eol-style set to native
Line 
1#include "camera.h"
2
3#ifndef STDLIB_H
4#define STDLIB_H
5
6#define NULL            ((void*)0)
7
8#define SEEK_SET        0
9#define SEEK_CUR        1
10#define SEEK_END        2
11
12#define O_RDONLY        0
13#define O_WRONLY        1
14#define O_RDWR          2
15
16
17#if !CAM_DRYOS
18
19#define O_TRUNC         0x400
20#define O_CREAT         0x200
21
22struct  stat
23    {
24    unsigned long       st_dev;         /* device ID number */
25    unsigned long       st_ino;         /* file serial number */
26    unsigned short      st_mode;        /* file mode (see below) */
27    short               st_nlink;       /* number of links to file */
28    short               st_uid;         /* user ID of file's owner */
29    short               st_gid;         /* group ID of file's group */
30    unsigned long       st_rdev;        /* device ID, only if special file */
31    unsigned long       st_size;        /* size of file, in bytes */
32    unsigned long       st_atime;       /* time of last access */
33    unsigned long       st_mtime;       /* time of last modification */
34    unsigned long       st_ctime;       /* time of last change of file status */
35    long                st_blksize;
36    long                st_blocks;
37    unsigned char       st_attrib;      /* file attribute byte (dosFs only) */
38    int                 reserved1;      /* reserved for future use */
39    int                 reserved2;      /* reserved for future use */
40    int                 reserved3;      /* reserved for future use */
41    int                 reserved4;      /* reserved for future use */
42    int                 reserved5;      /* reserved for future use */
43    int                 reserved6;      /* reserved for future use */
44};
45
46#else
47
48#define O_TRUNC         0x200
49#define O_CREAT         0x100
50
51struct  stat
52    {
53    unsigned long       st_dev;         //?
54    unsigned long       st_ino;         //?     
55    unsigned short      st_mode;        //?     
56    short               st_nlink;       //?     
57    short               st_uid;         //?     
58    short               st_gid;         //?     
59    unsigned long       st_atime;       //?     
60    unsigned long       st_mtime;       //?     
61    unsigned long       st_ctime;       //?     
62    unsigned long       st_size;       
63    long                st_blksize;     //?
64    long                st_blocks;      //?
65    unsigned char       st_attrib;
66    int                 reserved1;      //     
67    int                 reserved2;      //
68    int                 reserved3;      //
69    int                 reserved4;      //
70    int                 reserved5;      //
71    int                 reserved6;      //
72};
73
74#endif
75
76extern int rand(void);
77extern void* srand(unsigned int seed);
78
79extern void qsort (void *__base, int __nelem, int __size, int (*__cmp)(const void *__e1, const void *__e2));
80
81extern int isdigit(int c);
82extern int isspace(int c);
83extern int isalpha(int c);
84extern int isupper(int c);
85
86extern long sprintf(char *s, const char *st, ...);
87
88extern long strlen(const char *s);
89extern int strcmp(const char *s1, const char *s2);
90extern int strncmp(const char *s1, const char *s2, long n);
91extern char *strchr(const char *s, int c);
92extern char *strcpy(char *dest, const char *src);
93extern char *strncpy(char *dest, const char *src, long n);
94extern char *strcat(char *dest, const char *app);
95extern char *strrchr(const char *s, int c);
96extern char *strpbrk(const char *s, const char *accept);
97
98extern long strtol(const char *nptr, char **endptr, int base);
99#define atoi(n) strtol((n),NULL,0)
100
101extern void *malloc(long size);
102extern void free(void *p);
103extern void *umalloc(long size);
104extern void ufree(void *p);
105
106extern void *memcpy(void *dest, const void *src, long n);
107extern void *memset(void *s, int c, int n);
108extern int memcmp(const void *s1, const void *s2, long n);
109
110
111extern void SleepTask(long msec);
112extern long taskLock();
113extern long taskUnlock();
114
115extern long Fopen_Fut(const char *filename, const char *mode);
116extern long Fclose_Fut(long file);
117extern long Fread_Fut(void *buf, long elsize, long count, long f);
118extern long Fwrite_Fut(const void *buf, long elsize, long count, long f);
119extern long Fseek_Fut(long file, long offset, long whence);
120// TODO can we just use these all the time ?
121extern long RenameFile_Fut(const char *oldname, const char *newname);
122extern long MakeDirectory_Fut(const char *name);
123extern long DeleteFile_Fut(const char *name);
124extern long Feof_Fut(long file);
125extern long Fflush_Fut(long file);
126extern char *Fgets_Fut(char *buf, int n, long f);
127
128extern int creat (const char *name, int flags);
129extern int open (const char *name, int flags, int mode );
130extern int close (int fd);
131extern int write (int fd, const void *buffer, long nbytes);
132extern int read (int fd, void *buffer, long nbytes);
133extern int lseek (int fd, long offset, int whence);
134extern long mkdir(const char *dirname);
135
136#ifdef STDIO_COMPAT_FILE
137// don't use this directly unless you absolutely need to
138// don't EVER try to create one yourself, as this isn't the full structure.
139typedef struct FILE_S {
140    int fd;         // used by Read/Write
141    unsigned len;   // +4 verfied in Fseek_FileStream
142    int unk0;       // +8
143    unsigned pos;   // +0xC verified in Fseek_FileStream
144    // unk1;        // +0x10
145    // unk2;        // +0x14
146    // io_buf;      // +0x18 32k uncached allocated in Fopen_FileStream
147    // unk3;        // +0x20 related to StartFileAccess_Sem
148    // ...name
149} FILE;
150static inline FILE *fopen(const char *filename, const char *mode) {
151    return (FILE *)Fopen_Fut(filename,mode);
152}
153static inline long fclose(FILE *f) {
154    return Fclose_Fut((long)f);
155}
156static inline long fread(void *buf, long elsize, long count, FILE *f) {
157    return Fread_Fut(buf,elsize,count,(long)f);
158}
159static inline long fwrite(const void *buf, long elsize, long count, FILE *f) {
160    return Fwrite_Fut(buf,elsize,count,(long)f);
161}
162static inline long fseek(FILE *file, long offset, long whence) {
163    return Fseek_Fut((long)file,offset,whence);
164}
165static inline long fflush(FILE *file) {
166    return Fflush_Fut((long)file);
167}
168static inline long feof(FILE *file) {
169    return Feof_Fut((long)file);
170}
171static inline long ftell(FILE *file) {
172    if(!file) return -1;
173    return file->pos;
174}
175static inline char *fgets(char *buf, int n, FILE *f) {
176    return Fgets_Fut(buf,n,(int)f);
177}
178#else
179#define fopen(a,b) Fopen_Fut(a,b)
180#define fclose(a) Fclose_Fut(a)
181#define fread(a,b,c,d) Fread_Fut(a,b,c,d)
182#define fwrite(a,b,c,d) Fwrite_Fut(a,b,c,d)
183#define fseek(a,b,c) Fseek_Fut(a,b,c)
184#define fflush(a) Fflush_Fut(a)
185#define feof(a) Feof_Fut(a)
186typedef long FILE;
187#endif //STDIO_COMPAT
188#define fdelete(a) DeleteFile_Fut(a)
189/**
190 * No STUBS!
191 * You can't use these two directly from THUMB code (core), only from platform.
192 */
193extern int fprintf(FILE *fd, char*buf, ...);
194extern int printf(char *buf, ...);
195
196extern void msleep(long msec);
197extern long task_lock();
198extern long task_unlock();
199extern const char *task_name(int id);
200int task_id_list_get(int *idlist,int size);
201extern const char *strerror(int num);
202// on vxworks we could find the actual errno, but this is easier to automate sig
203// doesn't exist on dryos, but we stub it
204extern int errnoOfTaskGet(int tid);
205#define errno (errnoOfTaskGet(0))
206
207#define DOS_ATTR_RDONLY         0x01            /* read-only file */
208#define DOS_ATTR_HIDDEN         0x02            /* hidden file */
209#define DOS_ATTR_SYSTEM         0x04            /* system file */
210#define DOS_ATTR_VOL_LABEL      0x08            /* volume label (not a file) */
211#define DOS_ATTR_DIRECTORY      0x10            /* entry is a sub-directory */
212#define DOS_ATTR_ARCHIVE        0x20            /* file subject to archiving */
213
214#if !CAM_DRYOS
215struct dirent {
216    char                name[100];
217};
218#else
219struct dirent {
220    char                name[13];
221    unsigned long       unk1;
222    unsigned char       attrib;
223    unsigned long       size;
224    unsigned long       time1;
225    unsigned long       time2;
226    unsigned long       time3;
227};
228#endif
229
230typedef struct {
231    unsigned int        fd;
232    unsigned int        loc;
233    struct dirent       dir;
234} DIR;
235
236
237
238extern DIR*           opendir (const char* name);
239extern struct dirent* readdir (DIR*);
240extern int            closedir (DIR*);
241extern void           rewinddir (DIR*);
242extern int            stat (const char *name, struct stat *pStat);
243
244
245struct tm
246        {
247        int tm_sec;     /* seconds after the minute     - [0, 59] */
248        int tm_min;     /* minutes after the hour       - [0, 59] */
249        int tm_hour;    /* hours after midnight         - [0, 23] */
250        int tm_mday;    /* day of the month             - [1, 31] */
251        int tm_mon;     /* months since January         - [0, 11] */
252        int tm_year;    /* years since 1900     */
253        int tm_wday;    /* days since Sunday            - [0, 6] */
254        int tm_yday;    /* days since January 1         - [0, 365] */
255        int tm_isdst;   /* Daylight Saving Time flag */
256        };
257
258typedef unsigned long time_t;
259
260extern struct tm * localtime(const unsigned long *_tod);
261
262extern int rename(const char *oldname, const char *newname);
263extern int chdir(char *pathname);
264extern int remove(const char *name);
265#ifdef FS_USE_FUT
266#define mkdir(x) MakeDirectory_Fut(x)
267#define rename(x,y) RenameFile_Fut(x,y)
268#define remove(x) DeleteFile_Fut(x)
269#endif
270struct utimbuf {
271    unsigned long actime;       /* set the access time */
272    unsigned long modtime;      /* set the modification time */
273};
274
275extern int utime(char *file, struct utimbuf *newTimes);
276extern unsigned long time(unsigned long *timer);
277extern long strftime(char *s, unsigned long maxsize, const char *format, const struct tm *timp);
278extern time_t mktime(struct tm *timp);
279
280static inline int abs( int v ) {
281  return v<0 ? -v : v;
282}
283
284#endif
Note: See TracBrowser for help on using the repository browser.