dup chdir getcwd ls 管道

上午沒有上課調到了晚上,今天心情有點陰暗,linux中目錄的東西沒有聽懂,上課的時候抑制不住的困,下午講了dup getcwd chdirstat目錄(沒聽懂),晚上講的管道,都聽懂了,並且親自動手碼了一遍,成功。現將今天學的代碼貼一下:

1、getcwd()

 

/*************************************************************************

> File Name: my_cwd.c

> Author: Comst

> Mail:[email protected] 

> Created Time: Wed 28 Jan 2015 03:43:26 PM CST

 ************************************************************************/

 

#include<stdio.h>

#include<stdlib.h>

#include<string.h>

#include<unistd.h>

int main(int argc, char* argv[])

{

char* pdir = getcwd(NULL, 0);

printf("cwd1: %s\n", pdir);

chdir("/home/comst");

 pdir = getcwd(NULL, 0);

printf("cwd2: %s\n", pdir);

while(1);

return 0 ;

}

 

2、Dup

 

/*************************************************************************

  > File Name: my_dup.c

  > Author: Comst

  > Mail:[email protected] 

  > Created Time: Wed 28 Jan 2015 03:01:40 PM CST

 ************************************************************************/

 

#include<stdio.h>

#include<string.h>

#include<sys/stat.h>

#include<sys/types.h>

#include<fcntl.h>

#include<unistd.h>

int main(int argc, char* argv[])

{

char* p = "hello world! \n" ;

int fd = open(argv[1], O_WRONLY | O_CREAT, 0666);

dup2(fd, 1);

close(fd);

read(0);

write(1 , p, strlen(p));

return 0 ;

}

3、chdir()

 

/*************************************************************************

> File Name: my_rdir.c

> Author: Comst

> Mail:[email protected] 

> Created Time: Wed 28 Jan 2015 03:56:59 PM CST

 ************************************************************************/

 

#include<stdio.h>

#include<sys/types.h>

#include<dirent.h>

#include<stdlib.h>

int main(int argc, char* argv[])

{

 

DIR* fp_dir ;

struct dirent* pent ;

//chdir("..");

fp_dir = opendir(argv[1]);

if(fp_dir == NULL)

{

perror("open");

exit(1);

}

printf("OK!\n");

while((pent = readdir(fp_dir)) != NULL )

{

printf("ino: %u\noff_t:%u\nreclen:%u\nname: %s\ntype:%x\n" ,pent ->d_ino,pent ->d_off,pent ->d_reclen, pent ->d_name, pent ->d_type );

printf("--------------------------------\n");

}

closedir(fp_dir);

return 0 ;

}

4、ls -l 

//ls.h

#ifndef MY_LS

#define MY_LS

#include<stdio.h>

#include<sys/types.h>

#include<sys/stat.h>

#include<unistd.h>

#include<stdlib.h>

#include<string.h>

#include<grp.h>

#include<pwd.h>

#include<time.h>

#include<dirent.h>

void show_info(char* path);

#endif

 

//ls.c

/*************************************************************************

> File Name: my_ls.c

> Author: Comst

> Mail:[email protected] 

> Created Time: Wed 28 Jan 2015 05:03:33 PM CST

 ************************************************************************/

#include"my_ls.h"

int main(int argc, char* argv[])

{

 

DIR* fp_dir ;

struct dirent* pent ;

 

fp_dir = opendir(argv[1]);

if(fp_dir == NULL)

{

perror("open");

exit(1);

}

printf("OK!\n");

while((pent = readdir(fp_dir)) != NULL )

{

show_info(pent ->d_name);

}

closedir(fp_dir);

return 0 ;

}

//show_info

 

/*************************************************************************

> File Name: show_info.c

> Author: Comst

> Mail:[email protected] 

> Created Time: Wed 28 Jan 2015 05:08:19 PM CST

 ************************************************************************/

#include"my_ls.h"

static void mode_to_str(mode_t md, char* buf) ;

static char* time_handle(char* pt);

void show_info(char* path)

{

struct stat my_stat ;

char buf[11] ;

memset(&my_stat, 0, sizeof(my_stat));

if(-1 == stat(path, &my_stat))

{

perror("stat");

exit(1);

 

}

mode_to_str(my_stat.st_mode, buf);

printf("%s.%3d%8s%8s%5d %s %s\n", buf, my_stat.st_nlink, getpwuid(my_stat.st_uid) ->pw_name, getgrgid(my_stat.st_gid) -> gr_name, my_stat.st_size, time_handle( ctime(&my_stat.st_mtime) ), path);

}

