home *** CD-ROM | disk | FTP | other *** search
/ The Atari Compendium / The Atari Compendium (Toad Computers) (1994).iso / files / prgtools / gnustuff / tos / progut~1 / stdwin.zoo / gen / askfile.c next >
Encoding:
C/C++ Source or Header  |  1989-10-17  |  1.2 KB  |  61 lines

  1. /* STDWIN -- UNIVERSAL WASKFILE. */
  2.  
  3. #include "tools.h"
  4. #include "filedefs.h"
  5. #include "stdwin.h"
  6.  
  7. /* Ask for a file name; default is initial contents of buf.
  8.    Checks are made that the name typed is sensible:
  9.    if 'new' is TRUE, it should be writable or creatable,
  10.    and if it already exists confirmation is asked;
  11.    if 'new' is FALSE, it should exist. */
  12.  
  13. bool
  14. waskfile(prompt, buf, len, new)
  15.     char *prompt;
  16.     char *buf;
  17.     int len;
  18.     bool new;
  19. {
  20.     for (;;) {
  21.         if (!waskstr(prompt, buf, len) || buf[0] == EOS)
  22.             return FALSE;
  23.         if (new) {
  24.             if (access(buf, NOMODE) >= 0) {    /* Existing file */
  25.                 if (access(buf, WMODE) >= 0) {
  26.                     switch (waskync("Overwrite? [ny]", 0)) {
  27.                     case -1:    return FALSE;
  28.                     case 1:        return TRUE;
  29.                     }
  30.                     wmessage("Try again.");
  31.                 }
  32.                 else
  33.                     wmessage("No write permission.");
  34.             }
  35.             else {
  36.                 char *p= rindex(buf, SEP);
  37.                 if (p == NULL) {
  38.                     if (access(CURDIR, WMODE) >= 0)
  39.                         return TRUE;
  40.                 }
  41.                 else {
  42.                     *p= EOS;
  43.                     if (access(buf, WMODE) >= 0) {
  44.                         *p= SEP;
  45.                         return TRUE;
  46.                     }
  47.                 }
  48.                 wmessage("Can't create file.");
  49.             }
  50.         }
  51.         else {
  52.             if (access(buf, RMODE) >= 0)
  53.                 return TRUE;
  54.             wmessage("File not found.");
  55.         }
  56.         wfleep();
  57.         break;
  58.     }
  59.     return FALSE;
  60. }
  61.