-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Tangboshi
authored and
Tangboshi
committed
Apr 15, 2015
0 parents
commit 9939180
Showing
9 changed files
with
189 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |