]>
git.saurik.com Git - apple/security.git/blob - OSX/utilities/SecurityTool/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
);
144 bytes_read
= pread(fd
, buffer
, length
, 0);
145 } while (bytes_read
== -1 && errno
== EINTR
);
147 if (bytes_read
== -1)
149 fprintf(stderr
, "pread %s: %s", name
, strerror(errno
));
153 if (bytes_read
!= (ssize_t
)length
)
155 fprintf(stderr
, "read %s: only read %zu of %zu bytes", name
, bytes_read
, length
);
162 } while (result
== -1 && errno
== EINTR
);
166 fprintf(stderr
, "close %s: %s", name
, strerror(errno
));
170 *outData
= (uint8_t *)buffer
;
182 CFDataRef
copyFileContents(const char *path
) {
183 CFMutableDataRef data
= NULL
;
184 int fd
= open(path
, O_RDONLY
, 0666);
186 fprintf(stderr
, "open %s: %s", path
, strerror(errno
));
190 off_t fsize
= lseek(fd
, 0, SEEK_END
);
191 if (fsize
== (off_t
)-1) {
192 fprintf(stderr
, "lseek %s, 0, SEEK_END: %s", path
, strerror(errno
));
196 if (fsize
> (off_t
)INT32_MAX
) {
197 fprintf(stderr
, "file %s too large %llu bytes", path
, fsize
);
201 data
= CFDataCreateMutable(kCFAllocatorDefault
, (CFIndex
)fsize
);
202 CFDataSetLength(data
, (CFIndex
)fsize
);
203 void *buf
= CFDataGetMutableBytePtr(data
);
204 off_t total_read
= 0;
205 while (total_read
< fsize
) {
208 bytes_read
= pread(fd
, buf
, (size_t)(fsize
- total_read
), total_read
);
209 if (bytes_read
== -1) {
210 fprintf(stderr
, "read %s: %s", path
, strerror(errno
));
213 if (bytes_read
== 0) {
214 fprintf(stderr
, "read %s: unexpected end of file", path
);
217 total_read
+= bytes_read
;
220 if (close(fd
) == -1) {
221 fprintf(stderr
, "close %s: %s", path
, strerror(errno
));
222 /* Failure to close the file isn't fatal. */
228 if (close(fd
) == -1) {
229 fprintf(stderr
, "close %s: %s", path
, strerror(errno
));
238 bool writeFileContents(const char *path
, CFDataRef data
) {
239 int fd
= open(path
, O_WRONLY
| O_CREAT
| O_TRUNC
, 0666);
241 fprintf(stderr
, "open %s: %s", path
, strerror(errno
));
245 const void *buf
= CFDataGetBytePtr(data
);
246 off_t fsize
= CFDataGetLength(data
);
248 off_t total_write
= 0;
249 while (total_write
< fsize
) {
252 bytes_write
= pwrite(fd
, buf
, (size_t)(fsize
- total_write
), total_write
);
253 if (bytes_write
== -1) {
254 fprintf(stderr
, "write %s: %s", path
, strerror(errno
));
257 if (bytes_write
== 0) {
258 fprintf(stderr
, "write %s: unexpected end of file", path
);
261 total_write
+= bytes_write
;
264 if (close(fd
) == -1) {
265 fprintf(stderr
, "close %s: %s", path
, strerror(errno
));
266 /* Failure to close the file isn't fatal. */
272 if (close(fd
) == -1) {
273 fprintf(stderr
, "close %s: %s", path
, strerror(errno
));