#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <OS.h>
static void print_sem_info(sem_info *info)
{
printf("%7" B_PRId32 "%31s%7" B_PRId32 "\n", info->sem, info->name,
info->count);
}
static void print_header(team_info *tinfo)
{
if (tinfo != NULL)
printf("TEAM %" B_PRId32 " (%s):\n", tinfo->team, tinfo->args);
printf(" ID name count\n");
printf("---------------------------------------------\n");
}
static void list_sems(team_info *tinfo)
{
sem_info info;
int32 cookie = 0;
print_header(tinfo);
while (get_next_sem_info(tinfo->team, &cookie, &info) == B_OK)
print_sem_info(&info);
printf("\n");
}
int main(int argc, char **argv)
{
team_info tinfo;
int32 cookie = 0;
int32 i;
system_info sysinfo;
get_system_info(&sysinfo);
printf("sem: total: %5" B_PRIu32 ", used: %5" B_PRIu32 ", left: %5" B_PRIu32
"\n\n", sysinfo.max_sems, sysinfo.used_sems,
sysinfo.max_sems - sysinfo.used_sems);
if (argc == 1) {
while (get_next_team_info( &cookie, &tinfo) == B_OK)
list_sems(&tinfo);
return 0;
}
for (i = 1; i < argc; i++) {
if (strcmp(argv[i], "--help") == 0 || strcmp(argv[i], "-h") == 0) {
fprintf(stderr, "Usage: %s [-s semid] [teamid]\n", argv[0]);
fputs(" List the semaphores allocated by the specified\n",
stderr);
fputs(" team, or all teams if none is specified.\n",
stderr);
fputs("\n", stderr);
fputs(" The -s option displays the sem_info data for a\n",
stderr);
fputs(" specified semaphore.\n", stderr);
return 0;
} else if (strcmp(argv[i], "-s") == 0) {
if (argc < i + 2)
printf("-s used without associated sem id\n");
else {
sem_id id = atoi(argv[i + 1]);
sem_info info;
if (get_sem_info(id, &info) == B_OK) {
print_header(NULL);
print_sem_info(&info);
} else
printf("semaphore %" B_PRId32 " unknown\n\n", id);
i++;
}
} else {
team_id team;
team = atoi(argv[i]);
if (get_team_info(team, &tinfo) == B_OK)
list_sems(&tinfo);
else
printf("team %" B_PRId32 " unknown\n\n", team);
}
}
return 0;
}