Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game. |
| Notices |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
07-16-2002, 05:36 PM
|
#1
|
|
Senior Member
Registered: Mar 2002
Location: Los Angeles, CA
Distribution: Debian, Ubuntu
Posts: 1,334
Rep:
|
couple C++ questions - mac address & last file access time.
I need to be able to find out how to determine the MAC address inside C++.
I also need to be able to find out the last access time of a file (as in, I give the file name and get a time_t back).
I've done this stuff in windows (which was quite a pain) and now need to port to Linux.
any help would be great.
|
|
|
|
07-16-2002, 06:54 PM
|
#2
|
|
Senior Member
Registered: Mar 2002
Location: Los Angeles, CA
Distribution: Debian, Ubuntu
Posts: 1,334
Original Poster
Rep:
|
well, in case anyone is interested, here's a super easy method a friend fo mine came up with that works better than anything else on the web...
Code:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <signal.h>
#include <ctype.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/time.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <linux/sockios.h>
#include <linux/if.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <netdb.h>
#include <fcntl.h>
int main (int argc, char *argv[])
{
int sd, sd2; /* socket descriptors */
int c;
unsigned char foo[14];
struct ifreq ifr;
int idx;
fprintf(stderr,"socket %d\n",sd);
sprintf(ifr.ifr_ifrn.ifrn_name,"eth0");
// fprintf(stderr,"ioctl ret %d\n",ioctl(sd, SIOCGIFHWADDR,&ifr));
memcpy(foo,&ifr.ifr_ifru.ifru_hwaddr.sa_data,14);
for(idx=0;idx<6;idx++){
fprintf(stderr,"%02x:",foo[idx]);
}
fprintf(stderr, "\n");
}
there's a lot of extra includes, but you get the picture...
|
|
|
|
07-16-2002, 07:41 PM
|
#3
|
|
Senior Member
Registered: Mar 2002
Location: Los Angeles, CA
Distribution: Debian, Ubuntu
Posts: 1,334
Original Poster
Rep:
|
wait a minute, that was totally wrong, this is better:
Code:
int main (int argc, char *argv[])
{
struct protoent *ptrp; /* pointer to a protocol table entry */
struct sockaddr_in sad; /* structure to hold server's address */
int sd, sd2; /* socket descriptors */
int port=5193; /* protocol port number */
int c;
unsigned char foo[14];
struct ifreq ifr;
int idx;
if (port > 0) /* test for illegal value */
sad.sin_port = htons((u_short)port);
else { /* print error message and exit */
fprintf (stderr, "bad port number %s/n",argv[1]);
exit (1);
}
if ( ((int)(ptrp = getprotobyname("tcp"))) == 0) {
fprintf(stderr, "cannot map \"tcp\" to protocol number");
exit (1);
}
sd = socket (PF_INET, SOCK_STREAM, ptrp->p_proto);
if (sd < 0) {
fprintf(stderr, "socket creation failed\n");
exit(1);
}
sprintf(ifr.ifr_ifrn.ifrn_name,"eth0");
ioctl(sd, SIOCGIFHWADDR,&ifr);
memcpy(foo,&ifr.ifr_ifru.ifru_hwaddr.sa_data,14);
printf("MAC address: %02X:%02X:%02X:%02X:%02X:%02X\n",
foo[0],
foo[1],
foo[2],
foo[3],
foo[4],
foo[5]);
}
|
|
|
|
07-17-2002, 03:17 AM
|
#4
|
|
Senior Member
Registered: Dec 2001
Location: The Netherlands
Distribution: Ubuntu
Posts: 1,316
Rep:
|
To get the last access time of a file you would do something like this (this program would also work in windows):
Code:
#include <stdio.h>
#include <sys/stat.h>
#include <time.h>
int main()
{
char filename[] = "atime.c";
char timeStr[100];
struct stat buf;
if (!stat(filename, &buf))
{
struct tm* atime = localtime(&buf.st_atime);
strftime(timeStr, 100, "%Y-%m-%d %H:%M.%S", atime);
printf("atime = %s\n", timeStr);
}
else
printf("error getting atime\n");
return 0;
}
|
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 07:44 AM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|