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

  1. /*
  2.  * Open a window to prompt for a new directory.  Checks to see if the
  3.  * directory exists, but doesn't move to there (yet).  Returns the path
  4.  * to the new directory, or NULL if you chickened out.
  5.  */
  6.  
  7. #include <curses.h>
  8. #include "misc.h"
  9.  
  10. char *
  11. change_dir(fd)
  12. int fd;
  13. {
  14.     WINDOW *ch_win, *newwin();
  15.     int got_it;
  16.     char *ans, *dir, *expand(), *cwd, *getcwd(), cwdbuf[200];
  17.     char *get_str();
  18.     void free_ptr();
  19.  
  20.     cwd = getcwd(cwdbuf, 200);
  21.  
  22.     ch_win = newwin(6, 70, 5, 5);
  23.  
  24.     mvwprintw(ch_win, 2, 4, "Current directory: %s", cwd);
  25.     mvwaddstr(ch_win, 3, 4, "New directory: ");
  26.     box(ch_win, '|', '-');
  27.  
  28.     mvwattrstr(ch_win, 0, 3, A_BOLD, " Change directory ");
  29.     wmove(ch_win, 3, 19);
  30.     wrefresh(ch_win);
  31.                     /* get the answer */
  32.     got_it = 0;
  33.     while ((ans = get_str(ch_win, 60, NULL, "     ")) != NULL) {
  34.         if (*ans == NULL)
  35.             break;
  36.                     /* expand the input */
  37.         dir = expand(ans);
  38.                     /* if it exists */
  39.         if (!access(dir, 1)) {
  40.             got_it++;
  41.             break;
  42.         }
  43.         beep();
  44.         mvwattrstr(ch_win, 4, 15, A_BOLD, "No such directory or no access permission");
  45.         wrefresh(ch_win);
  46.         wait_key(ch_win, 3);
  47.                     /* cleanup the mess */
  48.         clear_line(ch_win, 3, 19, 1);
  49.         clear_line(ch_win, 4, 14, 1);
  50.         wmove(ch_win, 3, 19);
  51.         wrefresh(ch_win);
  52.         free_ptr(dir);
  53.     }
  54.     if (fd == -1) {
  55.         werase(ch_win);
  56.         wrefresh(ch_win);
  57.     }
  58.     delwin(ch_win);
  59.     if (got_it)
  60.         return(dir);
  61.     return(NULL);
  62. }
  63.