BSD 4_3_Net_2 release
[unix-history] / usr / src / usr.bin / pascal / libpc / GETNAME.c
index 7b6af55..22de6ce 100644 (file)
@@ -1,9 +1,42 @@
-/* Copyright (c) 1979 Regents of the University of California */
+/*-
+ * Copyright (c) 1979 The Regents of the University of California.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ * 3. All advertising materials mentioning features or use of this software
+ *    must display the following acknowledgement:
+ *     This product includes software developed by the University of
+ *     California, Berkeley and its contributors.
+ * 4. Neither the name of the University nor the names of its contributors
+ *    may be used to endorse or promote products derived from this software
+ *    without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
 
 
-static char sccsid[] = "@(#)GETNAME.c 1.2 %G%";
+#ifndef lint
+static char sccsid[] = "@(#)GETNAME.c  1.13 (Berkeley) 4/9/90";
+#endif /* not lint */
 
 #include "h00vars.h"
 
 #include "h00vars.h"
-#include "h01errs.h"
+#include "libpc.h"
 
 /*
  * GETNAME - activate a file
 
 /*
  * GETNAME - activate a file
@@ -16,23 +49,32 @@ static char sccsid[] = "@(#)GETNAME.c 1.2 %G%";
  * names are blank trimmed.
  */
 
  * names are blank trimmed.
  */
 
+static char *tmpname = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
+
 struct iorec *
 struct iorec *
-GETNAME(filep, name, maxnamlen, datasize)
+GETNAME(filep, name, namlim, datasize)
 
        register struct iorec   *filep;
        char                    *name;
 
        register struct iorec   *filep;
        char                    *name;
