LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   list directory/file in 2 columns (printf with termcap)? (https://www.linuxquestions.org/questions/programming-9/list-directory-file-in-2-columns-printf-with-termcap-4175603497/)

Xeratul 04-09-2017 01:58 AM

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;
}


tronayne 04-09-2017 11:55 AM

Um, ls -c [dirname] or without dirname the current directory.

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

danielbmartin 04-09-2017 01:48 PM

Quote:

Originally Posted by Xeratul (Post 5694621)
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

Xeratul 04-10-2017 10:14 AM

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

NevemTeve 04-10-2017 01:36 PM

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

ondoho 04-10-2017 01:39 PM

bucks and ballerinas, you're all missing the point:
Xeratul is set on recreating standard Linux utilities with C.

tronayne 04-10-2017 02:14 PM

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

Xeratul 04-10-2017 05:00 PM

Quote:

Originally Posted by NevemTeve (Post 5695267)
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.

Xeratul 04-13-2017 12:32 PM

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;
}



All times are GMT -5. The time now is 12:15 PM.