summaryrefslogtreecommitdiffstats
path: root/win/kde_file_win.c
blob: 269f0fab61f7ef49244009429ac7c72a703920a7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
/*
   This file is part of the KDE libraries
   Copyright (C) 2004 Jaroslaw Staniek <js@iidea.pl>

   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Library General Public
   License version 2 as published by the Free Software Foundation.

   This library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Library General Public License for more details.

   You should have received a copy of the GNU Library General Public License
   along with this library; see the file COPYING.LIB.  If not, write to
   the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
   Boston, MA 02110-1301, USA.
*/

#include <sys/stat.h>
#include <stdarg.h>

#include "kde_file_win.h"

KDEWIN32_EXPORT int kdewin32_stat(const char *file_name, struct stat *buf)
{
	char fixed_file_name[4];
	char *fixed_file_name2;
	int len, result;
	len = strlen(file_name);
	if ((len==2 || len==3) && file_name[1]==':' && isalpha(file_name[0])) {
		/* 1) */
		if (len==3)
			return stat(file_name, buf);
		strncpy(fixed_file_name, file_name, len);
		fixed_file_name[2]='\\';
		fixed_file_name[3]=0;
		return stat(fixed_file_name, buf);
	}
	if (len>1 && (file_name[len-1]=='\\' || file_name[len-1]=='/')) {
		/* 2) */
		fixed_file_name2 = strndup(file_name, len);
		fixed_file_name2[len-1]=0;
		result = stat(fixed_file_name2, buf);
		free(fixed_file_name2);
		return result;
	}
//TODO: is stat("/") ok?
	return stat(file_name, buf);
}

KDEWIN32_EXPORT int kdewin32_lstat(const char *file_name, struct stat *buf)
{
	return kdewin32_stat(file_name, buf);
}

/** @internal */
int kdewin32_fix_flags(int flags)
{
	if ((flags & O_TEXT) == 0 && (flags & O_BINARY) == 0)
		return flags | O_BINARY;
	return flags;
}

/*KDEWIN32_EXPORT int kdewin32_open(const char *path, int flags)
{
	return open(path, kdewin32_fix_flags(flags));
}*/

KDEWIN32_EXPORT int kdewin32_open(const char *path, int flags, ... /*mode_t mode*/)
{
	mode_t mode = 0;
	if (flags & O_CREAT) {
		va_list list;
		va_start(list, flags);
		mode = (mode_t)va_arg(list, int);
		va_end(list);
	}
	return open(path, kdewin32_fix_flags(flags), mode);
}

/** @internal */
int kdewin32_fix_mode_string(char *fixed_mode, const char *mode)
{
	if (strlen(mode)<1 || strlen(mode)>3)
		return 1;
	
	strncpy(fixed_mode, mode, 3);
	if (fixed_mode[0]=='b' || fixed_mode[1]=='b' || fixed_mode[0]=='t' || fixed_mode[1]=='t')
		return 0;
	/* no 't' or 'b': append 'b' */
	if (fixed_mode[1]=='+') {
		fixed_mode[1]='b';
		fixed_mode[2]='+';
		fixed_mode[3]=0;
	}
	else {
		fixed_mode[1]='b';
		fixed_mode[2]=0;
	}
	return 0;
}

KDEWIN32_EXPORT FILE *kdewin32_fopen(const char *path, const char *mode)
{
	char fixed_mode[4];
	if (0!=kdewin32_fix_mode_string(fixed_mode, mode))
		return 0;
	return fopen(path, fixed_mode);
}

KDEWIN32_EXPORT FILE *kdewin32_fdopen(int fd, const char *mode)
{
	char fixed_mode[4];
	if (0!=kdewin32_fix_mode_string(fixed_mode, mode))
		return 0;
	return fdopen(fd, fixed_mode);
}

KDEWIN32_EXPORT FILE *kdewin32_freopen(const char *path, const char *mode, FILE *stream)
{
	char fixed_mode[4];
	if (0!=kdewin32_fix_mode_string(fixed_mode, mode))
		return 0;
	return freopen(path, fixed_mode, stream);
}

KDEWIN32_EXPORT int kdewin32_rename(const char *src, const char *dest)
{
	if (0==access(dest, 0/*exists*/)
		&& 0 != remove(dest))
		return -1;
	return rename(src, dest);
}

KDEWIN32_EXPORT int kdewin32_mkdir(const char *path, mode_t mode)
{
	return mkdir(path);
}