标签:
代码的理解。
Who1.c
#include <stdio.h>//标准输入输出
#include <stdlib.h>//C标准函数库
#include <utmp.h>
#include <fcntl.h>//定义了很多宏和open,fcntl函数原型
#include <unistd.h>//Unix类系统定义符号常量
#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("\n");
return 0;
}
int main()
{
struct utmp current_record;
int utmpfd;
int reclen = sizeof(current_record);
if ( (utmpfd = open(UTMP_FILE, O_RDONLY)) == -1 ){/*打开UTMP_FILE读取信息,如果打开失败则输出失败信息。*/
perror( UTMP_FILE );
exit(1);
}/*读取信息到存储器中,reclen就是是读的字节数,然后再调用函数打印出来。*/
while ( read(utmpfd, ¤t_record, reclen) == reclen )
show_info(¤t_record);
close(utmpfd);
return 0;
}
/*从UTMP_FILE文件中读取信息到存储器中,然后再用标准输出函数打印到屏幕上,最后关闭文件*/
Ls1.c
#include <stdio.h>
#include <sys/types.h>//基本系统数据类型
#include <dirent.h>//unix类目录操作的头文件
void do_ls(char []);
int main(int argc, char *argv[])/*如果操作数只有1个,表明ls后面没有带参数,默认为当前目录*/
{
if ( argc == 1 )
do_ls( "." );
else/*如果ls后面有参数,就把参数读入argv中。*/
while ( --argc ){
printf("%s:\n", *++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\n", dirname);
else
{
while ( ( direntp = readdir( dir_ptr ) ) != NULL )
printf("%s\n", direntp->d_name );
closedir(dir_ptr);
}
}
echostate.c
#include <stdio.h>
#include <stdlib.h>
#include <termios.h>
int main()
{
struct termios info;
int rv;
rv = tcgetattr( 0, &info ); /* read values from driver */
if ( rv == -1 ){
perror( "tcgetattr");
exit(1);
}
if ( info.c_lflag & ECHO )
printf(" echo is on , since its bit is 1\n");
else
printf(" echo is OFF, since its bit is 0\n");
return 0;
}/* 检查命令行中的提示符是否显示的,如果显示,输入的命令都可见,不显示则表示输入的命令不可见*/
Sstecho.c
#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 ) /* get attribs */
oops("tcgettattr", 1);
if ( argv[1][0] == ‘y‘ )
info.c_lflag |= ECHO ; /* turn on bit */
else
info.c_lflag &= ~ECHO ; /* turn off bit */
if ( tcsetattr(0,TCSANOW,&info) == -1 ) /* set attribs */
oops("tcsetattr",2);
return 0;
}
/*当echo is on的时候,输入的指令是可见的,当设置为off的时候,输入指令不可见*/
Filesize.c/*用st_size成员来计算文件的字节数大小,先判断是否有错误,没有的话就调用*/
#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\n", infobuf.st_size );
}
Fileinfo.c
#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将报错信息打印出来*/
perror(argv[1]);
}
return 1;
}
void show_stat_info(char *fname, struct stat *buf)
{
printf(" mode: %o\n", buf->st_mode);
printf(" links: %d\n", buf->st_nlink);
printf(" user: %d\n", buf->st_uid);
printf(" group: %d\n", buf->st_gid);
printf(" size: %d\n", (int)buf->st_size);
printf("modtime: %d\n", (int)buf->st_mtime);
printf(" name: %s\n", fname );
}
/*这个功能用来实现显示文件信息,建立了一个stat数据结构 */
Cp1.c
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#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\n", *argv);//argc的值不为三,返回标准错误
exit(1);
}
if ((in_fd = open(argv[1], O_RDONLY)) == -1)
oops("Cannot open ", argv[1]);//open打开如果返回-1,打开失败
if ((out_fd = creat(argv[2], COPYMODE)) == -1)//用create在目的地址创建新文件,out_fd为open返回的描述符
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);
}
参考资料:闫佳欣同学上周的实践总结心得:C语言基础不好,通过百度了解了每个单词每个头文件的意思,通过参考和自学,大致理解了每个代码的基本功能。
标签:
原文地址:http://www.cnblogs.com/java44/p/4967212.html