]>
git.saurik.com Git - apple/file_cmds.git/blob - file/readfat.c
c48c3256db9d856b9b8a33e036e7c48ef52606ee
2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * Portions Copyright (c) 1999 Apple Computer, Inc. All Rights
7 * Reserved. This file contains Original Code and/or Modifications of
8 * Original Code as defined in and that are subject to the Apple Public
9 * Source License Version 1.1 (the "License"). You may not use this file
10 * except in compliance with the License. Please obtain a copy of the
11 * License at http://www.apple.com/publicsource and read it before using
14 * The Original Code and all software distributed under the License are
15 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
16 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
17 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE OR NON- INFRINGEMENT. Please see the
19 * License for the specific language governing rights and limitations
22 * @APPLE_LICENSE_HEADER_END@
28 #include <sys/types.h>
29 #include <sys/param.h> /* for MAXPATHLEN */
31 #include <unistd.h> /* for read() */
33 #include <mach-o/fat.h>
34 #include <mach-o/arch.h>
35 #include <mach-o/swap.h>
39 static void print_arch_name_for_file(
41 cpu_subtype_t cpusubtype
);
50 struct fat_header fat_header
;
51 struct fat_arch
*fat_archs
;
52 unsigned long i
, arch_size
, tbytes
;
54 unsigned char tmpbuf
[HOWMANY
+1]; /* one extra for terminating '\0' */
57 if(nbytes
< sizeof(struct fat_header
)){
60 memcpy(&fat_header
, buf
, sizeof(struct fat_header
));
61 #ifdef __LITTLE_ENDIAN__
62 swap_fat_header(&fat_header
, NX_LittleEndian
);
63 #endif /* __LITTLE_ENDIAN__ */
64 arch_size
= fat_header
.nfat_arch
* sizeof(struct fat_arch
);
65 if(arch_size
+ sizeof(struct fat_header
) > nbytes
){
68 arch_buf
= malloc(nbytes
);
71 memcpy(arch_buf
, buf
+ sizeof(struct fat_header
), arch_size
);
72 fat_archs
= (struct fat_arch
*)(arch_buf
);
73 #ifdef __LITTLE_ENDIAN__
74 swap_fat_arch(fat_archs
, fat_header
.nfat_arch
, NX_LittleEndian
);
75 #endif /* __LITTLE_ENDIAN__ */
76 for(i
= 0; i
< fat_header
.nfat_arch
; i
++){
77 printf("\n%s", inname
);
78 print_arch_name_for_file(fat_archs
[i
].cputype
,
79 fat_archs
[i
].cpusubtype
);
81 lseek(fd
, fat_archs
[i
].offset
, L_SET
);
83 * try looking at the first HOWMANY bytes
85 if ((tbytes
= read(fd
, (char *)tmpbuf
, HOWMANY
)) == -1) {
86 error("read failed (%s).\n", strerror(errno
));
89 tryit(tmpbuf
, tbytes
, 0);
95 print_arch_name_for_file(
97 cpu_subtype_t cpusubtype
)
99 const NXArchInfo
*ArchInfoTable
, *ai
;
101 ArchInfoTable
= NXGetAllArchInfos();
102 for(ai
= ArchInfoTable
; ai
->name
!= NULL
; ai
++){
103 if(ai
->cputype
== cputype
&&
104 ai
->cpusubtype
== cpusubtype
){
105 printf(" (for architecture %s)", ai
->name
);
109 printf(" (for architecture cputype (%d) cpusubtype (%d))",
110 cputype
, cpusubtype
);
112 #endif /* BUILTIN_FAT */