LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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 04-15-2009, 01:01 PM   #1
craigorymaas
LQ Newbie
 
Registered: Mar 2009
Posts: 9

Rep: Reputation: 0
Smile How to Get directory time stamp in c++


Hi, I am new to both C++ and Linux. I am working on a license key generator. One of the unique characteristics that we are going to grab off of a linux machine is the datestamp of when /bin was created. I would like the date as a long. Is there an easy way to do this?
Please help!
thank you very much in advance.
 
Old 04-15-2009, 02:34 PM   #2
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
Probably the easiest way is with "stat":
Code:
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
...

  struct stat st;
  int ierr = stat ("/bin", &st);
  if (ierr != 0)
    << error handling >>
  unsigned long date = (unsigned long)stat.st_mtime;
  ...
You can easily find more with "man stat" (from a command prompt) or Google for "Linux struct stat" code examples.

'Hope that helps .. PSM
 
Old 04-15-2009, 04:08 PM   #3
craigorymaas
LQ Newbie
 
Registered: Mar 2009
Posts: 9

Original Poster
Rep: Reputation: 0
Hi, thank you for the quick response. I tried running the code you gave me..
This is what I am running currently:

#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <iostream>

int main()
{
struct stat st;
int ierr = stat ("/bin", &st);
if (ierr != 0) {
std::cout << "errori\n";
}
unsigned long date = (unsigned long) stat.st_mtime;
std::cout << "date = " << date << "\n";
}

There error i get when i compile (w/ g++ in a terminal shell) is
craig.cpp:13: error: request for member 'st_mtim' in 'stat', which is of non-class type 'int ()(const char*, stat*)throw ()'

I thought maybe it was a cast problem, but I tried changing to int w/ no luck. I checked the man entry but didn't see much info. thanks again
 
Old 04-15-2009, 04:22 PM   #4
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by craigorymaas View Post
Hi, thank you for the quick response. I tried running the code you gave me..
This is what I am running currently:

#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <iostream>

int main()
{
struct stat st;
int ierr = stat ("/bin", &st);
if (ierr != 0) {
std::cout << "errori\n";
}
unsigned long date = (unsigned long) stat.st_mtime;
std::cout << "date = " << date << "\n";
}

There error i get when i compile (w/ g++ in a terminal shell) is
craig.cpp:13: error: request for member 'st_mtim' in 'stat', which is of non-class type 'int ()(const char*, stat*)throw ()'

I thought maybe it was a cast problem, but I tried changing to int w/ no luck. I checked the man entry but didn't see much info. thanks again

The person who suggested the code used 'st_mtime', the error message for some reason says 'st_mtim'.

I'd suggest to start from this discrepancy.
 
Old 04-16-2009, 11:33 AM   #5
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
craigorymaas -

"The person who suggested you start with the discrepency" is correct.

Here's sample code:
Code:
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>

#define BIN_DIR "/bin"

int
main (int argc, char *argv[])
{
  struct stat st;
  int ierr = stat (BIN_DIR, &st);
  if (ierr != 0)
  {
    printf("Sorry: couldn't find %s: ierr= %d\n", BIN_DIR, ierr);
    return 1;
  }
  printf (
    "dev_t      st_dev:     %lu  /* device */\n"
    "ino_t      st_ino:     %lu  /* inode */\n"
    "mode_t     st_mode:    %lu  /* protection */\n"
    "nlink_t    st_nlink:   %lu  /* number of hard links */\n"
    "uid_t      st_uid:     %lu  /* user ID of owner */\n"
    "gid_t      st_gid:     %lu  /* group ID of owner */\n"
    "dev_t      st_rdev:    %lu  /* device type (if inode device) */\n"
    "off_t      st_size:    %lu  /* total size, in bytes */\n"
    "blksize_t  st_blksize: %lu  /* blocksize for filesystem I/O */\n"
    "blkcnt_t   st_blocks:  %lu  /* number of blocks allocated */\n"
    "time_t     st_atime:   %lu  /* time of last access */\n"
    "time_t     st_mtime:   %lu  /* time of last modification */\n"
    "time_t     st_ctime:   %lu  /* time of last status change */\n",
    (unsigned long)st.st_dev,
    (unsigned long)st.st_ino,
    (unsigned long)st.st_mode,
    (unsigned long)st.st_nlink,
    (unsigned long)st.st_uid,
    (unsigned long)st.st_gid,
    (unsigned long)st.st_rdev,
    (unsigned long)st.st_size,
    (unsigned long)st.st_blksize,
    (unsigned long)st.st_blocks,
    (unsigned long)st.st_atime,
    (unsigned long)st.st_mtime,
    (unsigned long)st.st_ctime);
  return 0;
}
Quote:
gcc -Wall -g -o x x.c
./x
dev_t st_dev: 770 /* device */
ino_t st_ino: 52 /* inode */
mode_t st_mode: 16877 /* protection */
nlink_t st_nlink: 2 /* number of hard links */
uid_t st_uid: 0 /* user ID of owner */
gid_t st_gid: 0 /* group ID of owner */
dev_t st_rdev: 0 /* device type (if inode device) */
off_t st_size: 2896 /* total size, in bytes */
blksize_t st_blksize: 4096 /* blocksize for filesystem I/O */
blkcnt_t st_blocks: 5 /* number of blocks allocated */
time_t st_atime: 1237848350 /* time of last access */
time_t st_mtime: 1160427118 /* time of last modification */
time_t st_ctime: 1160427118 /* time of last status change */
'Hope that helps - "The person who suggested the code that used st_mtime" ;-)
 
