Skip to content

Commit d880ec7

Browse files
Merge pull request #2 from sanscript-tech/main
Updating
2 parents 04ecbf2 + 24bf8fa commit d880ec7

File tree

363 files changed

+9694
-8
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

363 files changed

+9694
-8
lines changed

C++/counting-days/README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# Counting days left to birthday
2+
- - - - - -
3+
## Aim
4+
Aim of the script is to count the number of days left to your birthday
5+
6+
## To use
7+
Run ```count.cpp```</br>
8+
Enter the day,month and year of upcoming birthday

C++/counting-days/count.cpp

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
#include <iostream>
2+
#include <ctime>
3+
using namespace std;
4+
int main()
5+
{
6+
// asking user input for next birthday
7+
time_t now = time(0);
8+
tm *ltm = localtime(&now);
9+
int m,d,y;
10+
cout<<"Enter month of birthday ";
11+
cin>>m;
12+
cout<<"Enter day of birthday ";
13+
cin>>d;
14+
cout<<"Enter the year of your next birthday";
15+
cin>>y;
16+
17+
//current day and date
18+
19+
int current_day = ltm->tm_mday;
20+
int current_month = 1+(ltm->tm_mon);
21+
int current_year = 1900 +(ltm->tm_year);
22+
int monthDays[12]= { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
23+
int middle_days;
24+
middle_days= 0;
25+
26+
27+
//if birthday falls the same year
28+
if(current_year == y)
29+
30+
{
31+
//fallling same month
32+
if(current_month==m)
33+
{
34+
cout<<d-current_day;
35+
}
36+
else
37+
//different month
38+
{
39+
for(int i = current_month; i<m-1;i++)
40+
middle_days+=monthDays[i];
41+
cout<<d+(monthDays[current_month-1]-current_day)+middle_days;
42+
}
43+
44+
cout<<" days are left for your birthday";
45+
}
46+
47+
middle_days = 0;
48+
//if birthday falls the next year
49+
if(current_year != y)
50+
{
51+
for(int i = current_month;i<12;i++)
52+
{
53+
middle_days+=monthDays[i];
54+
}
55+
for(int i = 0;i<m-1;i++)
56+
{
57+
middle_days+=monthDays[i];
58+
}
59+
cout<<d+(monthDays[current_month-1]-current_day)+middle_days;
60+
cout<<" days are left for your birthday"<<endl;
61+
}
62+
63+
}

C/Program-Timer/README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# Program Timing
2+
This script checks the time required to run a function in C.
3+
4+
5+
## To use
6+
* Edit `void function` and write your own operations.
7+
* Compile with `gcc timer.c -o timer`.
8+
* Execute with `./timer`
9+
10+
**Time Complexity:** O(1)
11+
**Space Complexity:** 0(1)
12+
13+
## Output
14+
![output](output.png)

C/Program-Timer/output.png

15.5 KB
Loading

C/Program-Timer/timer.c

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#include<time.h>
2+
#include<stdio.h>
3+
4+
// Placeholer function to edit:
5+
void function(){
6+
int a, b;
7+
printf("Enter numbers to add(space separated):");
8+
scanf("%d %d", &a, &b);
9+
printf("Sum of %d and %d is %d.\n", a, b, a+b);
10+
}
11+
12+
int main(int argc, char *argv[])
13+
{
14+
/*
15+
Create start and end clock variables of type `clock_t`
16+
and time the function.
17+
*/
18+
clock_t start = clock();
19+
function();
20+
clock_t end = clock();
21+
22+
// Divide clock difference with CLOCKS_PER_SEC to convert time to seconds
23+
// and then typecast it to double.
24+
double time_taken = ((double)(end - start))/CLOCKS_PER_SEC;
25+
26+
printf("function took %f seconds to execute\n", time_taken);
27+
28+
return 0;
29+
}

C/Weather-Details/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
key

C/Weather-Details/Makefile

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
geom: weather.c
2+
gcc weather.c -lcurl -ljson-c -o weather
3+
touch key
4+
5+
cleanup:
6+
rm weather

C/Weather-Details/README.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# Get Weather Details of a city
2+
The following program uses cURL to perform an HTTP GET request to Open Weather API. The reponse(JSON) is then parsed with the help of json-c library to extract the necessary information. Makefile is responsible for automated compilation of the script. Terminal output is colored using ANSI codes for green and yellow.
3+
4+
## Dependencies
5+
* cURL
6+
* json-c
7+
8+
## Running script
9+
* Build by running: `make`
10+
* Save your API key in `key` file.
11+
* Execute by : `./weather <city-name>`
12+
* for e.g : `./weather Kolkata`
13+
14+
## Output
15+
16+
![output](output.png)

C/Weather-Details/output.png

21 KB
Loading

C/Weather-Details/weather

18.9 KB
Binary file not shown.

C/Weather-Details/weather.c

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
#include<stdio.h>
2+
#include<curl/curl.h>
3+
#include<json-c/json.h>
4+
#include<string.h>
5+
6+
// Callback function to handle API response.
7+
static size_t write_callback(char *contents, size_t size, size_t nmemb, void *userp)
8+
{
9+
// Json objects to parse response.
10+
json_object *parsed_json, *main_data, *temp, *wind, *wind_speed, *humidity;
11+
12+
// Tokenise JSON
13+
parsed_json = json_tokener_parse(contents);
14+
15+
// Extract data from JSON response.
16+
json_object_object_get_ex(parsed_json, "main", &main_data);
17+
json_object_object_get_ex(main_data, "temp", &temp);
18+
json_object_object_get_ex(main_data, "humidity", &humidity);
19+
json_object_object_get_ex(parsed_json, "wind", &wind);
20+
json_object_object_get_ex(wind, "speed", &wind_speed);
21+
22+
// Print json response with color.
23+
printf("\033[93mTemperature: %s°C\033[0m\n", json_object_get_string(temp));
24+
printf("\033[93mHumidity : %s%%\033[0m\n", json_object_get_string(humidity));
25+
printf("\033[93mWind Speed : %sknots\033[0m\n", json_object_get_string(wind_speed));
26+
27+
// return size of the data handled.
28+
return size * nmemb;
29+
}
30+
31+
32+
int main(int argc, char **argv)
33+
{
34+
// Define necessary variables.
35+
char *location = argv[1];
36+
char key[64], url[1024];
37+
38+
// Initialise CURL structures.
39+
CURL *curl;
40+
CURLcode res;
41+
42+
// Open key file to extract API key.
43+
FILE *fp = fopen("key", "r");
44+
if(fp == NULL)
45+
{
46+
fprintf(stderr, "Unable to open `key` file.");
47+
exit(1);
48+
}
49+
fgets(key, 64, fp);
50+
fclose(fp);
51+
52+
// Formulate the API endpoint.
53+
sprintf(url, "https://api.openweathermap.org/data/2.5/weather?q=%s&units=metric&appid=%s", location, key);
54+
55+
printf("\033[92mWeather details for %s:\n", location);
56+
57+
// Perform a GET HTTP request.
58+
curl = curl_easy_init();
59+
if(curl)
60+
{
61+
// Set CURL options
62+
curl_easy_setopt(curl, CURLOPT_URL, url);
63+
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_callback);
64+
65+
// Perform the request, res will get the return code
66+
res = curl_easy_perform(curl);
67+
/* Check for errors */
68+
if(res != CURLE_OK)
69+
fprintf(stderr, "curl_easy_perform() failed: %s\n", curl_easy_strerror(res));
70+
71+
// cleanup
72+
curl_easy_cleanup(curl);
73+
}
74+
75+
return 0;
76+
}

