]>
git.saurik.com Git - apple/security.git/blob - SecurityTool/sharedTool/readline.c
2 * Copyright (c) 2003-2004,2006-2010,2013-2014 Apple Inc. All Rights Reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
21 * @APPLE_LICENSE_HEADER_END@
35 /* Inspects a file's existence and size. Returns a file handle or -1 on failure */
36 int inspect_file_and_size(const char* name
, off_t
*out_off_end
) {
42 fd
= open(name
, O_RDONLY
, 0);
43 } while (fd
== -1 && errno
== EINTR
);
47 fprintf(stderr
, "open %s: %s", name
, strerror(errno
));
52 off_end
= lseek(fd
, 0, SEEK_END
);
55 fprintf(stderr
, "lseek %s, SEEK_END: %s", name
, strerror(errno
));
60 if (off_end
> (unsigned)SIZE_MAX
) {
61 fprintf(stderr
, "file %s too large %llu bytes", name
, off_end
);
67 *out_off_end
= off_end
;
77 /* Read a line from stdin into buffer as a null terminated string. If buffer is
78 non NULL use at most buffer_size bytes and return a pointer to buffer. Otherwise
79 return a newly malloced buffer.
80 if EOF is read this function returns NULL. */
82 readline(char *buffer
, int buffer_size
)
84 int ix
= 0, bytes_malloced
= 0;
89 buffer
= (char *)malloc(bytes_malloced
);
90 buffer_size
= bytes_malloced
;
97 if (ix
== buffer_size
- 1)
101 bytes_malloced
+= bytes_malloced
;
102 buffer
= (char *)realloc(buffer
, bytes_malloced
);
103 buffer_size
= bytes_malloced
;
118 /* 0 terminate buffer. */
124 /* Read the file name into buffer. On return buffer contains a newly
125 malloced buffer or length buffer_size. Return 0 on success and -1 on failure. */
127 read_file(const char *name
, uint8_t **outData
, size_t *outLength
)
135 if( (fd
= inspect_file_and_size(name
, &off_end
)) == -1 ){
140 length
= (size_t)off_end
;
141 buffer
= malloc(length
);
142 if (buffer
== NULL
) {
148 bytes_read
= pread(fd
, buffer
, length
, 0);
149 } while (bytes_read
== -1 && errno
== EINTR
);
151 if (bytes_read
== -1)
153 fprintf(stderr
, "pread %s: %s", name
, strerror(errno
));
157 if (bytes_read
!= (ssize_t
)length
)
159 fprintf(stderr
, "read %s: only read %zu of %zu bytes", name
, bytes_read
, length
);
166 } while (result
== -1 && errno
== EINTR
);
170 fprintf(stderr
, "close %s: %s", name
, strerror(errno
));
174 *outData
= (uint8_t *)buffer
;
186 CFDataRef
copyFileContents(const char *path
) {
187 CFMutableDataRef data
= NULL
;
188 int fd
= open(path
, O_RDONLY
, 0666);
190 fprintf(stderr
, "open %s: %s", path
, strerror(errno
));
194 off_t fsize
= lseek(fd
, 0, SEEK_END
);
195 if (fsize
== (off_t
)-1) {
196 fprintf(stderr
, "lseek %s, 0, SEEK_END: %s", path
, strerror(errno
));
200 if (fsize
> (off_t
)INT32_MAX
) {
201 fprintf(stderr
, "file %s too large %llu bytes", path
, fsize
);
205 data
= CFDataCreateMutable(kCFAllocatorDefault
, (CFIndex
)fsize
);
206 CFDataSetLength(data
, (CFIndex
)fsize
);
207 void *buf
= CFDataGetMutableBytePtr(data
);
208 off_t total_read
= 0;
209 while (total_read
< fsize
) {
212 bytes_read
= pread(fd
, buf
, (size_t)(fsize
- total_read
), total_read
);
213 if (bytes_read
== -1) {
214 fprintf(stderr
, "read %s: %s", path
, strerror(errno
));
217 if (bytes_read
== 0) {
218 fprintf(stderr
, "read %s: unexpected end of file", path
);
221 total_read
+= bytes_read
;
224 if (close(fd
) == -1) {
225 fprintf(stderr
, "close %s: %s", path
, strerror(errno
));
226 /* Failure to close the file isn't fatal. */
232 if (close(fd
) == -1) {
233 fprintf(stderr
, "close %s: %s", path
, strerror(errno
));
242 bool writeFileContents(const char *path
, CFDataRef data
) {
243 int fd
= open(path
, O_WRONLY
| O_CREAT
| O_TRUNC
, 0666);
245 fprintf(stderr
, "open %s: %s", path
, strerror(errno
));
249 const void *buf
= CFDataGetBytePtr(data
);
250 off_t fsize
= CFDataGetLength(data
);
252 off_t total_write
= 0;
253 while (total_write
< fsize
) {
256 bytes_write
= pwrite(fd
, buf
, (size_t)(fsize
- total_write
), total_write
);
257 if (bytes_write
== -1) {
258 fprintf(stderr
, "write %s: %s", path
, strerror(errno
));
261 if (bytes_write
== 0) {
262 fprintf(stderr
, "write %s: unexpected end of file", path
);
265 total_write
+= bytes_write
;
268 if (close(fd
) == -1) {
269 fprintf(stderr
, "close %s: %s", path
, strerror(errno
));
270 /* Failure to close the file isn't fatal. */
276 if (close(fd
) == -1) {
277 fprintf(stderr
, "close %s: %s", path
, strerror(errno
));