cmd命令行换行

在cmd命令行窗口,可以使用^符号来进行换行,类似linux的\换行符,举例:

netstat ^
-ano

上边的指令和使用netstat -ano指令的效果一样。

 

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个简单的myshell代码,支持上述要求的命令: ```c #include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> #include <sys/wait.h> #include <fcntl.h> #include <errno.h> #include <dirent.h> #define MAX_CMD_LEN 1024 #define MAX_ARG_NUM 64 #define MAX_PATH_LEN 256 char* envp[]; void print_prompt() { char path[MAX_PATH_LEN]; getcwd(path, MAX_PATH_LEN); printf("[myshell]%s$ ", path); fflush(stdout); } void parse_cmd(char* cmd, char** args, int* redirect_in, int* redirect_out) { char* ptr = strtok(cmd, " \t\n"); int index = 0; while (ptr != NULL) { if (strcmp(ptr, "<") == 0) { // 输入重定向 ptr = strtok(NULL, " \t\n"); *redirect_in = open(ptr, O_RDONLY); if (*redirect_in < 0) { perror("open"); exit(1); } } else if (strcmp(ptr, ">") == 0) { // 输出重定向 ptr = strtok(NULL, " \t\n"); *redirect_out = open(ptr, O_WRONLY | O_CREAT | O_TRUNC, 0666); if (*redirect_out < 0) { perror("open"); exit(1); } } else { args[index++] = ptr; } ptr = strtok(NULL, " \t\n"); } args[index] = NULL; } void exec_cmd(char** args, int redirect_in, int redirect_out) { int pid = fork(); if (pid < 0) { perror("fork"); exit(1); } else if (pid == 0) { // 子进程 if (redirect_in != 0) { dup2(redirect_in, STDIN_FILENO); close(redirect_in); } if (redirect_out != 0) { dup2(redirect_out, STDOUT_FILENO); close(redirect_out); } if (execvp(args[0], args) < 0) { perror("execvp"); exit(1); } } else { // 父进程 wait(NULL); } } void cd(char* path) { if (chdir(path) < 0) { perror("chdir"); } } void environ() { char** env = envp; while (*env) { printf("%s\n", *env++); } } void ls(char* path) { DIR* dir = opendir(path); if (dir == NULL) { perror("opendir"); exit(1); } struct dirent* entry; while ((entry = readdir(dir)) != NULL) { printf("%s\n", entry->d_name); } closedir(dir); } void help() { printf("cd [directory]: change current working directory\n"); printf("environ: list all environment variables\n"); printf("ls [directory]: list all files in directory\n"); printf("help: display all supported commands\n"); printf("echo [args]: display arguments on the screen\n"); printf("quit: exit the shell\n"); } void echo(char** args) { int i = 1; while (args[i] != NULL) { printf("%s ", args[i++]); } printf("\n"); } int main() { char cmd[MAX_CMD_LEN]; char* args[MAX_ARG_NUM]; int redirect_in, redirect_out; while (1) { redirect_in = redirect_out = 0; print_prompt(); if (fgets(cmd, MAX_CMD_LEN, stdin) == NULL) { printf("\n"); exit(0); } parse_cmd(cmd, args, &redirect_in, &redirect_out); if (args[0] == NULL) { continue; } if (strcmp(args[0], "cd") == 0) { cd(args[1]); } else if (strcmp(args[0], "environ") == 0) { environ(); } else if (strcmp(args[0], "ls") == 0) { ls(args[1]); } else if (strcmp(args[0], "help") == 0) { help(); } else if (strcmp(args[0], "echo") == 0) { echo(args); } else if (strcmp(args[0], "quit") == 0) { exit(0); } else { exec_cmd(args, redirect_in, redirect_out); } } return 0; } ``` 编译后直接运即可。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值