All pastes #1275368 Raw Edit

Anonymous

public text v1 · immutable
#1275368 ·published 2008-12-03 22:21 UTC
rendered paste body
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <limits.h>
#include <sys/types.h>
#include <sys/wait.h>
#include "plist.h"

int main(){
	char *cwd = NULL;
	char *eingabezeile = NULL;
	char *eingabebackup = NULL; /* Sicherungskopie der eingabezeile */
	char buf[PATH_MAX];
	char **argumente = NULL; /* Befehl ist argumente[0] */
	int maxargumentanzahl;
	int i;	/* laufindex fuer forschleifen */
	int hintergrundprozess;	/* wird 1, wenn letzter token ein & ist */
	pid_t pid;

	maxargumentanzahl = (sysconf(_SC_LINE_MAX) / 2) + 1;

	if(!(argumente = (char **) malloc(maxargumentanzahl * sizeof(char *)))){
		perror("malloc");
		exit(EXIT_FAILURE);
	}
	
	while(1){
		hintergrundprozess = 0;	/* flag ist erstmal nicht gesetzt bzw. false */

		/* terminieren wenn CTRL-D bzw. EOF */
		if(feof(stdin)){
			printf("\n");
			exit(EXIT_SUCCESS);
		}
		
		/* Speicher fuer eingabezeile*/
		if(!(eingabezeile = (char *) malloc(sysconf(_SC_LINE_MAX) * sizeof(char)))){
			perror("malloc eingabezeile");
			exit(EXIT_FAILURE);
		}

		/* Speicher fuer eingabebackup */
		if(!(eingabebackup = (char *) malloc(sysconf(_SC_LINE_MAX) * sizeof(char)))){
			perror("malloc eingabebackup");
			exit(EXIT_FAILURE);
		}
		
		
		if(!(cwd = getcwd(buf, PATH_MAX))){
			perror("getcwd");
			exit(EXIT_FAILURE);
		}
  
		printf("%s: ", cwd);
		
		/* eingabezeile von stdin holen */
		if(!(fgets(eingabezeile, _SC_LINE_MAX, stdin))){
			continue;
		}
		
		/* eingabezeile in eingabebackup sichern */
		strcpy(eingabebackup, eingabezeile);
		eingabebackup[strlen(eingabebackup) - 1] = '\0';	/* \n "abschneiden" */

		
		/* starte: eingabezeile zerrlegen (in Kommandonamen und Argumente) */	
		argumente[0] = strtok(eingabezeile, " \t\n");

		/* zu viel pruefen schadet nicht (ausser der performance) */ 
		if(!(argumente[0])){
			continue;
		}

		for(i = 1; (argumente[i] = strtok(NULL, " \t\n")); i++);
		/* fertig: eingabezeile zerlegen */


		/* cd befehl */
		if(0 == (strcmp(argumente[0], "cd"))){
			/* Fehlerbehandlung */
			if(chdir(argumente[1])){
				perror("chdir");
			}
			continue;
		}


		/* wenn letztes argument & ist */
		if(i && *argumente[i - 1] == '&'){
			argumente[i - 1] = '\0';	/* "abschneiden" des & */
			hintergrundprozess = 1;		/* flag setzen */
			
			printf("hintergrundprozess\n");
		}

		
		if((pid = fork()) > 0){ /* Vater */
			int status;

			if(!hintergrundprozess){
				waitpid(pid, &status, 0);	/* fehlerbehandlung nicht vergessen */
			
				if(WIFEXITED(status)){
					printf("Exitstatus [%s] = %i\n", eingabebackup, WEXITSTATUS(status));
				}else if(WIFSIGNALED(status)){
					printf("Signal [%s] = %i\n", eingabebackup, WEXITSTATUS(status));
				}
			}else{
				switch(insertElement(pid, eingabebackup)){
					case -1:	/* existiert bereits */
						continue;
					break;

					case -2:	/* nicht genuegend speicher */
						continue;
					break;

					default:
					break;
				}
			}

		}else{
			if(pid == 0){	/* Kind */
				execvp(argumente[0], argumente);
				perror("exec");
				exit(EXIT_FAILURE);
			}
		}
	}
  
	/* Beenden */
	free(argumente);
	free(eingabezeile);
	free(eingabebackup);
	printf("\n");
	exit(EXIT_SUCCESS);
}