LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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


Reply
  Search this Thread
Old 07-16-2002, 05:36 PM   #1
BrianK
Senior Member
 
Registered: Mar 2002
Location: Los Angeles, CA
Distribution: Debian, Ubuntu
Posts: 1,334

Rep: Reputation: 51
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.
 
Old 07-16-2002, 06:54 PM   #2
BrianK
Senior Member
 
Registered: Mar 2002
Location: Los Angeles, CA
Distribution: Debian, Ubuntu
Posts: 1,334

Original Poster
Rep: Reputation: 51
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...
 
Old 07-16-2002, 07:41 PM   #3
BrianK
Senior Member
 
Registered: Mar 2002
Location: Los Angeles, CA
Distribution: Debian, Ubuntu
Posts: 1,334

Original Poster
Rep: Reputation: 51
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]);

}
 
Old 07-17-2002, 03:17 AM   #4
Mik
Senior Member
 
Registered: Dec 2001
Location: The Netherlands
Distribution: Ubuntu
Posts: 1,316

Rep: Reputation: 47
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;
}
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
restrict server access by mac address? stinkpot Linux - Software 4 11-22-2005 07:05 AM
Questions on DHCP Filtering via MAC address brainee28 Linux - Networking 1 02-03-2005 02:11 PM
FYI: Ndiswrapper, Kernel-Source 2.6.9-4, Linux-2.6.10 && Couple O' Questions Outabux Debian 0 12-29-2004 03:13 PM
MAC address iptables questions scottman Linux - Security 2 10-01-2004 01:26 PM
Installing Linux (Mandrake) for the first time couple questions zzzmarcus Linux - Distributions 11 10-01-2002 09:38 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:37 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration