Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <sys/wait.h>
- #include <iostream>
- #include <unistd.h>
- #include <stdio.h>
- #include <cstdlib>
- #include <string.h>
- using namespace std;
- char** getlastline(int& l){
- char** ans=NULL;
- char c;
- l=0;
- while(true){
- cin>>c;
- while (c==' ') cin>>c;
- if ((c=='\n') || (c==-1))
- break;
- char* word;
- int wlength=0;
- while ((c!=' ') && (c!='\n') && (c!=-1)){
- if (wlength==0)
- word=(char *)malloc(sizeof(char));
- else
- word=(char *)realloc(word, (wlength+1)*sizeof(char));
- word[wlength++]=c;
- cin>>c;
- }
- if (wlength!=0){
- if (l==0)
- ans=(char**)malloc(sizeof(char*));
- else
- ans=(char**)realloc(ans, (l+1)*sizeof(char));
- ans[l++]=(char*)malloc((wlength+1)*sizeof(char));
- strcpy(ans[l-1], word);
- }
- cout<<word<<endl;
- free(word);
- }
- if (l!=0){
- ans=(char**)realloc(ans, (l+1)*sizeof(char));
- ans[l++]=(char*)malloc(sizeof(char));
- ans[l-1]=NULL;
- }
- return ans;
- }
- int main(){
- int n;
- while (true){
- int l=0;
- char** s=getlastline(l);
- if (l==0)
- return 1;
- n=fork();
- int status;
- if (n==0){
- if (execvp(s[0], s) == -1){
- perror("exec ");
- return 1;
- }
- }
- else
- wait(&status);
- if (!WIFEXITED(status) || WEXITSTATUS(status)){
- cout<<"It all went wrong!";
- return 1;
- }
- for (int i=0; i<l; i++)
- free(s[i]);
- free(s);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement