zoukankan      html  css  js  c++  java
  • 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
    ";
    	char *buf02="open /dev/urandom failed
    ";
    	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;
    }
    
  • 相关阅读:
    call,apply和bind的用法及区别
    JavaScript数组去重的方法
    JavaScript原型与原型链
    判断数组的方法
    两栏布局和三栏布局的实现
    小作品
    CSS垂直居中的方法
    闭包实现add(1)(2), add(1,2)的功能
    1.JavaScript的组成
    常用指令
  • 原文地址:https://www.cnblogs.com/muahao/p/7172094.html
Copyright © 2011-2022 走看看