-static int comparetgs(const void *_a, const void *_b) {
- const trustgroup *a = _a;
- const trustgroup *b = _b;
-
- if(a->id > b->id)
- return 1;
- if(a->id < b-> id)
- return -1;
- return 0;
-}
-
-static int trusts_cmdtrustdump(void *source, int argc, char **argv) {
- trusthost *th;
- trustgroup *tg, **atg;
- unsigned int wanted, max, maxid, totalcount, i, groupcount, linecount;
- nick *np = source;
-
- if((argc < 2) || (argv[0][0] != '#'))
- return CMD_USAGE;
-
- wanted = atoi(&argv[0][1]);
- max = atoi(argv[1]);
-
- for(maxid=totalcount=0,tg=tglist;tg;tg=tg->next) {
- if(totalcount == 0 || tg->id > maxid)
- maxid = tg->id;
-
- totalcount++;
- }
-
- if(maxid > totalcount) {
- controlreply(np, "Start ID cannot exceed current maximum group ID (#%u)", maxid);
- return CMD_OK;
- }
-
- atg = nsmalloc(POOL_TRUSTS, sizeof(trusthost *) * totalcount);
- if(!atg) {
- controlreply(np, "Memory error.");
- return CMD_ERROR;
- }
-
- for(i=0,tg=tglist;i<totalcount&&tg;tg=tg->next,i++)
- atg[i] = tg;
-
- qsort(atg, totalcount, sizeof(trustgroup *), comparetgs);
-
- for(i=0;i<totalcount;i++)
- if(atg[i]->id >= wanted)
- break;
-
- for(groupcount=linecount=0;i<totalcount;i++) {
- linecount++;
- groupcount++;
-
- controlreply(np, "G,%s", dumptg(atg[i], 1));
-
- for(th=atg[i]->hosts;th;th=th->next) {
- linecount++;
- controlreply(np, "H,%s", dumpth(th, 1));
- }
-
- if(--max == 0)
- break;
- }
- nsfree(POOL_TRUSTS, atg);
-
- controlreply(np, "End of list, %u groups and %u lines returned.", groupcount, linecount);
- return CMD_OK;
-}
-