diff --git a/README.md b/README.md index f3ad3f1..bb8d68a 100644 --- a/README.md +++ b/README.md @@ -11,7 +11,8 @@ git clone https://github.com/chethanyadav456/quickcmd.git ``` 2. Change the directory ```bash -cd quickcmd +./run.cmd // For Windows +sh run.sh // For Linux ``` **Before Compiling you need to install the compiler (GCC)** @@ -19,8 +20,7 @@ cd quickcmd 3. Compile the code and run the script ```bash -./RUN.cmd // For Windows -./RUN.sh // For Linux (change the file [RUN.cmd] extension to [RUN.sh]) +make run ``` 4. Add the path to the environment variables (optional) ```bash diff --git a/cmd_functions.c b/cmd_functions.c index c885855..e304855 100644 --- a/cmd_functions.c +++ b/cmd_functions.c @@ -6,14 +6,8 @@ * Under License of MIT * */ + #include "cmd_functions.h" -#include -#include -#include -#include -#include // For IP address retrieval -#pragma comment(lib, "Ws2_32.lib") // Link with Winsock library -#pragma comment(lib, "Iphlpapi.lib") // Link with IP Helper library void printfNewLine(char *str); void ipaddr(); @@ -23,6 +17,7 @@ void display_help(); // Function to retrieve and display the local machine's IP addresses void ipaddr() { +#ifdef _WIN32 DWORD dwSize = 0; DWORD dwRetVal = 0; IP_ADAPTER_INFO *pAdapterInfo; @@ -70,11 +65,35 @@ void ipaddr() // Free allocated memory if (pAdapterInfo) free(pAdapterInfo); + +#else + struct ifaddrs *ifaddr, *ifa; + char ipstr[INET_ADDRSTRLEN]; + + if (getifaddrs(&ifaddr) == -1) { + perror("getifaddrs"); + return; + } + + for (ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next) { + if (ifa->ifa_addr && ifa->ifa_addr->sa_family == AF_INET) { + if (inet_ntop(AF_INET, &((struct sockaddr_in *)ifa->ifa_addr)->sin_addr, ipstr, sizeof(ipstr))) { + printf("Adapter Name: %s\n", ifa->ifa_name); + printf("IP Address: %s\n", ipstr); + } else { + perror("inet_ntop"); + } + } + } + + freeifaddrs(ifaddr); +#endif } // Function to get and display the local machine's hostname int hostname() { +#ifdef _WIN32 WSADATA wsaData; int result; @@ -102,6 +121,16 @@ int hostname() // Clean up Winsock WSACleanup(); +#else + char hostname[256]; + if (gethostname(hostname, sizeof(hostname)) == 0) { + printf("Hostname: %s\n", hostname); + } else { + perror("gethostname"); + return EXIT_FAILURE; + } +#endif + return EXIT_SUCCESS; } diff --git a/cmd_functions.h b/cmd_functions.h index 3ed3b68..b8d4e8c 100644 --- a/cmd_functions.h +++ b/cmd_functions.h @@ -10,6 +10,26 @@ #ifndef CMD_FUNCTIONS_H #define CMD_FUNCTIONS_H +#include +#include + +// For Windows +#ifdef _WIN32 +#include +#include +#include +#include +#pragma comment(lib, "Ws2_32.lib") +#pragma comment(lib, "Iphlpapi.lib") + +// For Linux +#else +#include +#include +#include +#include +#endif + void ipaddr(); int hostname(); void display_help(); diff --git a/RUN.cmd b/run.cmd similarity index 100% rename from RUN.cmd rename to run.cmd diff --git a/run.sh b/run.sh new file mode 100644 index 0000000..77cd8c7 --- /dev/null +++ b/run.sh @@ -0,0 +1,7 @@ +#!/bin/bash + +mkdir -p outputs + +gcc -Wall -Wextra -g -o outputs/quickcmd main.c cmd_functions.c + +./outputs/quickcmd \ No newline at end of file