File: fileio.c

package info (click to toggle)
fhist 1.18-1
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd, squeeze, wheezy
  • size: 2,564 kB
  • ctags: 1,332
  • sloc: ansic: 15,161; sh: 4,749; makefile: 1,027; awk: 154; yacc: 101
file content (323 lines) | stat: -rw-r--r-- 8,905 bytes parent folder | download | duplicates (3)
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
/*
 *      fhist - file history and comparison tools
 *      Copyright (C) 1991-1994, 1998-2000, 2002, 2008 Peter Miller
 *
 *      Derived from a work
 *      Copyright (C) 1990 David I. Bell.
 *
 *      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 3 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, see
 *      <http://www.gnu.org/licenses/>.
 *
 * Functions to position within an ascii file and read lines from it.
 */

#include <ac/errno.h>
#include <fcntl.h>
#include <ac/libintl.h>
#include <ac/stdarg.h>
#include <ac/stdio.h>
#include <ac/string.h>
#include <ac/unistd.h>

#include <error_intl.h>
#include <fcheck.h>
#include <fileio.h>
#include <cmalloc.h>
#include <str.h>

#define LINEALLOCSIZE 132       /* allocation size for line buffer */


/*
 * Position absolutely within a file to the indicated byte position,
 * returns a negative value if failed.
 */

void
seekf(FILE *fp, long pos, const char *filename)
{
    if (fseek(fp, pos, SEEK_SET) == -1)
    {
        sub_context_ty  *scp;

        scp = sub_context_new();
        sub_errno_set(scp);
        sub_var_set_charstar(scp, "File_Name", filename);
        sub_var_set_long(scp, "Number", pos);
        fatal_intl(scp, i18n("fseek(\"$filename\", $number): $errno"));
    }
}


/*
 * Skip forwards through a file by the specified number of lines.
 * Returns nonzero on end of file or any error.
 */

int
skipf(FILE *fp, long count, const char *filename)
{
    int             c;

    while (count > 0)
    {
        c = getc(fp);
        if (c == EOF)
        {
            if (ferror(fp))
            {
                sub_context_ty  *scp;

                scp = sub_context_new();
                sub_errno_set(scp);
                sub_var_set_charstar(scp, "File_Name", filename);
                fatal_intl(scp, i18n("read \"$filename\": $errno"));
            }
            return -1;
        }
        if (c == '\n')
            --count;
    }
    return 0;
}


/*
 * Function to read in the next line from a file, no matter how long it is.
 * This returns a pointer to the string, and also indirectly its length.
 * The string is normally returned from a static string, which is reused
 * for each call.  But if keep is non-zero, then the returned string
 * belongs to the caller and must later be freed.  The returned line
 * is ended with a newline and null character, but the returned length
 * does not count the null character.  Returns 0 on an error, with the
 * error stored in the fp structure.
 */

char *
readlinef(FILE *fp, long *retlen, int keep, const char *filename,
    int *is_bin_file)
{
    char            *cp;                /* pointer to character string */
    char            *dp;                /* destination pointer */
    long            cursize;            /* current size */
    long            count;              /* length of data read from file */
    long            totallen;           /* total length of data */
    static char     *linebuffer;        /* common line buffer */
    static long     linelength;         /* total length of line */

    totallen = 0;
    if (linelength == 0)
    {
        /* allocate line buffer */
        linebuffer = r_alloc_and_check(LINEALLOCSIZE + 1);
        linelength = LINEALLOCSIZE;
    }
    cp = linebuffer;
    cursize = linelength;
    for (;;)
    {
        count = readf(fp, cp, cursize, filename, is_bin_file);
        if (count <= 0)
        {
            if (totallen == 0)
                return 0;
            warning_last_line_unterminated(filename);
            count = 1;
            cp[0] = '\n';
        }
        totallen += count;
        if (cp[count - 1] == '\n')
            break;
        linebuffer =
            r_realloc_and_check(linebuffer, linelength + LINEALLOCSIZE + 1);
        cp = linebuffer + totallen;
        linelength += LINEALLOCSIZE;
        cursize = LINEALLOCSIZE;
    }
    if (retlen)
        *retlen = totallen;
    if (!keep)
        dp = linebuffer;
    else
    {
        dp = cm_alloc_and_check(totallen + 1);
        memcpy(dp, linebuffer, (size_t)totallen);
    }
    dp[totallen] = 0;
    return dp;
}


