Rev 6412 | 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 | |||
10 | |||
647 | andrew_pro | 11 | const char* getfullpath(const char *path){ |
12 | |||
13 | int i,j,relpath_pos,localpath_size; |
||
14 | int filename_size; |
||
15 | char local_path; |
||
16 | char *programpath; |
||
17 | char *newpath; |
||
18 | |||
19 | i=0; |
||
20 | local_path=1; //enable local path |
||
21 | while((*(path+i)!='\0') || (*(path+i)!=0)) |
||
22 | { |
||
23 | if (*(path+i)=='.') |
||
24 | { |
||
25 | if (*(path+i+1)=='/') |
||
26 | { //detected relative path |
||
27 | relpath_pos=i+2; |
||
28 | local_path=0; |
||
29 | break; |
||
30 | } |
||
31 | } |
||
32 | if (*(path+i)=='/') |
||
33 | { //disabple local path |
||
34 | local_path=0; |
||
35 | return(path); |
||
36 | } |
||
37 | i++; |
||
38 | } |
||
39 | filename_size=i; |
||
40 | |||
41 | programpath=&__path; |
||
42 | |||
43 | if (local_path==1) |
||
44 | { |
||
6433 | siemargl | 45 | i=FILENAME_MAX; |
647 | andrew_pro | 46 | //find local path of program |
47 | while(*(programpath+i)!='/') |
||
48 | { |
||
49 | i--; |
||
50 | } |
||
51 | localpath_size=i; |
||
6433 | siemargl | 52 | newpath=malloc(FILENAME_MAX); |
53 | if(!newpath) |
||
54 | { |
||
55 | errno = E_NOMEM; |
||
56 | return NULL; |
||
57 | } |
||
58 | |||
647 | andrew_pro | 59 | //copy local path to the new path |
60 | for(i=0;i<=localpath_size;i++) |
||
61 | { |
||
62 | *(newpath+i)=*(programpath+i); |
||
63 | } |
||
64 | //copy filename to the new path |
||
65 | for(i=0;i |
||
66 | { |
||
67 | *(newpath+localpath_size+1+i)=*(path+i); |
||
68 | } |
||
69 | return(newpath); |
||
70 | } |
||
71 | |||
72 | //if we here than path is a relative |
||
6433 | siemargl | 73 | i=FILENAME_MAX; |
647 | andrew_pro | 74 | //find local path of program |
75 | while(*(programpath+i)!='/') |
||
76 | { |
||
77 | i--; |
||
78 | } |
||
79 | localpath_size=i; |
||
80 | i=0; |
||
81 | //find file name size |
||
82 | while((*(path+relpath_pos+i)!='\0') || (*(path+relpath_pos+i)!=0)) |
||
83 | { |
||
84 | i++; |
||
85 | } |
||
86 | filename_size=i; |
||
6433 | siemargl | 87 | newpath=malloc(FILENAME_MAX); |
88 | if(!newpath) |
||
89 | { |
||
90 | errno = E_NOMEM; |
||
91 | return NULL; |
||
92 | } |
||
647 | andrew_pro | 93 | //copy local path to the new path |
94 | for(i=0;i<=localpath_size;i++) |
||
95 | { |
||
96 | *(newpath+i)=*(programpath+i); |
||
97 | } |
||
98 | //copy filename to the new path |
||
99 | for(i=0;i |
||
100 | { |
||
101 | *(newpath+localpath_size+1+i)=*(path+relpath_pos+i); |
||
102 | } |
||
103 | return(newpath); |
||
104 | } |
||
105 | |||
106 | |||
107 | FILE* fopen(const char* filename, const char *mode) |
||
108 | { |
||
109 | FILE* res; |
||
110 | int imode; |
||
111 | imode=0; |
||
112 | if (*mode=='r') |
||
113 | { |
||
114 | imode=FILE_OPEN_READ; |
||
115 | mode++; |
||
116 | }else if (*mode=='w') |
||
117 | { |
||
118 | imode=FILE_OPEN_WRITE; |
||
119 | mode++; |
||
120 | }else if (*mode=='a') |
||
121 | { |
||
122 | imode=FILE_OPEN_APPEND; |
||
123 | mode++; |
||
124 | }else |
||
125 | return 0; |
||
6433 | siemargl | 126 | if (*mode=='+') |
127 | { |
||
128 | imode|=FILE_OPEN_PLUS; |
||
129 | mode++; |
||
130 | } |
||
647 | andrew_pro | 131 | if (*mode=='t') |
132 | { |
||
133 | imode|=FILE_OPEN_TEXT; |
||
134 | mode++; |
||
135 | }else if (*mode=='b') |
||
136 | mode++; |
||
137 | if (*mode=='+') |
||
138 | { |
||
139 | imode|=FILE_OPEN_PLUS; |
||
140 | mode++; |
||
141 | } |
||
142 | if (*mode!=0) |
||
6433 | siemargl | 143 | return NULL; |
647 | andrew_pro | 144 | res=malloc(sizeof(FILE)); |
6433 | siemargl | 145 | if (res) |
146 | { |
||
147 | res->buffer=malloc(BUFSIZ); |
||
148 | res->buffersize=BUFSIZ; |
||
149 | res->filesize=0; |
||
150 | res->filepos=0; |
||
151 | res->mode=imode; |
||
152 | res->filename=(char*)getfullpath(filename); |
||
153 | } |
||
154 | if(!res || !res->buffer || !res->filename) |
||
155 | { |
||
156 | errno = E_NOMEM; |
||
157 | return NULL; |
||
158 | } |
||
647 | andrew_pro | 159 | |
160 | if ((imode==FILE_OPEN_READ) || (imode==FILE_OPEN_APPEND)) |
||
161 | { |
||
162 | res->filesize=_ksys_get_filesize(res->filename); |
||
163 | } |
||
164 | return res; |
||
165 | } |