zoukankan      html  css  js  c++  java
  • 20145217《信息安全系统设计基础》第10周学习总结

    实践部分

    编译运行代码及使用man查看帮助文档

    1. cp1.c

    功能:复制文件或目录

    运行截图

    程序部分

    #include        <stdio.h>  //标准输入输出头文件
    #include        <stdlib.h>  //标准库头文件 
    #include        <unistd.h>  //unix类系统定义符号常量的头文件
    #include        <fcntl.h>   //定义了一组基于C的非缓冲的文件操作函数,可用于文件和设备(及socket等)的I/O操作
    
    #define BUFFERSIZE      4096   //缓冲区大小
    #define COPYMODE        0644   // 新文件的访问权限位
    
    void oops(char *, char *);  //错误处理函数
    
    int main(int argc, char *argv[])
    {
        int in_fd, out_fd, n_chars;    
        char buf[BUFFERSIZE];   //定义缓冲区
        if (argc != 3)  //当输入参数不足时
        {
            fprintf(stderr, "usage: %s source destination
    ", *argv);
            exit(1);
        }
    
        if ((in_fd = open(argv[1], O_RDONLY)) == -1)      /打开文件失败
            oops("Cannot open ", argv[1]);
    
        if ((out_fd = creat(argv[2], COPYMODE)) == -1)   //创建新文件失败
            oops("Cannot creat", argv[2]);
    
        while ((n_chars = read(in_fd, buf, BUFFERSIZE)) > 0)   //缓冲区不空时,开始写文件
            if (write(out_fd, buf, n_chars) != n_chars)  //写文件出错
                oops("Write error to ", argv[2]);
        if (n_chars == -1)   //从文件中读数据出错
            oops("Read error from ", argv[1]);
    
    
        if (close(in_fd) == -1 || close(out_fd) == -1)   //关闭输入文件出错或关闭输出文件出错
            oops("Error closing files", "");
    }
    
    void oops(char *s1, char *s2)   //错误处理
    {
        fprintf(stderr, "Error: %s ", s1);
        perror(s2);
        exit(1);
    }
    

    2. echostate.c

    功能:查看在命令行中输入命令时是否可见,可见返回1,否则返回0

    运行截图

    查看帮助文档

    (1)tcgetattr
    tcgetattr函数用于获取与终端相关的参数。参数fd为终端的文件描述符,返回的结果保存在termios 结构体中

    (2)perror :打印错误信息

    (3)ECHO :值为0000010

    (4)c_lflag:本地模式标志,控制终端编辑功能

    程序过程

    #include        <stdio.h>
    #include        <stdlib.h>
    #include        <termios.h>  //串口配置中,终端的工作模式,是一个结构体
    
    int main()
    {
            struct termios info;    //定义标准接口结构体
            int rv;
    
            rv = tcgetattr( 0, &info );     /* 从驱动器读值 */
    
            if ( rv == -1 ){
                    perror( "tcgetattr");//错误处理
                    exit(1);
            }
            if ( info.c_lflag & ECHO )  //获得状态标记位
                    printf(" echo is on , since its bit is 1
    ");
            else
                    printf(" echo is OFF, since its bit is 0
    ");
    
            return 0;
    }
    

    3. fileinfo.c

    功能:查看文件信息

    运行截图

    查看帮助文档

    (1)stat:显示文件或文件系统的状态

    (2)搜索stat和file作为关键字,查看系统调用

    (3)找到系统函数

    程序过程

    #include <stdio.h>
    #include <sys/types.h>  //基本系统数据类型
    #include <sys/stat.h>   //文件属性结构体
    
    void show_stat_info(char *, struct stat *);
    //显示文件属性函数
    int main(int argc, char *argv[])
    {
        struct stat info;        
    
        if (argc>1)     //运行时有参数
        {
    
            if( stat(argv[1], &info) != -1 )    //显示文件状态
            {
                show_stat_info( argv[1], &info );
                return 0;
            }
            else
                perror(argv[1]);  //错误处理
        }
        return 1;
    }
    void show_stat_info(char *fname, struct stat *buf)
    {
        printf("   mode: %o
    ", buf->st_mode);         
        printf("  links: %d
    ", buf->st_nlink);        
        printf("   user: %d
    ", buf->st_uid);          
        printf("  group: %d
    ", buf->st_gid);          
        printf("   size: %d
    ", (int)buf->st_size);         
        printf("modtime: %d
    ", (int)buf->st_mtime);        
        printf("   name: %s
    ", fname );               
    }
    

    4. filesize.c

    功能:查看文件大小

    运行截图

    程序过程

    #include <stdio.h>
    #include <sys/stat.h>   //文件属性结构体
    
    int main()
    {
        struct stat infobuf;           
    
        if ( stat( "/etc/passwd", &infobuf) == -1 )
            //查看文件属性函数
            perror("/etc/passwd");  //错误处理
        else
            printf(" The size of /etc/passwd is %d
    ", infobuf.st_size );
    }
    

    5. ls1.c

    功能:显示当前目录下的所有文件

    运行截图

    程序过程

    #include    <stdio.h>
    #include    <sys/types.h>   //基本系统数据类型
    #include    <dirent.h>
    
    void do_ls(char []);
    
    int main(int argc, char *argv[])
    {
        if ( argc == 1 )    //没有参数是,显示 当前万人当
            do_ls( "." );
        else
            while ( --argc ){
                printf("%s:
    ", *++argv ); //显示参数
                do_ls( *argv );
            }
    
        return 0;
    }
    
    void do_ls( char dirname[] )
    {
        DIR     *dir_ptr;        //DIR表示目录类型
        struct dirent   *direntp;   //目录结构体 
    
        if ( ( dir_ptr = opendir( dirname ) ) == NULL )
            fprintf(stderr,"ls1: cannot open %s
    ", dirname);
        else
        {
            while ( ( direntp = readdir( dir_ptr ) ) != NULL )
                printf("%s
    ", direntp->d_name );
            closedir(dir_ptr);
        }
    }
    

    6. ls2.c

    功能:显示指定目录下的文件详细信息

    运行截图

    程序过程
    #include <stdio.h>
    #include <string.h>
    #include <sys/types.h>
    #include <dirent.h>
    #include <sys/stat.h>

    void do_ls(char[]);     //显示指定目录下的文件
    void dostat(char *);    //显示文件属性
    void show_file_info( char *, struct stat *);    //显示文件读取详细信息
    void mode_to_letters( int , char [] );  //显示文件读取权限信息
    char *uid_to_name( uid_t );
    char *gid_to_name( gid_t );
    
    int main(int argc, char *argv[])
    {
        if ( argc == 1 )
            do_ls( "." );
        else
            while ( --argc ){
                printf("%s:
    ", *++argv );
                do_ls( *argv );
            }
    
        return 0;
    }
    
    void do_ls( char dirname[] )
    {
        DIR     *dir_ptr;       
        struct dirent   *direntp;       
    
        if ( ( dir_ptr = opendir( dirname ) ) == NULL )
            fprintf(stderr,"ls1: cannot open %s
    ", dirname);
        else
        {
            while ( ( direntp = readdir( dir_ptr ) ) != NULL )
                dostat( direntp->d_name );
            closedir(dir_ptr);
        }
    }
    
    void dostat( char *filename )
    {
        struct stat info;
    
        if ( stat(filename, &info) == -1 )      
            perror( filename );         
        else                    
            show_file_info( filename, &info );
    }
    
    void show_file_info( char *filename, struct stat *info_p )
    {
        char    *uid_to_name(), *ctime(), *gid_to_name(), *filemode();
        void    mode_to_letters();
            char    modestr[11];
    
        mode_to_letters( info_p->st_mode, modestr );
    
        printf( "%s"    , modestr );
        printf( "%4d "  , (int) info_p->st_nlink);  
        printf( "%-8s " , uid_to_name(info_p->st_uid) );
        printf( "%-8s " , gid_to_name(info_p->st_gid) );
        printf( "%8ld " , (long)info_p->st_size);
        printf( "%.12s ", 4+ctime(&info_p->st_mtime));
        printf( "%s
    "  , filename );
    
    }
    
    void mode_to_letters( int mode, char str[] )
    {
        strcpy( str, "----------" );           
    
        if ( S_ISDIR(mode) )  str[0] = 'd';    
        if ( S_ISCHR(mode) )  str[0] = 'c';    
        if ( S_ISBLK(mode) )  str[0] = 'b';    
    
        if ( mode & S_IRUSR ) str[1] = 'r';    
        if ( mode & S_IWUSR ) str[2] = 'w';
        if ( mode & S_IXUSR ) str[3] = 'x';
    
        if ( mode & S_IRGRP ) str[4] = 'r';    
        if ( mode & S_IWGRP ) str[5] = 'w';
        if ( mode & S_IXGRP ) str[6] = 'x';
    
        if ( mode & S_IROTH ) str[7] = 'r';    
        if ( mode & S_IWOTH ) str[8] = 'w';
        if ( mode & S_IXOTH ) str[9] = 'x';
    }
    
    #include    <pwd.h>     //定义口令结构体
    
    char *uid_to_name( uid_t uid )
    {
        struct  passwd *getpwuid(), *pw_ptr;
        static  char numstr[10];
    
        if ( ( pw_ptr = getpwuid( uid ) ) == NULL ){
            sprintf(numstr,"%d", uid);
            return numstr;
        }
        else
            return pw_ptr->pw_name ;
    }
    
    #include    <grp.h>     //包含组结构的定义
    
    char *gid_to_name( gid_t gid )
    {
        struct group *getgrgid(), *grp_ptr;
        static  char numstr[10];
    
        if ( ( grp_ptr = getgrgid(gid) ) == NULL ){
            sprintf(numstr,"%d", gid);
            return numstr;
        }
        else
            return grp_ptr->gr_name;    //返回组名
    }
    

    7. setecho.c

    功能:设置echo的状态

    程序过程

    #include        <stdio.h>
    #include        <stdlib.h>
    #include        <termios.h> //串口配置中,终端的工作模式,是一个结构体
    
    #define  oops(s,x) { perror(s); exit(x); }
    //用于错误处理的宏
    
    int main(int argc, char *argv[])
    {
            struct termios info;
    
            if ( argc == 1 ) 
            exit(0);
    
            if ( tcgetattr(0,&info) == -1 )          /* 获取文件属性   */
                oops("tcgettattr", 1);
    
            if ( argv[1][0] == 'y' )
                    info.c_lflag |= ECHO ;          /* 设置打开标志位    */
            else
                    info.c_lflag &= ~ECHO ;         /* 设置关闭标志位   */
    
            if ( tcsetattr(0,TCSANOW,&info) == -1 ) /* 设置文件属性    */
                   oops("tcsetattr",2);
    
            return 0;
    }
    

    8. spwd.c

    功能:显示当前目录路径

    运行截图

    程序过程
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <sys/types.h>
    #include <sys/stat.h>
    #include <dirent.h>

    ino_t   get_inode(char *);  //  获取i-node节点号
    void    printpathto(ino_t); //  打印当前目录路径
    void    inum_to_name(ino_t , char *, int ); //根据i-node节点找到对应文件名
    
    int main()
    {
        printpathto( get_inode( "." ) );    
        putchar('
    ');              
        return 0;
    }
    
    void printpathto( ino_t this_inode )
    {
        ino_t   my_inode ;
        char    its_name[BUFSIZ];
    
        if ( get_inode("..") != this_inode )
        {
            chdir( ".." );              
    
            inum_to_name(this_inode,its_name,BUFSIZ);
    
            my_inode = get_inode( "." );        
            printpathto( my_inode );        
            printf("/%s", its_name );       
    
        }
    }
    
    void inum_to_name(ino_t inode_to_find , char *namebuf, int buflen)
    {
        DIR     *dir_ptr;       
        struct dirent   *direntp;       
    
        dir_ptr = opendir( "." );
        if ( dir_ptr == NULL ){
            perror( "." );
            exit(1);
        }
    
    
        while ( ( direntp = readdir( dir_ptr ) ) != NULL )
            if ( direntp->d_ino == inode_to_find )
            {
                strncpy( namebuf, direntp->d_name, buflen);
                namebuf[buflen-1] = '';   
                closedir( dir_ptr );
                return;
            }
        fprintf(stderr, "error looking for inum %d
    ", (int) inode_to_find);
        exit(1);
    }
    
    ino_t get_inode( char *fname )
    {
        struct stat info;
    
        if ( stat( fname , &info ) == -1 ){
            fprintf(stderr, "Cannot stat ");
            perror(fname);
            exit(1);
        }
        return info.st_ino; //返回i-node节点号
    }
    

    9. testioctl.c

    功能:查看窗体大小信息

    运行截图

    查看MAN帮助文档

    (1)isatty:判断一个文件描述符是否指向一个终端

    (2)ioctl:控制驱动器

    程序过程

    #include <stdio.h>
    #include <stdlib.h>
    #include <unistd.h>
    #include <sys/ioctl.h>
    
    int main()
    {
        struct winsize size;
        if( isatty(STDOUT_FILENO) == 0) //判断一个文件描述符是否指向一个终端
            exit(1);
        if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &size) < 0)    //控制驱动器 
        {
            perror("ioctl TIOCGWINSZ error");
            exit(1);
        }
    
        printf("%d rows %d columns
    ", size.ws_row, size.ws_col);
        return 0;
    }
    

    10. who1.c

    功能:查看当前登录用户信息

    程序过程
    #include <stdio.h>
    #include <stdlib.h>
    #include <utmp.h> // 定义用户信息结构体
    #include <fcntl.h>
    #include <unistd.h> //对 POSIX 操作系统 API 的访问功能的头文件

    #define SHOWHOST    
    
    int show_info( struct utmp *utbufp )
    {
        printf("%-8.8s", utbufp->ut_name);  
        printf(" ");                
        printf("%-8.8s", utbufp->ut_line);  
        printf(" ");                
        printf("%10ld", utbufp->ut_time);   
        printf(" ");                
    #ifdef  SHOWHOST
        printf("(%s)", utbufp->ut_host);    
    #endif
        printf("
    ");               
    
        return 0;
    }
    int main()
    {
        struct utmp  current_record;    
        int     utmpfd;     
        int     reclen = sizeof(current_record);
    
        if ( (utmpfd = open(UTMP_FILE, O_RDONLY)) == -1 ){
            perror( UTMP_FILE );    
            exit(1);
        }
        while ( read(utmpfd, &current_record, reclen) == reclen )
            show_info(&current_record);
        close(utmpfd);
        return 0;           
    }
    

    git

    遇到了点问题稍后补上

    学习进度条

    代码行数(新增/累积) 博客量(新增/累积) 学习时间(新增/累积) 重要成长
    目标 5000行 30篇 400小时
    第二周 50/50 1/3 12/25
    第三周 150/200 1/4 12/37
    第五周 50/250 2/6 18/55
    第六周 30/280 2/8 20/75
    第七周 30/310 1/9 20/95
    第八周 30/340 1/10 20/115
    第九周 50/390 1/11 20/135
    第十周 200/590 1/12 20/155

    参考资料

  • 相关阅读:
    web访问权限实现方法-探面向对象的编码设计
    解析二进制反码算数求和
    可以把erp当做一个分支-找自己的方向
    电脑开机是怎样自动加载进程
    球管模型和Java
    发明和发现
    漂浮
    js中的一些循环
    ES5中新增的一些方法
    git的一些操作
  • 原文地址:https://www.cnblogs.com/jokebright/p/6083971.html
Copyright © 2011-2022 走看看