LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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


Reply
  Search this Thread
Old 04-19-2004, 10:50 PM   #1
dilberim82
Member
 
Registered: Apr 2001
Location: NY
Distribution: used to be Redhat, now Debian Sarge
Posts: 291

Rep: Reputation: 30
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...
 
Old 04-19-2004, 11:05 PM   #2
aluser
Member
 
Registered: Mar 2004
Location: Massachusetts
Distribution: Debian
Posts: 557

Rep: Reputation: 43
try "man ctime"
 
Old 04-19-2004, 11:13 PM   #3
dilberim82
Member
 
Registered: Apr 2001
Location: NY
Distribution: used to be Redhat, now Debian Sarge
Posts: 291

Original Poster
Rep: Reputation: 30
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.
 
Old 04-19-2004, 11:16 PM   #4
aluser
Member
 
Registered: Mar 2004
Location: Massachusetts
Distribution: Debian
Posts: 557

Rep: Reputation: 43
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
 
Old 04-20-2004, 05:59 PM   #5
The_Nerd
Member
 
Registered: Aug 2002
Distribution: Debian
Posts: 540

Rep: Reputation: 32
he gave you the command! Sheesh! Anyhow, look up time.h in Google
 
Old 04-20-2004, 07:00 PM   #6
aluser
Member
 
Registered: Mar 2004
Location: Massachusetts
Distribution: Debian
Posts: 557

Rep: Reputation: 43
Quote:
he gave you the command! Sheesh! Anyhow, look up time.h in Google
er? reread the thread
 
Old 04-21-2004, 04:18 PM   #7
The_Nerd
Member
 
Registered: Aug 2002
Distribution: Debian
Posts: 540

Rep: Reputation: 32
Quote:
Originally posted by aluser
er? reread the thread
What the???
 
Old 04-21-2004, 04:32 PM   #8
dilberim82
Member
 
Registered: Apr 2001
Location: NY
Distribution: used to be Redhat, now Debian Sarge
Posts: 291

Original Poster
Rep: Reputation: 30
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.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[c] unix systems programming in MS VS C++? saiz66 Programming 2 10-07-2004 08:16 AM
unix system programming mile1982 Programming 4 09-30-2004 10:06 PM
UNIX (Linux, BSD, etc) Programming :: UNIX kuphryn Programming 8 04-04-2004 11:50 PM
is there actually a market for Unix programming? ShawnD Programming 37 05-16-2003 04:16 PM
unix programming questions eboats General 7 10-18-2001 05:48 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:32 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration