File:  [Coherent Logic Development] / freem / src / glocks.c
Revision 1.2: download - view: text, annotated - select for diffs
Sun Mar 9 15:20:18 2025 UTC (5 months, 2 weeks ago) by snw
Branches: MAIN
CVS tags: HEAD
Begin formatting overhaul and REUSE compliance

    1: /*
    2:  *                            *
    3:  *                           * *
    4:  *                          *   *
    5:  *                     ***************
    6:  *                      * *       * *
    7:  *                       *  MUMPS  *
    8:  *                      * *       * *
    9:  *                     ***************
   10:  *                          *   *
   11:  *                           * *
   12:  *                            *
   13:  *
   14:  *   glocks.c
   15:  *    display and clear M locks
   16:  *
   17:  *  
   18:  *   Author: Serena Willis <snw@coherent-logic.com>
   19:  *    Copyright (C) 1998 MUG Deutschland
   20:  *    Copyright (C) 2020 Coherent Logic Development LLC
   21:  *
   22:  *
   23:  *   This file is part of FreeM.
   24:  *
   25:  *   FreeM is free software: you can redistribute it and/or modify
   26:  *   it under the terms of the GNU Affero Public License as published by
   27:  *   the Free Software Foundation, either version 3 of the License, or
   28:  *   (at your option) any later version.
   29:  *
   30:  *   FreeM is distributed in the hope that it will be useful,
   31:  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
   32:  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   33:  *   GNU Affero Public License for more details.
   34:  *
   35:  *   You should have received a copy of the GNU Affero Public License
   36:  *   along with FreeM.  If not, see <https://www.gnu.org/licenses/>.
   37:  *
   38:  **/
   39: 
   40: #include <stdlib.h>
   41: #include <stddef.h>
   42: #include "mpsdef0.h"
   43: #include "errmsg.h"
   44: #include <signal.h>
   45: #include <setjmp.h>
   46: #include <fcntl.h>
   47: #include <unistd.h>
   48: #include <sys/types.h>
   49: #include <sys/wait.h>
   50: #include <string.h>
   51: 
   52: #ifndef FREEBSD
   53: #define EOF -1
   54: #endif
   55: 
   56: #include <stdio.h>
   57: #ifdef SYSFIVE
   58: #include <fcntl.h>
   59: #endif /* SYSFIVE */
   60: 
   61: /* needed if byte data are to be interpreted as unsigned integer */
   62: #define UNSIGN(A) ((A)&0377)
   63: void    unlock ();
   64: void    zname ();
   65: short int znamenumeric ();
   66: long int tell ();
   67: 
   68: #ifndef SYSFIVE
   69: #define FreeM_timezone -3600
   70: #else
   71: 
   72: #ifdef __CYGWIN__
   73: #define FreeM_timezone _timezone
   74: #else
   75: extern long FreeM_timezone;
   76: #endif /* __CYGWIN__ */
   77: 
   78: #endif /* SYSFIVE */
   79: 
   80: 
   81: 
   82: int
   83: main (argc, argv)
   84: int     argc;			/* arguments count     */
   85: char  **argv;			/* arguments string    */
   86: 
   87: {
   88:     static char locktab[80] = "/usr/tmp/locktab";	/* file with LOCKs */
   89:     static long rempid = 0L;		/* remove entry with rem PID */
   90:     short   ltab;			/* file descr. for locktab */
   91:     int     pid;
   92:     short   type;
   93:     char    ch;
   94:     char    line[300], varnam[300];
   95:     int     i, j, n;
   96: 
   97:     if (argc > 1) {
   98:         j = 0;
   99:         while (--argc > 0) {
  100:             j++;
  101:             if (argv[j][0] == '-') {
  102:                 if (rempid) {
  103:                     fprintf (stderr, "usage is: %s [-pid] [lockfile]\n", *argv);
  104:                     exit (0);
  105:                 }
  106:                 n = 0;
  107:                 rempid = 0L;
  108:                 while ((ch = argv[j][++n])) {
  109:                     if (ch < '0' || ch > '9') {
  110:                         fprintf (stderr, "usage is: %s [-pid] [lockfile]\n", *argv);
  111:                         exit (0);
  112:                     }
  113:                     rempid = rempid * 10 + ch - '0';
  114:                 }
  115:                 continue;
  116:             }
  117:             strcpy (locktab, *(argv + j));
  118:         }
  119:     }
  120:     if (rempid)
  121:         unlock (locktab, rempid);
  122:     while ((ltab = open (locktab, 0)) == -1) {
  123:         printf ("cannot open '%s'\n", locktab);
  124:         exit (0);
  125:     }
  126: 
  127:     lseek (ltab, 0L, 0);
  128:     for (;;)
  129:     {
  130:         read (ltab, line, 3);
  131:         pid = UNSIGN (line[0]) * 256 + UNSIGN (line[1]);
  132:         if (pid == 0)
  133:             break;
  134:         type = line[2];
  135:         i = 0;
  136:         do {
  137:             read (ltab, &ch, 1);
  138:             if (ch == EOF)
  139:                 goto done;
  140:             varnam[i++] = ch;
  141:         } while (ch != EOL);
  142:         zname (line, varnam);
  143:         printf ("%d\t%s %s\n", pid, type == 'D' ? "ZA" : "L ", line);
  144:     }
  145: 
  146: done:
  147:     close (ltab);
  148:     exit (0);
  149: }
  150: 
  151: void
  152: unlock (locktab, pid)			/* unLOCK all entries of pid */
  153: char   *locktab;		/* locktable */
  154: long    pid;			/* process ID */
  155: 
  156: {
  157: short   ltab;			/* file descr. for locktab */
  158:     int     cpid;
  159:     char    entry[256];
  160: long int r_pos;			/* position to read  */
  161: long int w_pos;			/* position to write */
  162:     int     i,
  163:     j;
  164: 
  165: /*      open locktab, quit if nothing to be done */
  166:     if ((ltab = open (locktab, 2)) == -1)
  167:         return;
  168: 
  169: /*      request exclusive access to locktab (read & write) */
  170:     locking (ltab, 1, 0L);
  171: 
  172: /*      free all of your own locks; we do it by copying the whole stuff */
  173: /*      within 'locktab' omitting the old entries under our PID         */
  174: j = 0;				/* count your old entries */
  175:     lseek (ltab, 0L, 0);
  176:     w_pos = 0L;
  177:     for (;;)
  178:     {
  179:         read (ltab, entry, 2);
  180:         cpid = UNSIGN (entry[0]) * 256 + UNSIGN (entry[1]);
  181:         if (cpid == 0) {
  182:             lseek (ltab, w_pos, 0);
  183:             write (ltab, entry, 2);
  184:             break;
  185:         }
  186:         i = 1;
  187:         do {
  188:             read (ltab, &entry[++i], 1);
  189:         } while (entry[i] != EOL);
  190:         i++;
  191:         if (cpid != pid) {
  192:             if (j) {
  193:                 r_pos = tell (ltab);
  194:                 lseek (ltab, w_pos, 0);
  195:                 write (ltab, entry, (unsigned) i);
  196:                 lseek (ltab, r_pos, 0);
  197:             }
  198:             w_pos += i;
  199:         } else
  200:         j++;
  201:     }
  202:     locking (ltab, 0, 0L);
  203:     close (ltab);
  204:     return;
  205: }					/* end lock() */

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>