UNIX第一章

unix高级编程1-1 

#include "apue.h"
#include <dirent.h>
#include <error.h>

int main (int argc, char *argv[])
{
DIR *dp;
struct dirent *dirp;
if (argc != 2)                                           -----输入的是 ./a.out filepath
err_quit ("usage: ls directory_name");      
if((dp = opendir(argv[1])) == NULL)         ------打开filepath目录句柄,argv[1]指定目录对象,成功将返回一组目录流;错返回error
err_sys("can not open %s", argv[1]);        
while ((dirp = readdir(dp))!= NULL)          ------返回是dirent结构体指针,参数是目录对象
printf("%s\n", dirp ->name);                  ------dirent结构体:long d_ino:索引节点号 ;off_t d_off:在目录文件中的偏移;unsigned short d_reclen:文件名长;unsigned char d_type:文件类型 ;char d_name [NAME_MAX+1]:文件名,最255字符 

closedir(dp);
exit(0);
}

DIR

struct __dirstream
{
void *__fd;
char *__data;
int __entry_data;
char *__ptr;
int __entry_ptr;
size_t __allocation;
size_t __size;
__libc_lock_define (, __lock)
};

typedef struct __dirstream DIR;  

DIRENT

struct dirent

{
long d_ino; /* inode number 索引节点号 */
  
off_t d_off; /* offset to this dirent 在目录文件中的偏移 */
  
unsigned short d_reclen; /* length of this d_name 文件名长 */
  
unsigned char d_type; /* the type of d_name 文件类型 */
  
char d_name [NAME_MAX+1]; /* file name (null-terminated) 文件名,最长255字符 */
}

 

int stat(const char *file_name, struct stat *buf); 

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;     //该文件所占的磁盘块   
  };  

 unix高级编程1-2

#include "apue.h"

#define BUFFSIZE 4096

int main (void)
{
int n;
char buf[BUFFSIZE];
while((n= read(STDIN_FILENO, buf, BUFFSIZE))>0)           //把STDIN_FIFENO里面BUFFSIZE个字节传送到buf中去(当然不能大于buf的bufsize),返回大于0为1实际传送的
if(write(STDOUT_FILENO, buf, n)!= n)                              //buf里面数据写入n个到STDOUT_FIFENO中去
if (n<0)
err_sys("read error");

exit(0);
}
 

unix高级编程1-3

#include "apue.h"

int main(void)
{
int c;
while ((c= getc(stdin))!=EOF)                 //从stdin中一次取得一个字符;EOF:文字流的结尾,endoffile
if (putc(c,stdout) ==EOF)                       //输出一个字符到指定流中,此处每上一条取得一个字符,此处往指定流中putc一个字符,所以当上一条能够取得一个字符,此处缺不能put出一个字符,就是有问题了
err_sys("output error");
if(ferror(stdin))
err_sys("input error");
exit(0);
}

unix高级编程1-4

#include "apue.h"
#include <sys/wait.h>

int main(void)
{
char buf[MAXLINE];
pid_t pid;
int status;

printf("%% ");

while(fgets(buf, MAXLINE, stdin) != NULL)               //与putc有本质区别,putc每次取得一个字符,放在指定流中;fgets每次从stdin取得一行,取得之后放在buf里面,每次最多读MAXLINE-1个字符
{
if (buf[strlen(buf)-1] == '\n')                                  //如果取得的最末一位是'\n',这个是换行符

buf[strlen(buf)-1]=0;                                             //最后一个字符换成0,execlp要求以参数NULL结束,而不是换行符
}
if ((pid = fork())<0)                                               //fork()<0说明创建失败了,之所以要创建一个子程序,是因为execlp需要另起一个进程才可以,不能在原进程直接运行
{err_sys("fork error");}
else if(pid == 0)                                                   //fork() ==0 是子进程
{
execlp(buf ,buf,(char *)0);                                     //execl是用来执行参数path字符串所代表的文件路径,并为新程序复制最后一个参数所指示的环境变量。接下来的参数代表执行该文件时传递过去的argv(0)……,最后一个参数必须用空指针(NULL)作结束。
err_ret("could not execute: %s",buf);

exit(127);
}

if((pid = waitpid(pid, &status, 0))<0)                        
err_sys("waitpid error");
printf("%%");
}                                                                        //while到此处才结束,说明以上操作都是一行一行进行的

exit(0);
}

 

 

posted @ 2016-12-08 20:12  尹盛  阅读(142)  评论(0)    收藏  举报