linux中模拟sh.c
来源:互联网 发布:pmp含金量 知乎 编辑:程序博客网 时间:2024/06/10 06:04
#include<stdlib.h>#include<string.h>#include<stdio.h>#include<signal.h>#defineMAXARGS20/* cmdline args*/#defineARGLEN100/* token length*/main(){char*arglist[MAXARGS+1];/* an array of ptrs*/intnumargs;/* index into array*/charargbuf[ARGLEN];/* read stuff here*/char*makestring();/* malloc etc*/numargs = 0;while ( numargs < MAXARGS ){printf("Arg[%d]? ", numargs);if ( fgets(argbuf, ARGLEN, stdin) && *argbuf != '\n' )arglist[numargs++] = makestring(argbuf);else{if ( numargs > 0 ){/* any args?*/arglist[numargs]=NULL;/* close list*/execute( arglist );/* do it*/numargs = 0;/* and reset*/}}}return 0;}execute( char *arglist[] )/* *use fork and execvp and wait to do it */{intpid,exitstatus;/* of child*/pid = fork();/* make new process */switch( pid ){case -1:perror("fork failed");exit(1);case 0:execvp(arglist[0], arglist);/* do it */perror("execvp failed");exit(1);default:while( wait(&exitstatus) != pid );printf("child exited with status %d,%d\n",exitstatus>>8, exitstatus&0377);}}char *makestring( char *buf )/* * trim off newline and create storage for the string */{char*cp;buf[strlen(buf)-1] = '\0';/* trim newline*/cp = malloc( strlen(buf)+1 );/* get memory*/if ( cp == NULL ){/* or die*/fprintf(stderr,"no memory\n");exit(1);}strcpy(cp, buf);/* copy chars*/return cp;/* return ptr*/}
阅读全文
0 0
- linux中模拟sh.c
- 在Linux中执行.sh脚本,异常/bin/sh^M
- linux在sh中运行java程序
- Linux 中进程的查看.sh文件
- linux中 sh 与 ./ filename 命令
- 在Linux中执行.sh脚本,异常
- Linux中bash与sh的区别
- sh linux
- linux sh
- Linux中.sh脚本,异常/bin/sh^M: bad interpreter: No such file or directory
- 在Linux中执行.sh脚本,异常/bin/sh^M: 解决办法
- 在Linux中执行.sh脚本,/bin/sh^M: bad interpreter: No such file or directory
- Linux中执行.sh脚本,异常/bin/sh^M: bad interpreter: No such file or directory。
- Linux中执行.sh脚本,异常/bin/sh^M: bad interpreter: No such file or directory
- Linux中执行.sh脚本,异常/bin/sh^M: bad interpreter: No such file or directory。
- linux c模拟ls命令
- linux c 模拟闹钟程序
- linux c语言 模拟键盘输入
- 项目整合memcached提高前后交互效率
- IE8样式不正确显示问题 -在IE8上样式表格错乱
- ajxa的跨域处理
- CentOS下NTP安装配置
- linux编程之信号
- linux中模拟sh.c
- Angular学习:http请求失败的问题
- Android WebView与JS页面相互调用(四)
- linux安装,配置,使用Node.js问题总结
- 数组、List中,删除容器中的某些元素时使用倒叙删除
- jquery点击回车键实现登录效果并默认焦点
- 【java-算法】【华为真题】老师想知道从某某同学当中,分数最高的是多少,现在请你编程模拟老师的询问。当然,老师有时候需要更新某位同学的成绩.
- mysql Error 1040 too many connection解决办法
- JTA 深度历险