[net.sources] Pathalias lookup and utilities for 4.2BSD.

stephen@dcl-cs.UUCP (Stephen J. Muir) (10/09/85)

Here are three utilities for those sites running the "pathalias" program to do
the following:
1) Look up the database and print out the path corresponding to any UUCP site.
2) Generate a database where the UUCP site is the key and the filename
   containing the description of that site is the data.
3) Look up the database built by program (2).

The programs are called "findpath", "findfile" and "mapfile" respectively.
I am sorry that I haven't had time to produce manual pages but I find these
programs essential for those sites who use "pathalias".
----------------------------------- Cut Here ----------------------------------
#!/bin/sh
echo 'Start of pack.out, part 01 of 01:'
echo 'x - Makefile'
sed 's/^X//' > Makefile << '/'
Xall:		mapfile findpath findfile
X
Xmapfile:	mapfile.c
X		cc -O mapfile.c -o mapfile -ldbm -s
X
Xfindpath:	findpath.c
X		cc -O findpath.c -o findpath -ldbm -s
X
Xfindfile:	findfile.c psystem.c
X		cc -O findfile.c psystem.c -o findfile -ldbm -s
/
echo 'x - findfile.c'
sed 's/^X//' > findfile.c << '/'
X/* Written by Stephen J. Muir, Computing Dept., Lancaster University */
X
X# include <stdio.h>
X# include <strings.h>
X
Xtypedef struct
X	{ char	*dptr;
X	  int	dsize;
X	}	datum;
X
Xextern datum	fetch ();
X
Xchar	*findpath = "exec /usr/local/bin/findpath -", buf [4096];
X
Xint	interactive, wpfd = -1, rpfd, newline;
X
XFILE	*frpfd;
X
Xdatum	key, value;
X
Xchar	*
Xgetline (prompt)
X	char	*prompt;
X	{ register char	c, *cp;
X	  register int	i;
X	  cp = &buf [0];
X	  if (interactive && prompt)
X	  { printf (prompt);
X	    fflush (stdout);
X	  }
X	  while ((i = getchar ()) != EOF)
X	  { if ((c = i & 0377) == '\n' || c == '\0')
X	    { newline = c == '\n';
X	      *cp = '\0';
X	      return (buf);
X	    }
X	    *cp++ = c;
X	  }
X	  return (0);
X	}
X
X/*ARGSUSED*/
Xmain (argc, argv, envp)
X	char	*argv [], *envp [];
X	{ register char	*cp;
X	  if (argc == 1)
X		++interactive;
X	  dbminit ("/usr/local/lib/pfile");
X	  while ((key.dptr = getline ("USENET site: ")) != 0)
X	  { key.dsize = strlen (key.dptr);
X	    value = fetch (key);
X	    if (value.dsize)
X	    { if (interactive)
X		printf ("%s\n", value.dptr);
X	      else
X	      { if (newline)
X			*(value.dptr + value.dsize - 1) = '\n';
X		write (1, value.dptr, value.dsize);
X	      }
X	    }
X	    else
X	    { if (interactive)
X		printf ("site may be hidden ... ");
X	      if (wpfd == -1)
X	      { if (interactive)
X		{ printf ("calling up \"findpath\" ... ");
X		  fflush (stdout);
X		}
X		if (psystem (findpath, &wpfd, &rpfd, 0, 0) == -1)
X		{ perror (findpath + 5);
X		  exit (1);
X		}
X		frpfd = fdopen (rpfd, "r");
X	      }
X	      write (wpfd, key.dptr, key.dsize + 1);
X	      cp = &buf [0];
X	      while ((*cp++ = (fgetc (frpfd) & 0377)) != '\0');
X	      if (buf [0])
X	      { if (interactive)
X			printf ("returns: %s\n", buf);
X		do
X		{ if (cp = rindex (buf, '!'))
X		  { *cp++ = '\0';
X		    if (!strcmp (cp, "%s"))
X			continue;
X		    if (!strncmp (cp, "%s@", 3))
X			cp += 3;
X		  }
X		  else
X			cp = &buf [0];
X		  if (interactive)
X			printf ("looking up: %s\n", cp);
X		  key.dsize = strlen (key.dptr = cp);
X		  value = fetch (key);
X		  if (value.dsize)
X		  { if (interactive)
X			printf ("%s\n", value.dptr);
X		    else
X		    { if (newline)
X			*(value.dptr + value.dsize - 1) = '\n';
X		      write (1, value.dptr, value.dsize);
X		    }
X		    break;
X		  }
X		}
X		while (cp != &buf [0]);
X		continue;
X	      }
X	      fflush (stdout);
X	      if (interactive)
X		fprintf (stderr, "No such site\n");
X	      else
X		write (1, newline ? "\n" : "\0", 1);
X	    }
X	  }
X	  if (interactive)
X		putchar ('\n');
X	  exit (0);
X	}
/
echo 'x - findpath.c'
sed 's/^X//' > findpath.c << '/'
X/* Written by Stephen J. Muir, Computing Dept., Lancaster University */
X
X# include <stdio.h>
X# include <strings.h>
X
Xtypedef struct
X	{ char	*dptr;
X	  int	dsize;
X	}	datum;
X
Xextern datum	fetch ();
X
Xchar	buf [4096];
X
Xint	interactive, newline;
X
Xdatum	key, value;
X
Xchar	*
Xgetline (prompt)
X	char	*prompt;
X	{ register char	c, *cp;
X	  register int	i;
X	  cp = &buf [0];
X	  if (interactive && prompt)
X	  { printf (prompt);
X	    fflush (stdout);
X	  }
X	  while ((i = getchar ()) != EOF)
X	  { if ((c = i & 0377) == '\n' || c == '\0')
X	    { newline = c == '\n';
X	      *cp = '\0';
X	      return (buf);
X	    }
X	    *cp++ = c;
X	  }
X	  return (0);
X	}
X
X/*ARGSUSED*/
Xmain (argc, argv, envp)
X	char	*argv [], *envp [];
X	{ if (argc == 1)
X		++interactive;
X	  dbminit ("/usr/local/lib/palias");
X	  while ((key.dptr = getline ("USENET site: ")) != 0)
X	  { key.dsize = strlen (key.dptr) + 1;
X	    value = fetch (key);
X	    if (value.dsize)
X	    { if (interactive)
X		printf ("%s\n", value.dptr);
X	      else
X	      { if (newline)
X			*(value.dptr + value.dsize - 1) = '\n';
X		write (1, value.dptr, value.dsize);
X	      }
X	    }
X	    else
X	    { if (interactive)
X		fprintf (stderr, "No such site\n");
X	      else
X		write (1, newline ? "\n" : "\0", 1);
X	    }
X	  }
X	  if (interactive)
X		putchar ('\n');
X	  exit (0);
X	}
/
echo 'x - mapfile.c'
sed 's/^X//' > mapfile.c << '/'
X/* Written by Stephen J. Muir, Computing Dept., Lancaster University */
X
X# include <sys/types.h>
X# include <sys/dir.h>
X# include <sys/file.h>
X# include <stdio.h>
X# include <strings.h>
X
Xtypedef struct
X	{ char	*dptr;
X	  int	dsize;
X	}	datum;
X
Xchar	*filename, *mapdir = "/usr/spool/uucpmaps",
X	*database = "/usr/local/lib/pfile", buf [4096];
X
Xstruct direct	*curfile;
X
Xdatum	key, content;
X
XDIR	*mapdirp;
X
XFILE	*ffd;
X
X/*ARGSUSED*/
Xmain (argc, argv, envp)
X	char	*argv [], *envp [];
X	{ register char	*cp;
X	  register int	fd;
X	  if (chdir (mapdir) == -1 || (mapdirp = opendir (".")) == 0)
X	  { perror (mapdir);
X	    exit (1);
X	  }
X	  strcpy (buf, database);
X	  strcat (buf, ".pag");
X	  if ((fd = open (buf, O_CREAT|O_TRUNC, 0666)) == -1)
X	  { perror (buf);
X	    exit (1);
X	  }
X	  close (fd);
X	  strcpy (buf, database);
X	  strcat (buf, ".dir");
X	  if ((fd = open (buf, O_CREAT|O_TRUNC, 0666)) == -1)
X	  { perror (buf);
X	    exit (1);
X	  }
X	  dbminit (database);
X	  while (curfile = readdir (mapdirp))
X	  { filename = curfile->d_name;
X	    if (*filename == '.' || index (filename, '.') == 0)
X		continue;
X	    if ((ffd = fopen (filename, "r")) == 0)
X	    { perror (filename);
X	      continue;
X	    }
X	    while (fgets (buf, sizeof (buf), ffd) != NULL)
X	    { if (strncmp (buf, "#N", 2))
X		continue;
X	      for (cp = &buf [2]; *cp != '\n' && *cp != '\0'; ++cp);
X	      while (*cp == '\n' || *cp == '\0' || *cp == ' ' || *cp == '\t')
X		*cp-- = '\0';
X	      for (cp = &buf [2]; *cp == ' ' || *cp == '\t'; ++cp);
X	      key.dptr = cp;
X	      key.dsize = strlen (cp);
X	      content.dptr = filename;
X	      content.dsize = strlen (filename) + 1;
X	      if (store (key,content) == -1)
X	      { perror ("store()");
X		exit (1);
X	      }
X	    }
X	    fclose (ffd);
X	  }
X	  exit (0);
X	}
/
echo 'x - psystem.c'
sed 's/^X//' > psystem.c << '/'
X# include <signal.h>
X
Xint	psyspid;
X
Xpsystem (com, fd0, fd1, fd2, child)
X	char	*com;
X	int	*fd0, *fd1, *fd2;
X	void	(*child) ();
X	{ int	p0 [2], p1 [2], p2 [2];
X	  if (fd0 && pipe (p0) == -1)
X		return (-1);
X	  if (fd1 && pipe (p1) == -1)
X	  { if (fd0)
X	    { close (p0 [0]);
X	      close (p0 [1]);
X	    }
X	    return (-1);
X	  }
X	  if (fd2 && pipe (p2) == -1)
X	  { if (fd0)
X	    { close (p0 [0]);
X	      close (p0 [1]);
X	    }
X	    if (fd1)
X	    { close (p1 [0]);
X	      close (p1 [1]);
X	    }
X	    return (-1);
X	  }
X	  while ((psyspid = fork ()) == -1);
X	  if (psyspid == 0)
X	  { if (fd0)
X	    { close (p0 [1]);
X	      dup2 (p0 [0], 0);
X	      close (p0 [0]);
X	    }
X	    if (fd1)
X	    { close (p1 [0]);
X	      dup2 (p1 [1], 1);
X	      close (p1 [1]);
X	    }
X	    if (fd2)
X	    { close (p2 [0]);
X	      dup2 (p2 [1], 2);
X	      close (p2 [1]);
X	    }
X	    signal (SIGQUIT, SIG_IGN);
X	    if (child)
X		(*child) ();
X	    execl ("/bin/sh", "sh", "-c", com, 0);
X	    _exit (127);
X	  }
X	  if (fd0)
X	  { close (p0 [0]);
X	    *fd0 = p0 [1];
X	  }
X	  if (fd1)
X	  { close (p1 [1]);
X	    *fd1 = p1 [0];
X	  }
X	  if (fd2)
X	  { close (p2 [1]);
X	    *fd2 = p2 [0];
X	  }
X	  return (0);
X	}
/
echo 'Part 01 of pack.out complete.'
exit
-- 
UUCP:	...!seismo!mcvax!ukc!dcl-cs!stephen
DARPA:	stephen%lancs.comp@ucl-cs	| Post: University of Lancaster,
JANET:	stephen@uk.ac.lancs.comp	|	Department of Computing,
Phone:	+44 524 65201 Ext. 4599		|	Bailrigg, Lancaster, UK.
Project:Alvey ECLIPSE Distribution	|	LA1 4YR