From 317b0500829e2bc9e88f6191dfe363e848dce019 Mon Sep 17 00:00:00 2001 From: Veerhan-Glitch Date: Sat, 26 Oct 2024 00:18:23 +0530 Subject: [PATCH 1/2] made it work on linux, now use makefile to setup and run --- README.md | 3 +-- RUN.cmd | 1 - cmd_functions.c | 43 ++++++++++++++++++++++++++++++++++++------- cmd_functions.h | 20 ++++++++++++++++++++ makefile | 24 ++++++++++++++++++++++++ 5 files changed, 81 insertions(+), 10 deletions(-) delete mode 100644 RUN.cmd create mode 100644 makefile diff --git a/README.md b/README.md index f3ad3f1..0263a7f 100644 --- a/README.md +++ b/README.md @@ -19,8 +19,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/RUN.cmd b/RUN.cmd deleted file mode 100644 index 27958f9..0000000 --- a/RUN.cmd +++ /dev/null @@ -1 +0,0 @@ -gcc main.c cmd_functions.c -o outputs/quickcmd.exe -liphlpapi -lws2_32 && "./outputs/quickcmd.exe" \ No newline at end of file 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/makefile b/makefile new file mode 100644 index 0000000..8c1c67f --- /dev/null +++ b/makefile @@ -0,0 +1,24 @@ +ifeq ($(OS),Windows_NT) + LIBS=-liphlpapi -lws2_32 +else # else empty as linux dosent need extra lib + LIBS= +endif + +all: outputs outputs/quickcmd + +outputs: + mkdir outputs + +outputs/quickcmd: main.c cmd_functions.c + gcc -Wall -Wextra -g -o $@ $^ $(LIBS) + +run: outputs/quickcmd + @echo "Running the program..." + @outputs/quickcmd + +clean: + @if [ "$(OS)" = "Windows_NT" ]; then \ + rmdir /s /q outputs; \ + else \ + rm -rf outputs; \ + fi \ No newline at end of file From 533c575d7338f57783a4afe1d1f39217835a8a97 Mon Sep 17 00:00:00 2001 From: Veerhan-Glitch Date: Sat, 26 Oct 2024 00:36:59 +0530 Subject: [PATCH 2/2] reverted to using cmd ans sh files as seting up make might be troublesome for window users --- README.md | 3 ++- makefile | 24 ------------------------ run.cmd | 1 + run.sh | 7 +++++++ 4 files changed, 10 insertions(+), 25 deletions(-) delete mode 100644 makefile create mode 100644 run.cmd create mode 100644 run.sh diff --git a/README.md b/README.md index 0263a7f..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)** diff --git a/makefile b/makefile deleted file mode 100644 index 8c1c67f..0000000 --- a/makefile +++ /dev/null @@ -1,24 +0,0 @@ -ifeq ($(OS),Windows_NT) - LIBS=-liphlpapi -lws2_32 -else # else empty as linux dosent need extra lib - LIBS= -endif - -all: outputs outputs/quickcmd - -outputs: - mkdir outputs - -outputs/quickcmd: main.c cmd_functions.c - gcc -Wall -Wextra -g -o $@ $^ $(LIBS) - -run: outputs/quickcmd - @echo "Running the program..." - @outputs/quickcmd - -clean: - @if [ "$(OS)" = "Windows_NT" ]; then \ - rmdir /s /q outputs; \ - else \ - rm -rf outputs; \ - fi \ No newline at end of file diff --git a/run.cmd b/run.cmd new file mode 100644 index 0000000..27958f9 --- /dev/null +++ b/run.cmd @@ -0,0 +1 @@ +gcc main.c cmd_functions.c -o outputs/quickcmd.exe -liphlpapi -lws2_32 && "./outputs/quickcmd.exe" \ No newline at end of file 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