1998-08-26 00:59:36 +04:00
|
|
|
/* $NetBSD: tsort.c,v 1.13 1998/08/25 20:59:42 ross Exp $ */
|
1994-12-07 04:06:23 +03:00
|
|
|
|
1993-03-21 12:45:37 +03:00
|
|
|
/*
|
1994-12-07 04:06:23 +03:00
|
|
|
* Copyright (c) 1989, 1993, 1994
|
1994-02-04 10:02:09 +03:00
|
|
|
* The Regents of the University of California. All rights reserved.
|
1993-03-21 12:45:37 +03:00
|
|
|
*
|
|
|
|
* This code is derived from software contributed to Berkeley by
|
|
|
|
* Michael Rendell of Memorial University of Newfoundland.
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
1997-10-20 05:09:53 +04:00
|
|
|
#include <sys/cdefs.h>
|
1993-03-21 12:45:37 +03:00
|
|
|
#ifndef lint
|
1997-10-20 05:09:53 +04:00
|
|
|
__COPYRIGHT("@(#) Copyright (c) 1989, 1993, 1994\n\
|
|
|
|
The Regents of the University of California. All rights reserved.\n");
|
1993-03-21 12:45:37 +03:00
|
|
|
#endif /* not lint */
|
|
|
|
|
|
|
|
#ifndef lint
|
1994-12-07 04:06:23 +03:00
|
|
|
#if 0
|
1995-09-01 02:06:22 +04:00
|
|
|
static char sccsid[] = "@(#)tsort.c 8.3 (Berkeley) 5/4/95";
|
1994-12-07 04:06:23 +03:00
|
|
|
#endif
|
1998-08-26 00:59:36 +04:00
|
|
|
__RCSID("$NetBSD: tsort.c,v 1.13 1998/08/25 20:59:42 ross Exp $");
|
1993-03-21 12:45:37 +03:00
|
|
|
#endif /* not lint */
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
1994-12-07 04:06:23 +03:00
|
|
|
|
|
|
|
#include <ctype.h>
|
|
|
|
#include <db.h>
|
|
|
|
#include <err.h>
|
1993-03-21 12:45:37 +03:00
|
|
|
#include <errno.h>
|
1994-02-04 10:02:09 +03:00
|
|
|
#include <fcntl.h>
|
1993-03-21 12:45:37 +03:00
|
|
|
#include <stdio.h>
|
1994-02-04 10:02:09 +03:00
|
|
|
#include <stdlib.h>
|
1993-03-21 12:45:37 +03:00
|
|
|
#include <string.h>
|
1995-09-01 02:06:22 +04:00
|
|
|
#include <unistd.h>
|
1993-03-21 12:45:37 +03:00
|
|
|
|
|
|
|
/*
|
1994-02-04 10:02:09 +03:00
|
|
|
* Topological sort. Input is a list of pairs of strings separated by
|
1993-03-21 12:45:37 +03:00
|
|
|
* white space (spaces, tabs, and/or newlines); strings are written to
|
|
|
|
* standard output in sorted order, one per line.
|
|
|
|
*
|
|
|
|
* usage:
|
1994-02-04 10:02:09 +03:00
|
|
|
* tsort [-l] [inputfile]
|
1993-03-21 12:45:37 +03:00
|
|
|
* If no input file is specified, standard input is read.
|
|
|
|
*
|
|
|
|
* Should be compatable with AT&T tsort HOWEVER the output is not identical
|
|
|
|
* (i.e. for most graphs there is more than one sorted order, and this tsort
|
|
|
|
* usually generates a different one then the AT&T tsort). Also, cycle
|
|
|
|
* reporting seems to be more accurate in this version (the AT&T tsort
|
|
|
|
* sometimes says a node is in a cycle when it isn't).
|
|
|
|
*
|
|
|
|
* Michael Rendell, michael@stretch.cs.mun.ca - Feb 26, '90
|
|
|
|
*/
|
|
|
|
#define HASHSIZE 53 /* doesn't need to be big */
|
|
|
|
#define NF_MARK 0x1 /* marker for cycle detection */
|
|
|
|
#define NF_ACYCLIC 0x2 /* this node is cycle free */
|
1994-02-04 10:02:09 +03:00
|
|
|
#define NF_NODEST 0x4 /* Unreachable */
|
|
|
|
|
1993-03-21 12:45:37 +03:00
|
|
|
typedef struct node_str NODE;
|
|
|
|
|
|
|
|
struct node_str {
|
|
|
|
NODE **n_prevp; /* pointer to previous node's n_next */
|
|
|
|
NODE *n_next; /* next node in graph */
|
1994-02-04 10:02:09 +03:00
|
|
|
NODE **n_arcs; /* array of arcs to other nodes */
|
1993-03-21 12:45:37 +03:00
|
|
|
int n_narcs; /* number of arcs in n_arcs[] */
|
|
|
|
int n_arcsize; /* size of n_arcs[] array */
|
|
|
|
int n_refcnt; /* # of arcs pointing to this node */
|
|
|
|
int n_flags; /* NF_* */
|
1994-02-04 10:02:09 +03:00
|
|
|
char n_name[1]; /* name of this node */
|
1993-03-21 12:45:37 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
typedef struct _buf {
|
|
|
|
char *b_buf;
|
|
|
|
int b_bsize;
|
|
|
|
} BUF;
|
|
|
|
|
1994-02-04 10:02:09 +03:00
|
|
|
DB *db;
|
1994-12-07 04:06:23 +03:00
|
|
|
NODE *graph, **cycle_buf, **longest_cycle;
|
1996-01-17 23:37:49 +03:00
|
|
|
int debug, longest, quiet;
|
1994-02-04 10:02:09 +03:00
|
|
|
|
|
|
|
void add_arc __P((char *, char *));
|
1997-10-20 05:09:53 +04:00
|
|
|
void clear_cycle __P((void));
|
1994-02-04 10:02:09 +03:00
|
|
|
int find_cycle __P((NODE *, NODE *, int, int));
|
|
|
|
NODE *get_node __P((char *));
|
|
|
|
void *grow_buf __P((void *, int));
|
1997-10-20 05:09:53 +04:00
|
|
|
int main __P((int, char **));
|
1994-02-04 10:02:09 +03:00
|
|
|
void remove_node __P((NODE *));
|
|
|
|
void tsort __P((void));
|
|
|
|
void usage __P((void));
|
|
|
|
|
|
|
|
int
|
1993-03-21 12:45:37 +03:00
|
|
|
main(argc, argv)
|
|
|
|
int argc;
|
1994-02-04 10:02:09 +03:00
|
|
|
char *argv[];
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
1997-10-20 05:09:53 +04:00
|
|
|
BUF *b;
|
|
|
|
int c, n;
|
1993-03-21 12:45:37 +03:00
|
|
|
FILE *fp;
|
1994-02-04 10:02:09 +03:00
|
|
|
int bsize, ch, nused;
|
1993-03-21 12:45:37 +03:00
|
|
|
BUF bufs[2];
|
|
|
|
|
1997-10-20 05:09:53 +04:00
|
|
|
fp = NULL;
|
|
|
|
while ((ch = getopt(argc, argv, "dlq")) != -1)
|
1994-12-07 04:06:23 +03:00
|
|
|
switch (ch) {
|
1994-02-04 10:02:09 +03:00
|
|
|
case 'd':
|
1994-12-07 04:06:23 +03:00
|
|
|
debug = 1;
|
|
|
|
break;
|
1994-02-04 10:02:09 +03:00
|
|
|
case 'l':
|
1994-12-07 04:06:23 +03:00
|
|
|
longest = 1;
|
|
|
|
break;
|
1996-01-17 23:37:49 +03:00
|
|
|
case 'q':
|
|
|
|
quiet = 1;
|
|
|
|
break;
|
1994-02-04 10:02:09 +03:00
|
|
|
case '?':
|
|
|
|
default:
|
|
|
|
usage();
|
|
|
|
}
|
|
|
|
argc -= optind;
|
|
|
|
argv += optind;
|
|
|
|
|
1994-12-07 04:06:23 +03:00
|
|
|
switch (argc) {
|
1994-02-04 10:02:09 +03:00
|
|
|
case 0:
|
1993-03-21 12:45:37 +03:00
|
|
|
fp = stdin;
|
1994-02-04 10:02:09 +03:00
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
if ((fp = fopen(*argv, "r")) == NULL)
|
1994-12-07 04:06:23 +03:00
|
|
|
err(1, "%s", *argv);
|
1994-02-04 10:02:09 +03:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
usage();
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
for (b = bufs, n = 2; --n >= 0; b++)
|
1994-02-04 10:02:09 +03:00
|
|
|
b->b_buf = grow_buf(NULL, b->b_bsize = 1024);
|
1993-03-21 12:45:37 +03:00
|
|
|
|
|
|
|
/* parse input and build the graph */
|
|
|
|
for (n = 0, c = getc(fp);;) {
|
|
|
|
while (c != EOF && isspace(c))
|
|
|
|
c = getc(fp);
|
|
|
|
if (c == EOF)
|
|
|
|
break;
|
|
|
|
|
|
|
|
nused = 0;
|
|
|
|
b = &bufs[n];
|
|
|
|
bsize = b->b_bsize;
|
|
|
|
do {
|
|
|
|
b->b_buf[nused++] = c;
|
1994-02-04 10:02:09 +03:00
|
|
|
if (nused == bsize)
|
|
|
|
b->b_buf = grow_buf(b->b_buf, bsize *= 2);
|
1993-03-21 12:45:37 +03:00
|
|
|
c = getc(fp);
|
|
|
|
} while (c != EOF && !isspace(c));
|
|
|
|
|
|
|
|
b->b_buf[nused] = '\0';
|
|
|
|
b->b_bsize = bsize;
|
|
|
|
if (n)
|
|
|
|
add_arc(bufs[0].b_buf, bufs[1].b_buf);
|
|
|
|
n = !n;
|
|
|
|
}
|
|
|
|
(void)fclose(fp);
|
1994-02-04 10:02:09 +03:00
|
|
|
if (n)
|
1994-02-04 10:18:27 +03:00
|
|
|
errx(1, "odd data count");
|
1993-03-21 12:45:37 +03:00
|
|
|
|
|
|
|
/* do the sort */
|
|
|
|
tsort();
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* double the size of oldbuf and return a pointer to the new buffer. */
|
1994-02-04 10:02:09 +03:00
|
|
|
void *
|
1993-03-21 12:45:37 +03:00
|
|
|
grow_buf(bp, size)
|
1994-02-04 10:02:09 +03:00
|
|
|
void *bp;
|
1993-03-21 12:45:37 +03:00
|
|
|
int size;
|
|
|
|
{
|
1994-02-04 10:02:09 +03:00
|
|
|
if ((bp = realloc(bp, (u_int)size)) == NULL)
|
1997-10-20 05:09:53 +04:00
|
|
|
err(1, "realloc");
|
1994-02-04 10:02:09 +03:00
|
|
|
return (bp);
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* add an arc from node s1 to node s2 in the graph. If s1 or s2 are not in
|
|
|
|
* the graph, then add them.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
add_arc(s1, s2)
|
|
|
|
char *s1, *s2;
|
|
|
|
{
|
1997-10-20 05:09:53 +04:00
|
|
|
NODE *n1;
|
1993-03-21 12:45:37 +03:00
|
|
|
NODE *n2;
|
1993-11-17 15:05:46 +03:00
|
|
|
int bsize, i;
|
1993-03-21 12:45:37 +03:00
|
|
|
|
1994-02-04 10:02:09 +03:00
|
|
|
n1 = get_node(s1);
|
1993-03-21 12:45:37 +03:00
|
|
|
|
|
|
|
if (!strcmp(s1, s2))
|
|
|
|
return;
|
|
|
|
|
1994-02-04 10:02:09 +03:00
|
|
|
n2 = get_node(s2);
|
1993-03-21 12:45:37 +03:00
|
|
|
|
|
|
|
/*
|
1993-11-17 15:01:04 +03:00
|
|
|
* Check if this arc is already here.
|
|
|
|
*/
|
|
|
|
for (i = 0; i < n1->n_narcs; i++)
|
|
|
|
if (n1->n_arcs[i] == n2)
|
|
|
|
return;
|
|
|
|
/*
|
|
|
|
* Add it.
|
1993-03-21 12:45:37 +03:00
|
|
|
*/
|
|
|
|
if (n1->n_narcs == n1->n_arcsize) {
|
|
|
|
if (!n1->n_arcsize)
|
|
|
|
n1->n_arcsize = 10;
|
|
|
|
bsize = n1->n_arcsize * sizeof(*n1->n_arcs) * 2;
|
1994-02-04 10:02:09 +03:00
|
|
|
n1->n_arcs = grow_buf(n1->n_arcs, bsize);
|
1993-03-21 12:45:37 +03:00
|
|
|
n1->n_arcsize = bsize / sizeof(*n1->n_arcs);
|
|
|
|
}
|
|
|
|
n1->n_arcs[n1->n_narcs++] = n2;
|
|
|
|
++n2->n_refcnt;
|
|
|
|
}
|
|
|
|
|
1994-02-04 10:02:09 +03:00
|
|
|
/* Find a node in the graph (insert if not found) and return a pointer to it. */
|
1993-03-21 12:45:37 +03:00
|
|
|
NODE *
|
1994-02-04 10:02:09 +03:00
|
|
|
get_node(name)
|
1993-03-21 12:45:37 +03:00
|
|
|
char *name;
|
|
|
|
{
|
1994-02-04 10:02:09 +03:00
|
|
|
DBT data, key;
|
|
|
|
NODE *n;
|
|
|
|
|
|
|
|
if (db == NULL &&
|
|
|
|
(db = dbopen(NULL, O_RDWR, 0, DB_HASH, NULL)) == NULL)
|
1994-12-07 04:06:23 +03:00
|
|
|
err(1, "db: %s", name);
|
1994-02-04 10:02:09 +03:00
|
|
|
|
|
|
|
key.data = name;
|
|
|
|
key.size = strlen(name) + 1;
|
|
|
|
|
1994-12-07 04:06:23 +03:00
|
|
|
switch ((*db->get)(db, &key, &data, 0)) {
|
1994-02-04 10:02:09 +03:00
|
|
|
case 0:
|
1997-10-20 05:09:53 +04:00
|
|
|
memmove(&n, data.data, sizeof(n));
|
1994-02-04 10:02:09 +03:00
|
|
|
return (n);
|
|
|
|
case 1:
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
case -1:
|
1994-12-07 04:06:23 +03:00
|
|
|
err(1, "db: %s", name);
|
1994-02-04 10:02:09 +03:00
|
|
|
}
|
1993-03-21 12:45:37 +03:00
|
|
|
|
1994-02-04 10:02:09 +03:00
|
|
|
if ((n = malloc(sizeof(NODE) + key.size)) == NULL)
|
1997-10-20 05:09:53 +04:00
|
|
|
err(1, "malloc");
|
1993-03-21 12:45:37 +03:00
|
|
|
|
|
|
|
n->n_narcs = 0;
|
|
|
|
n->n_arcsize = 0;
|
1994-02-04 10:02:09 +03:00
|
|
|
n->n_arcs = NULL;
|
1993-03-21 12:45:37 +03:00
|
|
|
n->n_refcnt = 0;
|
|
|
|
n->n_flags = 0;
|
1997-10-20 05:09:53 +04:00
|
|
|
memmove(n->n_name, name, key.size);
|
1993-03-21 12:45:37 +03:00
|
|
|
|
1994-02-04 10:02:09 +03:00
|
|
|
/* Add to linked list. */
|
1994-12-07 04:06:23 +03:00
|
|
|
if ((n->n_next = graph) != NULL)
|
1993-03-21 12:45:37 +03:00
|
|
|
graph->n_prevp = &n->n_next;
|
|
|
|
n->n_prevp = &graph;
|
|
|
|
graph = n;
|
|
|
|
|
1994-02-04 10:02:09 +03:00
|
|
|
/* Add to hash table. */
|
|
|
|
data.data = &n;
|
|
|
|
data.size = sizeof(n);
|
|
|
|
if ((*db->put)(db, &key, &data, 0))
|
1994-12-07 04:06:23 +03:00
|
|
|
err(1, "db: %s", name);
|
1994-02-04 10:02:09 +03:00
|
|
|
return (n);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Clear the NODEST flag from all nodes.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
clear_cycle()
|
|
|
|
{
|
|
|
|
NODE *n;
|
|
|
|
|
1994-12-07 04:06:23 +03:00
|
|
|
for (n = graph; n != NULL; n = n->n_next)
|
1994-02-04 10:02:09 +03:00
|
|
|
n->n_flags &= ~NF_NODEST;
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/* do topological sort on graph */
|
|
|
|
void
|
|
|
|
tsort()
|
|
|
|
{
|
1997-10-20 05:09:53 +04:00
|
|
|
NODE *n, *next;
|
|
|
|
int cnt, i;
|
1993-03-21 12:45:37 +03:00
|
|
|
|
1994-12-07 04:06:23 +03:00
|
|
|
while (graph != NULL) {
|
1993-03-21 12:45:37 +03:00
|
|
|
/*
|
1994-02-04 10:02:09 +03:00
|
|
|
* Keep getting rid of simple cases until there are none left,
|
1993-03-21 12:45:37 +03:00
|
|
|
* if there are any nodes still in the graph, then there is
|
|
|
|
* a cycle in it.
|
|
|
|
*/
|
|
|
|
do {
|
1994-12-07 04:06:23 +03:00
|
|
|
for (cnt = 0, n = graph; n != NULL; n = next) {
|
1993-03-21 12:45:37 +03:00
|
|
|
next = n->n_next;
|
|
|
|
if (n->n_refcnt == 0) {
|
|
|
|
remove_node(n);
|
|
|
|
++cnt;
|
|
|
|
}
|
|
|
|
}
|
1994-12-07 04:06:23 +03:00
|
|
|
} while (graph != NULL && cnt);
|
1993-03-21 12:45:37 +03:00
|
|
|
|
1994-12-07 04:06:23 +03:00
|
|
|
if (graph == NULL)
|
1993-03-21 12:45:37 +03:00
|
|
|
break;
|
|
|
|
|
|
|
|
if (!cycle_buf) {
|
|
|
|
/*
|
1994-02-04 10:02:09 +03:00
|
|
|
* Allocate space for two cycle logs - one to be used
|
1993-03-21 12:45:37 +03:00
|
|
|
* as scratch space, the other to save the longest
|
|
|
|
* cycle.
|
|
|
|
*/
|
1994-12-07 04:06:23 +03:00
|
|
|
for (cnt = 0, n = graph; n != NULL; n = n->n_next)
|
1993-03-21 12:45:37 +03:00
|
|
|
++cnt;
|
1994-02-04 10:02:09 +03:00
|
|
|
cycle_buf = malloc((u_int)sizeof(NODE *) * cnt);
|
|
|
|
longest_cycle = malloc((u_int)sizeof(NODE *) * cnt);
|
|
|
|
if (cycle_buf == NULL || longest_cycle == NULL)
|
1997-10-20 05:09:53 +04:00
|
|
|
err(1, "malloc");
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
1998-08-26 00:59:36 +04:00
|
|
|
for (n = graph; n != NULL; n = n->n_next) {
|
|
|
|
if (!(n->n_flags & NF_ACYCLIC)) {
|
1997-10-20 05:09:53 +04:00
|
|
|
if ((cnt = find_cycle(n, n, 0, 0)) != 0) {
|
1996-01-17 23:37:49 +03:00
|
|
|
if (!quiet) {
|
|
|
|
warnx("cycle in data");
|
|
|
|
for (i = 0; i < cnt; i++)
|
|
|
|
warnx("%s",
|
|
|
|
longest_cycle[i]->n_name);
|
|
|
|
}
|
1993-03-21 12:45:37 +03:00
|
|
|
remove_node(n);
|
1994-02-04 10:02:09 +03:00
|
|
|
clear_cycle();
|
1993-03-21 12:45:37 +03:00
|
|
|
break;
|
1994-02-04 10:02:09 +03:00
|
|
|
} else {
|
1993-03-21 12:45:37 +03:00
|
|
|
/* to avoid further checks */
|
1994-02-04 10:02:09 +03:00
|
|
|
n->n_flags |= NF_ACYCLIC;
|
|
|
|
clear_cycle();
|
|
|
|
}
|
1998-08-26 00:59:36 +04:00
|
|
|
}
|
|
|
|
}
|
1994-12-07 04:06:23 +03:00
|
|
|
if (n == NULL)
|
|
|
|
errx(1, "internal error -- could not find cycle");
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* print node and remove from graph (does not actually free node) */
|
|
|
|
void
|
|
|
|
remove_node(n)
|
1997-10-20 05:09:53 +04:00
|
|
|
NODE *n;
|
1993-03-21 12:45:37 +03:00
|
|
|
{
|
1997-10-20 05:09:53 +04:00
|
|
|
NODE **np;
|
|
|
|
int i;
|
1993-03-21 12:45:37 +03:00
|
|
|
|
|
|
|
(void)printf("%s\n", n->n_name);
|
|
|
|
for (np = n->n_arcs, i = n->n_narcs; --i >= 0; np++)
|
|
|
|
--(*np)->n_refcnt;
|
|
|
|
n->n_narcs = 0;
|
|
|
|
*n->n_prevp = n->n_next;
|
|
|
|
if (n->n_next)
|
|
|
|
n->n_next->n_prevp = n->n_prevp;
|
|
|
|
}
|
|
|
|
|
1994-02-04 10:02:09 +03:00
|
|
|
|
|
|
|
/* look for the longest? cycle from node from to node to. */
|
|
|
|
int
|
1993-03-21 12:45:37 +03:00
|
|
|
find_cycle(from, to, longest_len, depth)
|
|
|
|
NODE *from, *to;
|
|
|
|
int depth, longest_len;
|
|
|
|
{
|
1997-10-20 05:09:53 +04:00
|
|
|
NODE **np;
|
|
|
|
int i, len;
|
1993-03-21 12:45:37 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* avoid infinite loops and ignore portions of the graph known
|
|
|
|
* to be acyclic
|
|
|
|
*/
|
1994-02-04 10:02:09 +03:00
|
|
|
if (from->n_flags & (NF_NODEST|NF_MARK|NF_ACYCLIC))
|
|
|
|
return (0);
|
|
|
|
from->n_flags |= NF_MARK;
|
1993-03-21 12:45:37 +03:00
|
|
|
|
|
|
|
for (np = from->n_arcs, i = from->n_narcs; --i >= 0; np++) {
|
|
|
|
cycle_buf[depth] = *np;
|
|
|
|
if (*np == to) {
|
|
|
|
if (depth + 1 > longest_len) {
|
|
|
|
longest_len = depth + 1;
|
|
|
|
(void)memcpy((char *)longest_cycle,
|
|
|
|
(char *)cycle_buf,
|
|
|
|
longest_len * sizeof(NODE *));
|
|
|
|
}
|
|
|
|
} else {
|
1994-02-04 10:02:09 +03:00
|
|
|
if ((*np)->n_flags & (NF_MARK|NF_ACYCLIC|NF_NODEST))
|
|
|
|
continue;
|
1993-03-21 12:45:37 +03:00
|
|
|
len = find_cycle(*np, to, longest_len, depth + 1);
|
1994-02-04 10:02:09 +03:00
|
|
|
|
|
|
|
if (debug)
|
1994-12-07 04:06:23 +03:00
|
|
|
(void)printf("%*s %s->%s %d\n", depth, "",
|
|
|
|
from->n_name, to->n_name, len);
|
1994-02-04 10:02:09 +03:00
|
|
|
|
|
|
|
if (len == 0)
|
1994-12-07 04:06:23 +03:00
|
|
|
(*np)->n_flags |= NF_NODEST;
|
1994-02-04 10:02:09 +03:00
|
|
|
|
|
|
|
if (len > longest_len)
|
1993-03-21 12:45:37 +03:00
|
|
|
longest_len = len;
|
1994-02-04 10:02:09 +03:00
|
|
|
|
|
|
|
if (len > 0 && !longest)
|
1993-12-01 08:34:16 +03:00
|
|
|
break;
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
}
|
1994-02-04 10:02:09 +03:00
|
|
|
from->n_flags &= ~NF_MARK;
|
|
|
|
return (longest_len);
|
1993-03-21 12:45:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
1994-02-04 10:02:09 +03:00
|
|
|
usage()
|
|
|
|
{
|
1996-01-17 23:37:49 +03:00
|
|
|
(void)fprintf(stderr, "usage: tsort [-lq] [file]\n");
|
1994-02-04 10:02:09 +03:00
|
|
|
exit(1);
|
|
|
|
}
|