diff --git a/.vscode/c_cpp_properties.json b/.vscode/c_cpp_properties.json new file mode 100644 index 00000000..4aab71ae --- /dev/null +++ b/.vscode/c_cpp_properties.json @@ -0,0 +1,20 @@ +{ + "configurations": [ + { + "name": "Win32", + "includePath": [ + "${workspaceFolder}/**" + ], + "defines": [ + "_DEBUG", + "UNICODE", + "_UNICODE" + ], + "compilerPath": "D:\\MinGW\\bin\\gcc.exe", + "cStandard": "gnu17", + "cppStandard": "gnu++14", + "intelliSenseMode": "windows-gcc-x86" + } + ], + "version": 4 +} \ No newline at end of file diff --git a/C/print ip address and hostname.c b/C/print ip address and hostname.c new file mode 100644 index 00000000..1cffc8fe --- /dev/null +++ b/C/print ip address and hostname.c @@ -0,0 +1,69 @@ +// C program to display hostname +// and IP address +#include +#include +#include +#include +#include +#include +#include +#include +#include + +// Returns hostname for the local computer +void checkHostName(int hostname) +{ + if (hostname == -1) + { + perror("gethostname"); + exit(1); + } +} + +// Returns host information corresponding to host name +void checkHostEntry(struct hostent * hostentry) +{ + if (hostentry == NULL) + { + perror("gethostbyname"); + exit(1); + } +} + +// Converts space-delimited IPv4 addresses +// to dotted-decimal format +void checkIPbuffer(char *IPbuffer) +{ + if (NULL == IPbuffer) + { + perror("inet_ntoa"); + exit(1); + } +} + +// Driver code +int main() +{ + char hostbuffer[256]; + char *IPbuffer; + struct hostent *host_entry; + int hostname; + + // To retrieve hostname + hostname = gethostname(hostbuffer, sizeof(hostbuffer)); + checkHostName(hostname); + + // To retrieve host information + host_entry = gethostbyname(hostbuffer); + checkHostEntry(host_entry); + + // To convert an Internet network + // address into ASCII string + IPbuffer = inet_ntoa(*((struct in_addr*) + host_entry->h_addr_list[0])); + + printf("Hostname: %s\n", hostbuffer); + printf("Host IP: %s", IPbuffer); + + return 0; +}