void get_next(char* client, int socket) { int last_msg = DictSearch( clients, client ); int new_msg = last_msg + 1; char to_ret[MSG_SIZE]; printf(" Client: %s Last_Msg: %d New_Msg: %d \n", client, last_msg, new_msg); if( msg_in_history(client, last_msg) == 0 ) { if( new_msg < num_msg) { sprintf(to_ret,"%s", DictSearchS(messages, new_msg)); DictDelete(clients, client); DictInsert(clients, client, new_msg); } else { sprintf(to_ret,"%s","No new messages "); } } write(socket, to_ret, sizeof(to_ret)); }//end get_next
//чтение данных, присланных пользователем int handlerRequest(int epollfd, int connectionfd, struct UserParams userParams, pthread_mutex_t mutex){ int buffCount = 1024; int i = 3; char *buffer = (char*) malloc(buffCount * sizeof(char)); char conBuf[4]; ssize_t count = read(connectionfd, buffer, sizeof buffer); char *ret; //динамическое выделение памяти для буфера char bufferN[buffCount]; ssize_t countN = read(connectionfd, bufferN, sizeof bufferN); while(countN > 0){ ret = (char*)realloc(buffer,i*buffCount*sizeof(char)); if(!ret){ perror("realloc"); exit(1); } strcat(buffer, bufferN); count+=countN; i++; countN = read(connectionfd, bufferN, sizeof bufferN); } switch(count){ case -1: if (errno != EAGAIN) perror ("failed to read data"); break; case 0: printf("client closed the connection"); break; default: snprintf(conBuf, 4,"%d",connectionfd); //ввод логина if(!strcmp(DictSearch(userParams.login, conBuf),"")){ if (count > 1){ char *tmpLogin = (char*) malloc(count+1); strncpy(tmpLogin, buffer, count); tmpLogin[count] = '\0'; DictInsert(userParams.login,conBuf,tmpLogin); printf("%s\n", DictSearch(userParams.login, conBuf)); dprintf(connectionfd, "Enter password: "******"Enter login: "******"0")){ char *tmpPass=(char*) malloc(count+1); strncpy(tmpPass, buffer, count); tmpPass[count] = '\0'; //вызов проверки pthread_mutex_lock(&mutex); int check = checkLogin(DictSearch(userParams.login, conBuf), tmpPass); pthread_mutex_unlock(&mutex); if(check == 1){ DictDelete(userParams.fLogin,conBuf); DictInsert(userParams.fLogin,conBuf,"1"); DictInsert(userParams.location,conBuf,"cd user_root/;"); dprintf(connectionfd, "!!Enter command: "); } else{ DictDelete(userParams.login,conBuf); DictInsert(userParams.login,conBuf,""); dprintf(connectionfd,"Enter login: "******"logout",6)) close(connectionfd); else{ int c; FILE *pp; extern FILE *popen(); char* tempCd = (char*) malloc(1024*sizeof(char)); strcpy(tempCd, DictSearch(userParams.location, conBuf)); //strcat(tempCd, ";"); printf("----->%s\n",tempCd); if ( !(pp=popen(strcat(tempCd, buffer), "r")) ){ free(tempCd); return 1; } while ( (c=fgetc(pp)) != EOF ) { putc(c, stdout); dprintf(connectionfd,"%c",c); fflush(pp); } pclose(pp); free(tempCd); printf("%d user command: %.*s", connectionfd, count, buffer); dprintf(connectionfd, "Enter command: "); } } } } free(buffer); }