Hello i'm getting a segmentation fault in my is_validport() function and could somebody people point out what i'm doing wrong please?
#include <stdio.h>
#include <stdlib.h>
#include <stddef.h>
#include <ctype.h>
#include <string.h>
/* Function definitions. */
//char *is_uid(char *s);
char *is_destportseq(char *s);
char *is_destport(char *s);
char *is_validport(char *s);
char *trimwhitespacestartandend(char *str);
/* Main method - driver. */
int main() {
char *string = "80, 443, 8080";
/*
char *string2 = "65536";
if(atoi(string2) <= 65535){
if(0 <= atoi(string2)){
printf("%s %s\n", string2, "Is Valid");
}
}else{
printf("%s %s\n", string2, "Is NOT Valid");
}
*/
if (is_validport(string)) {
printf("Valid PORTS.\n");
return EXIT_SUCCESS;
} else {
printf("Invalid PORTS.\n");
return EXIT_FAILURE;
}
/* The entire program is checking if our input is a sentences. */
if (is_destportseq(string)) {
printf("Valid sentence.\n");
return EXIT_SUCCESS; /* Zero means success. */
} else {
printf("Invalid sentence.\n");
return EXIT_FAILURE;
}
}
char *is_destportseq(char *s) {
char *r1, *r2; /* Report variables. */
/* We recursively evaluate the string, passing back the pointer after each
call. */
r1 = is_destport(s);
if (r1) {
if (*r1 == 0) {
return r1;
/* Either it's a sentences or it isn't. */
} else {
if (*r1 == ',') {
r1++;
} else {
return NULL;
}
while (*r1 == ' ') {
r1++;
}
r2 = is_destportseq(r1);
if (r2) {
return r2;
} else {
return NULL;
}
}
}
return NULL;
}
char *is_destport(char *s) {
if (!isdigit((int) * s)) {
return NULL;
}
/* Remember, for a root element, you return one past. */
while (isdigit((int) * s)) {
s++;
}
return s;
}
//segmentation fault
char *is_validport(char *s) {
char *firstport;
char *temps;
char *temps2;
*temps = *s;
//printf("s %s \n", s);
//printf("temps %s \n", temps);
firstport = strtok(s, ",");
if (*s == 0) {
return s;
} else {
if (atoi(firstport) <= 65535) {
if (0 <= atoi(firstport)) {
//printf("s = %s \n", s);
temps2 = strstr(s, " ");
temps2 = trimwhitespacestartandend(temps2);
is_validport(temps2);
}
} else {
return NULL;
}
}
return NULL;
}
char *trimwhitespacestartandend(char *str) {
char *end;
// Trim leading space
while (isspace((int) * str)) str++;
// Trim trailing space
end = str + strlen(str) - 1;
while (end > str && isspace((int) * end)) end--;
// Write new null terminator
*(end + 1) = 0;
return str;
}
its trying to break up a string "80, 443, 8080" into "80" "443" "8080" and make sure each number >=0 & <= 65535.
Thanks in advance.