I have a simple function to get the MAC address on a linux machine (assuming eth0 exists. I'll write more later on).
Side Question: Is there an interface universal across ALL Linux distros? Other than lo?
C Function:
char* getmac(){
FILE *mf;
mf = fopen("/sys/class/net/eth0/address", "r");
if(!mf) exit(-1);
char *mac;
fgets(mac, 18, mf);
printf("%2\n", mac);
return mac;
}
Now, this prints out the MAC perfectly. However, when I return it, I get a totally different value.
char *m;
m = getmac();
printf("%s\n", m);
yields a completely different, mostly unreadable string. The first 2 characters are ALWAYS correct, after that, it's unreadable...
This worked like a charm:
char* getmac(){
FILE *mf;
mf = fopen("/sys/class/net/eth0/address", "r");
if(!mf) exit(-1);
char *mac;
mac = calloc(18, sizeof(char));
if(!mac) exit(-1);
fgets(mac, 18, mf);
printf("%s\n", mac);
return mac;
}
Thanks for the answers!! Also, free'd later on.
char *mac;is uninitialised. It points to nothing.