xref: /illumos-gate/usr/src/cmd/sgs/libelf/common/rawput.c (revision 7c478bd9)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*	Copyright (c) 1988 AT&T	*/
23 /*	  All Rights Reserved  	*/
24 
25 
26 /*
27  * Copyright (c) 1998 by Sun Microsystems, Inc.
28  * All rights reserved.
29  */
30 
31 
32 #pragma ident	"%Z%%M%	%I%	%E% SMI" 	/* SVr4.0 1.2	*/
33 
34 #include "syn.h"
35 #include <stdlib.h>
36 #include <unistd.h>
37 #include <errno.h>
38 #include "libelf.h"
39 #include "decl.h"
40 #include "msg.h"
41 
42 
43 /*
44  * Raw file input/output
45  * Read pieces of input files.
46  */
47 
48 
49 char *
50 _elf_read(int fd, off_t off, size_t fsz)
51 {
52 	char		*p;
53 
54 	if (fsz == 0)
55 		return (0);
56 
57 	if (fd == -1) {
58 		_elf_seterr(EREQ_NOFD, 0);
59 		return (0);
60 	}
61 
62 	if (lseek(fd, off, 0) != off) {
63 		_elf_seterr(EIO_SEEK, errno);
64 		return (0);
65 	}
66 	if ((p = (char *)malloc(fsz)) == 0) {
67 		_elf_seterr(EMEM_DATA, errno);
68 		return (0);
69 	}
70 
71 	if (read(fd, p, fsz) != fsz) {
72 		_elf_seterr(EIO_READ, errno);
73 		free(p);
74 		return (0);
75 	}
76 	return (p);
77 }
78