-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathargsHandler.c
executable file
·103 lines (82 loc) · 1.98 KB
/
argsHandler.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <getopt.h>
#include <sys/types.h>
#include <sys/wait.h>
#include "argsHandler.h"
#include <string.h>
static struct option myLongOptions[] =
{
{"name", required_argument, 0, 'n'}, //optarg is the char* used to know the argument
{"exec", required_argument, 0, 'e'},
{"print", no_argument, 0, 'p'},
{0,0,0,0}
};
int errInArg(int argc, char** argv){
int iOption = 0;
int rsFindOptions;
int status;
if(fork()==0){ // FORK ! SON
while((rsFindOptions = getopt_long(argc, argv, "lt:in:e:p", myLongOptions, &iOption)) != -1){
if (optarg!=NULL){
if(optarg[0]=='-'){
exit(1); // Une erreur se produit
}
}else{
if (rsFindOptions=='?')
exit(2); //Missing an argument here
}
}
exit(0);
}else{ // FORK ! FATHER
wait(&status);
if(WIFEXITED(status)){
if(WEXITSTATUS(status)==0){
return 0; // Good.
}else{
return 1; // Not Good.
}
}
}
return 0;
}
int optDispatcher(int argc, char** argv, int* lOpt, int* tOpt, char** tArg, int* iOpt, int* nOpt, char** nArg, int* eOpt, char** eArg, int* pOpt){
int iOption =0;
int rsFindOptions;
while((rsFindOptions = getopt_long(argc, argv, "lt:in:e:p", myLongOptions, &iOption)) != -1){
switch(rsFindOptions){
case 'l':
*lOpt = 1;
break;
case 't':
*tOpt = 1;
*tArg = malloc(sizeof(char)*(strlen(optarg)+1) );
memset(*tArg, 0,sizeof(char)*(strlen(optarg)+1) );
strcpy(*tArg, optarg);
break;
case 'i':
*iOpt = 1;
break;
case 'n':
*nOpt = 1;
*nArg = malloc(sizeof(char)*(strlen(optarg)+1) );
memset(*nArg, 0,sizeof(char)*(strlen(optarg)+1) );
strcpy(*nArg, optarg);
break;
case 'e':
*eOpt = 1;
*eArg = malloc(sizeof(char)*(strlen(optarg)+1) );
memset(*eArg, 0,sizeof(char)*(strlen(optarg)+1) );
strcpy(*eArg, optarg);
break;
case 'p':
*pOpt = 1;
break;
default:
//invalid argument !
return 1;
}
}
return 0;
}