Subversion Repositories Kolibri OS

Rev

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