home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume14 / pcomm / part04 / list_dir.c < prev    next >
Encoding:
C/C++ Source or Header  |  1988-05-17  |  1.4 KB  |  78 lines

  1. /*
  2.  * Do a shell escape with an 'ls' command
  3.  */
  4.  
  5. #include <stdio.h>
  6. #include <curses.h>
  7. #include "misc.h"
  8.  
  9. void
  10. list_dir(fd)
  11. int fd;
  12. {
  13.     WINDOW *ls_win, *newwin();
  14.     FILE *pfp, *native_popen();
  15.     int lines, oops;
  16.     char *ans, *cwd, *getcwd(), buf[200], *get_str();
  17.  
  18.     ls_win = newwin(6, 70, 8, 5);
  19.  
  20.     cwd = getcwd(buf, 200);
  21.  
  22.     mvwprintw(ls_win, 2, 4, "Current directory: %s", cwd);
  23.     mvwaddstr(ls_win, 3, 4, "File spec (wildcards allowed): ");
  24.     box(ls_win, '|', '-');
  25.  
  26.     mvwattrstr(ls_win, 0, 3, A_BOLD, " List Directory ");
  27.     wmove(ls_win, 3, 35);
  28.     wrefresh(ls_win);
  29.  
  30.     if ((ans = get_str(ls_win, 60, NULL, NULL)) == NULL) {
  31.         if (fd == -1) {
  32.             werase(ls_win);
  33.             wrefresh(ls_win);
  34.         }
  35.         delwin(ls_win);
  36.         return;
  37.     }
  38.                     /* popen() an ls */
  39.     sprintf(buf, "ls -aC %s", ans);
  40.     pfp = native_popen(buf, "r");
  41.                     /* make a bigger window */
  42.     werase(ls_win);
  43.     wrefresh(ls_win);
  44.     delwin(ls_win);
  45.     ls_win = newwin(LINES-1, COLS, 0, 0);
  46.  
  47.     oops = 0;
  48.     lines = 0;
  49.     while (fgets(buf, BUFSIZ, pfp) != NULL) {
  50.         waddstr(ls_win, buf);
  51.         lines++;
  52.         if (lines == LINES-2) {
  53.             lines = 0;
  54.             mvwaddstr(ls_win, LINES-2, 28, "Press any key for more");
  55.             wrefresh(ls_win);
  56.             if (wgetch(ls_win) == 27) {
  57.                 oops++;
  58.                 break;
  59.             }
  60.             werase(ls_win);
  61.             wrefresh(ls_win);
  62.         }
  63.     }
  64.     native_pclose(pfp);
  65.  
  66.     if (!oops) {
  67.         mvwaddstr(ls_win, LINES-2, 25, "Press any key to continue");
  68.         wrefresh(ls_win);
  69.         wgetch(ls_win);
  70.     }
  71.     if (fd == -1) {
  72.         werase(ls_win);
  73.         wrefresh(ls_win);
  74.     }
  75.     delwin(ls_win);
  76.     return;
  77. }
  78.