minishell中实现重定向功能
捕捉键盘输入 --ls -a \0> test.txt 解析输入信息(重定向类型、重定向文件名称、命名名称、运行参数) · 根据命令中的>确定是否包含重定向,根据个数确定重定向类型 · >之前是基础指令部分,解析出命令名称和运行参数 · >之后是重定向信息部分,解析出重定向类型、重定向文件名称 创建子进程 在程序替换之前,进行标准输出重定向到指定的文件,open,dup2 在子进程中进行程序替换(替换失败要退出子进程) 在父进程中进行进程等待
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/wait.h>
int main ( int argc, char * argv[ ] )
{
while ( 1 ) {
printf ( "[user@host path]$ " ) ;
fflush ( stdout ) ;
char cmd[ 1024 ] = { 0 } ;
fgets ( cmd, 1023 , stdin ) ;
cmd[ strlen ( cmd) - 1 ] = '\0' ;
int direct_flag = 0 ;
char * ptr = cmd;
char * redirect_file = NULL ;
while ( * ptr != '\0' ) {
if ( * ptr == '>' ) {
direct_flag = 1 ;
* ptr = '\0' ;
ptr++ ;
if ( * ptr == '>' ) {
direct_flag = 2 ;
* ptr = '\0' ;
ptr++ ;
}
while ( * ptr != '\0' && * ptr == ' ' ) ptr++ ;
redirect_file = ptr;
while ( * ptr != '\0' && * ptr != ' ' ) ptr++ ;
* ptr = '\0' ;
}
ptr++ ;
}
ptr = cmd;
char * arg[ 32 ] = { NULL } ;
int ac = 0 ;
while ( * ptr != '\0' ) {
if ( ! isspace ( * ptr) ) {
arg[ ac] = ptr;
ac++ ;
while ( * ptr != '\0' && ! isspace ( * ptr) ) ptr++ ;
* ptr = '\0' ;
}
ptr++ ;
}
arg[ ac] = NULL ;
if ( strcmp ( arg[ 0 ] , "cd" ) == 0 ) {
chdir ( arg[ 1 ] ) ;
continue ;
}
pid_t pid = fork ( ) ;
if ( pid < 0 ) {
continue ;
} else if ( pid == 0 ) {
if ( direct_flag == 1 ) {
int fd;
fd = open ( redirect_file,
O_CREAT| O_RDWR| O_TRUNC, 0664 ) ;
dup2 ( fd, 1 ) ;
} else if ( direct_flag == 2 ) {
int fd;
fd = open ( redirect_file,
O_CREAT| O_RDWR| O_APPEND, 0664 ) ;
dup2 ( fd, 1 ) ;
}
execvp ( arg[ 0 ] , arg) ;
exit ( - 1 ) ;
}
wait ( NULL ) ;
}
return 0 ;
}