This repository was archived by the owner on Nov 4, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
chat : client : getListOfClient.c
sᴀʟᴠᴀᴛᴏʀᴇ ʙ edited this page Jul 23, 2017
·
2 revisions
void getListOfClients() {
/*
* 1) Invia richiesta al server
* 2) Alloca lo spazio per salvare la lista
* 3) Legge un carattere alla volta da fdClientPipe
* 4) se necessario rialloca più spazio
* 5) stampa la lista su stdout
* 6) libera lo spazio occupato
*/
//dimensione iniziale della lista, se si rivelera' insufficiente verra' riallocata
int dim = 64;
sendRequestToServer();
char *list = malloc(sizeof(char) * dim);
char* start;
if (list != NULL) {
start = list;
int n, count = 0;
do { // Legge fino a '\0' o EOF
n = read(fdClientPipe, list, 1);
if (n != 0) {
count++;
if (count == dim) {
//rialloco il doppio della dimensione precedente
list = realloc(list, sizeof(char) * (dim * 2));
start = list;
dim *= 2;
list += (count - 1); //riposiziono il puntatore
}
}
} while (n > 0 && *list++ != '\0');
printf("%s", start);
free(start);
}
}Made with ❤️ by Owanesh and MatteoMauro | MIT ©