Old 04-16-2009, 11:54 AM   #6
craigorymaas
LQ Newbie
 
Registered: Mar 2009
Posts: 9

Original Poster
Rep: Reputation: 0
Thanks so much for the help. I haven't attempted to run the code yet, I will start in just a second but wanted to send a quick thanks first!
 
Old 04-16-2009, 12:12 PM   #7
craigorymaas
LQ Newbie
 
Registered: Mar 2009
Posts: 9

Original Poster
Rep: Reputation: 0
hmm..

Ok, I added the extra import, etc. Changed my couts to printf, and now my error is
/tmp/ccqV0frA.o.eh_frame+0x11): undefined reference to `__gxx_personality_v0'
 
Old 04-16-2009, 12:36 PM   #8
craigorymaas
LQ Newbie
 
Registered: Mar 2009
Posts: 9

Original Poster
Rep: Reputation: 0
Oops.
OK, here is the error without smilies
gcc -o craig craig.cpp

/tmp/ccs3MTJB.o:(.eh_frame+0x11): undefined reference to `__gxx_personality_v0'


I've tried it w/ -Wall, -g, etc
All result in the same error. Has someone seen this before? Am I missing some imports or something?
 
Old 04-16-2009, 12:54 PM   #9
craigorymaas
LQ Newbie
 
Registered: Mar 2009
Posts: 9

Original Poster
Rep: Reputation: 0
fixed it

OK, nm those last messages, it was just a c++ linking error. I compiled w/ g++ and everything was fine.

My concern now is that it is getting the mtime or the ctime, which are modified and changed, correct?
The time that I get absolutely has to be something that is not going to change, because it is for a unique key for a system. If these times change, then the key will cease to work. Are these times something that will change?

(thanks again!)
 
Old 04-16-2009, 04:35 PM   #10
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
No - you're safe:

http://en.wikipedia.org/wiki/Unix_time
 
Old 04-16-2009, 05:05 PM   #11
craigorymaas
LQ Newbie
 
Registered: Mar 2009
Posts: 9

Original Poster
Rep: Reputation: 0
Hey paul,
I've been able to simply execute "touch bin", at which point the ctime, mtime, and atime all change from their previous values. I have read on some forums that linux actually does not save any "creation" time variables and that it is basically impossible to retrieve this information. I am ready to give up on this idea and chalk it up to impossible.


On a side note, I was able to get the first logins to the system with the function "last" called from command line. Do you know if this can also be stored in a struct? I have searched around but have been unable so far to find information.

Thanks again
Craig
 
Old 04-17-2009, 12:23 AM   #12
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
There are lots of things you can use to identify a host as "unique".

Mac addresses and CPU IDs come immediately to mind.

You can also create your own "marker file", give it some unique stamp (write a GUID into it, perhaps).

'Hope that helps .. PSM
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
time stamp of files ZAMO Linux - General 1 12-17-2008 05:45 AM
Using time-stamp in emacs dualcore75 Programming 1 02-08-2007 06:41 AM
presentation time stamp nesta Programming 1 11-24-2006 01:57 AM
Time stamp Kalyani1 Linux - Software 0 11-07-2005 02:58 PM
Time stamp in Samba is 11 hours behind time stamp in Linux Linh Linux - General 3 09-04-2003 12:44 PM

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

All times are GMT -5. The time now is 11:21 AM.

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