Browse Source

And back to standard

Piotr Czajkowski 4 years ago
parent
commit
b85f7b2174
2 changed files with 25 additions and 16 deletions
  1. 2 2
      makefile
  2. 23 14
      pwned.c

+ 2 - 2
makefile

@@ -1,7 +1,7 @@
-CFLAGS=-Wall -Wextra -Wshadow -O3 -std=gnu99
+CFLAGS=-Wall -Wextra -Wshadow -O3 -std=c99
 LDLIBS=-lcurl -lssl -lcrypto
 objects=curl.o sha.o
-mingwCFLAGS=-Wall -Wextra -O3 -std=gnu99
+mingwCFLAGS=-Wall -Wextra -O3 -std=c99
 mingwLDLIBS=-lcurl -lssl -lcrypto
 mingw=x86_64-w64-mingw32-gcc
 

+ 23 - 14
pwned.c

@@ -44,26 +44,35 @@ char *getSuffixUppercase(const char *hash) {
 	return suffixUpper;
 }
 
-int findSuffix(const char *suffix, const char *data) {
-	char *token, *string, *tofree;
-	tofree = string = strdup(data);
+int printNumber(char *text) {
+	char *state;
+	char *part = strtok_r(text, ":", &state);
+	if (part == NULL) return 0;
+	
+	part = strtok_r(NULL, ":", &state);
+	if (part != NULL) {
+		printf("This is how many times your password was pwned: %s\n", part);
+		return 1;
+	}
 
-	while ((token = strsep(&string, "\n")) != NULL) {
-		if (strncmp(token, suffix, HASH_SUFFIX_LENGTH) == 0) {
-			char *part = strsep(&token, ":");
+	return 0;
+}
 
-			if (part != NULL) {
-				printf("This is how many times your password was pwned: %s\n", token);
-				free(tofree);
-				return 1;
+int findSuffix(const char *suffix, char *data) {
+	char *token = strtok(data, "\n");
+
+	while (token != NULL) {
+		if (strncmp(token, suffix, HASH_SUFFIX_LENGTH) == 0) {
+			if (!printNumber(token)) {
+				puts("Hash found, but can't obtain the number!");
+				return 0;
 			}
 
-			puts("Hash found, but can't obtain the number!");
-			break;
+			return 1;
 		}
-	}
 
-	free(tofree);
+		token = strtok(NULL, "\n");
+	}
 
 	return 0;
 }