-
Notifications
You must be signed in to change notification settings - Fork 0
/
baseFun.c
74 lines (59 loc) · 1.31 KB
/
baseFun.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#include <stdio.h>
#include <unistd.h>
#include <errno.h>
#include <time.h>
//#include "unp.h"
/*
size_t readn(int fd , void *buf , size_t nbytes);
size_t writen(int fd , const void *buf , size_t nbytes);
size_t readline(int fd , void *buf , size_t bytes);
*/
size_t readn(int fd , void *vptr , size_t n){
size_t nleft = n;
size_t nread = 0;
char *ptr = vptr;
while(nleft>0){
if ( (nread = read(fd,ptr,nleft))<0){
if (errno == EINTR)
nread = 0;
else
return -1;
}
else if (nread==0)
break; //EOF
nleft -= nread;
ptr += nread;
}
return (n-nleft);
}
size_t writen(int fd , void *vptr , size_t n){
char *ptr = vptr;
size_t nleft = n;
size_t nwrite = 0 ;
while(nleft>0){
if(( nwrite = write(fd,ptr,nleft))<0){
if (errno == EINTR)
nwrite = 0;
else
return -1;
}
else if (nwrite == 0)
break;
nleft -= nwrite;
ptr += nwrite;
}
return (n-nleft);
}
int socket_init( int port){
}
void freeBuf(void *ptr)
{
if(ptr!=NULL)
free(ptr) , ptr = NULL;
}
unsigned long GetTickCount()
{
struct timespec ts;
clock_gettime(CLOCK_MONOTONIC, &ts);
return (ts.tv_sec * 1000 + ts.tv_nsec / 1000000);
}