-       int                     maxnamlen;
-       int                     datasize;
+       long                    namlim;
+       long                    datasize;
 {
 {
+       int             maxnamlen = namlim;
        struct iorec    *prev;
        struct iorec    *next;
        register int    cnt;
        struct iorec    locvar;
        struct iorec    *prev;
        struct iorec    *next;
        register int    cnt;
        struct iorec    locvar;
-       extern char     *mktemp();
 
 
-       if (filep->fblk >= MAXFILES || _actfile[filep->fblk] != filep) {
+       if (filep->fblk < MAXFILES && _actfile[filep->fblk] == filep) {
+               /* 
+                * Close and immediately reactivate the file.
+                */
+               PFCLOSE(filep, name != NULL);
+               _actfile[filep->fblk] = filep;
+               filep->funit &= (TEMP | FTEXT);
+       } else {
                /*
                /*
-                * initialize a new filerecord
+                * Initialize a new file record.
                 */
                filep->funit = 0;
                if (datasize == 0) {
                 */
                filep->funit = 0;
                if (datasize == 0) {
@@ -44,8 +86,9 @@ GETNAME(filep, name, maxnamlen, datasize)
                filep->lcount = 0;
                filep->llimit = 0x7fffffff;
                filep->fileptr = &filep->window[0];
                filep->lcount = 0;
                filep->llimit = 0x7fffffff;
                filep->fileptr = &filep->window[0];
+               *filep->fname = NULL;
                /*
                /*
-                * check to see if file is global, or allocated in
+                * Check to see if file is global, or allocated in
                 * the stack by checking its address against the
                 * address of one of our routine's local variables.
                 */
                 * the stack by checking its address against the
                 * address of one of our routine's local variables.
                 */
@@ -53,14 +96,19 @@ GETNAME(filep, name, maxnamlen, datasize)
                        filep->flev = GLVL;
                else
                        filep->flev = filep;
                        filep->flev = GLVL;
                else
                        filep->flev = filep;
-               do {
-                       if (++_filefre == MAXFILES)
-                               _filefre = PREDEF + 1;
-               } while (_actfile[_filefre] != FILNIL);
+               for (_filefre++; _filefre < MAXFILES; _filefre++)
+                       if (_actfile[_filefre] == FILNIL)
+                               goto gotone;
+               for (_filefre = PREDEF + 1; _filefre < MAXFILES; _filefre++)
+                       if (_actfile[_filefre] == FILNIL)
+                               goto gotone;
+               ERROR("File table overflow\n");
+               return;
+gotone:
                filep->fblk = _filefre;
                _actfile[_filefre] = filep;
                /*
                filep->fblk = _filefre;
                _actfile[_filefre] = filep;
                /*
-                * link the newrecord into the file chain
+                * Link the new record into the file chain.
                 */
                prev = (struct iorec *)&_fchain;
                next = _fchain.fchain;
                 */
                prev = (struct iorec *)&_fchain;
                next = _fchain.fchain;
@@ -68,63 +116,52 @@ GETNAME(filep, name, maxnamlen, datasize)
                        prev = next;
                        next = next->fchain;
                }
                        prev = next;
                        next = next->fchain;
                }
-               filep->fchain = next;
-               prev->fchain = filep;
-       } else {
-               if (filep->funit & FDEF) {
-                       filep->funit &= (TEMP | FTEXT);
-               } else {
-                       /*
-                        * have a previous buffer, close associated file
-                        */
-                       fclose(filep->fbuf);
-                       if (ferror(filep->fbuf)) {
-                               ERROR(ECLOSE, filep->pfname);
-                               return;
-                       }
+               if (filep->flev == GLVL)
                        /*
                        /*
-                        * renamed temporary files are discarded
+                        * Must order global files so that all dynamic files
+                        * within a record are grouped together.
                         */
                         */
-                       if ((filep->funit & TEMP) &&
-                           (name != NULL) &&
-                           (unlink(filep->pfname))) {
-                               ERROR(EREMOVE, filep->pfname);
-                               return;
+                       while ((next != FILNIL) &&
+                              (next->flev == GLVL) &&
+                              ((struct iorec *)filep > next)) {
+                               prev = next;
+                               next = next->fchain;
                        }
                        }
-                       filep->funit &= (TEMP | FTEXT);
-               }
+               filep->fchain = next;
+               prev->fchain = filep;
        }
        /*
        }
        /*
-        * get the filename associated with the buffer
+        * Get the filename associated with the buffer.
         */
        if (name == NULL) {
                if (*filep->fname != NULL) {
                        return(filep);
                }
                /*
         */
        if (name == NULL) {
                if (*filep->fname != NULL) {
                        return(filep);
                }
                /*
-                * no name given and no previous name, so generate
-                * a new one of the form tmp.xxxxxx
+                * No name given and no previous name, so generate
+                * a new one of the form #tmp.xxxxxx.
                 */
                filep->funit |= TEMP;
                 */
                filep->funit |= TEMP;
-               name = mktemp("tmp.XXXXXX");
-               maxnamlen = 10;
-       } else {
-               /*
-                * trim trailing blanks, and insure that the name 
-                * will fit into the file structure
-                */
-               for (cnt = 0; cnt < maxnamlen; cnt++)
-                       if (name[cnt] == '\0' || name[cnt] == ' ')
-                               break;
-               if (cnt >= NAMSIZ) {
-                       ERROR(ENAMESIZE, name);
-                       return;
-               }
-               maxnamlen = cnt;
-               filep->funit &= ~TEMP;
+               sprintf(filep->fname, "#tmp.%c%d", tmpname[filep->fblk],
+                   getpid());
+               filep->pfname = &filep->fname[0];
+               return(filep);
+       }
+       /*
+        * Trim trailing blanks, and insure that the name 
+        * will fit into the file structure.
+        */
+       for (cnt = 0; cnt < maxnamlen; cnt++)
+               if (name[cnt] == '\0' || name[cnt] == ' ')
+                       break;
+       if (cnt >= NAMSIZ) {
+               ERROR("%s: File name too long\n", name);
+               return;
        }
        }
+       maxnamlen = cnt;
+       filep->funit &= ~TEMP;
        /*
        /*
-        * put the new name into the structure
+        * Put the new name into the structure.
         */
        for (cnt = 0; cnt < maxnamlen; cnt++)
                filep->fname[cnt] = name[cnt];
         */
        for (cnt = 0; cnt < maxnamlen; cnt++)
                filep->fname[cnt] = name[cnt];