xref: /haiku/src/bin/listport.c (revision 4c8e85b316c35a9161f5a1c50ad70bc91c83a76f)
1 // ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~
2 //
3 //  Copyright (c) 2001-2003, Haiku
4 //
5 //  This software is part of the Haiku distribution and is covered
6 //  by the MIT License.
7 //
8 //
9 //  File:        listport.c
10 //  Author:      Daniel Reinhold (danielre@users.sf.net)
11 //  Description: lists all open ports in the system, organized by team
12 //
13 // ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~
14 
15 #include <stdio.h>
16 #include <stdlib.h>
17 #include <OS.h>
18 
19 
20 void list_team_ports  (team_id id);
21 void show_port_totals (void);
22 
23 
24 
25 int
26 main(int argc, char **argv)
27 {
28 	show_port_totals();
29 
30 	if (argc == 1) {
31 		int32     cookie = 0;
32 		team_info info;
33 
34 		// list for all teams
35 		while (get_next_team_info(&cookie, &info) >= B_OK)
36 			list_team_ports(info.team);
37 	}
38 	else
39 		// list for each team_id on the command line
40 		while (--argc)
41 			list_team_ports(atoi(*++argv));
42 
43 	return 0;
44 }
45 
46 
47 void
48 show_port_totals()
49 {
50 	int32       max = 0, used = 0, left;
51 	system_info sys;
52 
53 	if (get_system_info(&sys) == B_OK) {
54 		max  = sys.max_ports;
55 		used = sys.used_ports;
56 	}
57 
58 	left = max - used;
59 
60 	printf("port: total: %5" B_PRId32 ", "
61 		"used: %5" B_PRId32 ", "
62 		"left: %5" B_PRId32 "\n",
63 			max,
64 			used,
65 			left);
66 }
67 
68 
69 void
70 list_team_ports(team_id id)
71 {
72 	int32      cookie = 0;
73 	port_info  this_port;
74 	team_info  this_team;
75 
76 	if (get_team_info(id, &this_team) == B_BAD_TEAM_ID) {
77 		printf("\nteam %" B_PRId32 " unknown\n", id);
78 		return;
79 	}
80 
81 	printf("\nTEAM %4" B_PRId32 " (%s):\n", id, this_team.args);
82 	printf("   ID                         name  capacity  queued\n");
83 	printf("----------------------------------------------------\n");
84 
85 	while (get_next_port_info(id, &cookie, &this_port) == B_OK) {
86 		printf("%5" B_PRId32 " %28s  %8" B_PRId32 "  %6" B_PRId32 "\n",
87 		        this_port.port,
88 		        this_port.name,
89 		        this_port.capacity,
90 		        this_port.queue_count);
91 	}
92 }
93 
94