Subversion Repositories Kolibri OS

Rev

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

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