xref: /haiku/src/bin/listport.c (revision 893988af824e65e49e55f517b157db8386e8002b)
1 // ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~
2 //
3 //  Copyright (c) 2001-2003, OpenBeOS
4 //
5 //  This software is part of the OpenBeOS distribution and is covered
6 //  by the OpenBeOS 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: %5ld, used: %5ld, left: %5ld\n", max, used, left);
61 }
62 
63 
64 void
65 list_team_ports(team_id id)
66 {
67 	int32      cookie = 0;
68 	port_info  this_port;
69 	team_info  this_team;
70 
71 	if (get_team_info(id, &this_team) == B_BAD_TEAM_ID) {
72 		printf("\nteam %ld unknown\n", id);
73 		return;
74 	}
75 
76 	printf("\nTEAM %4ld (%s):\n", id, this_team.args);
77 	printf("   ID                         name  capacity  queued\n");
78 	printf("----------------------------------------------------\n");
79 
80 	while (get_next_port_info(id, &cookie, &this_port) == B_OK) {
81 		printf("%5ld %28s  %8ld  %6ld\n",
82 		        this_port.port,
83 		        this_port.name,
84 		        this_port.capacity,
85 		        this_port.queue_count);
86 	}
87 }
88 
89