1/*
2 * Copyright (c) 2001-2014 Haiku, Inc. All rights reserved.
3 * Distributed under the terms of the MIT License.
4 *
5 * Authors:
6 *		Daniel Reinhold, danielre@users.sf.net
7 *		John Scipione, jscipione@gmail.com
8 */
9
10/*!	Lists image info for all currently running teams. */
11
12
13#include <ctype.h>
14#include <image.h>
15#include <inttypes.h>
16#include <stdio.h>
17#include <stdlib.h>
18#include <string.h>
19
20#include <OS.h>
21
22
23static status_t
24list_images_for_team_by_id(team_id id)
25{
26	image_info imageInfo;
27	int32 cookie = 0;
28	team_info teamInfo;
29	char* header;
30	char* format;
31	int i;
32	status_t result = get_team_info(id, &teamInfo);
33	if (id != 1 && result < B_OK)
34		return result;
35
36	i = asprintf(&header, "   ID   %*s   %*s  Seq#      Init# Name",
37		sizeof(uintptr_t) * 2, "Text", sizeof(uintptr_t) * 2, "Data");
38	if (i == -1)
39		return B_NO_MEMORY;
40
41	i = asprintf(&format, "%%5" B_PRId32 " 0x%%0%" B_PRIu32 PRIxPTR
42		" 0x%%0%" B_PRIu32 PRIxPTR "  %%4" B_PRId32 " %%10" B_PRIu32 " %%s\n",
43		sizeof(uintptr_t) * 2, sizeof(uintptr_t) * 2);
44	if (i == -1) {
45		free(header);
46		return B_NO_MEMORY;
47	}
48
49	if (id == 1)
50		printf("\nKERNEL TEAM:\n");
51	else
52		printf("\nTEAM %4" B_PRId32 " (%s):\n", id, teamInfo.args);
53
54	puts(header);
55	for (i = 0; i < 80; i++)
56		putchar('-');
57
58	printf("\n");
59	while ((result = get_next_image_info(id, &cookie, &imageInfo)) == B_OK) {
60		printf(format, imageInfo.id, imageInfo.text, imageInfo.data,
61			imageInfo.sequence, imageInfo.init_order, imageInfo.name);
62	}
63
64	free(header);
65	free(format);
66
67	if (result != B_ENTRY_NOT_FOUND && result != EINVAL) {
68		printf("get images failed: %s\n", strerror(result));
69		return result;
70	}
71
72	return B_OK;
73}
74
75
76static void
77list_images_for_team(const char* arg)
78{
79	int32 cookie = 0;
80	team_info info;
81	status_t result;
82
83	if (atoi(arg) > 0 && list_images_for_team_by_id(atoi(arg)) == B_OK)
84		return;
85
86	/* search for the team by name */
87
88	while (get_next_team_info(&cookie, &info) >= B_OK) {
89		if (strstr(info.args, arg)) {
90			result = list_images_for_team_by_id(info.team);
91			if (result != B_OK) {
92				printf("\nCould not retrieve information about team %"
93					B_PRId32 ": %s\n", info.team, strerror(result));
94			}
95		}
96	}
97}
98
99
100int
101main(int argc, char** argv)
102{
103	int32 cookie = 0;
104	team_info info;
105	status_t result;
106
107	if (argc == 1) {
108		/* list for all teams */
109		while (get_next_team_info(&cookie, &info) >= B_OK) {
110			result = list_images_for_team_by_id(info.team);
111			if (result != B_OK) {
112				printf("\nCould not retrieve information about team %"
113					B_PRId32 ": %s\n", info.team, strerror(result));
114			}
115		}
116	} else {
117		/* list for each team_id on the command line */
118		while (--argc > 0 && ++argv != NULL)
119			list_images_for_team(*argv);
120	}
121
122	return 0;
123}
124