static void mode_to_str(mode_t md, char* buf)

{

if(S_ISREG(md))

{

buf[0] ='-' ;

}else if(S_ISDIR(md))

{

buf[0] ='d';

}else if(S_ISCHR(md))

{

buf[0] = 'c' ;

}else if(S_ISBLK(md))

{

buf[0]='b';

}else if(S_ISFIFO(md))

{

buf[0] = 'p';

}else if(S_ISLNK(md))

{

buf[0] = 'l' ;

}else

{

buf[0] = 's' ;

}

if(md & S_IRUSR)

{

buf[1] = 'r' ;

}else

{

buf[1] = '-' ;

}

 

if(md & S_IWUSR)

{

buf[2] = 'w' ;

}else

{

buf[2] = '-' ;

}

 

if(md & S_IXUSR)

{

buf[3] = 'x' ;

}else

{

buf[3] = '-' ;

}

 

 

if(md & S_IRGRP)

{

buf[4] = 'r' ;

}else

{

buf[4] = '-' ;

}

 

if(md & S_IWGRP)

{

buf[5] = 'w' ;

}else

{

buf[5] = '-' ;

}

 

if(md & S_IXGRP)

{

buf[6] = 'x' ;

}else

{

buf[6] = '-' ;

}

 

if(md & S_IROTH)

{

buf[7] = 'r' ;

}else

{

buf[7] = '-' ;

}

 

if(md & S_IWOTH)

{

buf[8] = 'w' ;

}else

{

buf[8] = '-' ;

}

if(md & S_IXOTH)

{

buf[9] = 'x' ;

}else

{

buf[9] = '-' ;

}

buf[10] = 0;

 

}

static char* time_handle(char* pt)

{

pt[strlen(pt) - 1] = 0 ;

return pt + 4 ;

}

 

 

5、管道通信 cl1發 cl2 收  cl2 發 cl1

 

/*************************************************************************

> File Name: cl1.c

> Author: Comst

> Mail:[email protected] 

> Created Time: Wed 28 Jan 2015 08:22:04 PM CST

 ************************************************************************/

 

#include<stdio.h>

#include<stdlib.h>

#include<string.h>

#include<sys/types.h>

#include<sys/stat.h>

#include<fcntl.h>

#include<unistd.h>

int main(int argc, char* argv[])//cl1->cl2  cl2->cl1 

{

int fd_recv = open(argv[2], O_RDONLY);

int fd_send = open(argv[1], O_WRONLY);

if(fd_send == -1 || fd_recv == -1)

{

perror("open");

exit(1);

}

printf("OK!\n");

char buf[1024] ;

while(memset(buf, 0, 1024), read(0, buf, 1024) != 0)

{

write(fd_send, buf, strlen(buf));

memset(buf, 0, 1024);

read(fd_recv, buf, 1024);

printf("from cl2: %s\n", buf);

}

close(fd_send);

close(fd_recv);

return 0 ;

}

 

 

 

 

/*************************************************************************

> File Name: cl2.c

> Author: Comst

> Mail:[email protected] 

> Created Time: Wed 28 Jan 2015 08:22:10 PM CST

 ************************************************************************/

 

#include<stdio.h>

#include<stdlib.h>

#include<string.h>

#include<sys/types.h>

#include<sys/stat.h>

#include<fcntl.h>

#include<unistd.h>

int main(int argc, char* argv[])//cl2 ->cl1 cl1 ->cl2 

{

int fd_send = open(argv[1], O_WRONLY);

int fd_recv = open(argv[2], O_RDONLY);

if(fd_send == -1 || fd_recv == -1) 

{

perror("open");

exit(1);

}

printf("OK!\n");

char buf[1024] ;

while(memset(buf, 0, 1024),read(fd_recv, buf, 1024) != 0)

{

printf("from cl1: %s\n", buf);

memset(buf, 0, 1024);

read(0, buf, 1024);

write(fd_send, buf, strlen(buf));

}

printf("cl1 exit!\n");

close(fd_send);

close(fd_recv);

return 0 ;

}

 

 

 

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章