Operating System - Linux
1748011 Members
3564 Online
108757 Solutions
New Discussion юеВ

c script to get inode time_t info from file on hpux

 
SOLVED
Go to solution
Frank de Vries
Respected Contributor

c script to get inode time_t info from file on hpux

I would like to retain the timestamp
of a file in a directory.
I can query the timestamp with ls -ls,
but I want to to use a variable to store
it in, and I see there is an inbuilt function available for this on hpux called inode().
see man inode.

So, It must be possible to use a smal c prog
to do that , only I have limited c experience and I do not know how I can make sure I retrieve the inode value of :
time_t di_mtime; /* time last modified */ from the file I want.

If anyone had some work done in this directory and help me in the right direction.

Cheers.

Look before you leap
7 REPLIES 7
James R. Ferguson
Acclaimed Contributor
Solution

Re: c script to get inode time_t info from file on hpux

Hi Frant:

The system call that you are looking for is called 'stat(2)'. You can easily use this in Perl:

# perl -le '$t=(stat($ARGV[0]))[9];print "$t\n",scalar localtime($t)' file

...will print the 'mtime' in epoch seconda and a second line expressed in your localtime for the filename passed as an argument.

Regards!

...JRF...
Court Campbell
Honored Contributor

Re: c script to get inode time_t info from file on hpux

Which time stamp do you want to perserve? the creation timestamp, the access time, or the modification time. The output from ls -ls is going to be the modification time. You could just write a shell script that strips the mtime from the ls command. And if you really wanted to you could use the touch command to modify the mtime. I don't really see the point though.
"The difference between me and you? I will read the man page." and "Respect the hat." and "You could just do a search on ITRC, you don't need to start a thread on a topic that's been answered 100 times already." Oh, and "What. no points???"
Peter Godron
Honored Contributor

Re: c script to get inode time_t info from file on hpux

Hi,
Frank if you really still want a C program,
see Josephs answer here:
http://forums1.itrc.hp.com/service/forums/questionanswer.do?threadId=25592

A. Clay Stephenson
Acclaimed Contributor

Re: c script to get inode time_t info from file on hpux

Well, you specifically asked fior a C solution (although I would use Perl for this), so here is a rather robust example that I threw together in about 2 minutes from existing functions.

------------------------------------------
#include
#include
#include
#include
#include
#include

#define FALSE (0)
#define TRUE (1)

#define assign_errno(x) ((errno != 0) ? errno : (x))

static void problem(const char *s, int err)
{
(void) fprintf(stderr,"%s (%d)\n",s,err);
(void) fflush(stderr);
return;
} /* problem */

static int format_time(time_t s)
{
int cc = 0;
struct tm *t;
static int done = FALSE;

if (!done)
{
tzset();
done = TRUE;
}
t = localtime(&s);
if (t != NULL)
{
(void) printf("%4d%02d%2d %02d:%02d:%02d",(int) (t->tm_year + 1900),
(int) (t->tm_mon + 1), (int)t->tm_mday,
(int) t->tm_hour, (int) t->tm_min, (int) t->tm_sec);
if ((t->tm_isdst >= 0) && (t->tm_isdst <= 1))
{
(void) printf(" %s",tzname[t->tm_isdst]);
}
}
else cc = assign_errno(250);
return(cc);
} /* format_time */

int main(int argc, char *argv[])
{
int cc = 0;

if (argc > 1)
{
struct stat s;

cc = stat(argv[1],&s);
if (cc == 0)
{
(void) printf("%ld ",(unsigned long) s.st_mtime);
(void) format_time(s.st_mtime);
(void) printf("\n");
}
else
{
cc = assign_errno(254);
(void) fprintf(stderr,"Can't stat '%s'\n",argv[1]);
problem("Stat failed",cc);
}
}
else
{
cc = 255;
problem("Missing argument",cc);
}
return(cc);
} /* main */
--------------------------------------------

Oh, and to be nit-picky as it pertains to one of the responses you got, there is no creation time associated with a file. UNIX has no notion of creation time. The tm_ctime in struct tm refers not to creation time but rather change time which is one of UNIX's more confusing concepts is different from modification time. ctime refers to when metadata changes (owner, group, mode, length) and mtime refers to actual changes to the data itself. For example, an update (write) of an existing block of a file would alter mtime but leave ctime intact. However,
a write() at the end of file (ie an append) would modify both mtime and ctime because the length of the file changed and thus the metadata requires an update.
If it ain't broke, I can fix that.
James R. Ferguson
Acclaimed Contributor

Re: c script to get inode time_t info from file on hpux

Hi (again) Frank:

...and while we are on the subject of timestamps, the Perl solution I showed can easily be modified to sample either the 'atime' (last access time) or the 'ctime' (last inode change time). The 'stat()' structure given to Perl consists of thirteen elements. The timestamps are respectively:

atime => 8
mtime => 9
ctime => 10

Hence if you wanted the 'atime' of a file, do:

# perl -le '$t=(stat($ARGV[0]))[8];print "$t\n",scalar localtime($t)' file

Regards!

...JRF...


Frank de Vries
Respected Contributor

Re: c script to get inode time_t info from file on hpux

Thanks Clay :)
In sharing your wizardry and for
sticking up for me :).

In the meantime I followed Peter's link
to a fair bit of ingredients.
I did a wee bit of cooking and came up
with what I wanted.

However I will review your stuff in order
to improve my first version.

Thanks to all

/* get original inode stats of file */

#include
#include
#include
#include

main() {
struct stat info;
struct tm *tm;
struct tf *tf;
char s[32];

if (stat("getu", &info) != 0)
perror("stat() error");
else {
tm = localtime(&info.st_mtime);

strftime(s, sizeof(s), "%d/%m/%Y %H:%M%S", tm);
/*strftime(buf, sizeof(buf), "%d/%m/%Y", tm); */
puts("stat() returned the following information:");
printf(" time_t: %d\n", info.st_mtime); /* Last modification time */
printf(" time: %d\n", tm); /* Last modification time */
printf(" time_f: %s\n", s);
printf(" inode: %d\n", (int) info.st_ino);
printf(" dev id: %d\n", (int) info.st_dev);
printf(" mode: %08x\n", info.st_mode);
printf(" links: %d\n", info.st_nlink);
printf(" uid: %d\n", (int) info.st_uid);
printf(" gid: %d\n", (int) info.st_gid);
exit(0);
}
}

Fr..
Look before you leap
Frank de Vries
Respected Contributor

Re: c script to get inode time_t info from file on hpux

This is great :┬░))))
Look before you leap