#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;//0-没有,1-清空,2-追加
char *ptr = cmd;
char *redirect_file = NULL;
//[ ls -a \0\0 text.txt\0]
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;
//[ ls -a ]
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;
}
简介 “ 大家好我是帅哥欢迎来到帅哥的程序人生我会把经历分享出来助你了解圈内...
console.log ,作为一个前端开发者,可能每天都会用它来分析调试,但这个简单函...
一、Postman背景介绍 用户在开发或者调试网络程序或者是网页B/S模式的程序的时候...
开发过程中,我们经常会遇到代码回滚的情况。正常人都知道,git 回滚有两大宝: ...
本文转载自微信公众号「Linux开发那些事儿」,作者 LinuxThings 。转载本文请联...
前言 aop即是面向切面编程,众多Aop框架里Castle是最为人所知的,另外还有死去的...
互联网业务往往使用MySQL数据库作为后台存储,存储引擎使用InnoDB。我们针对互联...
不少Windows 10用户之前都抱怨一个问题,那就是系统的屏幕出现了渲染问题,而微...
2月26日消息 众所周知,Windows 10 的安全更新和其他重要累计更新通常是在同一天...
继 Australis 和 Photon 之后,Mozilla 现又酝酿为 Firefox 带来名为Proton的全...