Skip to content

Commit

Permalink
init commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Tangboshi authored and Tangboshi committed Apr 15, 2015
0 parents commit 9939180
Show file tree
Hide file tree
Showing 9 changed files with 189 additions and 0 deletions.
13 changes: 13 additions & 0 deletions isset.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#include "isset.h"

int isset(char* var, int argc,...){
va_list options;
va_start(options, argc);

if (strcmp(va_arg(options, char*), "-v") == 0)
printf("%s: %s\n", var, getenv(var));

if (getenv(var)!=NULL)
return 1;
return 0;
}
11 changes: 11 additions & 0 deletions isset.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#ifndef ISSET_H_INCLUDED
#define ISSET_H_INCLUDED

#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>

int isset(char* var, int argc,...);

#endif // ISSET_H_INCLUDED
94 changes: 94 additions & 0 deletions main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
#include "myecho.h"
#include "myenv.h"
#include "isset.h"
#include "myprocess.h"
/*
#include <readline/readline.h>
#include <readline/history.h>
*/

#define ARGC 3

#include <stdlib.h>

int main(int argc, char* argv[], char* envp[]){

/* ------------- AUFGABE 1 ------------- */
/* Erste, nicht so schöne Variante für echo */
char* myArray[ARGC] = {"fancyWord", "anotherFancyWord", "thirdFancyString"};
myecho(ARGC, &myArray);

/* Zweite, schönere Variante für echo */
myechoAlt(ARGC, "string", "stringsSoundNice", "wohooILoveStrings!");

/* Umgebungsvariablen ausgeben */
myenv(envp);


/* ------------- AUFGABE 2 ------------- */
/* Überprüfen, ob Umgebungsvariablen gesetzt */
int isMyVarSet = isset("HOME", argc);
printf("isset returned %d.\n\n", isMyVarSet);

/* Namen und Wert der Umgebungsvariablen ausgeben lassen */
isMyVarSet = isset("HOME", argc, "-v");
printf("isset returned %d\n\n", isMyVarSet);


/* ------------- AUFGABE 3 ------------- */
/* Prozess starten, pid ausgeben lassen*/
pid_t myPid = fork();
printf("%d\n\n", (int)myPid);

myProcess("/usr/bin/gedit", argv);

/* Aufgabe 4 hat nicht so richtig geklappt */

/* folgendes wurde unternommen:
error: readline/readline.h could not be resolved
Lösung: Mit Terminal installieren
sudo apt-get install libreadline-dev
#include <readline/readline.h>
#include <readline/history.h>
char* path = readline(const char* path);
error: expected expression before const.
Frage: wozu wird hier history verwendet?
werde nicht so ganz schlau aus der man-page
bzw. andere Varianten mit
scanf und fgets, z.B:
#define MAX_ARGC = 100;
...
char* path[100];
printf("Geben Sie einen Pfad an.");
fgets(path, 100, stdin);
char* pArgv[100]
char quit = 'n';
int i = 1;
while((quit!='j') && i<MAX_ARGC){
printf("Wollen Sie noch ein Argument eingeben [j\n]");
scanf("%c", &quit);
fgets(pArgv, 100, stdin);
i++;
}
...
myProcess(path, pArgv);
verschiedene Varianten dieses Ansatzes führten
zu segmentation faults oder undefined behaviour.
*/

/* ---------------- Sonstiges/Testfunktionen --------------- */
printf("\n\n");


return EXIT_SUCCESS;
}
26 changes: 26 additions & 0 deletions myecho.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#include "myecho.h"

void myecho(int argc, char* argv[]){
int i;

for(i=0;i<argc;++i){
printf("%s ", argv[i]);
}

printf("\n\n");
}

void myechoAlt(int argc,...){
va_list fancyWords;
va_start(fancyWords, argc);

int i;

for(i=0;i<argc;++i){
printf("%s ", va_arg(fancyWords, char*));
}

va_end(fancyWords);

printf("\n\n");
}
11 changes: 11 additions & 0 deletions myecho.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#ifndef MYECHO_H_INCLUDED
#define MYECHO_H_INCLUDED

#include <stdio.h>
#include <stdarg.h>
#include <stdlib.h>

void myecho(int argc, char* argv[]);
void myechoAlt(int argc,...);

#endif // MYECHO_H_INCLUDED
9 changes: 9 additions & 0 deletions myenv.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#include "myenv.h"

int myenv(char* envp[]){
int i;
for(i=0;envp[i]!=NULL;i++) printf("%s\n",envp[i]);

printf("\n\n");
return EXIT_SUCCESS;
}
9 changes: 9 additions & 0 deletions myenv.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#ifndef MYENV_H_INCLUDED
#define MYENV_H_INCLUDED

#include<stdio.h>
#include<stdlib.h>

int myenv(char* envp[]);

#endif // MYENV_H_INCLUDED
5 changes: 5 additions & 0 deletions myprocess.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#include "myprocess.h"

void myProcess(char* path, char* argv[]){
execv(path, argv);
}
11 changes: 11 additions & 0 deletions myprocess.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#ifndef MYPROCESS_H_INCLUDED
#define MYPROCESS_H_INCLUDED

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>

void myProcess(char* path, char* argv[]);

#endif // MYPROCESS_H_INCLUDED

0 comments on commit 9939180

Please sign in to comment.