linux下自定义的bash
来源:互联网 发布:小米盒子 电视直播软件 编辑:程序博客网 时间:2024/06/05 17:47
#include <stdio.h>#include <stdlib.h>#include <assert.h>#include <unistd.h>#include <string.h>#include <signal.h>#include <pwd.h>#define MAX_ARGV_NUM 10#define PATH //"/home/Desktop/project/mybin/exec/" 通过exec调用的可执行文件的目的路径void printinfo();int main(int agrc, char *agrv[]){ char *myargv[MAX_ARGV_NUM] = {0}; while(1) {memset(myargv, 0, MAX_ARGV_NUM);//避免前次的输入对下此的影响 printinfo(); char buff[128] = {0};fgets(buff, 128, stdin);//从标准输入获取命令 buff[strlen(buff)-1] = 0; myargv[0] = strtok(buff, " "); int i = 0; char *s = NULL; while ((s = strtok(NULL, " ")) != NULL) {myargv[++i] = s; } int tag = 0;if (myargv[0] == NULL){continue;} if (!(strcmp("exit", myargv[0]))) {exit(0); } if (!(strcmp(myargv[0], "cd"))) {if (chdir(myargv[1]) == -1){printf("error\n");continue;}}char path_info[256] = {0};if (strncmp(buff, "/", 1)!=0 && strncmp(buff, "./", 2)!=0){strcpy(path_info, PATH);} int pid = fork(); assert(pid != -1);strcat(path_info, myargv[0]); if (pid == 0) {//execvp(myargv[0], myargv);execv(path_info, myargv);printf("bash: command not found\n");exit(0); } wait(NULL); }}void printinfo(){ uid_t uid = getuid(); char s = '#'; if (uid != 0){s = '$'; } struct passwd *p = getpwuid(uid);//获取当前用户的uid assert(p!=NULL); char hostname[128] = {0} ; gethostname(hostname, 128);//获取当前用户的名字 char address[256] = {0};if (getcwd(address, 256) == NULL) { perror("error"); } char *q1 = "/"; char *q = strtok(address, "/"); while(q != NULL){q1 = q; q = strtok(NULL,"/");} printf("[%s@%s %s]%c",p->pw_name, hostname, q1, s);}
0 0
- linux下自定义的bash
- Linux下的bash
- Linux下的bash配置文件
- Linux下的bash配置文件
- Linux下bash的快捷键
- linux下的bash shell
- linux自动装载Windows下的分区以及bash启动脚本自定义命令
- linux自动装载Windows下的分区以及bash启动脚本自定义命令
- Linux下的Bash基础知识,tab,type,\
- linux下的sh和bash区别
- linux下基于bash环境变量的详解
- 汇总linux下的bash快捷键
- Linux下的Bash基础知识,tab,type,\
- Win10下的Linux子系统之Bash
- Linux下source bash sh ./的区别
- Linux下Bash Shell的常用配置文件
- Linux下配置自己的bash
- 自己写的一个linux下的bash安装
- hadoop 2.7.3 源码分析(一):环境搭建
- 使用Oracle数据库登录时被告知用户被锁怎么解决
- MIT 大牛林达华
- [2016NOIPPJ04]魔法阵
- Jquery的each里面return作用
- linux下自定义的bash
- NOIP2016小结
- 汇编idiv带符合除法指令。。。。
- Swift 学习笔记---Optional Chaning
- shell脚本分析 nginx日志访问次数最多及最耗时的页面(慢查询)
- noip2016退役记
- 玩转矩阵的C小加
- 林达华推荐的几本数学书
- 数据降维和特征选择