xref: /haiku/src/bin/listsem.c (revision 22440f4105cafc95cc1d49f9bc65bb395c527d86)
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 <string.h>
37 #include <OS.h>
38 
39 
40 static void print_sem_info(sem_info *info)
41 {
42 	printf("%7ld%31s%7ld\n", info->sem ,info->name , info->count);
43 }
44 
45 
46 static void print_header(team_info *tinfo)
47 {
48 	if (tinfo != NULL)
49 		printf("TEAM  %ld (%s):\n", tinfo->team, tinfo->args );
50 
51 	printf("     ID                           name  count\n");
52 	printf("---------------------------------------------\n");
53 }
54 
55 
56 static void list_sems(team_info *tinfo)
57 {
58 	sem_info info;
59 	int32 cookie = 0;
60 
61 	print_header(tinfo);
62 
63 	while (get_next_sem_info(tinfo->team, &cookie, &info) == B_OK)
64 		print_sem_info(&info);
65 
66 	printf("\n");
67 }
68 
69 int main(int argc, char **argv)
70 {
71 	team_info tinfo;
72 	int32 cookie = 0;
73 	int32 i;
74 	system_info sysinfo;
75 
76 	// show up some stats first...
77 	get_system_info(&sysinfo);
78 	printf("sem: total: %5li, used: %5li, left: %5li\n\n", sysinfo.max_sems, sysinfo.used_sems, sysinfo.max_sems - sysinfo.used_sems);
79 
80 	if (argc == 1) {
81 		while (get_next_team_info( &cookie, &tinfo) == B_OK)
82 			list_sems(&tinfo);
83 
84 		return 0;
85 	}
86 
87 	for (i = 1; i < argc; i++) {
88 		if (strcmp(argv[i], "--help") == 0 || strcmp(argv[i], "-h") == 0) {
89 			fprintf(stderr, "Usage:  %s [-s semid]  [teamid]\n", argv[0]);
90 			fputs("        List the semaphores allocated by the specified\n",
91 				stderr);
92 			fputs("        team, or all teams if none is specified.\n",
93 				stderr);
94 			fputs("\n", stderr);
95 			fputs("        The -s option displays the sem_info data for a\n",
96 				stderr);
97 			fputs("        specified semaphore.\n", stderr);
98 			return 0;
99 		} else if (strcmp(argv[i], "-s") == 0) {
100 			if (argc < i + 2)
101 				printf("-s used without associated sem id\n");
102 			else {
103 				sem_id id = atoi(argv[i + 1]);
104 				sem_info info;
105 				if (get_sem_info(id, &info) == B_OK) {
106 					print_header(NULL);
107 					print_sem_info(&info);
108 				} else
109 					printf("semaphore %ld unknown\n\n", id);
110 
111 				i++;
112 			}
113 		} else {
114 			team_id team;
115 			team = atoi(argv[i]);
116 			if (get_team_info(team, &tinfo) == B_OK)
117 				list_sems(&tinfo);
118 			else
119 				printf("team %ld unknown\n\n", team);
120 		}
121 	}
122 
123 	return 0;
124 }
125 
126