Skip to content

Commit 74c2edd

Browse files
committed
Refined some of the bugs of gets() and freed the memory
1 parent 6b03529 commit 74c2edd

29 files changed

+485
-428
lines changed

CommandConsole.exe

1 KB
Binary file not shown.

calc_file.h renamed to calc_file.c

+66-66
Original file line numberDiff line numberDiff line change
@@ -1,67 +1,67 @@
1-
#include <stdio.h>
2-
#include <stdlib.h>
3-
4-
void calc()
5-
{
6-
char type;
7-
printf("Enter d for decimal operations and i for integer operation: ");
8-
scanf("%c", &type);
9-
if (type == 'i')
10-
{
11-
getchar();
12-
int a, b;
13-
printf("Enter first number: ");
14-
scanf("%d", &a);
15-
printf("Enter second number: ");
16-
scanf("%d", &b);
17-
char o;
18-
printf("Enter operation(+, -, /, *): ");
19-
getchar();
20-
scanf("%c", &o);
21-
if (o == '+')
22-
{
23-
printf("%d\n\n", a + b);
24-
}
25-
else if (o == '-')
26-
{
27-
printf("%d\n\n", a - b);
28-
}
29-
else if (o == '*')
30-
{
31-
printf("%d\n\n", a * b);
32-
}
33-
else if (o == '/')
34-
{
35-
printf("%d\n\n", a / b);
36-
}
37-
}
38-
else
39-
{
40-
getchar();
41-
float x, y;
42-
printf("Enter first number: ");
43-
scanf("%f", &x);
44-
printf("Enter second number: ");
45-
scanf("%f", &y);
46-
char o;
47-
printf("Enter operation(+, -, /, *): ");
48-
getchar();
49-
scanf("%c", &o);
50-
if (o == '+')
51-
{
52-
printf("%.2f\n\n", x + y);
53-
}
54-
else if (o == '-')
55-
{
56-
printf("%.2f\n\n", x - y);
57-
}
58-
else if (o == '*')
59-
{
60-
printf("%.2f\n\n", x * y);
61-
}
62-
else if (o == '/')
63-
{
64-
printf("%.2f\n\n", x / y);
65-
}
66-
}
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
4+
void calc()
5+
{
6+
char type;
7+
printf("Enter d for decimal operations and i for integer operation: ");
8+
scanf("%c", &type);
9+
if (type == 'i')
10+
{
11+
getchar();
12+
int a, b;
13+
printf("Enter first number: ");
14+
scanf("%d", &a);
15+
printf("Enter second number: ");
16+
scanf("%d", &b);
17+
char o;
18+
printf("Enter operation(+, -, /, *): ");
19+
getchar();
20+
scanf("%c", &o);
21+
if (o == '+')
22+
{
23+
printf("%d\n\n", a + b);
24+
}
25+
else if (o == '-')
26+
{
27+
printf("%d\n\n", a - b);
28+
}
29+
else if (o == '*')
30+
{
31+
printf("%d\n\n", a * b);
32+
}
33+
else if (o == '/')
34+
{
35+
printf("%d\n\n", a / b);
36+
}
37+
}
38+
else
39+
{
40+
getchar();
41+
float x, y;
42+
printf("Enter first number: ");
43+
scanf("%f", &x);
44+
printf("Enter second number: ");
45+
scanf("%f", &y);
46+
char o;
47+
printf("Enter operation(+, -, /, *): ");
48+
getchar();
49+
scanf("%c", &o);
50+
if (o == '+')
51+
{
52+
printf("%.2f\n\n", x + y);
53+
}
54+
else if (o == '-')
55+
{
56+
printf("%.2f\n\n", x - y);
57+
}
58+
else if (o == '*')
59+
{
60+
printf("%.2f\n\n", x * y);
61+
}
62+
else if (o == '/')
63+
{
64+
printf("%.2f\n\n", x / y);
65+
}
66+
}
6767
}

ccwd_file.c

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
#include "change_dir_path.c" //including the change_dir_path.h to get the variable change_dir.
4+
#include <dirent.h>
5+
#include <unistd.h>
6+
7+
void ccwd()
8+
{
9+
printf("Enter the path to change this directory to: \n");
10+
getchar();
11+
fgets(change_dir, 100, stdin);
12+
change_dir[strlen(change_dir) - 1] = 0;
13+
if (chdir(change_dir) == 0) //implementing chdir function.
14+
{
15+
printf("successfully changed\n");
16+
}
17+
else
18+
{
19+
printf("Cannot change the directory.\n");
20+
}
21+
}

ccwd_file.h

-16
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#include <stdio.h>
2-
#include <stdlib.h>
3-
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
44
char change_dir[100];//assigning at least 100 characters to the new directory name entered by user.
+27-23
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,28 @@
1-
#include <stdio.h>
2-
#include <string.h>
3-
4-
5-
//function to change the username and password
6-
void change(){
7-
char username_new[10], password_new[10];
8-
int i =0;
9-
getchar();
10-
printf("Now you can change the username and password.\n");
11-
printf("Enter new username-");
12-
gets(username_new);//new username
13-
printf("Enter new password-");
14-
gets(password_new);//new password
15-
FILE *new_user;
16-
FILE *new_password;
17-
//writing in the files the new username and password
18-
new_user = fopen("root_username.txt", "w");
19-
new_password = fopen("password_root.txt", "w");
20-
fprintf(new_user, username_new);
21-
fprintf(new_password, password_new);
22-
fclose(new_user);
23-
fclose(new_password);
1+
#include <stdio.h>
2+
#include <string.h>
3+
4+
5+
//function to change the username and password
6+
void change(){
7+
char username_new[10], password_new[10];
8+
int i =0;
9+
getchar();
10+
printf("Now you can change the username and password.\n");
11+
printf("Enter new username-");
12+
fgets(username_new, 10, stdin);//new username
13+
username_new[strlen(username_new) - 1] = 0;
14+
15+
printf("Enter new password-");
16+
fgets(password_new, 10, stdin);//new password
17+
password_new[strlen(password_new) - 1] = 0;
18+
19+
FILE *new_user;
20+
FILE *new_password;
21+
//writing in the files the new username and password
22+
new_user = fopen("root_username.txt", "w");
23+
new_password = fopen("password_root.txt", "w");
24+
fprintf(new_user, username_new);
25+
fprintf(new_password, password_new);
26+
fclose(new_user);
27+
fclose(new_password);
2428
}

clr_file.h renamed to clr_file.c

+14-14
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
1-
#include <stdio.h>
2-
#include <stdlib.h>
3-
4-
//I have made this function thinking that the clearScreen will work on every system.
5-
void clearScreen()
6-
{
7-
#if defined(__linux__) || defined(__unix__) || defined(__APPLE__)
8-
system("clear");
9-
#endif
10-
11-
#if defined(_WIN32) || defined(_WIN64)
12-
system("cls");
13-
#endif
14-
}
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
4+
//I have made this function thinking that the clearScreen will work on every system.
5+
void clearScreen()
6+
{
7+
#if defined(__linux__) || defined(__unix__) || defined(__APPLE__)
8+
system("clear");
9+
#endif
10+
11+
#if defined(_WIN32) || defined(_WIN64)
12+
system("cls");
13+
#endif
14+
}

copy_file.c

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
#include <string.h>
4+
5+
// Fucntion to copy file
6+
void copy()
7+
{
8+
FILE *ptr1;
9+
FILE *ptr2;
10+
char filename1[25], filename2[25], content;
11+
12+
getchar();
13+
printf("Enter the filename to be copied- ");
14+
fgets(filename1, 25, stdin);
15+
filename1[strlen(filename1) - 1] = 0;
16+
17+
printf("Now enter the name of the file in which the contents of the %s to be copied - ", filename1);
18+
fgets(filename2, 25, stdin);
19+
filename2[strlen(filename2) - 1] = 0;
20+
21+
ptr1 = fopen(filename1, "r");
22+
23+
if (ptr1 == NULL)
24+
{
25+
printf("Cannot copy\n");
26+
}
27+
else
28+
{
29+
ptr2 = fopen(filename2, "w");
30+
31+
content = fgetc(ptr1);
32+
33+
while (content != EOF)
34+
{
35+
fputc(content, ptr2);
36+
content = fgetc(ptr1);
37+
}
38+
39+
printf("\nSuccessfully copied to %s \n", filename1);
40+
fclose(ptr2);
41+
}
42+
43+
fclose(ptr1);
44+
}

copy_file.h

-32
This file was deleted.

date_file.h renamed to date_file.c

+11-11
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
#include <stdio.h>
2-
#include <stdlib.h>
3-
#include <time.h>
4-
5-
6-
//date function. If the user clears the screen it can be usefule to see the date.
7-
void date()
8-
{
9-
time_t t;
10-
time(&t);
11-
printf("%s\n\n", ctime(&t));
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
#include <time.h>
4+
5+
6+
//date function. If the user clears the screen it can be usefule to see the date.
7+
void date()
8+
{
9+
time_t t;
10+
time(&t);
11+
printf("%s\n\n", ctime(&t));
1212
}

echo_file.h renamed to echo_file.c

+3-3
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
//Function to print the anything which user has given.
55
void echo()
66
{
7-
getchar();
7+
fgetc(stdin);
88
char user_str[1000000];
9-
gets(user_str);
10-
puts(user_str);
9+
fgets(user_str, 1000000, stdin);
10+
printf("%s", user_str);
1111
}

0 commit comments

Comments
 (0)