Concrete Geist
08-18-2001, 04:54 PM
I have installed everything related to the compiler, all JaVA support and C++ (EVERYTHING). I don't know where to run the program from, so I type gcc into my Terminal, and all it tells me is :
gcc-2.96 no input files
then waits for the next command.
:confused:
bdg1983
08-18-2001, 04:58 PM
Running gcc from the commandline requires the source code also.
gcc -o name nameofsource.c
Try this code. Copy and rename as getip.c
Then do 'gcc -o getip getip.c' and you should have a binary executable named getip.
* compile with: gcc -o getipaddr getipaddr.c
*/
#include <stdio.h>
#include <sys/ioctl.h>
#include <net/if.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <string.h>
void list_all();
void display_iface(char *name);
int fd;
int main(int argc, char *argv[])
{
fd = socket(PF_INET,SOCK_STREAM,0);
if (fd == -1) {
perror("socket() failed");
return 3;
}
if (argc == 1) {
list_all();
return 0;
}
display_iface(argv[1]);
return 0;
}
void display_iface(char *name)
{
struct sockaddr *sa;
struct sockaddr_in *sin;
struct ifreq ifr;
/* Copy the interface name into the buffer */
strncpy(ifr.ifr_name,name,IFNAMSIZ);
if (ioctl(fd,SIOCGIFADDR,&ifr) == -1) {
perror("ioctl failed");
return;
}
/* Now the buffer will contain the information we requested */
printf(ifr.ifr_name);
sa = (struct sockaddr *)&(ifr.ifr_addr);
if (sa->sa_family == AF_INET) {
sin = (struct sockaddr_in*) sa;
printf(": %s\n",inet_ntoa(sin->sin_addr));
} else {
printf(": Unknown Address Family (%d)\n",sa->sa_family);
}
}
void list_all()
{
struct sockaddr *sa;
struct sockaddr_in *sin;
struct ifreq iflist[10];
struct ifconf ifc;
int i;
/* We will list up to 10 interfaces,
* (if you have access to a machine with any more than that,
* you should already know all of this )
*/
ifc.ifc_len = sizeof(struct ifreq) * 10;
ifc.ifc_req = iflist;
if (ioctl(fd,SIOCGIFCONF,&ifc) == -1) {
perror("ioctl failed");
return;
}
for (i = 0; i < ifc.ifc_len/sizeof(struct ifreq); i++) {
printf(iflist[i].ifr_name);
sa = (struct sockaddr *)&(iflist[i].ifr_addr);
if (sa->sa_family == AF_INET) {
sin = (struct sockaddr_in*) sa;
printf(": %s\n",inet_ntoa(sin->sin_addr));
} else {
printf(": Unknown Address Family (%d)\n",sa->sa_family);
}
}
return;
}
bdg1983
08-18-2001, 06:19 PM
Your welcome.
As soon as you write some cutting edge apps, pass them on to us.