source: trunk/include/stdlib.h @ 3421

Revision 2723, 7.4 KB checked in by reyalp, 12 months ago (diff)

make lua module loading closer to stock lua, now uses package.path etc

  • Property svn:eol-style set to native
Line 
1#ifndef STDLIB_H
2#define STDLIB_H
3
4// CHDK stdlib
5
6// Note: used in modules and platform independent code.
7// Do not add platform dependent stuff in here (#ifdef/#endif compile options or camera dependent values)
8
9//==========================================================
10
11#include "versions.h"
12
13typedef struct
14{
15    unsigned short  major;
16    unsigned short  minor;
17} _version_t;
18
19typedef int             coord;
20typedef unsigned short  color;
21
22#define MAKE_COLOR(bg, fg)  ((color)((((char)(bg))<<8)|((char)(fg))))
23#define FG_COLOR(color)     ((unsigned char)(color & 0xFF))
24#define BG_COLOR(color)     ((unsigned char)(color >> 8))
25
26//==========================================================
27
28#define NULL            ((void*)0)
29
30#define SEEK_SET        0
31#define SEEK_CUR        1
32#define SEEK_END        2
33
34#define O_RDONLY        0
35#define O_WRONLY        1
36#define O_RDWR          2
37#define O_APPEND        8   // DryOS only, wrapper code will removed this for VxWorks so file will be overwritten instead of appended
38
39// CHDK defined values - note does not match VxWorks values
40// Values are corrected in 'open' function to match OS requirements
41#define O_TRUNC         0x200
42#define O_CREAT         0x100
43
44// CHDK 'stat' structure - does not match VxWorks or DryOS internal structs
45// Converted in stat function (generic/wrappers.c)
46// This is the minimal set of values now available from the firmware
47struct stat
48{
49    unsigned long       st_attrib;
50    unsigned long       st_size;
51    unsigned long       st_ctime;
52    unsigned long       st_mtime;
53};
54
55extern int rand(void);
56extern void* srand(unsigned int seed);
57
58extern void qsort (void *__base, int __nelem, int __size, int (*__cmp)(const void *__e1, const void *__e2));
59
60extern int isdigit(int c);
61extern int isspace(int c);
62extern int isalpha(int c);
63extern int isupper(int c);
64extern int islower(int c);
65extern int ispunct(int c);
66extern int isxdigit(int c);
67extern int iscntrl(int c);
68extern int isalnum(int c);
69
70extern long sprintf(char *s, const char *st, ...);
71
72extern long strlen(const char *s);
73extern int strcmp(const char *s1, const char *s2);
74extern int strncmp(const char *s1, const char *s2, long n);
75extern char *strchr(const char *s, int c);
76extern char *strcpy(char *dest, const char *src);
77extern char *strncpy(char *dest, const char *src, long n);
78extern char *strcat(char *dest, const char *app);
79extern char *strrchr(const char *s, int c);
80extern char *strpbrk(const char *s, const char *accept);
81extern char *strstr(const char *s, const char *s2);
82
83extern long strtol(const char *nptr, char **endptr, int base);
84extern unsigned long strtoul(const char *nptr, char **endptr, int base);
85#define atoi(n) strtol((n),NULL,0)
86
87extern int tolower(int c);
88extern int toupper(int c);
89
90extern void *malloc(long size);
91extern void free(void *p);
92extern void *umalloc(long size);
93extern void ufree(void *p);
94
95extern void *memcpy(void *dest, const void *src, long n);
96extern void *memset(void *s, int c, int n);
97extern int memcmp(const void *s1, const void *s2, long n);
98extern void *memchr(const void *s, int c, int n);
99
100extern void SleepTask(long msec);
101extern long taskLock();
102extern long taskUnlock();
103
104extern int creat (const char *name, int flags);
105extern int open (const char *name, int flags, int mode );
106extern int close (int fd);
107extern int write (int fd, const void *buffer, long nbytes);
108extern int read (int fd, void *buffer, long nbytes);
109extern int lseek (int fd, long offset, int whence);
110extern long mkdir(const char *dirname);
111extern long mkdir_if_not_exist(const char *dirname);
112extern int rename(const char *oldname, const char *newname);
113extern int chdir(char *pathname);
114extern int remove(const char *name);
115extern int stat (const char *name, struct stat *pStat);
116
117extern unsigned char SetFileAttributes(const char* fn, unsigned char attr);
118
119// reverse engineered file struct. Appears to be valid for both vxworks and dryos
120// don't use this directly unless you absolutely need to
121// don't EVER try to create one yourself, as this isn't the full structure.
122typedef struct FILE_S {
123    int fd;         // used by Read/Write
124    unsigned len;   // +4 verfied in Fseek_FileStream
125    int unk0;       // +8
126    unsigned pos;   // +0xC verified in Fseek_FileStream
127    // unk1;        // +0x10
128    // unk2;        // +0x14
129    // io_buf;      // +0x18 32k uncached allocated in Fopen_FileStream
130    // unk3;        // +0x20 related to StartFileAccess_Sem
131    // ...name
132} FILE;
133
134extern FILE *fopen(const char *filename, const char *mode);
135extern long fclose(FILE *f);
136extern long fread(void *buf, long elsize, long count, FILE *f);
137extern long fwrite(const void *buf, long elsize, long count, FILE *f);
138extern long fseek(FILE *file, long offset, long whence);
139extern long fflush(FILE *file);
140extern long feof(FILE *file);
141extern long ftell(FILE *file);
142extern char *fgets(char *buf, int n, FILE *f);
143
144extern void msleep(long msec);
145extern long task_lock();
146extern long task_unlock();
147extern const char *task_name(int id);
148int task_id_list_get(int *idlist,int size);
149extern const char *strerror(int num);
150// on vxworks we could find the actual errno, but this is easier to automate sig
151// doesn't exist on dryos, but we stub it
152extern int errnoOfTaskGet(int tid);
153#define errno (errnoOfTaskGet(0))
154
155//-------------------------------------------------------------------------------------
156
157#define DOS_ATTR_RDONLY         0x01            /* read-only file */
158#define DOS_ATTR_HIDDEN         0x02            /* hidden file */
159#define DOS_ATTR_SYSTEM         0x04            /* system file */
160#define DOS_ATTR_VOL_LABEL      0x08            /* volume label (not a file) */
161#define DOS_ATTR_DIRECTORY      0x10            /* entry is a sub-directory */
162#define DOS_ATTR_ARCHIVE        0x20            /* file subject to archiving */
163
164//-------------------------------------------------------------------------------------
165
166// CHDK structs for opendir/readdir/closedir
167// Conversion to/from camera specific versions done in wrapper code
168struct dirent   // Returned from readdir
169{
170    char            d_name[100];    // We only use the name value
171};
172
173// Returned from opendir
174typedef struct
175{
176    void            *cam_DIR;   // Camera specific internal DIR structure
177    struct dirent   dir;        // Last info returned from readdir
178} DIR;
179
180extern DIR*           opendir (const char* name);
181extern struct dirent* readdir (DIR*);
182extern int            closedir (DIR*);
183//extern void           rewinddir (DIR*);   // Not used
184
185//-------------------------------------------------------------------------------------
186
187struct tm
188        {
189        int tm_sec;     /* seconds after the minute     - [0, 59] */
190        int tm_min;     /* minutes after the hour       - [0, 59] */
191        int tm_hour;    /* hours after midnight         - [0, 23] */
192        int tm_mday;    /* day of the month             - [1, 31] */
193        int tm_mon;     /* months since January         - [0, 11] */
194        int tm_year;    /* years since 1900     */
195        int tm_wday;    /* days since Sunday            - [0, 6] */
196        int tm_yday;    /* days since January 1         - [0, 365] */
197        int tm_isdst;   /* Daylight Saving Time flag */
198        };
199
200typedef unsigned long time_t;
201
202extern struct tm * localtime(const time_t *_tod);
203extern struct tm * get_localtime();
204
205struct utimbuf {
206    unsigned long actime;       /* set the access time */
207    unsigned long modtime;      /* set the modification time */
208};
209
210extern int utime(const char *file, struct utimbuf *newTimes);
211extern unsigned long time(unsigned long *timer);
212extern long strftime(char *s, unsigned long maxsize, const char *format, const struct tm *timp);
213extern time_t mktime(struct tm *timp);
214
215extern int abs( int v );
216
217#endif
Note: See TracBrowser for help on using the repository browser.