| 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 | |
|---|
| 13 | typedef struct |
|---|
| 14 | { |
|---|
| 15 | unsigned short major; |
|---|
| 16 | unsigned short minor; |
|---|
| 17 | } _version_t; |
|---|
| 18 | |
|---|
| 19 | typedef int coord; |
|---|
| 20 | typedef 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 |
|---|
| 47 | struct stat |
|---|
| 48 | { |
|---|
| 49 | unsigned long st_attrib; |
|---|
| 50 | unsigned long st_size; |
|---|
| 51 | unsigned long st_ctime; |
|---|
| 52 | unsigned long st_mtime; |
|---|
| 53 | }; |
|---|
| 54 | |
|---|
| 55 | extern int rand(void); |
|---|
| 56 | extern void* srand(unsigned int seed); |
|---|
| 57 | |
|---|
| 58 | extern void qsort (void *__base, int __nelem, int __size, int (*__cmp)(const void *__e1, const void *__e2)); |
|---|
| 59 | |
|---|
| 60 | extern int isdigit(int c); |
|---|
| 61 | extern int isspace(int c); |
|---|
| 62 | extern int isalpha(int c); |
|---|
| 63 | extern int isupper(int c); |
|---|
| 64 | extern int islower(int c); |
|---|
| 65 | extern int ispunct(int c); |
|---|
| 66 | extern int isxdigit(int c); |
|---|
| 67 | extern int iscntrl(int c); |
|---|
| 68 | extern int isalnum(int c); |
|---|
| 69 | |
|---|
| 70 | extern long sprintf(char *s, const char *st, ...); |
|---|
| 71 | |
|---|
| 72 | extern long strlen(const char *s); |
|---|
| 73 | extern int strcmp(const char *s1, const char *s2); |
|---|
| 74 | extern int strncmp(const char *s1, const char *s2, long n); |
|---|
| 75 | extern char *strchr(const char *s, int c); |
|---|
| 76 | extern char *strcpy(char *dest, const char *src); |
|---|
| 77 | extern char *strncpy(char *dest, const char *src, long n); |
|---|
| 78 | extern char *strcat(char *dest, const char *app); |
|---|
| 79 | extern char *strrchr(const char *s, int c); |
|---|
| 80 | extern char *strpbrk(const char *s, const char *accept); |
|---|
| 81 | extern char *strstr(const char *s, const char *s2); |
|---|
| 82 | |
|---|
| 83 | extern long strtol(const char *nptr, char **endptr, int base); |
|---|
| 84 | extern unsigned long strtoul(const char *nptr, char **endptr, int base); |
|---|
| 85 | #define atoi(n) strtol((n),NULL,0) |
|---|
| 86 | |
|---|
| 87 | extern int tolower(int c); |
|---|
| 88 | extern int toupper(int c); |
|---|
| 89 | |
|---|
| 90 | extern void *malloc(long size); |
|---|
| 91 | extern void free(void *p); |
|---|
| 92 | extern void *umalloc(long size); |
|---|
| 93 | extern void ufree(void *p); |
|---|
| 94 | |
|---|
| 95 | extern void *memcpy(void *dest, const void *src, long n); |
|---|
| 96 | extern void *memset(void *s, int c, int n); |
|---|
| 97 | extern int memcmp(const void *s1, const void *s2, long n); |
|---|
| 98 | extern void *memchr(const void *s, int c, int n); |
|---|
| 99 | |
|---|
| 100 | extern void SleepTask(long msec); |
|---|
| 101 | extern long taskLock(); |
|---|
| 102 | extern long taskUnlock(); |
|---|
| 103 | |
|---|
| 104 | extern int creat (const char *name, int flags); |
|---|
| 105 | extern int open (const char *name, int flags, int mode ); |
|---|
| 106 | extern int close (int fd); |
|---|
| 107 | extern int write (int fd, const void *buffer, long nbytes); |
|---|
| 108 | extern int read (int fd, void *buffer, long nbytes); |
|---|
| 109 | extern int lseek (int fd, long offset, int whence); |
|---|
| 110 | extern long mkdir(const char *dirname); |
|---|
| 111 | extern long mkdir_if_not_exist(const char *dirname); |
|---|
| 112 | extern int rename(const char *oldname, const char *newname); |
|---|
| 113 | extern int chdir(char *pathname); |
|---|
| 114 | extern int remove(const char *name); |
|---|
| 115 | extern int stat (const char *name, struct stat *pStat); |
|---|
| 116 | |
|---|
| 117 | extern 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. |
|---|
| 122 | typedef 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 | |
|---|
| 134 | extern FILE *fopen(const char *filename, const char *mode); |
|---|
| 135 | extern long fclose(FILE *f); |
|---|
| 136 | extern long fread(void *buf, long elsize, long count, FILE *f); |
|---|
| 137 | extern long fwrite(const void *buf, long elsize, long count, FILE *f); |
|---|
| 138 | extern long fseek(FILE *file, long offset, long whence); |
|---|
| 139 | extern long fflush(FILE *file); |
|---|
| 140 | extern long feof(FILE *file); |
|---|
| 141 | extern long ftell(FILE *file); |
|---|
| 142 | extern char *fgets(char *buf, int n, FILE *f); |
|---|
| 143 | |
|---|
| 144 | extern void msleep(long msec); |
|---|
| 145 | extern long task_lock(); |
|---|
| 146 | extern long task_unlock(); |
|---|
| 147 | extern const char *task_name(int id); |
|---|
| 148 | int task_id_list_get(int *idlist,int size); |
|---|
| 149 | extern 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 |
|---|
| 152 | extern 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 |
|---|
| 168 | struct dirent // Returned from readdir |
|---|
| 169 | { |
|---|
| 170 | char d_name[100]; // We only use the name value |
|---|
| 171 | }; |
|---|
| 172 | |
|---|
| 173 | // Returned from opendir |
|---|
| 174 | typedef struct |
|---|
| 175 | { |
|---|
| 176 | void *cam_DIR; // Camera specific internal DIR structure |
|---|
| 177 | struct dirent dir; // Last info returned from readdir |
|---|
| 178 | } DIR; |
|---|
| 179 | |
|---|
| 180 | extern DIR* opendir (const char* name); |
|---|
| 181 | extern struct dirent* readdir (DIR*); |
|---|
| 182 | extern int closedir (DIR*); |
|---|
| 183 | //extern void rewinddir (DIR*); // Not used |
|---|
| 184 | |
|---|
| 185 | //------------------------------------------------------------------------------------- |
|---|
| 186 | |
|---|
| 187 | struct 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 | |
|---|
| 200 | typedef unsigned long time_t; |
|---|
| 201 | |
|---|
| 202 | extern struct tm * localtime(const time_t *_tod); |
|---|
| 203 | extern struct tm * get_localtime(); |
|---|
| 204 | |
|---|
| 205 | struct utimbuf { |
|---|
| 206 | unsigned long actime; /* set the access time */ |
|---|
| 207 | unsigned long modtime; /* set the modification time */ |
|---|
| 208 | }; |
|---|
| 209 | |
|---|
| 210 | extern int utime(const char *file, struct utimbuf *newTimes); |
|---|
| 211 | extern unsigned long time(unsigned long *timer); |
|---|
| 212 | extern long strftime(char *s, unsigned long maxsize, const char *format, const struct tm *timp); |
|---|
| 213 | extern time_t mktime(struct tm *timp); |
|---|
| 214 | |
|---|
| 215 | extern int abs( int v ); |
|---|
| 216 | |
|---|
| 217 | #endif |
|---|