LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   How to Get directory time stamp in c++ (https://www.linuxquestions.org/questions/programming-9/how-to-get-directory-time-stamp-in-c-719347/)

craigorymaas 04-15-2009 01:01 PM

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.

paulsm4 04-15-2009 02:34 PM

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

craigorymaas 04-15-2009 04:08 PM

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

Sergei Steshenko 04-15-2009 04:22 PM

Quote:

Originally Posted by craigorymaas (Post 3510171)
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.

paulsm4 04-16-2009 11:33 AM

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" ;-)

craigorymaas 04-16-2009 11:54 AM

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!

craigorymaas 04-16-2009 12:12 PM

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'

craigorymaas 04-16-2009 12:36 PM

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?

craigorymaas 04-16-2009 12:54 PM

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!)

paulsm4 04-16-2009 04:35 PM

No - you're safe:

http://en.wikipedia.org/wiki/Unix_time

craigorymaas 04-16-2009 05:05 PM

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

paulsm4 04-17-2009 12:23 AM

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


All times are GMT -5. The time now is 05:20 PM.