xref: /haiku/src/bin/listsem.c (revision 1e36cfc2721ef13a187c6f7354dc9cbc485e89d3)
1 /*
2  *	listsem.c
3  *
4  *	Lists all semaphores in all Teams.
5  *	by O.Siebenmarck.
6  *
7  *      04-27-2002 - mmu_man
8  *       added command line args
9  *
10  *	Legal stuff follows:
11 
12 	Copyright (c) 2002 Oliver Siebenmarck <olli@ithome.de>, OpenBeOS project
13 
14 	Permission is hereby granted, free of charge, to any person obtaining a copy of
15 	this software and associated documentation files (the "Software"), to deal in
16 	the Software without restriction, including without limitation the rights to
17 	use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
18 	of the Software, and to permit persons to whom the Software is furnished to do
19 	so, subject to the following conditions:
20 
21 	The above copyright notice and this permission notice shall be included in all
22 	copies or substantial portions of the Software.
23 
24 	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
25 	IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
26 	FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
27 	AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
28 	LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
29 	OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
30 	THE SOFTWARE.
31 
32 */
33 
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <OS.h>
37 
38 static void list_sems(team_info *tinfo)
39 {
40 	sem_info info;
41 	int32 cookie = 0;
42 
43 	printf("TEAM  %ld (%s):\n", tinfo->team, tinfo->args );
44 	printf("     ID                           name  count\n");
45 	printf("---------------------------------------------\n");
46 
47 	while (get_next_sem_info(tinfo->team, &cookie, &info) == B_OK)
48 	{
49 		printf("%7ld%31s%7ld\n", info.sem ,info.name , info.count );
50 	}
51 	printf("\n");
52 }
53 
54 int main(int argc, char **argv)
55 {
56 	team_info tinfo;
57 	int32 cook = 0;
58 	int i;
59 	system_info sysinfo;
60 
61 	// show up some stats first...
62 	get_system_info(&sysinfo);
63 	printf("sem: total: %5li, used: %5li, left: %5li\n\n", sysinfo.max_sems, sysinfo.used_sems, sysinfo.max_sems - sysinfo.used_sems);
64 
65 	if (argc == 1) {
66 		while (get_next_team_info( &cook, &tinfo) == B_OK)
67 		{
68 			list_sems(&tinfo);
69 		}
70 		return 0;
71 	}
72 	for (i = 1; i < argc; i++) {
73 		if (!strcmp(argv[i], "--help") || !strcmp(argv[i], "-h")) {
74 			fprintf(stderr, "Usage:  %s [-s semid]  [teamid]\n", argv[0]);
75 			fputs("        List the semaphores allocated by the specified\n", stderr);
76 			fputs("        team, or all teams if none is specified.\n", stderr);
77 			fputs("\n", stderr);
78 			fputs("        The -s option displays the sem_info data for a\n", stderr);
79 			fputs("        specified semaphore.\n", stderr);
80 			return 0;
81 		} else {
82 			int t;
83 			t = atoi(argv[i]);
84 			if (get_team_info(t, &tinfo) == B_OK)
85 				list_sems(&tinfo);
86 			else
87 				printf("team %i unknown\n\n", t);
88 		}
89 	}
90 	return 0;
91 }
92 
93