Skip to content

Commit

Permalink
Merge pull request #3 from Veerhan-glitch/v0.0.1
Browse files Browse the repository at this point in the history
Now works on linux properly
  • Loading branch information
chethanyadav456 authored Oct 27, 2024
2 parents 4105d76 + 533c575 commit 00a5de1
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 10 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,16 @@ 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)**

**Follow this guide to Install GNU Compiler Collection (GCC) - [MinGw](https://www.geeksforgeeks.org/installing-mingw-tools-for-c-c-and-changing-environment-variable/)**

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
Expand Down
43 changes: 36 additions & 7 deletions cmd_functions.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,8 @@
* Under License of MIT
*
*/

#include "cmd_functions.h"
#include <stdio.h>
#include <winsock2.h>
#include <ws2tcpip.h>
#include <windows.h>
#include <iphlpapi.h> // 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();
Expand All @@ -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;
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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;
}

Expand Down
20 changes: 20 additions & 0 deletions cmd_functions.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,26 @@
#ifndef CMD_FUNCTIONS_H
#define CMD_FUNCTIONS_H

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

// For Windows
#ifdef _WIN32
#include <winsock2.h>
#include <ws2tcpip.h>
#include <windows.h>
#include <iphlpapi.h>
#pragma comment(lib, "Ws2_32.lib")
#pragma comment(lib, "Iphlpapi.lib")

// For Linux
#else
#include <unistd.h>
#include <ifaddrs.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#endif

void ipaddr();
int hostname();
void display_help();
Expand Down
File renamed without changes.
7 changes: 7 additions & 0 deletions run.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/bin/bash

mkdir -p outputs

gcc -Wall -Wextra -g -o outputs/quickcmd main.c cmd_functions.c

./outputs/quickcmd

0 comments on commit 00a5de1

Please sign in to comment.