19b0c212aSAxel Dörfler /*
269ac139bSJohn Scipione * Copyright (c) 2001-2014 Haiku, Inc. All rights reserved.
326de917cSJohn Scipione * Distributed under the terms of the MIT License.
49b0c212aSAxel Dörfler *
526de917cSJohn Scipione * Authors:
626de917cSJohn Scipione * Daniel Reinhold, danielre@users.sf.net
769ac139bSJohn Scipione * John Scipione, jscipione@gmail.com
89b0c212aSAxel Dörfler */
99b0c212aSAxel Dörfler
1026de917cSJohn Scipione /*! Lists image info for all currently running teams. */
1117049c45SAxel Dörfler
1217049c45SAxel Dörfler
1326de917cSJohn Scipione #include <ctype.h>
1417049c45SAxel Dörfler #include <image.h>
1569ac139bSJohn Scipione #include <inttypes.h>
1617049c45SAxel Dörfler #include <stdio.h>
1717049c45SAxel Dörfler #include <stdlib.h>
1817049c45SAxel Dörfler #include <string.h>
1926de917cSJohn Scipione
2026de917cSJohn Scipione #include <OS.h>
2117049c45SAxel Dörfler
2217049c45SAxel Dörfler
2317049c45SAxel Dörfler static status_t
list_images_for_team_by_id(team_id id)2417049c45SAxel Dörfler list_images_for_team_by_id(team_id id)
2517049c45SAxel Dörfler {
2617049c45SAxel Dörfler image_info imageInfo;
2717049c45SAxel Dörfler int32 cookie = 0;
28a846b0eeSJohn Scipione team_info teamInfo;
29*c425d6cbSJohn Scipione char* header;
3069ac139bSJohn Scipione char* format;
3169ac139bSJohn Scipione int i;
3269ac139bSJohn Scipione status_t result = get_team_info(id, &teamInfo);
3326de917cSJohn Scipione if (id != 1 && result < B_OK)
3426de917cSJohn Scipione return result;
3517049c45SAxel Dörfler
3669ac139bSJohn Scipione i = asprintf(&header, " ID %*s %*s Seq# Init# Name",
3769ac139bSJohn Scipione sizeof(uintptr_t) * 2, "Text", sizeof(uintptr_t) * 2, "Data");
3869ac139bSJohn Scipione if (i == -1)
3969ac139bSJohn Scipione return B_NO_MEMORY;
4069ac139bSJohn Scipione
4169ac139bSJohn Scipione i = asprintf(&format, "%%5" B_PRId32 " 0x%%0%" B_PRIu32 PRIxPTR
4269ac139bSJohn Scipione " 0x%%0%" B_PRIu32 PRIxPTR " %%4" B_PRId32 " %%10" B_PRIu32 " %%s\n",
4369ac139bSJohn Scipione sizeof(uintptr_t) * 2, sizeof(uintptr_t) * 2);
44*c425d6cbSJohn Scipione if (i == -1) {
45*c425d6cbSJohn Scipione free(header);
4669ac139bSJohn Scipione return B_NO_MEMORY;
47*c425d6cbSJohn Scipione }
4869ac139bSJohn Scipione
4917049c45SAxel Dörfler if (id == 1)
5017049c45SAxel Dörfler printf("\nKERNEL TEAM:\n");
5117049c45SAxel Dörfler else
5269ac139bSJohn Scipione printf("\nTEAM %4" B_PRId32 " (%s):\n", id, teamInfo.args);
5317049c45SAxel Dörfler
5469ac139bSJohn Scipione puts(header);
5569ac139bSJohn Scipione for (i = 0; i < 80; i++)
5669ac139bSJohn Scipione putchar('-');
5726de917cSJohn Scipione
5869ac139bSJohn Scipione printf("\n");
5926de917cSJohn Scipione while ((result = get_next_image_info(id, &cookie, &imageInfo)) == B_OK) {
6069ac139bSJohn Scipione printf(format, imageInfo.id, imageInfo.text, imageInfo.data,
6169ac139bSJohn Scipione imageInfo.sequence, imageInfo.init_order, imageInfo.name);
6217049c45SAxel Dörfler }
6326de917cSJohn Scipione
64*c425d6cbSJohn Scipione free(header);
6569ac139bSJohn Scipione free(format);
6669ac139bSJohn Scipione
6726de917cSJohn Scipione if (result != B_ENTRY_NOT_FOUND && result != EINVAL) {
6826de917cSJohn Scipione printf("get images failed: %s\n", strerror(result));
6926de917cSJohn Scipione return result;
7017049c45SAxel Dörfler }
7126de917cSJohn Scipione
7217049c45SAxel Dörfler return B_OK;
7317049c45SAxel Dörfler }
7417049c45SAxel Dörfler
7517049c45SAxel Dörfler
7617049c45SAxel Dörfler static void
list_images_for_team(const char * arg)7717049c45SAxel Dörfler list_images_for_team(const char* arg)
7817049c45SAxel Dörfler {
7917049c45SAxel Dörfler int32 cookie = 0;
8017049c45SAxel Dörfler team_info info;
8169ac139bSJohn Scipione status_t result;
8217049c45SAxel Dörfler
8326de917cSJohn Scipione if (atoi(arg) > 0 && list_images_for_team_by_id(atoi(arg)) == B_OK)
8417049c45SAxel Dörfler return;
8517049c45SAxel Dörfler
8669ac139bSJohn Scipione /* search for the team by name */
8717049c45SAxel Dörfler
8817049c45SAxel Dörfler while (get_next_team_info(&cookie, &info) >= B_OK) {
8917049c45SAxel Dörfler if (strstr(info.args, arg)) {
9069ac139bSJohn Scipione result = list_images_for_team_by_id(info.team);
9126de917cSJohn Scipione if (result != B_OK) {
9269ac139bSJohn Scipione printf("\nCould not retrieve information about team %"
9369ac139bSJohn Scipione B_PRId32 ": %s\n", info.team, strerror(result));
9426de917cSJohn Scipione }
9517049c45SAxel Dörfler }
9617049c45SAxel Dörfler }
9717049c45SAxel Dörfler }
9817049c45SAxel Dörfler
9917049c45SAxel Dörfler
10017049c45SAxel Dörfler int
main(int argc,char ** argv)10117049c45SAxel Dörfler main(int argc, char** argv)
10217049c45SAxel Dörfler {
10317049c45SAxel Dörfler int32 cookie = 0;
10417049c45SAxel Dörfler team_info info;
10569ac139bSJohn Scipione status_t result;
10617049c45SAxel Dörfler
10769ac139bSJohn Scipione if (argc == 1) {
10869ac139bSJohn Scipione /* list for all teams */
10917049c45SAxel Dörfler while (get_next_team_info(&cookie, &info) >= B_OK) {
11069ac139bSJohn Scipione result = list_images_for_team_by_id(info.team);
11126de917cSJohn Scipione if (result != B_OK) {
11269ac139bSJohn Scipione printf("\nCould not retrieve information about team %"
11369ac139bSJohn Scipione B_PRId32 ": %s\n", info.team, strerror(result));
11426de917cSJohn Scipione }
11517049c45SAxel Dörfler }
11617049c45SAxel Dörfler } else {
11769ac139bSJohn Scipione /* list for each team_id on the command line */
11869ac139bSJohn Scipione while (--argc > 0 && ++argv != NULL)
11969ac139bSJohn Scipione list_images_for_team(*argv);
12017049c45SAxel Dörfler }
12126de917cSJohn Scipione
12217049c45SAxel Dörfler return 0;
12317049c45SAxel Dörfler }
124