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. |
|
 |
04-15-2009, 01:01 PM
|
#1
|
|
LQ Newbie
Registered: Mar 2009
Posts: 9
Rep:
|
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.
|
|
|
|
04-15-2009, 02:34 PM
|
#2
|
|
Guru
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,861
Rep: 
|
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
|
|
|
|
04-15-2009, 04:08 PM
|
#3
|
|
LQ Newbie
Registered: Mar 2009
Posts: 9
Original Poster
Rep:
|
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
|
|
|
|
04-15-2009, 04:22 PM
|
#4
|
|
Senior Member
Registered: May 2005
Posts: 4,387
|
Quote:
Originally Posted by craigorymaas
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.
|
|
|
|
04-16-2009, 11:33 AM
|
#5
|
|
Guru
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,861
Rep: 
|
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" ;-)
|
|
|
|
04-16-2009, 11:54 AM
|
#6
|
|
LQ Newbie
Registered: Mar 2009
Posts: 9
Original Poster
Rep:
|
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!
|
|
|
|
04-16-2009, 12:12 PM
|
#7
|
|
LQ Newbie
Registered: Mar 2009
Posts: 9
Original Poster
Rep:
|
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'
|
|
|
|
04-16-2009, 12:36 PM
|
#8
|
|
LQ Newbie
Registered: Mar 2009
Posts: 9
Original Poster
Rep:
|
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?
|
|
|
|
04-16-2009, 12:54 PM
|
#9
|
|
LQ Newbie
Registered: Mar 2009
Posts: 9
Original Poster
Rep:
|
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!)
|
|
|
|
04-16-2009, 04:35 PM
|
#10
|
|
Guru
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,861
Rep: 
|
|
|
|
|
04-16-2009, 05:05 PM
|
#11
|
|
LQ Newbie
Registered: Mar 2009
Posts: 9
Original Poster
Rep:
|
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
|
|
|
|
04-17-2009, 12:23 AM
|
#12
|
|
Guru
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,861
Rep: 
|
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
|
|
|
|
| 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 03:39 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
|
|