简体   繁体   中英

ncurses and C- Display output of 'df' command in ncurses window

I am relatively new to ncurses and was just wondering what would be the simple way to display the output of a command executed in terminal/the command line in the ncurses TUI that I am starting. ie something like this psuedocode (which I know doesn't work, just to get the point accross :) The goal is to present a menu screen displaying various system information like available memory, network info, etc:

#include <ncurses.h>
#include <stdlib.h>
#include <stdio.h>


int main(){

initscr();
cbreak();
char command[] = "df";
printw(system(command));
}

You can do this by opening a pipe to the command (the example should use "df" , by the way). Something like this:

#include <ncurses.h>
#include <stdlib.h>
#include <stdio.h>

int
main(void)
{
    FILE *pp;

    initscr();
    cbreak();
    if ((pp = popen("df", "r")) != 0) {
        char buffer[BUFSIZ];
        while (fgets(buffer, sizeof(buffer), pp) != 0) {
            addstr(buffer);
        }
        pclose(pp);
    }
    getch();
    return EXIT_SUCCESS;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM