Efe Dervişoğlu
Efe Dervişoğlu

Reputation: 3

Some Troubles in A Little C Program

I don't really write codes but nowadays I wanted to make a secret folder which is going to stay in my desktop. I've made the batch files that makes the secret folder appear and disappear. I want to make a c program which is going to execute the batch files by entering password. But I have some issues in my code. I'm not really into this job and I need a simple description which is going to solve troubles in my code.

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

int main(int argc, char** argv) {    

    char *choose;
    char *password = 'efe';
    char *entered_password;

    printf("enter the password:\n");
    scanf("%s", &entered_password);   

    if(entered_password == password) {
        printf("access accepted!!!\n");
    }
    else {
        printf("access denied!!!\n");    
    }

    printf("What you want to do?\n\tOpen the Gate:open\n\tClose the gate:close\n");    

    int a = 1;
    char open[5] = "open";
    char close[6] = "close";

    do {
        scanf("%s", choose);

        if(strcmp(open, choose) == 0) { 
            printf("Starting\n");

            ShellExecute(NULL,"open","C:\\Program Files (x86)\\cl_op\\open.bat",NULL,NULL,SW_SHOWNORMAL);

            a--;
            return 0;
        }
        else if(strcmp(close, choose) == 0) {
            printf("Bye\n");

            ShellExecute(NULL,"open","C:\\Program Files (x86)\\cl_op\\close.bat",NULL,NULL,SW_SHOWNORMAL);

            a--;
        }   
        else {
            printf("Try again\n");
        }
    } while(a > 0);   

    return 0;
}

Upvotes: 0

Views: 86

Answers (2)

Rishikesh Raje
Rishikesh Raje

Reputation: 8614

  1. In the comparison code for entered_password you are comparing the strings with ==. You should use strcmp

  2. entered_password and choose should be declared as a char array of suitable length.

  3. In the case where the password is not matched, you should exit the program with exit(1) or return 0

  4. %s requires the address of the array. you can directly give the base address as the parameter

  5. You should use "efe" while initializing a the char pointer password

    char choose[30];
    char *password = "efe";
    char entered_password[30];
    
    printf("enter the password:\n");
    scanf("%s", entered_password);   
    
    if(strcmp(entered_password,password)==0){
        printf("access accepted!!!\n");
    }
    else{
        printf("access denied!!!\n");  
        exit (1);        
    }
    

Upvotes: 2

ctrl-alt-delor
ctrl-alt-delor

Reputation: 7735

The first problem that I spotted in that you have not allocated any memory for the inputted password. Next your first if compares two pointers, not two strings.

I recommend a different language:

python3 will be good for this, or go (if you need to compile to native code).

Other problems:

  • I can get your password by running strings «program name».
  • I can circumvent the the security by directly running the external programs (batch files). I can find the external programs by running strings «program name».

Upvotes: 2

Related Questions