- #include<sys/types.h>
- #include<sys/stat.h>
- #include<fcntl.h>
- #include<unistd.h>
- #include<stdio.h>
- #include<string.h>
- #include<stdlib.h>
- char doll_msg[70000];
- char doll_len;
- int article_open(char *pathname)
- {
- return open(pathname,O_WRONLY);
- }
- ssize_t push_single_line(int fd, char *message)
- {
- int i;
- struct flock f_tmp;
- f_tmp.l_type = F_WRLCK;
- f_tmp.l_start = 0;
- f_tmp.l_whence = SEEK_SET;
- f_tmp.l_len = 0;
- /*type start whence len*/
- for(i=0;message[i]!='\0' && message[i]!='\n';i++);
- message[i++]='\n';
- if(fcntl(fd,F_SETLKW,&f_tmp)<0)
- return -1;
- f_tmp.l_type= F_UNLCK;
- if(lseek(fd,0,SEEK_END)<0 && (fcntl(fd,F_SETLK,&f_tmp)||1) )
- return -1;
- if(write(fd,message,i)<0 && (fcntl(fd,F_SETLK,&f_tmp)||1) )
- return -1;
- fcntl(fd,F_SETLK,&f_tmp);
- return i;
- }
- ssize_t collect_messages(char *message)
- {
- int i;
- for(i=0;message[i]!='\0' && message[i]!='\n';i++);
- message[i]='\0';
- if(doll_len+i>65536)
- return -1;
- strcpy(doll_msg+doll_len,message);
- doll_len+=i;
- doll_msg[doll_len++]='\n';
- doll_msg[doll_len]='\0';
- return doll_len;
- }
- ssize_t push_doll(int fd)
- {
- struct flock f_tmp;
- /*type start whence len*/
- f_tmp.l_type = F_WRLCK;
- f_tmp.l_start = 0;
- f_tmp.l_whence = SEEK_SET;
- f_tmp.l_len = 0;
- if(fcntl(fd,F_SETLKW,&f_tmp)<0)
- return -1;
- f_tmp.l_type= F_UNLCK;
- if(lseek(fd,0,SEEK_END)<0 && (fcntl(fd,F_SETLK,&f_tmp)||1) )
- return -1;
- if(write(fd,doll_msg,doll_len)<0 && (fcntl(fd,F_SETLK,&f_tmp)||1) )
- return -1;
- fcntl(fd,F_SETLK,&f_tmp);
- int len=doll_len;
- doll_len=0;
- return len;
- }
Raw Paste