summaryrefslogtreecommitdiff
path: root/package/cfgfs/src/ft_dump.c
blob: ed702ba28bdfe57c20acc4290e7ab958a95987eb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
/* $MirOS: contrib/hosted/fwcf/ft_dump.c,v 1.5 2006/09/23 23:21:04 tg Exp $ */

/*-
 * Copyright (c) 2006
 *	Thorsten Glaser <tg@mirbsd.de>
 *
 * Licensee is hereby permitted to deal in this work without restric-
 * tion, including unlimited rights to use, publicly perform, modify,
 * merge, distribute, sell, give away or sublicence, provided all co-
 * pyright notices above, these terms and the disclaimer are retained
 * in all redistributions or reproduced in accompanying documentation
 * or other materials provided with binary redistributions.
 *
 * Licensor offers the work "AS IS" and WITHOUT WARRANTY of any kind,
 * express, or implied, to the maximum extent permitted by applicable
 * law, without malicious intent or gross negligence; in no event may
 * licensor, an author or contributor be held liable for any indirect
 * or other damage, or direct damage except proven a consequence of a
 * direct error of said person and intended use of this work, loss or
 * other issues arising in any way out of its use, even if advised of
 * the possibility of such damage or existence of a defect.
 */

#include <sys/param.h>
#include <err.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#include "defs.h"
#include "pack.h"

__RCSID("$MirOS: contrib/hosted/fwcf/ft_dump.c,v 1.5 2006/09/23 23:21:04 tg Exp $");

static int ft_dumpfile(char *);

void
ft_dump(char *buf)
{
	while (*buf)
		buf += ft_dumpfile(buf);
}

static int
ft_dumpfile(char *buf)
{
	uint8_t c;
	int i, type = 0, size = 0;
	uint8_t *p;
	uint32_t x;

	i = strlen(buf) + 1;
	printf("\nNAME=%s\n", buf);
	p = (uint8_t *)buf + i;
	while (*p)
		switch (c = *p++) {
		case 0x01:
			printf("BLOCK\n");
			type = 1;
			break;
		case 0x02:
			printf("CHARACTER\n");
			type = 1;
			break;
		case 0x03:
			printf("SYMLINK\n");
			type = 2;
			break;
		case 0x04:
			printf("HARDLINK\n");
			type = 1;
			break;
		case 0x05:
			printf("DIRECTORY\n");
			type = 1;
			break;
		case 0x10:
		    {
			time_t y = LOADD(p);
			p += 4;
			printf("MTIME=%d -> %s", (int)y, ctime(&y));
			break;
		    }
		case 'g':
		case 'G':
		case 'u':
		case 'U':
			x = (c & 0x20) ? *p : LOADD(p);
			p += (c & 0x20) ? 1 : 4;
			printf("%cID=%d\n", c & ~0x20, x);
			break;
		case 'i':
		case 'I':
			x = (c == 'i') ? *p : LOADW(p);
			p += (c == 'i') ? 1 : 2;
			printf("INODE=%d\n", x);
			break;
		case 'm':
		case 'M':
			x = (c == 'm') ? LOADW(p) : LOADD(p);
			p += (c == 'm') ? 2 : 4;
			printf("MODE=0%o\n", x);
			break;
		case 's':
		case 'S':
			x = (c == 's') ? *p : LOADT(p);
			p += (c == 's') ? 1 : 3;
			printf("SIZE=%d\n", size = x);
			break;
		default:
			errx(1, "unknown attribute %02Xh", c);
		}
	++p;
	if (type == 2) {
		char *target;

		if ((target = malloc(size + 1)) == NULL)
			err(1, "malloc");
		memcpy(target, p, size);
		target[size] = '\0';
		printf("LINK_TARGET=%s\n", target);
		free(target);
	} else if (type == 1) {
		if (size)
			printf("WARN: size not allowed, ignoring\n");
		size = 0;
	} else {
		printf("BEGIN DATA\n");
		fflush(stdout);
		write(STDOUT_FILENO, p, size);
		printf("\nEND DATA\n");
	}
	return ((p - (uint8_t *)buf) + size);
}