Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place! |
Notices |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
|
12-09-2009, 12:32 AM
|
#1
|
LQ Newbie
Registered: Dec 2009
Posts: 2
Rep:
|
Multiple Column output for LS
Hello,
I'm sure this is against the rules in some fashion, and its probably not the best way to start my posting life on this forum, but I am at my wits end here and figured I would give this a shot.
I am trying to write a program that will mirror the standard output of LS on a linux machine. Meaning:
Multiple columns
Column widths are dynamic
Alphabetical order is not important for what I am trying to do
My problem is this:
I have parsed all the names of the members of the directory into an array.
When I created the array, I kept track of the longest record I put into it. Then, I divided the width of the terminal window by the longest record and got the worst case number of columns.
The tricky part is that before I start printing the items to the screen, I have to know how many columns I need. The width of each column is dependent on its longest member. That being the case, I can't figure out a programmatic way to check this status.
I know it sounds like I'm just fishing for answers here, but I have been at this for quite a few days now and I figure at this point that I am just overlooking something. If someone could, in the least, point me in the right direction. Google has yeilded little help and small hints are better than nothing.
Thanks, and sorry in advance for being one of "those guys"
|
|
|
12-09-2009, 01:14 AM
|
#2
|
LQ Guru
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,419
|
Not sure what you mean about status, but in general, you find the worst case item (which you say you've done), then you assume all entries are that long and print accordingly in fixed-width/padded output.
You don't say which lang you are using and it'd be easier to show us your code, but the cmd in eg bash is http://linux.die.net/man/1/printf, base on the C fn of the same name.
http://www.tldp.org/LDP/abs/html/internal.html#EX47
Last edited by chrism01; 12-09-2009 at 06:24 PM.
|
|
|
12-09-2009, 01:42 AM
|
#3
|
LQ Newbie
Registered: Dec 2009
Posts: 2
Original Poster
Rep:
|
Sorry for not stating the language, its C, compiling using the GCC compiler on my machine.
If some of the stuff I am saying doesn't quite come through clearly, I am a little tired so I apologize for that.
Are you saying that I should just make all the columns the width of the worst case?
I am using the printf ("%-*s", max_column_width) statement right now, but don't yet have the information I need to feed to it.
When I run ls on this box, the column widths differ based on the longest item in that column, which is what I was having trouble with. Does ls behave that way on all flavors of linux?
My current idea was going to be to just run this whole thing in a loop. I would start with the worst case number of columns and each time the program realized that it could use more columns for the output, it would re-execute. Not the most efficient, but it would get the job done.
Once the items are all organized nicely in arrays, I can print them to the screen.
EDIT: I would post my code, but there are so many (wrong) things going on in it from my experimentations that it wouldn't be much help.
Last edited by Scratch59; 12-09-2009 at 01:43 AM.
|
|
|
12-09-2009, 02:41 AM
|
#4
|
LQ 5k Club
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
|
Quote:
Originally Posted by Scratch59
My current idea was going to be to just run this whole thing in a loop. I would start with the worst case number of columns and each time the program realized that it could use more columns for the output, it would re-execute. Not the most efficient, but it would get the job done.
|
I can't think of any other way of doing it to get the ls optimal column widths because how else to know which column any particular file will be listed in after the first line? You could examine the source code for ideas. Here is OpenSolaris ls source code (a mere 3,167 lines! )
|
|
|
12-09-2009, 06:28 PM
|
#5
|
LQ Guru
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,419
|
Well, my soln is the simple one. If you really wanted to get that complex, you could divide the file list into groups; each group as a separate column and use the longest file in each group as the col length, but it's more complex.
You'd have to ensure all the longest names can fit in one line...
You certainly can get the src code for all Linux cmds, so you could use that...
|
|
|
All times are GMT -5. The time now is 04:21 AM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|