ProgrammingThis 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.
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.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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
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'.
"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" ;-)
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'
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?
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.