C IO programming test code
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#include <string.h>
#include <sys/stat.h>
#include <stdlib.h>
#include <unistd.h>
#include <time.h>
#define BUFLEN 255
#define ERR_EXIT(m) \
do \
{ \
perror(m); \
exit(EXIT_FAILURE); \
} while(0)
int main(){
int fd;
int fd_log;
char *buf01="open /dev/urandom success\n";
char *buf02="open /dev/urandom failed\n";
char dest[100];
int size;
int max=604800;//7days
int i=0;
time_t timep;
char tmpBuf[BUFLEN];
umask(0);
fd_log = open("/var/log/test_urandom.log",O_CREAT|O_APPEND|O_RDWR,0666);
if (fd_log == -1){
ERR_EXIT("open error");
}else{
printf("open /var/log/test_urandom.log success");
}
while(i < max){
fd = open("/dev/urandom", O_RDONLY|O_CLOEXEC|O_NOCTTY);
//now
time(&timep);
strcpy(tmpBuf,ctime(&timep));
//failed
if (fd < 0){
strcpy(dest,tmpBuf);
strcat(dest,":");
strcat(dest,buf02);
if((size=write(fd_log,dest,strlen(dest))) < 0){
perror("write failed");
}
}else{ //success
strcpy(dest,tmpBuf);
strcat(dest,":");
strcat(dest,buf01);
if ((size=write(fd_log,dest,strlen(dest))) < 0){
perror("write failed");
}
}
close(fd);
sleep(1);
i+=1;
}
close(fd_log);
return 0;
}
muahao@aliyun.com