/*
 * Read data from the current position in a file until a newline is found
 * or the supplied buffer is full.  These two cases can be distinguished by
 * checking the last character of the returned data.  Returns the number of
 * characters in the line (including the newline), or -1 if an error or end
 * of file in the middle of the line is found.  Line is terminated by a null
 * character if space allows.  Returns zero if the end of file occurs at
 * the front of the line.
 */

long
readf(FILE *fp, char *buf, long size, const char *filename, int *is_bin_file)
{
    char            *bp;
    char            *ep;
    int             c;

    bp = buf;
    ep = buf + size;
    while (bp < ep)
    {
        c = getc(fp);
        if (c == EOF)
        {
            if (ferror(fp))
            {
                sub_context_ty  *scp;

                scp = sub_context_new();
                sub_errno_set(scp);
                sub_var_set_charstar(scp, "File_Name", filename);
                fatal_intl(scp, i18n("read \"$filename\": $errno"));
            }
            if (bp != buf)
                break;
            return -1;
        }
        if (c == 0)
        {
            c = 0x80;
            *is_bin_file = 1;
        }
        *bp++ = c;
        if (c == '\n')
            break;
    }
    return (bp - buf);
}


/*
 * Write data at the current position in a file.
 * This buffers the data in the current block, thus flushing is needed later.
 * Prints a fatal error message and exits on errors.
 */

void
writefx(FILE *fp, char *buf, long size, const char *filename)
{
    fwrite(buf, 1, size, fp);
    if (ferror(fp))
    {
        sub_context_ty  *scp;

        scp = sub_context_new();
        sub_errno_set(scp);
        sub_var_set_charstar(scp, "File_Name", filename);
        fatal_intl(scp, i18n("write \"$filename\": $errno"));
    }
}


/*
 * Function to copy data from one file to another.  Data is copied from
 * the current position of the input file to the current position of the
 * output file until end of file is reached on the input file, or until
 * the specified number of characters has been read.  Minus one means
 * there is no limit to the amount to be copied.  Line boundaries and
 * space expansions are ignored.  Returns nonzero on errors.
 */

int
copyf(FILE *ip, FILE *op, long count, const char *ifn, const char *ofn)
{
    char            buffer[1 << 10];
    long            len;

    while (count)
    {
        if (count < 0 || count > sizeof(buffer))
            len = sizeof(buffer);
        else
            len = count;
        len = fread(buffer, 1, len, ip);
        if (len < 0)
            return EOF;
        if (len == 0)
            break;
        fwrite(buffer, 1, len, op);
        if (ferror(op))
            return EOF;
        if (count >= 0)
            count -= len;
    }
    return 0;
}

void
copyfx(FILE *ip, FILE *op, long count, const char *ifn, const char *ofn)
{
    char            buffer[1 << 10];
    long            len;
    sub_context_ty  *scp;

    while (count)
    {
        if (count < 0 || count > sizeof(buffer))
            len = sizeof(buffer);
        else
            len = count;
        len = fread(buffer, 1, len, ip);
        if (len < 0)
        {
            scp = sub_context_new();
            sub_errno_set(scp);
            sub_var_set_charstar(scp, "File_Name", ifn);
            fatal_intl(scp, i18n("read \"$filename\": $errno"));
        }
        if (len == 0)
            break;
        fwrite(buffer, 1, len, op);
        if (ferror(op))
        {
            scp = sub_context_new();
            sub_errno_set(scp);
            sub_var_set_charstar(scp, "File_Name", ofn);
            fatal_intl(scp, i18n("write \"$filename\": $errno"));
        }
        if (count >= 0)
            count -= len;
    }
    fflush_and_check(op, ofn);
}


/*
 * Function to type out the rest of an input file to the terminal.
 * It is assumed that we are positioned at the beginning of a line.
 */

int
typef(FILE *fp, const char *filename)
{
    return copyf(fp, stdout, -1L, filename, gettext("standard output"));
}


void
typefx(FILE *fp, const char *filename)
{
    copyfx(fp, stdout, -1L, filename, gettext("standard output"));
}