Subversion Repositories Kolibri OS

Rev

Rev 6433 | Go to most recent revision | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 6433 Rev 7172
Line 5... Line 5...
5
extern char __argv;
5
extern char __argv;
6
extern char __path;
6
extern char __path;
Line 7... Line 7...
7
 
7
 
Line 8... Line -...
8
int errno = 0;
-
 
9
 
8
int errno = 0;
Line 10... Line 9...
10
 
9
 
11
const char* getfullpath(const char *path){
-
 
12
 
-
 
13
        int i,j,relpath_pos,localpath_size;
10
const char* getfullpath(const char *path){
14
        int filename_size;
11
 
-
 
12
        int  relpath_pos, localpath_size;
Line 15... Line -...
15
        char local_path;
-
 
16
        char *programpath;
13
        char *programpath;
17
        char *newpath;
-
 
18
 
-
 
19
        i=0;
-
 
20
        local_path=1; //enable local path
14
        char *newpath;
21
        while((*(path+i)!='\0') || (*(path+i)!=0))
-
 
22
        {
15
        char *prgname;
23
                if (*(path+i)=='.')
-
 
24
                {
-
 
25
                        if (*(path+i+1)=='/')
-
 
26
                        {       //detected relative path
-
 
27
                                relpath_pos=i+2;
16
 
-
 
17
        if (path[0] == '/') /* root */
28
                                local_path=0;
18
        {
29
                                break;
19
            return(strdup(path)); /* dup need as free in fclose() */
30
                        }
20
        }
31
                }
21
 
32
                if (*(path+i)=='/')
-
 
33
                {       //disabple local path
22
        relpath_pos = 0;
34
                        local_path=0;
23
        if (path[0] == '.' && path[1] == '/')
35
                        return(path);
-
 
Line 36... Line 24...
36
                }
24
        {
Line 37... Line -...
37
                i++;
-
 
38
        }
-
 
39
        filename_size=i;
-
 
40
 
25
            //detected relative path, begins with ./
41
        programpath=&__path;
26
            relpath_pos=2;
42
 
-
 
43
        if (local_path==1)
-
 
44
        {
-
 
45
                i=FILENAME_MAX;
-
 
46
                //find local path of program
-
 
47
                while(*(programpath+i)!='/')
-
 
48
                {
-
 
49
                        i--;
-
 
50
                }
27
        }
51
                localpath_size=i;
-
 
Line 52... Line -...
52
                newpath=malloc(FILENAME_MAX);
-
 
53
                if(!newpath)
-
 
54
                {
-
 
55
                    errno = E_NOMEM;
28
 
56
                    return NULL;
-
 
57
                }
-
 
58
 
-
 
59
                //copy local path to the new path
-
 
60
                for(i=0;i<=localpath_size;i++)
-
 
61
                {
-
 
62
                        *(newpath+i)=*(programpath+i);
-
 
63
                }
-
 
Line 64... Line -...
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;
29
        programpath=&__path;
80
       i=0;
30
 
81
       //find file name size
31
        //if we here than path is a relative or local
82
       while((*(path+relpath_pos+i)!='\0') || (*(path+relpath_pos+i)!=0))
32
        prgname = strrchr(programpath, '/');
83
       {
33
        if (!prgname) return strdup(path);
84
                i++;
34
 
85
       }
35
        localpath_size = prgname - programpath + 1;
86
       filename_size=i;
36
 
87
       newpath=malloc(FILENAME_MAX);
-
 
88
        if(!newpath)
37
        newpath = malloc(FILENAME_MAX);
89
        {
38
        if(!newpath)
90
            errno = E_NOMEM;
39
        {
91
            return NULL;
-
 
92
        }
-
 
93
        //copy local path to the new path
40
            errno = E_NOMEM;
94
       for(i=0;i<=localpath_size;i++)
41
            return NULL;
95
       {
42
        }
96
                *(newpath+i)=*(programpath+i);
43
        //copy local path to the new path
Line -... Line 44...
-
 
44
        strncpy(newpath, programpath, localpath_size);
97
       }
45
        newpath[localpath_size] = 0;
98
       //copy filename to the new path
46
 
99
       for(i=0;i
47
        //copy filename to the new path
100
       {
48
        strcpy(newpath + localpath_size, path + relpath_pos);
-
 
49
 
-
 
50
        return(newpath);
101
                *(newpath+localpath_size+1+i)=*(path+relpath_pos+i);
51
}
102
       }
52
 
103
       return(newpath);
53
 
104
}
54
 
105
 
55
FILE* fopen(const char* filename, const char *mode)
Line 139... Line 89...
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
        }
Line 159... Line 122...
159
 
122
 
160
	if ((imode==FILE_OPEN_READ) || (imode==FILE_OPEN_APPEND))
123
	if ((imode==FILE_OPEN_READ) || (imode==FILE_OPEN_APPEND))
-
 
124
	{
-
 
125
		if (sz > 0) /*already got*/
-
 
126
			res->filesize = sz;
161
	{
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;