ahhh thanks for the tips, finally i did it.
here is the working function:
int fdread(int fd, void *buffer, int size, int timeout) { // timeout in milliseconds
char *ptr = (char*)buffer;
int received = 0;
long int tout;
struct timespec current;
clock_gettime(CLOCK_REALTIME, ¤t);
tout = current.tv_nsec + (timeout*1000000);
while((received < size) && (tout > current.tv_nsec)) {
if (read(fd, ptr, 1) == 1) {
ptr++;
received++;
}
clock_gettime(CLOCK_REALTIME, ¤t);
}
return received;
}