Subversion Repositories Kolibri OS

Rev

Rev 6433 | Go to most recent revision | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

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