file_cmds-60.tar.gz
[apple/file_cmds.git] / file / readfat.c
1 /*
2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
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
12 * this file.
13 *
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
20 * under the License.
21 *
22 * @APPLE_LICENSE_HEADER_END@
23 */
24 #ifdef BUILTIN_FAT
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <sys/types.h>
29 #include <sys/param.h> /* for MAXPATHLEN */
30 #include <sys/file.h>
31 #include <unistd.h> /* for read() */
32
33 #include <mach-o/fat.h>
34 #include <mach-o/arch.h>
35 #include <mach-o/swap.h>
36
37 #include "file.h"
38
39 static void print_arch_name_for_file(
40 cpu_type_t cputype,
41 cpu_subtype_t cpusubtype);
42
43 void
44 tryfat(
45 const char *inname,
46 int fd,
47 char *buf,
48 int nbytes)
49 {
50 struct fat_header fat_header;
51 struct fat_arch *fat_archs;
52 unsigned long i, arch_size, tbytes;
53 char *arch_buf;
54 unsigned char tmpbuf[HOWMANY+1]; /* one extra for terminating '\0' */
55
56
57 if(nbytes < sizeof(struct fat_header)){
58 return;
59 }
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){
66 return;
67 }
68 arch_buf = malloc(nbytes);
69 if(arch_buf == NULL)
70 return;
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);
80 printf(":\t");
81 lseek(fd, fat_archs[i].offset, L_SET);
82 /*
83 * try looking at the first HOWMANY bytes
84 */
85 if ((tbytes = read(fd, (char *)tmpbuf, HOWMANY)) == -1) {
86 error("read failed (%s).\n", strerror(errno));
87 /*NOTREACHED*/
88 }
89 tryit(tmpbuf, tbytes, 0);
90 }
91 }
92
93 static
94 void
95 print_arch_name_for_file(
96 cpu_type_t cputype,
97 cpu_subtype_t cpusubtype)
98 {
99 const NXArchInfo *ArchInfoTable, *ai;
100
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);
106 return;
107 }
108 }
109 printf(" (for architecture cputype (%d) cpusubtype (%d))",
110 cputype, cpusubtype);
111 }
112 #endif /* BUILTIN_FAT */