Re: file 'modified' dates [NEWBIE?]

From: Mysidia (jmhess@i-55.com)
Date: 08/28/02


On Wed, Aug 28, 2002 at 01:12:30PM -0700, Shane P. Lee wrote:
> Sorry about the "newbie?" tag, but this may very well be a
> newbie post. I'm not very familiar with the capabilities of
> C, so I'm not sure when a coding question is newbie or not.

This isn't a C question, this is a linux question.

ANSI C doesn't provide a function to do this: the functions i'm
going to describe are available on a system that has POSIX
or BSD4.3 functions available.

To get information on a file, you need to use the stat ( ) function,
see the manual page for 'stat', alternatively you could use fstat
to stat an open file.

(Note: these examples are untested)

example:

#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <time.h>
#include <stdio.h>

int main() {
   struct stat st;

   if ( stat("/etc/passwd", &st) == -1 ) {
       puts("ERROR"); exit(0);
   }
   printf("Created: ");
   printf("%s", ctime(&st.st_ctime));
   printf("Modified: ");
   printf("%s", ctime(&st.st_mtime));
}

Use lstat() instead of stat() if you want symbolic links to be
statted rather than what they point to.

Now getting a list of files is a little more complicated, you
literally need to open the directory using opendir(), and
get the entries via readdir().

Also, the only way to tell whether an item you've read is another
directory, a file, or a symbolic link is to stat it via lstat().

see the opendir, readdir, closedir    manual pages

example:

#include <sys/types.h>
#include <dirent.h>
#include <unistd.h>
#include <sys/stat.h>
#include <stdio.h>

int main() { // Print all listings
   struct dirent* de;
   DIR * p = opendir("/etc");
   struct stat st;
   char buf[NAME_MAX];
   if (!p) { puts("ERROR"); abort(); }

   while( de = readdir(p) ) {
      sprintf(buf, "/etc/%.*s", NAME_MAX-5, de->d_name);
      if (lstat(buf, &st) == -1)  {
          printf("Unknown %s\n", buf /*de->d_name*/);
          continue;
      }

      printf("%10s  %s\n", S_ISREG(st.st_mode) ?"File" :
                       S_ISDIR(st.st_mode) ? "Dir" :
                       S_ISSOCK(st.st_mode) ? "Socket" :
                       S_ISLNK(st.st_mode) ? "Link" :
                       S_ISFIFO(st.st_mode) ? "Fifo" :
                       S_ISCHR(st.st_mode) ? "Char dev" :
                       S_ISBLK(st.st_mode) ? "blk dev" :
                      "Something" , buf);
   }

   closedir(p);
}

-Mysid
> I know that "ls -al" in bash (or shell?!?) sends you the directory
> listing with the last-modified dates, but I don't want all the
> extra information this provides, just the date and name of the file.
> I don't need actual code, just a basic idea of how to ask Linux.
> I'm also hoping to avoid using pipes and child-procs if at all

--
   +---------------------------------------------------------------+
   | FAQ: http://qsilver.queensu.ca/~fletchra/Circle/list-faq.html |
   | Archives: http://post.queensu.ca/listserv/wwwarch/circle.html |
   | Newbie List:  http://groups.yahoo.com/group/circle-newbies/   |
   +---------------------------------------------------------------+



This archive was generated by hypermail 2b30 : 06/25/03 PDT