LinuxQuestions.org
Help answer threads with 0 replies.
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-09-2017, 01:58 AM   #1
Xeratul
Senior Member
 
Registered: Jun 2006
Location: UNIX
Distribution: FreeBSD
Posts: 2,656

Rep: Reputation: 255Reputation: 255Reputation: 255
list directory/file in 2 columns (printf with termcap)?


Hello,

I would like to list files/directory (ls) and printf on 2 columns. I have here a first code, I clear the screen with termcap, opendir and read the files, but how to see width and printf it?

Sorry for my very easy question. I am just beginning in programming.

Code:
#include <stdio.h> 
#include <stdlib.h> 
#include <termcap.h>
#define PAMAX 250 
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <dirent.h> 
#include <ctype.h>
#include <sys/stat.h>
#include <dirent.h>
#include <sys/types.h>
#include <unistd.h>  


void clear_screen()
{
       char buf[1024];
       char *str;
       tgetent(buf, getenv("TERM"));
       str = tgetstr( "cl" , NULL);
       fputs( str, stdout );
}

void bin_ls()
{
    DIR *dirp;
    struct dirent *dp;
    dirp = opendir( "." );
    while ((dp = readdir( dirp )) != NULL )
         printf( "%s\n", dp->d_name );
    closedir( dirp );
}

/////////////////
int main()  
{  
   clear_screen();
   bin_ls();
   return 0;
}
 
Old 04-09-2017, 11:55 AM   #2
tronayne
Senior Member
 
Registered: Oct 2003
Location: Northeastern Michigan, where Carhartt is a Designer Label
Distribution: Slackware 32- & 64-bit Stable
Posts: 3,541

Rep: Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065
Um, ls -c [dirname] or without dirname the current directory.

The brackets enclose optional arguments, don't use them to enclose actual arguments.

Last edited by tronayne; 04-09-2017 at 12:00 PM.
 
Old 04-09-2017, 01:48 PM   #3
danielbmartin
Senior Member
 
Registered: Apr 2010
Location: Apex, NC, USA
Distribution: Mint 17.3
Posts: 1,881

Rep: Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660Reputation: 660
Quote:
Originally Posted by Xeratul View Post
I would like to list files/directory (ls) and printf on 2 columns.
Could this be as simple as:
Code:
ls -c '/home/daniel/Desktop/Birthdays/'  \
|paste -sd" \n"  \
|column -t
Daniel B. Martin
 
Old 04-10-2017, 10:14 AM   #4
Xeratul
Senior Member
 
Registered: Jun 2006
Location: UNIX
Distribution: FreeBSD
Posts: 2,656

Original Poster
Rep: Reputation: 255Reputation: 255Reputation: 255
Hi Guys,

The point is C language using termcap.
https://en.wikipedia.org/wiki/C_%28p...ng_language%29

Thank you anyhow for your help and answers
 
Old 04-10-2017, 01:36 PM   #5
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,856
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
To get the width of screen (if you have a screen at all), you can use getenv("COLUMNS") and/or ioctl(TIOCGWINSIZ)

Last edited by NevemTeve; 04-10-2017 at 01:40 PM.
 
Old 04-10-2017, 01:39 PM   #6
ondoho
LQ Addict
 
Registered: Dec 2013
Posts: 19,872
Blog Entries: 12

Rep: Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053Reputation: 6053
bucks and ballerinas, you're all missing the point:
Xeratul is set on recreating standard Linux utilities with C.
 
Old 04-10-2017, 02:14 PM   #7
tronayne
Senior Member
 
Registered: Oct 2003
Location: Northeastern Michigan, where Carhartt is a Designer Label
Distribution: Slackware 32- & 64-bit Stable
Posts: 3,541

Rep: Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065Reputation: 1065
I think what you're trying to do is best done with ncurses (if you want to do it in C).

Or, what you have is two arrays of strings (or one big array that you manage with the size of your screen split in half).

Either way, you're going to want the size of the screen which you get with ncurse to figure out where to place things.

Note that you can do the two columns with a system call in C (that's the easy way):
Code:
system ls -c

Last edited by tronayne; 04-10-2017 at 02:16 PM.
 
Old 04-10-2017, 05:00 PM   #8
Xeratul
Senior Member
 
Registered: Jun 2006
Location: UNIX
Distribution: FreeBSD
Posts: 2,656

Original Poster
Rep: Reputation: 255Reputation: 255Reputation: 255
Quote:
Originally Posted by NevemTeve View Post
To get the width of screen (if you have a screen at all), you can use getenv("COLUMNS") and/or ioctl(TIOCGWINSIZ)
well, termcap was meant... ioctl is ok for windows. I will find out...

The reason is that I work on a terminal like screen.
 
Old 04-13-2017, 12:32 PM   #9
Xeratul
Senior Member
 
Registered: Jun 2006
Location: UNIX
Distribution: FreeBSD
Posts: 2,656

Original Poster
Rep: Reputation: 255Reputation: 255Reputation: 255
Here it is.

Code:
#include <stdio.h>
#include <stdlib.h>
#include <termcap.h>
#include <error.h>

static char termbuf[2048];

int main(void)
{
    char *termtype = getenv("TERM");

    if (tgetent(termbuf, termtype) < 0) {
        error(EXIT_FAILURE, 0, "Could not access the termcap data base.\n");
    }

    int lines = tgetnum("li");
    int columns = tgetnum("co");
    printf("lines = %d; columns = %d.\n", lines, columns);
    return 0;
}
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
merge columns from multiple files in a directory based on match of two columns prasanthi yanamala Linux - Newbie 2 11-12-2015 10:11 AM
Append two columns to file with printf. gctaylor1 Programming 3 07-02-2015 09:55 PM
[SOLVED] Converting a file with Rows and Columns to just Columns mphillips67 Linux - Newbie 14 03-05-2014 10:31 AM
printf: postion cursor to output columns schneidz Programming 5 09-20-2005 08:59 AM
List the contents of a directory to a file MasterC Linux - General 19 11-26-2002 10:09 AM

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

All times are GMT -5. The time now is 01:00 AM.

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