summaryrefslogtreecommitdiff
path: root/lib/Output.cc
blob: e9016f9cb7ca53ed5ca3bbbb70ac0b068d667bee (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
#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() ? open(no.to_charp(), O_WRONLY | (O_CREAT * (create ? 1 : 0)) | (O_TRUNC * (trunc ? 1 : 0))
#if defined __linux__ || defined __CYGWIN32__
, 00666
#endif
) : 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);

    struct stat s;

    fstat(GetHandle(), &s);

    date_modif = s.st_mtime;
}

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 {
    struct stat s;

    fstat(GetHandle(), &s);

    return S_ISREG(s.st_mode);    
}

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;
}

#ifdef HOOK_STDS
Stdout_t::Stdout_t() {}
#endif

bool Stdout_t::CanSeek() const {
    return 0;
}

String Stdout_t::GetName() const {
    return "Stdout";
}

#ifdef HOOK_STDS
Stderr_t::Stderr_t() : Handle(dup(2)) {}
#endif

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";
}

Stdout_t Stdout;
Stderr_t Stderr;