Introduce old redir program
[lcapit-junk-code.git] / git / read-index.c
blob4e2839ce1e036398f5fb78903e8d95f345e6dd95
1 #include <stdio.h>
2 #include <unistd.h>
3 #include <fcntl.h>
4 #include <stdlib.h>
5 #include <sys/types.h>
6 #include <sys/stat.h>
7 #include <sys/mman.h>
8 #include <arpa/inet.h>
10 #ifndef FLEX_ARRAY
11 #if defined(__GNUC__) && (__GNUC__ < 3)
12 #define FLEX_ARRAY 0
13 #else
14 #define FLEX_ARRAY /* empty */
15 #endif
16 #endif
18 struct cache_header {
19 unsigned int hdr_signature;
20 unsigned int hdr_version;
21 unsigned int hdr_entries;
24 struct cache_time {
25 unsigned int sec;
26 unsigned int nsec;
29 struct cache_entry {
30 struct cache_time ce_ctime;
31 struct cache_time ce_mtime;
32 unsigned int ce_dev;
33 unsigned int ce_ino;
34 unsigned int ce_mode;
35 unsigned int ce_uid;
36 unsigned int ce_gid;
37 unsigned int ce_size;
38 unsigned char sha1[20];
39 unsigned short ce_flags;
40 char name[FLEX_ARRAY]; /* more */
43 #define CE_NAMEMASK (0x0fff)
45 #define offsetof(type, member) __builtin_offsetof (type, member)
46 #define cache_entry_size(len) ((offsetof(struct cache_entry,name) + (len) + 8) & ~7)
47 #define ce_namelen(ce) (CE_NAMEMASK & ntohs((ce)->ce_flags))
48 #define ce_size(ce) cache_entry_size(ce_namelen(ce))
50 int main(int argc, char *argv[])
52 void *index;
53 size_t offset;
54 int i, err, fd;
55 struct stat buf;
56 unsigned int active_nr;
57 struct cache_header *hdr;
59 if (argc != 2) {
60 printf("usage: %s: <index-file>\n", argv[0]);
61 exit(1);
64 fd = open(argv[1], O_RDONLY);
65 if (fd < 0) {
66 perror("open()");
67 exit(1);
70 err = fstat(fd, &buf);
71 if (err) {
72 perror("fstat()");
73 exit(1);
76 index = mmap(NULL, buf.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
77 if (index == MAP_FAILED) {
78 perror("mmap()");
79 exit(1);
81 close(fd);
83 hdr = index;
84 active_nr = ntohl(hdr->hdr_entries);
85 printf("Number of entries: %d\n\n", active_nr);
86 printf("SHA1 file name\n");
87 printf("---- ---------\n");
88 offset = sizeof(*hdr);
89 for (i = 0; i < active_nr; i++) {
90 struct cache_entry *ce;
92 ce = (struct cache_entry *) ((char *) index + offset);
93 printf("%#x %s\n", ce->sha1, ce->name);
95 offset = offset + ce_size(ce);
98 munmap(index, buf.st_size);
99 return 0;