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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
|
/*
* Baltisot
* Copyright (C) 1999-2003 Nicolas "Pixel" Noble
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
/* $Id: Output.cc,v 1.23 2005-02-17 08:33:50 pixel Exp $ */
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#else
#include <io.h>
#endif
#include <fcntl.h>
#include "Output.h"
#include "Exceptions.h"
#include "gettext.h"
#ifndef S_ISREG
#define S_ISREG(x) 1
#endif
Output::Output(String no, int create, int trunc) throw (GeneralException) :
Handle(no.strlen() ? wrapopen(no.to_charp(), create, trunc) : dup(1)),
n(no) {
if (GetHandle() < 0) {
throw IOGeneral(String(_("Error opening file ")) + no + _(" for writing: ") + strerror(errno));
}
size = lseek(GetHandle(), 0, SEEK_END);
lseek(GetHandle(), 0, SEEK_SET);
#ifndef __MIPSEL__
struct stat s;
fstat(GetHandle(), &s);
date_modif = s.st_mtime;
#else
date_modif = 0;
#endif
}
int Output::wrapopen(const String & n, int create, int trunc) {
hFile = 0;
#ifndef _WIN32
return open(n.to_charp(), (create ? O_CREAT : 0) |
(trunc ? O_TRUNC : 0) | O_WRONLY, 00666);
#else
#ifdef NO_HFILE
return _creat(n.to_charp(), _S_IREAD | _S_IWRITE);
#else
DWORD dwCreationDisposition;
switch ((create ? 1 : 0) | (trunc ? 2 : 0)) {
case 0: // no creation, no trunc
dwCreationDisposition = OPEN_EXISTING;
break;
case 1: // creation, no trunc if existing
dwCreationDisposition = OPEN_ALWAYS;
break;
case 2: // no creation, trunc of existing file
dwCreationDisposition = TRUNCATE_EXISTING;
break;
case 3: // creation, truc if existing
dwCreationDisposition = CREATE_ALWAYS;
break;
}
hFile = CreateFile(
n.to_charp(),
GENERIC_WRITE,
FILE_SHARE_READ,
0,
dwCreationDisposition,
FILE_ATTRIBUTE_ARCHIVE,
0);
return _open_osfhandle((INT_PTR) hFile, O_WRONLY | O_BINARY);
#endif
#endif
}
Output::Output(const Output & o) : Handle(o), n(o.n) {
}
bool Output::CanWrite() const {
return 1;
}
bool Output::CanRead() const {
return 0;
}
bool Output::CanSeek() const {
#ifdef __MIPSEL__
return true;
#else
struct stat s;
fstat(GetHandle(), &s);
return S_ISREG(s.st_mode);
#endif
}
off_t Output::seek(off_t offset, int whence) throw (GeneralException) {
if ((itell = lseek(GetHandle(), offset, whence)) < 0) {
throw IOGeneral(String(_("Error seeking file ")) + n + _(": ") + strerror(errno));
}
#ifdef PARANOID_SEEK
if (itell != lseek(GetHandle(), 0, SEEK_CUR)) {
throw IOGeneral(String(_("Error seeking file ")) + n + _(": the position does not match"));
}
#endif
return itell;
}
String Output::GetName() const {
return n;
}
Stdout_t::Stdout_t() {}
bool Stdout_t::CanSeek() const {
return 0;
}
String Stdout_t::GetName() const {
return "Stdout";
}
Stderr_t::Stderr_t() : Handle(dup(2)) {}
bool Stderr_t::CanWrite() const {
return 1;
}
bool Stderr_t::CanRead() const {
return 0;
}
bool Stderr_t::CanSeek() const {
return 0;
}
String Stderr_t::GetName() const {
return "Stderr";
}
#ifdef HOOK_STDS
Stdout_t Stdout;
Stderr_t Stderr;
#endif
|