-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwriter.c
152 lines (124 loc) · 3.81 KB
/
writer.c
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
/*
* This file is part of libsysperf
*
* Copyright (C) 2001, 2004-2007 by Nokia Corporation.
*
* Contact: Eero Tamminen <[email protected]>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* version 2 as published by the Free Software Foundation.
*
* 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., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA
*
*/
/* ========================================================================= *
* File: writer.c
*
* Author: Simo Piiroinen
*
* -------------------------------------------------------------------------
*
* History:
*
* 22-Jun-2005 Simo Piiroinen
* - structures & prototypes moved to writer.h
* - initial version
* ========================================================================= */
#include <unistd.h>
#include <string.h>
#include <fcntl.h>
// QUARANTINE #define MSG_DISABLE_DEBUG 0
#include "msg.h"
#include "writer.h"
/* ------------------------------------------------------------------------- *
* writer_flush
* ------------------------------------------------------------------------- */
void writer_flush(writer_t *self)
{
// QUARANTINE msg_debug("%s: %s\n", __FUNCTION__, self->wr_path);
if( self->wr_curr != self->wr_head )
{
size_t size = self->wr_curr - self->wr_head;
msg_debug("%s: %s -> %d bytes\n", __FUNCTION__, self->wr_path, size);
if( write(self->wr_file, self->wr_head, size) != size )
{
msg_fatal("write error %s\n", self->wr_path);
}
self->wr_curr = self->wr_head;
}
}
/* ------------------------------------------------------------------------- *
* writer_detach
* ------------------------------------------------------------------------- */
void writer_detach(writer_t *self)
{
// QUARANTINE msg_debug("%s: %s\n", __FUNCTION__, self->wr_path);
if( self->wr_file != -1 )
{
writer_flush(self);
close(self->wr_file);
self->wr_file = -1;
}
if( self->wr_path != 0 )
{
free(self->wr_path);
self->wr_path = 0;
}
}
/* ------------------------------------------------------------------------- *
* writer_attach
* ------------------------------------------------------------------------- */
int writer_attach(writer_t *self, const char *path)
{
// QUARANTINE msg_debug("%s: %s\n", __FUNCTION__, self->wr_path);
int err = -1;
writer_detach(self);
self->wr_path = strdup(path ? path : "<stdout>");
if( path == 0 )
{
self->wr_file = STDOUT_FILENO;
}
else
{
if( (self->wr_file = open(path, O_WRONLY|O_CREAT|O_TRUNC, 0666)) == -1 )
{
msg_error("can't open '%s' for writing\n", path);
goto cleanup;
}
}
err = 0;
cleanup:
// QUARANTINE msg_debug("%s: %s -> %d\n", __FUNCTION__, self->wr_path, err);
return err;
}
/* ------------------------------------------------------------------------- *
* writer_create
* ------------------------------------------------------------------------- */
writer_t *writer_create(void)
{
// QUARANTINE msg_debug("%s\n", __FUNCTION__);
writer_t *self = calloc(1, sizeof *self);
writer_ctor(self);
return self;
}
/* ------------------------------------------------------------------------- *
* writer_delete
* ------------------------------------------------------------------------- */
void writer_delete(writer_t *self)
{
// QUARANTINE msg_debug("%s: %s\n", __FUNCTION__, self->wr_path);
if( self != 0 )
{
writer_dtor(self);
free(self);
}
}