Skip to content

Commit

Permalink
Refined some of the bugs of gets() and freed the memory
Browse files Browse the repository at this point in the history
  • Loading branch information
CapedDemon committed Nov 24, 2021
1 parent 6b03529 commit 74c2edd
Show file tree
Hide file tree
Showing 29 changed files with 485 additions and 428 deletions.
Binary file modified CommandConsole.exe
Binary file not shown.
132 changes: 66 additions & 66 deletions calc_file.h → calc_file.c
Original file line number Diff line number Diff line change
@@ -1,67 +1,67 @@
#include <stdio.h>
#include <stdlib.h>

void calc()
{
char type;
printf("Enter d for decimal operations and i for integer operation: ");
scanf("%c", &type);
if (type == 'i')
{
getchar();
int a, b;
printf("Enter first number: ");
scanf("%d", &a);
printf("Enter second number: ");
scanf("%d", &b);
char o;
printf("Enter operation(+, -, /, *): ");
getchar();
scanf("%c", &o);
if (o == '+')
{
printf("%d\n\n", a + b);
}
else if (o == '-')
{
printf("%d\n\n", a - b);
}
else if (o == '*')
{
printf("%d\n\n", a * b);
}
else if (o == '/')
{
printf("%d\n\n", a / b);
}
}
else
{
getchar();
float x, y;
printf("Enter first number: ");
scanf("%f", &x);
printf("Enter second number: ");
scanf("%f", &y);
char o;
printf("Enter operation(+, -, /, *): ");
getchar();
scanf("%c", &o);
if (o == '+')
{
printf("%.2f\n\n", x + y);
}
else if (o == '-')
{
printf("%.2f\n\n", x - y);
}
else if (o == '*')
{
printf("%.2f\n\n", x * y);
}
else if (o == '/')
{
printf("%.2f\n\n", x / y);
}
}
#include <stdio.h>
#include <stdlib.h>

void calc()
{
char type;
printf("Enter d for decimal operations and i for integer operation: ");
scanf("%c", &type);
if (type == 'i')
{
getchar();
int a, b;
printf("Enter first number: ");
scanf("%d", &a);
printf("Enter second number: ");
scanf("%d", &b);
char o;
printf("Enter operation(+, -, /, *): ");
getchar();
scanf("%c", &o);
if (o == '+')
{
printf("%d\n\n", a + b);
}
else if (o == '-')
{
printf("%d\n\n", a - b);
}
else if (o == '*')
{
printf("%d\n\n", a * b);
}
else if (o == '/')
{
printf("%d\n\n", a / b);
}
}
else
{
getchar();
float x, y;
printf("Enter first number: ");
scanf("%f", &x);
printf("Enter second number: ");
scanf("%f", &y);
char o;
printf("Enter operation(+, -, /, *): ");
getchar();
scanf("%c", &o);
if (o == '+')
{
printf("%.2f\n\n", x + y);
}
else if (o == '-')
{
printf("%.2f\n\n", x - y);
}
else if (o == '*')
{
printf("%.2f\n\n", x * y);
}
else if (o == '/')
{
printf("%.2f\n\n", x / y);
}
}
}
21 changes: 21 additions & 0 deletions ccwd_file.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#include <stdio.h>
#include <stdlib.h>
#include "change_dir_path.c" //including the change_dir_path.h to get the variable change_dir.
#include <dirent.h>
#include <unistd.h>

void ccwd()
{
printf("Enter the path to change this directory to: \n");
getchar();
fgets(change_dir, 100, stdin);
change_dir[strlen(change_dir) - 1] = 0;
if (chdir(change_dir) == 0) //implementing chdir function.
{
printf("successfully changed\n");
}
else
{
printf("Cannot change the directory.\n");
}
}
16 changes: 0 additions & 16 deletions ccwd_file.h

This file was deleted.

6 changes: 3 additions & 3 deletions change_dir_path.h → change_dir_path.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include <stdio.h>
#include <stdlib.h>

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

char change_dir[100];//assigning at least 100 characters to the new directory name entered by user.
50 changes: 27 additions & 23 deletions change_root.h → change_root.c
Original file line number Diff line number Diff line change
@@ -1,24 +1,28 @@
#include <stdio.h>
#include <string.h>


//function to change the username and password
void change(){
char username_new[10], password_new[10];
int i =0;
getchar();
printf("Now you can change the username and password.\n");
printf("Enter new username-");
gets(username_new);//new username
printf("Enter new password-");
gets(password_new);//new password
FILE *new_user;
FILE *new_password;
//writing in the files the new username and password
new_user = fopen("root_username.txt", "w");
new_password = fopen("password_root.txt", "w");
fprintf(new_user, username_new);
fprintf(new_password, password_new);
fclose(new_user);
fclose(new_password);
#include <stdio.h>
#include <string.h>


//function to change the username and password
void change(){
char username_new[10], password_new[10];
int i =0;
getchar();
printf("Now you can change the username and password.\n");
printf("Enter new username-");
fgets(username_new, 10, stdin);//new username
username_new[strlen(username_new) - 1] = 0;

printf("Enter new password-");
fgets(password_new, 10, stdin);//new password
password_new[strlen(password_new) - 1] = 0;

FILE *new_user;
FILE *new_password;
//writing in the files the new username and password
new_user = fopen("root_username.txt", "w");
new_password = fopen("password_root.txt", "w");
fprintf(new_user, username_new);
fprintf(new_password, password_new);
fclose(new_user);
fclose(new_password);
}
28 changes: 14 additions & 14 deletions clr_file.h → clr_file.c
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
#include <stdio.h>
#include <stdlib.h>

//I have made this function thinking that the clearScreen will work on every system.
void clearScreen()
{
#if defined(__linux__) || defined(__unix__) || defined(__APPLE__)
system("clear");
#endif

#if defined(_WIN32) || defined(_WIN64)
system("cls");
#endif
}
#include <stdio.h>
#include <stdlib.h>

//I have made this function thinking that the clearScreen will work on every system.
void clearScreen()
{
#if defined(__linux__) || defined(__unix__) || defined(__APPLE__)
system("clear");
#endif

#if defined(_WIN32) || defined(_WIN64)
system("cls");
#endif
}
44 changes: 44 additions & 0 deletions copy_file.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

// Fucntion to copy file
void copy()
{
FILE *ptr1;
FILE *ptr2;
char filename1[25], filename2[25], content;

getchar();
printf("Enter the filename to be copied- ");
fgets(filename1, 25, stdin);
filename1[strlen(filename1) - 1] = 0;

printf("Now enter the name of the file in which the contents of the %s to be copied - ", filename1);
fgets(filename2, 25, stdin);
filename2[strlen(filename2) - 1] = 0;

ptr1 = fopen(filename1, "r");

if (ptr1 == NULL)
{
printf("Cannot copy\n");
}
else
{
ptr2 = fopen(filename2, "w");

content = fgetc(ptr1);

while (content != EOF)
{
fputc(content, ptr2);
content = fgetc(ptr1);
}

printf("\nSuccessfully copied to %s \n", filename1);
fclose(ptr2);
}

fclose(ptr1);
}
32 changes: 0 additions & 32 deletions copy_file.h

This file was deleted.

22 changes: 11 additions & 11 deletions date_file.h → date_file.c
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
#include <stdio.h>
#include <stdlib.h>
#include <time.h>


//date function. If the user clears the screen it can be usefule to see the date.
void date()
{
time_t t;
time(&t);
printf("%s\n\n", ctime(&t));
#include <stdio.h>
#include <stdlib.h>
#include <time.h>


//date function. If the user clears the screen it can be usefule to see the date.
void date()
{
time_t t;
time(&t);
printf("%s\n\n", ctime(&t));
}
6 changes: 3 additions & 3 deletions echo_file.h → echo_file.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
//Function to print the anything which user has given.
void echo()
{
getchar();
fgetc(stdin);
char user_str[1000000];
gets(user_str);
puts(user_str);
fgets(user_str, 1000000, stdin);
printf("%s", user_str);
}
Loading

0 comments on commit 74c2edd

Please # to comment.