]>
git.saurik.com Git - apple/security.git/blob - utilities/SecurityTool/readline.c
2 * Copyright (c) 2003-2004,2006-2010 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 /* Read a line from stdin into buffer as a null terminated string. If buffer is
36 non NULL use at most buffer_size bytes and return a pointer to buffer. Otherwise
37 return a newly malloced buffer.
38 if EOF is read this function returns NULL. */
40 readline(char *buffer
, int buffer_size
)
42 int ix
= 0, bytes_malloced
= 0;
47 buffer
= (char *)malloc(bytes_malloced
);
48 buffer_size
= bytes_malloced
;
55 if (ix
== buffer_size
- 1)
59 bytes_malloced
+= bytes_malloced
;
60 buffer
= (char *)realloc(buffer
, bytes_malloced
);
61 buffer_size
= bytes_malloced
;
76 /* 0 terminate buffer. */
82 /* Read the file name into buffer. On return buffer contains a newly
83 malloced buffer or length buffer_size. Return 0 on success and -1 on failure. */
85 read_file(const char *name
, uint8_t **outData
, size_t *outLength
)
94 fd
= open(name
, O_RDONLY
, 0);
95 } while (fd
== -1 && errno
== EINTR
);
99 fprintf(stderr
, "open %s: %s", name
, strerror(errno
));
104 off_end
= lseek(fd
, 0, SEEK_END
);
107 fprintf(stderr
, "lseek %s, SEEK_END: %s", name
, strerror(errno
));
112 if (off_end
> (off_t
)SIZE_MAX
) {
113 fprintf(stderr
, "file %s too large %llu bytes", name
, off_end
);
118 length
= (size_t)off_end
;
119 buffer
= malloc(length
);
122 bytes_read
= pread(fd
, buffer
, length
, 0);
123 } while (bytes_read
== -1 && errno
== EINTR
);
125 if (bytes_read
== -1)
127 fprintf(stderr
, "pread %s: %s", name
, strerror(errno
));
131 if (bytes_read
!= (ssize_t
)length
)
133 fprintf(stderr
, "read %s: only read %zu of %zu bytes", name
, bytes_read
, length
);
140 } while (result
== -1 && errno
== EINTR
);
144 fprintf(stderr
, "close %s: %s", name
, strerror(errno
));
148 *outData
= (uint8_t *)buffer
;
160 CFDataRef
copyFileContents(const char *path
) {
161 CFMutableDataRef data
= NULL
;
162 int fd
= open(path
, O_RDONLY
, 0666);
164 fprintf(stderr
, "open %s: %s", path
, strerror(errno
));
168 off_t fsize
= lseek(fd
, 0, SEEK_END
);
169 if (fsize
== (off_t
)-1) {
170 fprintf(stderr
, "lseek %s, 0, SEEK_END: %s", path
, strerror(errno
));
174 if (fsize
> (off_t
)INT32_MAX
) {
175 fprintf(stderr
, "file %s too large %llu bytes", path
, fsize
);
179 data
= CFDataCreateMutable(kCFAllocatorDefault
, (CFIndex
)fsize
);
180 CFDataSetLength(data
, (CFIndex
)fsize
);
181 void *buf
= CFDataGetMutableBytePtr(data
);
182 off_t total_read
= 0;
183 while (total_read
< fsize
) {
186 bytes_read
= pread(fd
, buf
, (size_t)(fsize
- total_read
), total_read
);
187 if (bytes_read
== -1) {
188 fprintf(stderr
, "read %s: %s", path
, strerror(errno
));
191 if (bytes_read
== 0) {
192 fprintf(stderr
, "read %s: unexpected end of file", path
);
195 total_read
+= bytes_read
;
198 if (close(fd
) == -1) {
199 fprintf(stderr
, "close %s: %s", path
, strerror(errno
));
200 /* Failure to close the file isn't fatal. */
206 if (close(fd
) == -1) {
207 fprintf(stderr
, "close %s: %s", path
, strerror(errno
));
216 bool writeFileContents(const char *path
, CFDataRef data
) {
217 int fd
= open(path
, O_WRONLY
| O_CREAT
| O_TRUNC
, 0666);
219 fprintf(stderr
, "open %s: %s", path
, strerror(errno
));
223 const void *buf
= CFDataGetBytePtr(data
);
224 off_t fsize
= CFDataGetLength(data
);
226 off_t total_write
= 0;
227 while (total_write
< fsize
) {
230 bytes_write
= pwrite(fd
, buf
, (size_t)(fsize
- total_write
), total_write
);
231 if (bytes_write
== -1) {
232 fprintf(stderr
, "write %s: %s", path
, strerror(errno
));
235 if (bytes_write
== 0) {
236 fprintf(stderr
, "write %s: unexpected end of file", path
);
239 total_write
+= bytes_write
;
242 if (close(fd
) == -1) {
243 fprintf(stderr
, "close %s: %s", path
, strerror(errno
));
244 /* Failure to close the file isn't fatal. */
250 if (close(fd
) == -1) {
251 fprintf(stderr
, "close %s: %s", path
, strerror(errno
));