Rev 6433 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
647 | andrew_pro | 1 | #include |
2 | #include |
||
3 | #include |
||
4 | |||
5 | extern char __argv; |
||
6 | extern char __path; |
||
7 | |||
6433 | siemargl | 8 | int errno = 0; |
9 | |||
647 | andrew_pro | 10 | const char* getfullpath(const char *path){ |
11 | |||
7172 | siemargl | 12 | int relpath_pos, localpath_size; |
647 | andrew_pro | 13 | char *programpath; |
14 | char *newpath; |
||
7172 | siemargl | 15 | char *prgname; |
647 | andrew_pro | 16 | |
7172 | siemargl | 17 | if (path[0] == '/') /* root */ |
647 | andrew_pro | 18 | { |
7172 | siemargl | 19 | return(strdup(path)); /* dup need as free in fclose() */ |
647 | andrew_pro | 20 | } |
21 | |||
7172 | siemargl | 22 | relpath_pos = 0; |
23 | if (path[0] == '.' && path[1] == '/') |
||
24 | { |
||
25 | //detected relative path, begins with ./ |
||
26 | relpath_pos=2; |
||
27 | } |
||
28 | |||
647 | andrew_pro | 29 | programpath=&__path; |
30 | |||
7172 | siemargl | 31 | //if we here than path is a relative or local |
32 | prgname = strrchr(programpath, '/'); |
||
33 | if (!prgname) return strdup(path); |
||
6433 | siemargl | 34 | |
7172 | siemargl | 35 | localpath_size = prgname - programpath + 1; |
647 | andrew_pro | 36 | |
7172 | siemargl | 37 | newpath = malloc(FILENAME_MAX); |
6433 | siemargl | 38 | if(!newpath) |
39 | { |
||
40 | errno = E_NOMEM; |
||
41 | return NULL; |
||
42 | } |
||
647 | andrew_pro | 43 | //copy local path to the new path |
7172 | siemargl | 44 | strncpy(newpath, programpath, localpath_size); |
45 | newpath[localpath_size] = 0; |
||
46 | |||
47 | //copy filename to the new path |
||
48 | strcpy(newpath + localpath_size, path + relpath_pos); |
||
49 | |||
50 | return(newpath); |
||
647 | andrew_pro | 51 | } |
52 | |||
53 | |||
7172 | siemargl | 54 | |
647 | andrew_pro | 55 | FILE* fopen(const char* filename, const char *mode) |
56 | { |
||
57 | FILE* res; |
||
7172 | siemargl | 58 | int imode, sz = -1; |
59 | char *fullname; |
||
60 | |||
647 | andrew_pro | 61 | imode=0; |
62 | if (*mode=='r') |
||
63 | { |
||
64 | imode=FILE_OPEN_READ; |
||
65 | mode++; |
||
66 | }else if (*mode=='w') |
||
67 | { |
||
68 | imode=FILE_OPEN_WRITE; |
||
69 | mode++; |
||
70 | }else if (*mode=='a') |
||
71 | { |
||
72 | imode=FILE_OPEN_APPEND; |
||
73 | mode++; |
||
74 | }else |
||
75 | return 0; |
||
6433 | siemargl | 76 | if (*mode=='+') |
77 | { |
||
78 | imode|=FILE_OPEN_PLUS; |
||
79 | mode++; |
||
80 | } |
||
647 | andrew_pro | 81 | if (*mode=='t') |
82 | { |
||
83 | imode|=FILE_OPEN_TEXT; |
||
84 | mode++; |
||
85 | }else if (*mode=='b') |
||
86 | mode++; |
||
87 | if (*mode=='+') |
||
88 | { |
||
89 | imode|=FILE_OPEN_PLUS; |
||
90 | mode++; |
||
91 | } |
||
92 | if (*mode!=0) |
||
6433 | siemargl | 93 | return NULL; |
7172 | siemargl | 94 | |
95 | fullname = (char*)getfullpath(filename); |
||
96 | if ((imode & 3) == FILE_OPEN_READ && fullname) /* check existense */ |
||
97 | { |
||
98 | sz = _ksys_get_filesize(fullname); |
||
99 | if (sz < 0) |
||
100 | { |
||
101 | free(fullname); |
||
102 | errno = sz; |
||
103 | return NULL; |
||
104 | } |
||
105 | } |
||
106 | |||
107 | res = malloc(sizeof(FILE)); |
||
6433 | siemargl | 108 | if (res) |
109 | { |
||
110 | res->buffer=malloc(BUFSIZ); |
||
111 | res->buffersize=BUFSIZ; |
||
112 | res->filesize=0; |
||
113 | res->filepos=0; |
||
114 | res->mode=imode; |
||
7172 | siemargl | 115 | res->filename=fullname; |
6433 | siemargl | 116 | } |
117 | if(!res || !res->buffer || !res->filename) |
||
118 | { |
||
119 | errno = E_NOMEM; |
||
120 | return NULL; |
||
121 | } |
||
647 | andrew_pro | 122 | |
123 | if ((imode==FILE_OPEN_READ) || (imode==FILE_OPEN_APPEND)) |
||
124 | { |
||
7172 | siemargl | 125 | if (sz > 0) /*already got*/ |
126 | res->filesize = sz; |
||
127 | else |
||
128 | res->filesize=_ksys_get_filesize(res->filename); |
||
647 | andrew_pro | 129 | } |
130 | return res; |
||
131 | }> |