0

I'm trying to create a login system for my operating system that checks whether the inputted username and password match the correct values ("admin" and "InHome123", respectively). I'm using a login function that takes in the user's input and returns a value indicating whether the input is correct. However, when I run my operating system and input a correct username and password, I see an "S" character printed at the end. Can anyone help me understand why this is happening?

I taken this screenshot while running my Operating System.

Here's the relevant code I'm using:


int login(char user, char pass, int userCorrect) {
    if (user == "admin" && pass == "InHome123") {
        userCorrect = 1;
        printf(userCorrect);
        return userCorrect;
    }
    else {
        userCorrect = 0;
        printf(userCorrect);
        return userCorrect;
    }
}


// In Main Function
char username[255];
char password[255];
int userCorrect = 0;

do {
    printf("| Lixt OS |\n");
    printf("Username: ");
    memset(username, 0, sizeof(username));
    getstr_bound(username, strlen(shell));    

    printf(username);

    printf("Password: ");
    memset(password, 0, sizeof(password));
    getstr_bound(password, strlen(shell));    

    printf(password);
    printf("\n\n\n\n\n\n");

    login(username, password, userCorrect);
} while(userCorrect == 0);
Lixt
  • 201
  • 4
  • 19
  • Always enable your compiler's warnings!!! (I use `-Wall -Wextra -pedantic` with gcc and clang.) It finds "coutless" [major problems](https://godbolt.org/z/vWxvq6c3W) with your program. – ikegami Dec 12 '21 at 21:35
  • `==` is not the correct way to compare strings in C. [How do I properly compare strings in C?](https://stackoverflow.com/questions/8004237/how-do-i-properly-compare-strings-in-c) – kaylum Dec 12 '21 at 21:38
  • `printf(userCorrect);` That is not how to use `printf`. Read the manual and search for examples. – kaylum Dec 12 '21 at 21:39
  • `char userCorrect (int)` What is that doing in a function parameter list? Even if it compiles it's certainly not what is intended. – kaylum Dec 12 '21 at 21:41
  • printf is a private made function that I made – Lixt Dec 12 '21 at 21:48
  • Huh ok. It would be good to mention that as most people would not realise that you have re-written a standard C function. And you need to show it as we need complete code. Especially since you are asking about some incorrect print output. Will be interesting to see how you have been able to code a better `printf` that can handle non-string types. See: [mre]. – kaylum Dec 12 '21 at 21:51
  • You are not catching the return value from function `login()`. Whatever you pass to argument `char userCorrect`, does not change at the caller's side. – Weather Vane Dec 12 '21 at 23:07

1 Answers1

1

Use strcmp(string1, string2)

void NewSudo(char* b, int* sudo (int))
{

    // Vars

    char rootName;
    char rootPass;


    printf("Root Username: ");
    memset(rootName, 0, sizeof(rootName));
    getstr_bound(rootName, strlen(rootName));

    printf("\n");

    printf("Root Password: ");
    memset(rootPass, 0, sizeof(rootPass));
    getstr_bound(rootPass, strlen(rootPass));

    printf("\n");

    printf("\n\nConfirm authentication:\nRoot Name: %s\nRoot Pass: %s\n", rootName, rootPass);



    if (strcmp(rootName, "luis") == 0 && strcmp(rootPass, "pipoca"))
    {
        printf("Login Correct, SuperUser Loged-in\n");
        sudo = 1;

        return sudo;
    } else {
        printf("Login Failed\n");
        return sudo;
    }
}

Then make a strcmp func:

int strcmp(const char *s1, char *s2) {
    int i = 0;

    while ((s1[i] == s2[i])) {
        if (s2[i++] == 0)
            return 0;
    }
    return 1;
}

And put your func into your header file

Lixt
  • 201
  • 4
  • 19