home *** CD-ROM | disk | FTP | other *** search
/ The Datafile PD-CD 3 / PDCD_3.iso / languages / pascal / PTC / _PtC1 / FOPEN < prev    next >
Encoding:
Text File  |  1991-03-04  |  1.7 KB  |  61 lines

  1. # include <stdio.h>
  2. # include <string.h>
  3. # include "ptcmain.h"
  4. # include "ptcerror.h"
  5.  
  6. static char rcsid[] = "$Id: c.FOPEN 1.3 91/03/09 20:57:15 gtoal Exp Locker: gtoal $";
  7.  
  8. FILE *
  9. F_open(n, l, m, lineno, fname)
  10.      char *n, *m;
  11.      int l, lineno;
  12.      char *fname;
  13. {
  14.                          /* fname is the internal filename as in the prog hdr */
  15.   FILE *f;
  16.   register char *s;
  17.   char tmp[MAXFILENAME+1];
  18. #ifndef __STDC__
  19.   extern int unlink();   /* remove = ANSI preferred to Posix */
  20. #define remove(f) unlink(f)
  21.   extern char *tempnam(); /* tmpnam = ANSI in <stdio.h> */
  22. #define tmpnam(s) tempnam(NULL, s)
  23. #endif
  24.   if (n == NULL) {
  25.     /*
  26.      *    Do an 'extended getopt' with fname as the keystring; if none
  27.      *    found, prompt from :TT using keystring as a prompt.  Ideally
  28.      *    would also use positional parameters but can't access them at
  29.      *    this stage.
  30.      */
  31.     static char line[256];
  32.     fprintf(stderr, "%s: ", fname); fflush(stderr);
  33.     gets(line); l = strlen(line);
  34.     if (line[l-1] == '\n') line[--l] = '\0';
  35.     if (line[0] != '\0') n = line;
  36.   }
  37.   if (n == NULL) {
  38.     if (*m == 'r') strcpy(tmp, "NULL:"); else strcpy(tmp, tmpnam("ptc"));
  39.   } else {
  40.     if (strcmp(n, "TTY:") == 0) n = ":TT";
  41.     if (l < 0)
  42.       l = strlen(n);
  43.     if (l > MAXFILENAME)
  44.       PTCerror(PTC_E_TOOLONGFILENAME, lineno, 0, 0, 0);
  45.     strncpy(tmp, n, l);
  46.     tmp[l] = '\0';
  47.     /* Scan for first blank or null */
  48.     for (s = tmp; *s != ' ' && *s != '\0'; )
  49.       s++;
  50.     *s = '\0';
  51.   }
  52.   s = tmp;
  53.   if ((f = fopen(s, m)) == NULL)
  54.     PTCerror(PTC_E_CANTOPEN, lineno, (int)s, (int)m, 0); /* Unsafe if ptr not same size as int */
  55.   if (n == NULL)
  56.     remove(tmp);
  57.   return (f);
  58. }
  59.  
  60.   
  61.