-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
63 lines (44 loc) · 1.31 KB
/
main.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <regex.h>
/* Std start to main, argc holds the number of arguments provided to the executable, and *argv[] an
array of strings/chars with the arguments (as strings).
*/
int main(int argc, char *argv[]){
/* GET name and port from arguments. */
char *hoststring,*portstring, *rest, *org;
org=strdup(argv[1]);
rest=argv[1];
hoststring=strtok_r(rest,":",&rest);
portstring=strtok_r(rest,":",&rest);
printf("Got %s split into %s and %s \n",org, hoststring,portstring);
/* This is to test nicknames */
char *expression="^[A-Za-z_]+$";
regex_t regularexpression;
int reti;
reti=regcomp(®ularexpression, expression,REG_EXTENDED);
if(reti){
fprintf(stderr, "Could not compile regex.\n");
exit(1);
}
int matches;
regmatch_t items;
printf("Testing nicknames. \n");
for(int i=2;i<argc;i++){
if(strlen(argv[i])<12){
reti=regexec(®ularexpression, argv[i],matches,&items,0);
if(!reti){
printf("Nick %s is accepted.\n",argv[i]);
} else {
printf("%s is not accepted.\n",argv[i]);
}
} else {
printf("%s is too long (%d vs 12 chars).\n", argv[i], strlen(argv[i]));
}
}
printf("Leaving\n");
regfree(®ularexpression);
free(org);
return(0);
}