LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   c programming in unix (https://www.linuxquestions.org/questions/programming-9/c-programming-in-unix-172322/)

dilberim82 04-19-2004 10:50 PM

c programming in unix
 
Ok i have a homework that is supposed to do what ls -al command does. I have most of the program done except from the date. Here is my code so far:

Code:

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

main(int argc, char *argv[])
{
        DIR *dirp;
        struct dirent *dentry;
        struct stat buffer;
        int errors = 0;
        char *dirname = ".";
        const char* type;

        if (argc > 1)
                dirname = argv[1];

        //change to specified directory and open for reading
        if (chdir(dirname) == -1 || (dirp = opendir(".")) == (DIR *)NULL)
        {
                fprintf(stderr, "%s: cannot access requested directory\n",
                        argv[0]);
                exit(1);
        }

        printf("Name \tType \tSize \t Access Date\n");

        // iterate through directory contents
        while ((dentry = readdir(dirp)) != (struct dirent *)NULL)
        {
                /*  fetch inode information  */
                if (stat(dentry->d_name, &buffer) == (-1))
                {
                        fprintf(stderr, "%s: cannot access %s\n",
                                argv[0], dentry->d_name);
                        errors++;
                        continue;
                }
                if(S_ISDIR (buffer.st_mode))
                type = "<DIR>";
                else
                type = "    ";

                printf("%-25s\t%s\t%d\t%d\n", dentry->d_name, type,
                                                                          buffer.st_size, buffer.st_atime);
    }
        closedir(dirp);

        exit(errors);
}

The problem with this is it shows the time in seconds and i need to display the date. my teacher wants this printed out to screen:

FileName FileType Size Day Date
writer <DIR> 288 Mon Dec 17 10:20:32 2001

and this is my output:
mail <DIR> 512 1082356227
public_html <DIR> 512 1082356227


thanks in advance...

aluser 04-19-2004 11:05 PM

try "man ctime" :)

dilberim82 04-19-2004 11:13 PM

I've been a member for 3 years and posted 244 times and i still don't know how to check man pages... Shame on me! Sorry everyone and thanks aluser.

aluser 04-19-2004 11:16 PM

:D it's all good

"man -k time" and "man -k date" both give well over 100 lines on my system anyway -- I was lucky to have remembered that localtime() was related to your problem

The_Nerd 04-20-2004 05:59 PM

he gave you the command! Sheesh! Anyhow, look up time.h in Google

aluser 04-20-2004 07:00 PM

Quote:

he gave you the command! Sheesh! Anyhow, look up time.h in Google
er? reread the thread :)

The_Nerd 04-21-2004 04:18 PM

Quote:

Originally posted by aluser
er? reread the thread :)
What the??? :confused:

dilberim82 04-21-2004 04:32 PM

Guys dont make this a big deal...I got the help i needed so there is no need to make a fuss about it. I do search before i post though.


All times are GMT -5. The time now is 04:26 AM.