-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinput.c
More file actions
125 lines (113 loc) · 3.43 KB
/
Copy pathinput.c
File metadata and controls
125 lines (113 loc) · 3.43 KB
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
/* $Id: input.c,v 1.1 2022/07/29 02:47:07 dave Exp dave $
input.c - Read contents of container file.
Copyright (C) 2008 David Shepperd
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/>.
*/
#include "rtpip.h"
/**
* @file input.c
* Read contents of container file.
*/
/**
* Read input file and do any crlf processing.
* @param options - pointer to options.
* @return 0 if success, 1 if failure
*/
int readInpFile(Options_t *options, const char *fileName)
{
int retv, inBufSize;
struct stat st;
FILE *inp;
InHandle_t *ihp;
retv = stat(fileName, &st);
if ( retv )
{
fprintf(stderr, "Unable to stat '%s': %s\n", fileName, strerror(errno));
return 1;
}
ihp = &options->iHandle;
ihp->fileTimeStamp = st.st_ctime;
/* Round up buffer size to multiple of 512 */
inBufSize = (st.st_size + BLKSIZ - 1) & -BLKSIZ;
if ( inBufSize > ihp->inFileBufSize )
{
ihp->inFileBuf = (char *)realloc(ihp->inFileBuf, inBufSize);
if ( !ihp->inFileBuf )
{
fprintf(stderr, "Unable to allocate %d bytes for input file: %s\n",
retv, strerror(errno));
return 1;
}
ihp->inFileBufSize = inBufSize;
}
inp = fopen(fileName, "rb");
if ( !inp )
{
fprintf(stderr, "Error opening '%s' for input: %s\n",
fileName, strerror(errno));
return 1;
}
retv = fread(ihp->inFileBuf, 1, st.st_size, inp);
if ( retv != st.st_size )
{
fprintf(stderr, "Error reading '%s'. Expected %ld bytes, got %d: %s\n",
fileName, st.st_size, retv, strerror(errno));
fclose(inp);
return 1;
}
fclose(inp);
if ( (options->inOpts & INOPTS_ASC) )
{
char *oBuf, *src, *dst;
int oBufSize, hist;
/* Copying an ASCII file.
All files have to be a multple of BLKSIZ (512). Although not strictly necessary (could be an exact file size without a trailing null),
we always null terminate the file so leave room for one additional byte */
oBufSize = (st.st_size * 2 + 1 + BLKSIZ - 1) & -BLKSIZ;
oBuf = (char *)calloc(oBufSize, 1);
if ( !oBuf )
{
fprintf(stderr, "Unable to allocate %d bytes for input file converted to crlf: %s\n",
oBufSize, strerror(errno));
return 1;
}
hist = 0;
dst = oBuf;
src = ihp->inFileBuf;
while ( src < ihp->inFileBuf + st.st_size )
{
/* Convert all lone lf's to crlf's */
if ( *src == '\n' && hist != '\r' )
*dst++ = '\r';
hist = *src++;
*dst++ = hist;
}
retv = (dst - oBuf);
free(ihp->inFileBuf);
ihp->inFileBuf = oBuf;
ihp->inFileBufSize = oBufSize;
if ( (options->cmdOpts & CMDOPT_DBG_NORMAL) )
{
printf("readInpFile: expanded '%s' from %ld bytes (%ld blocks) to %d bytes (%d blocks).\n",
fileName,
st.st_size, (st.st_size + BLKSIZ - 1) / BLKSIZ,
retv, (retv + BLKSIZ - 1) / BLKSIZ);
}
}
else if ( inBufSize - st.st_size )
{
/* pad file to multiple of BLKSIZ with 0's */
memset(ihp->inFileBuf + st.st_size, 0, inBufSize - st.st_size);
}
ihp->fileBlks = (retv + BLKSIZ - 1) / BLKSIZ;
return 0;
}