dup chdir getcwd ls 管道

上午没有上课调到了晚上,今天心情有点阴暗,linux中目录的东西没有听懂,上课的时候抑制不住的困,下午讲了dup getcwd chdirstat目录(没听懂),晚上讲的管道,都听懂了,并且亲自动手码了一遍,成功。现将今天学的代码贴一下:

1、getcwd()

 

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

> File Name: my_cwd.c

> Author: Comst

> Mail:750145240@qq.com 

> 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:750145240@qq.com 

  > 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:750145240@qq.com 

> 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:750145240@qq.com 

> 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:750145240@qq.com 

> 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:750145240@qq.com 

> 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:750145240@qq.com 

> 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 ;

}

 

 

 

 

 

 

很好,程序已经实现了我想要的所有功能。现在有两个要求:1.注释全部改成英文。2.在运行程序时用printf给出一些提示信息,同样用英文写。 #include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> #include <sys/wait.h> #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include <signal.h> #include <errno.h> #define MAX_INPUT 1024 // 最大输入长度 #define MAX_ARGS 64 // 最大参数数量 #define MAX_PATH 1024 // 最大路径长度 int main() { char input[MAX_INPUT] = {0}; // 初始化输入缓冲区 char *args[MAX_ARGS] = {NULL}; // 初始化参数数组 char current_dir[MAX_PATH] = {0}; // 初始化当前目录 pid_t pid = 0; int pipe_fd[2] = {0}; // 管道文件描述符 // 获取当前工作目录 if (getcwd(current_dir, MAX_PATH) == NULL) { perror("getcwd failed"); exit(EXIT_FAILURE); } while (1) { // 显示提示符(包含当前目录) printf("\033[1;32m%s\033[0m$ ", current_dir); // 绿色显示路径 fflush(stdout); // 读取用户输入 if (fgets(input, MAX_INPUT, stdin) == NULL) { break; // 处理EOF (Ctrl+D) } // 移除换行符 input[strcspn(input, "\n")] = '\0'; // 跳过空输入 if (strlen(input) == 0) { continue; } // 处理退出命令 if (strcmp(input, "exit") == 0) { break; } // 解析命令和参数 int i = 0; char *token = strtok(input, " "); while (token != NULL && i < MAX_ARGS - 1) { args[i++] = token; token = strtok(NULL, " "); } args[i] = NULL; // execvp要求以NULL结尾 // 检查cd命令(在父进程执行) if (strcmp(args[0], "cd") == 0) { char *path = (i > 1) ? args[1] : getenv("HOME"); if (chdir(path) == -1) { perror("chdir failed"); } else { // 更新当前目录显示 if (getcwd(current_dir, MAX_PATH) == NULL) { perror("getcwd failed"); } } continue; // cd不需要创建子进程 } // 创建管道用于读取命令输出 if (pipe(pipe_fd) == -1) { perror("pipe failed"); continue; } // 创建子进程 pid = fork(); if (pid < 0) { perror("fork failed"); close(pipe_fd[0]); close(pipe_fd[1]); continue; } if (pid == 0) { // 子进程 // 关闭管道读端 close(pipe_fd[0]); // 重定向标准输出到管道写端 dup2(pipe_fd[1], STDOUT_FILENO); close(pipe_fd[1]); // 执行命令 execvp(args[0], args); // 如果execvp失败 perror("execvp failed"); exit(EXIT_FAILURE); } else { // 父进程 // 关闭管道写端 close(pipe_fd[1]); char buffer[MAX_INPUT]; ssize_t bytes_read; int stop_detected = 0; // 读取命令输出 while ((bytes_read = read(pipe_fd[0], buffer, MAX_INPUT - 1)) > 0) { buffer[bytes_read] = '\0'; // 确保字符串终止 printf("%s", buffer); // 打印输出 fflush(stdout); // 检查是否包含"stop" if (strstr(buffer, "stop") != NULL) { stop_detected = 1; break; } } // 关闭管道读端 close(pipe_fd[0]); if (stop_detected) { // 发送终止信号 if (kill(pid, SIGTERM) == -1 && errno != ESRCH) { perror("kill failed"); } printf("Command terminated by 'stop' signal\n"); } // 等待子进程结束 waitpid(pid, NULL, 0); } // 重置参数数组 memset(args, 0, sizeof(args)); } return 0; }
最新发布
08-07
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值