rendered paste body#include "groups.h"void setGroupList(struct chatgroup * xgrouplist) { grouplist = xgrouplist;}struct chatgroup * getGroupList() { return grouplist;}/* Read groups from a stream */struct chatgroup * readgroups(int connection) { long value = 0, tvalue = 0; long groups = 0, tgroups = 0; long c = 0; char * gname, final; short glen, tglen; struct chatgroup * current = NULL, * tmpgroup = NULL; recv(connection, &tvalue, sizeof(tvalue), 0); value = ntohl(tvalue); recv(connection, &tgroups, sizeof(tgroups), 0); groups = ntohl(tgroups); for(c=0;c<groups;c++) { recv(connection, &tglen, sizeof(tglen), 0); glen = ntohs(tglen); gname = (char *) malloc(glen+1); recv(connection, gname, glen, 0); gname[glen]='\0'; printf("group \"%s\"\n", gname); // Create group here [...] tmpgroup = initgroup(gname); current = insertgroup(current, tmpgroup); free(gname); } recv(connection, &final, 1, 0); return firstgroup(current);}/* Create a group object */struct chatgroup * initgroup(char * name) { struct chatgroup * tmpgroup = NULL; if (name == NULL) { return NULL; }; tmpgroup = (struct chatgroup *) malloc(sizeof(struct chatgroup)); tmpgroup->prev = NULL; tmpgroup->next = NULL; tmpgroup->name = (char *) malloc(strlen(name) + 1); strcpy(tmpgroup->name,name); return tmpgroup;};void disposegroup(struct chatgroup * group) { struct chatgroup * tmpprev = NULL; struct chatgroup * tmpnext = NULL; if (group == NULL) { return; }; tmpprev = group->prev; tmpnext = group->next; if (tmpprev) { tmpprev->next = tmpnext; }; if (tmpnext) { tmpnext->prev = tmpprev; }; if (group->name) { free(group->name); }; free(group);};void disposeallgroups(struct chatgroup * group) { if (group) { struct chatgroup * current = firstgroup(group); while (current) { struct chatgroup * next = current->next; disposegroup(current); } }}struct chatgroup * insertgroup(struct chatgroup * before, struct chatgroup * newgroup) { struct chatgroup * tmpprev = NULL; struct chatgroup * tmpnext = NULL; if (before == NULL) { return newgroup; } tmpprev = before; tmpnext = before->next; before->next = newgroup; newgroup->prev = before; newgroup->next = tmpnext; if (tmpnext) { tmpnext->prev = newgroup; };};struct chatgroup * firstgroup(struct chatgroup * group) { if (group == NULL) { return NULL; }; while (group->prev != NULL) { group = group->prev; } return group;};struct chatgroup * lastgroup(struct chatgroup * group) { if (group == NULL) { return NULL; }; while (group->next != NULL) { group = group->next; } return group;};