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. |
Probably the easiest way is with "stat":
Code:
#include <sys/types.h> 'Hope that helps .. PSM |
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 |
Quote:
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. |
craigorymaas -
"The person who suggested you start with the discrepency" is correct. Here's sample code: Code:
#include <stdio.h> Quote:
|
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!
|
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' |
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? |
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!) |
|
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 |
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. |