C/Wifi-Passwords/README.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Saved Wifi Passwords
2+
A cross platform C program which queries and writes all saved WiFi networks and their passwords in a text file.
3+
4+
## Executing the Program:
5+
* Compile the program: `gcc gen-wifi.c`.
6+
* On windows, do `a.exe`.
7+
* On Linux, do `./a.out`.
8+
9+
**Sample use case:**
10+
Let's say you want to share the your wifi password to your friend but you dont remember it clearly then you can use this script to find the saved password.
11+
12+
**Contents of `wifi_password.txt`:**
13+
```txt
14+
<SSID 1>:<password>
15+
<SSID 2>:<password>
16+
.
17+
.
18+
.
19+
```

C/Wifi-Passwords/gen-wifi.c

Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
#include<stdio.h>
2+
#include<stdlib.h>
3+
#include<string.h>
4+
5+
// Add a global variable to skip the first line of shell output in windows.
6+
int first_skip = 1;
7+
8+
// Function to format shell output.
9+
char* formatLog(char *buffer)
10+
{
11+
// Check which OS the script is currently running on.
12+
#ifdef __linux__
13+
14+
// Tokenize the script using `:` as a delimiter.
15+
char *token_1 = strtok(buffer, ":");
16+
char *token_2 = strtok(NULL, ":");
17+
18+
// Extract the password tokenizing through `=`.
19+
char *ssid_part = strrchr(token_1, '/');
20+
char *psk = strrchr(token_2, '=');
21+
22+
// Extract the ssid.
23+
char *ssid = strtok(ssid_part+1, ".");
24+
25+
// Some formatting to make it more readable.
26+
psk += 1;
27+
strcat(ssid, ":");
28+
strcat(ssid, psk);
29+
30+
return ssid;
31+
32+
#elif _WIN32
33+
34+
#include<ctype.h>
35+
36+
// Skip the first line of shell output.
37+
if(first_skip == 1)
38+
{ first_skip = 0;
39+
return "";
40+
}
41+
42+
// Extract ssid by tokenizing through `:`.
43+
char *ssid = strtok(buffer, ":");
44+
ssid = strtok(NULL, ": ");
45+
46+
// Trim trailing whitespaces.
47+
char *end;
48+
end = ssid + strlen(ssid) - 1;
49+
while(end > ssid && isspace((unsigned char)*end)) end--;
50+
end[1] = '\0';
51+
52+
// Create a file pointer to access a different shell.
53+
FILE *shell_ptr;
54+
55+
// Format the shell command to extract the password.
56+
char psk_command[1024];
57+
sprintf(psk_command, "netsh wlan show profile %s key=clear | findstr \"Key Content\"", ssid);
58+
shell_ptr = popen(psk_command, "r");
59+
60+
// Create a buffer to read the data.
61+
char psk_temp[1024];
62+
fgets(psk_temp, sizeof(psk_temp), shell_ptr);
63+
64+
// Extract the password.
65+
char *psk = strtok(psk_temp, ":");
66+
psk = strtok(NULL, ": ");
67+
68+
// Ignore passwords which are null.
69+
if((psk != NULL) && (psk[0] == '\0') && (strcmp(psk, "1") != 0))
70+
{
71+
end = psk + strlen(psk) - 1;
72+
while(end > psk && isspace((unsigned char)*end)) end--;
73+
end[1] = '\0';
74+
}
75+
76+
// Set open networks as [Open Network]
77+
if(strlen(psk) == 2)
78+
psk = "[Open Network]";
79+
80+
// Close unused shell pointer.
81+
pclose(shell_ptr);
82+
83+
// Format output for better readability.
84+
strcat(ssid, ":");
85+
strcat(ssid, psk);
86+
return ssid;
87+
88+
#endif
89+
}
90+
91+
int main()
92+
{
93+
/*
94+
Create file pointers for:
95+
- Reading information from shell.
96+
- Writing to a Text File.
97+
98+
Also create a buffer to read the shell output, line by line.
99+
*/
100+
FILE *shell_op, *fptr;
101+
char buffer[3096];
102+
103+
// Check which OS the program is currently being run on.
104+
#ifdef _WIN32
105+
106+
char *command = "netsh wlan show profile | findstr \":\"";
107+
108+
#elif __linux__
109+
110+
char *command = "sudo grep -r '^psk=' /etc/NetworkManager/system-connections/";
111+
112+
#endif
113+
114+
// Open a text file for writing.
115+
fptr = fopen("wifi_password.txt", "w");
116+
117+
// Check if file was created or not.
118+
if(fptr == NULL)
119+
{
120+
puts("Error creating file...");
121+
exit(1);
122+
}
123+
124+
// Spawn a shell process to communitcate with OS.
125+
shell_op = popen(command, "r");
126+
if(shell_op == NULL)
127+
{
128+
puts("Failed to communicate with shell.");
129+
exit(1);
130+
}
131+
132+
// Read shell output line by line.
133+
while (fgets(buffer, sizeof(buffer), shell_op) != NULL)
134+
{
135+
// Format shell output for better readability.
136+
char *formattedLog = formatLog(buffer);
137+
138+
// Write the formated information to the file.
139+
fprintf(fptr, "%s", formattedLog);
140+
}
141+
142+
143+
// Close all file pointers and free up memory space.
144+
pclose(shell_op);
145+
fclose(fptr);
146+
147+
// Time Complexity = O(n); n = No. of saved wifi networks.
148+
// Space Complexity = O(1);
149+
150+
return 0;
151+
}

C/Wifi-Passwords/wifi_password.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<SSID>:<password>

0 commit comments

Comments
 (0)