echostate.c
&setecho.c
:
tcgetattr(int fd, & termios info)函数
:用于获取和终端相关的参数赋给infotcsetattr(int fd, int actions,&termios info)
:设置与终端相关的参数,actions参数决定修改何时生效,TCSANOW为改变当即发生结构体termios
:是在POSIX规范中定义的标准接口,用于存储一些和终端接口相关的信息man -k terminal|grep set|grep 1
能够找到系统自带的设置终端属性命令stty
,stty echo
即打开echo.stty -echo
关闭echoc_lflag
、c_lflag|ECHO
和c_lflag&~ECHO
的值的变化,编写一个echotest将三者值输出出来,当echo打开时三者值以下:
可见不管echo是打开仍是关闭,c_lflag|ECHO
和c_lflag&~ECHO
的值始终没有改变,不过具体缘由尚未想清楚,要继续查询或者GDB调试或许能更清楚具体是怎么变化的node
filesize
&fileinfo
:
stat结构体具体以下:linux
struct stat { mode_t st_mode; //文件访问权限 ino_t st_ino; //索引节点号 dev_t st_dev; //文件使用的设备号 dev_t st_rdev; //设备文件的设备号 nlink_t st_nlink; //文件的硬链接数 uid_t st_uid; //全部者用户识别号 gid_t st_gid; //组识别号 off_t st_size; //以字节为单位的文件容量 time_t st_atime; //最后一次访问该文件的时间 time_t st_mtime; //最后一次修改该文件的时间 time_t st_ctime;//最后一次改变该文件状态的时间 blksize_t st_blksize; //包含该文件的磁盘块的大小 blkcnt_t st_blocks; //该文件所占的磁盘块 };
filesize.c
中显示的文件时间是直接输出st_mtime是系统时间,对于用户来讲并不友好,可是能够调用函数ctime(),将输出模式改为用户友好的年月日(localtime()能够将其修改为对应中文格式)ls1.c
&ls2.c
linux ls
命令中还涉及目录的操做,因此除了stat结构体以外,还须要用到目录相关的结构体DIR
&dirent
DIR
结构体以下:ios
struct __dirstream { void *__fd; char *__data; int __entry_data; char *__ptr; int __entry_ptr; size_t __allocation; size_t __size; __libc_lock_define (, __lock) };
dirent
结构体以下:安全
struct dirent { long d_ino; /* 索引节点号 */ off_t d_off; /* 在目录文件中的偏移 */ unsigned short d_reclen; /* 文件名长 */ unsigned char d_type; /*文件类型 */ char d_name [NAME_MAX+1]; /*文件名 */ }
经过DIR *opendir(const char *filename)
和dirent *readdir(DIR *dirp)
等目录相关函数取得相应数据输出便可(opendir()对应记得closedir(),目录的打开,读取能够类比c语言中FILE的打开,读取,这也从侧面体现了在linux系统中一切都是以文件的形式来管理的)函数
spwd.c
spwd.c
要输出当前的工做路径,因此在编写的总体思路上采用一种循环调用的思路:
get_inode函数
&printpathto函数
&inum_to_name函数
get_inode()
printpaththo()
ui
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 ); } }
chdir(..)
即切换至其父目录的函数inum_to_name()