#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#define MAXLINE 32
#define MAX_PASSWORD 10

#define PASSWORD_FILE "pass.txt"
int main(void) {
    int n, user_length;
    char userid[MAXLINE];
    char password[MAXLINE];

    if ((n = read(STDIN_FILENO, userid, MAX_PASSWORD)) == -1) {
        perror("read");
        exit(1);
    } else if (n == 0) {
        fprintf(stderr, "Error: could not read from stdin");
        exit(1);
    }

    if (n <= MAX_PASSWORD) {
        userid[n] = '\0';
    }

    char *newline;
    if ((newline = strchr(userid, '\n')) != NULL) {
        *newline = '\0';
    }

    if ((n = read(STDIN_FILENO, password, MAX_PASSWORD)) == -1) {
        perror("read");
        exit(1);
    } else if (n == 0) {
        fprintf(stderr, "Error: could not read from stdin");
        exit(1);
    }

    if (n <= MAX_PASSWORD) {
        password[n] = '\0';
    }

    if ((newline = strchr(password, '\n')) != NULL) {
        *newline = '\0';
    }

    strncat(userid, ":", MAXLINE - strlen(userid) - 1);
    user_length = strlen(userid);
    strncat(userid, password, MAXLINE - strlen(userid) - 1);

    FILE *fp = fopen(PASSWORD_FILE, "r");
    if (!fp) {
        perror("fopen");
        exit(1);
    }

    char line[MAXLINE];
    while (fgets(line, sizeof(line) - 1, fp)) {
        line[strlen(line) - 1] = '\0';
        if (strcmp(userid, line) == 0) {
            fclose(fp);
            exit(0);
        } else if (strncmp(userid, line, user_length) == 0) {
            fclose(fp);
            exit(2);
        }
    }
    fclose(fp);
    exit(3);
}