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 10-18-2005, 07:59 PM   #1
babyboss
Member
 
Registered: Sep 2004
Posts: 124

Rep: Reputation: 15
help with programming a simple ls command in C


My school askes me to implement the ls command
I am having a problem to start.
Can anyone give me a pseudo code on the brief picture?
 
Old 10-18-2005, 08:11 PM   #2
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,789

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
"man opendir" would be a good start.
 
Old 10-19-2005, 03:27 PM   #3
babyboss
Member
 
Registered: Sep 2004
Posts: 124

Original Poster
Rep: Reputation: 15
do I need to know data structure to print out files or folders when my ls is invoked?
 
Old 10-19-2005, 03:51 PM   #4
lowpro2k3
Member
 
Registered: Oct 2003
Location: Canada
Distribution: Slackware
Posts: 340

Rep: Reputation: 30
Do you need to sort the data? If you're just listing all the files, think about it like this

Code:
while there is still entries in the directory
  print the entry
endwhile
Its that easy. Look into opendir(3) and readdir(3). If you need to sort the data it becomes a little trickier. For instance, entries in directories are not sorted by default, even though you see them in order when you run ls. So you need to think of a way to sort this. I would read the results into a data structure, sort it, then print the results. You dont know the number of entries, so you would have a tough time making an array of proper size. *hint* use a linked list, or dynamic array.

Code:
while there is still entries in the directory
  append entry to data structure
endwhile

sort data structure
print data structure
You'll to research at least:

opendir
readdir
stat (if you want to implement ls -l type functionality)

See you're man pages. Also a book on Linux/UNIX programming would help. Or online books. Theres no shortage of documentation available.
 
Old 10-19-2005, 05:01 PM   #5
perfect_circle
Senior Member
 
Registered: Oct 2004
Location: Athens, Greece
Distribution: Slackware, arch
Posts: 1,783

Rep: Reputation: 53
http://users.actcom.co.il/~choo/lupg...ing-files.html
 
  


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
simple shall programming question itz2000 Programming 1 11-05-2005 04:12 PM
Simple C programming Spooky Programming 5 10-22-2004 03:16 AM
A Simple Programming IDE for X. xarius Programming 8 02-25-2004 12:10 PM
Simple C Programming Question.. Bolt Programming 6 06-03-2003 12:05 PM
simple c programming problem abi_sh Programming 8 07-24-2002 08:40 PM

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

All times are GMT -5. The time now is 01:10 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