[BACK]Return to tsort.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / tsort

Annotation of src/usr.bin/tsort/tsort.c, Revision 1.4

1.4     ! millert     1: /*     $OpenBSD: tsort.c,v 1.3 1996/06/26 05:42:00 deraadt Exp $       */
1.2       deraadt     2: /*     $NetBSD: tsort.c,v 1.11 1996/01/17 20:37:53 mycroft Exp $       */
1.1       deraadt     3:
                      4: /*
                      5:  * Copyright (c) 1989, 1993, 1994
                      6:  *     The Regents of the University of California.  All rights reserved.
                      7:  *
                      8:  * This code is derived from software contributed to Berkeley by
                      9:  * Michael Rendell of Memorial University of Newfoundland.
                     10:  *
                     11:  * Redistribution and use in source and binary forms, with or without
                     12:  * modification, are permitted provided that the following conditions
                     13:  * are met:
                     14:  * 1. Redistributions of source code must retain the above copyright
                     15:  *    notice, this list of conditions and the following disclaimer.
                     16:  * 2. Redistributions in binary form must reproduce the above copyright
                     17:  *    notice, this list of conditions and the following disclaimer in the
                     18:  *    documentation and/or other materials provided with the distribution.
                     19:  * 3. All advertising materials mentioning features or use of this software
                     20:  *    must display the following acknowledgement:
                     21:  *     This product includes software developed by the University of
                     22:  *     California, Berkeley and its contributors.
                     23:  * 4. Neither the name of the University nor the names of its contributors
                     24:  *    may be used to endorse or promote products derived from this software
                     25:  *    without specific prior written permission.
                     26:  *
                     27:  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
                     28:  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
                     29:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
                     30:  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
                     31:  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
                     32:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
                     33:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                     34:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
                     35:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
                     36:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
                     37:  * SUCH DAMAGE.
                     38:  */
                     39:
                     40: #ifndef lint
                     41: static char copyright[] =
                     42: "@(#) Copyright (c) 1989, 1993, 1994\n\
                     43:        The Regents of the University of California.  All rights reserved.\n";
                     44: #endif /* not lint */
                     45:
                     46: #ifndef lint
                     47: #if 0
                     48: static char sccsid[] = "@(#)tsort.c    8.3 (Berkeley) 5/4/95";
                     49: #endif
1.4     ! millert    50: static char rcsid[] = "$OpenBSD: tsort.c,v 1.3 1996/06/26 05:42:00 deraadt Exp $";
1.1       deraadt    51: #endif /* not lint */
                     52:
                     53: #include <sys/types.h>
                     54:
                     55: #include <ctype.h>
                     56: #include <db.h>
                     57: #include <err.h>
                     58: #include <errno.h>
                     59: #include <fcntl.h>
                     60: #include <stdio.h>
                     61: #include <stdlib.h>
                     62: #include <string.h>
                     63: #include <unistd.h>
                     64:
                     65: /*
                     66:  *  Topological sort.  Input is a list of pairs of strings separated by
                     67:  *  white space (spaces, tabs, and/or newlines); strings are written to
                     68:  *  standard output in sorted order, one per line.
                     69:  *
                     70:  *  usage:
                     71:  *     tsort [-l] [inputfile]
                     72:  *  If no input file is specified, standard input is read.
                     73:  *
                     74:  *  Should be compatable with AT&T tsort HOWEVER the output is not identical
                     75:  *  (i.e. for most graphs there is more than one sorted order, and this tsort
                     76:  *  usually generates a different one then the AT&T tsort).  Also, cycle
                     77:  *  reporting seems to be more accurate in this version (the AT&T tsort
                     78:  *  sometimes says a node is in a cycle when it isn't).
                     79:  *
                     80:  *  Michael Rendell, michael@stretch.cs.mun.ca - Feb 26, '90
                     81:  */
                     82: #define        HASHSIZE        53              /* doesn't need to be big */
                     83: #define        NF_MARK         0x1             /* marker for cycle detection */
                     84: #define        NF_ACYCLIC      0x2             /* this node is cycle free */
                     85: #define        NF_NODEST       0x4             /* Unreachable */
                     86:
                     87: typedef struct node_str NODE;
                     88:
                     89: struct node_str {
                     90:        NODE **n_prevp;                 /* pointer to previous node's n_next */
                     91:        NODE *n_next;                   /* next node in graph */
                     92:        NODE **n_arcs;                  /* array of arcs to other nodes */
                     93:        int n_narcs;                    /* number of arcs in n_arcs[] */
                     94:        int n_arcsize;                  /* size of n_arcs[] array */
                     95:        int n_refcnt;                   /* # of arcs pointing to this node */
                     96:        int n_flags;                    /* NF_* */
                     97:        char n_name[1];                 /* name of this node */
                     98: };
                     99:
                    100: typedef struct _buf {
                    101:        char *b_buf;
                    102:        int b_bsize;
                    103: } BUF;
                    104:
                    105: DB *db;
                    106: NODE *graph, **cycle_buf, **longest_cycle;
1.2       deraadt   107: int debug, longest, quiet;
1.1       deraadt   108:
                    109: void    add_arc __P((char *, char *));
                    110: int     find_cycle __P((NODE *, NODE *, int, int));
                    111: NODE   *get_node __P((char *));
                    112: void   *grow_buf __P((void *, int));
                    113: void    remove_node __P((NODE *));
                    114: void    tsort __P((void));
                    115: void    usage __P((void));
                    116:
                    117: int
                    118: main(argc, argv)
                    119:        int argc;
                    120:        char *argv[];
                    121: {
                    122:        register BUF *b;
                    123:        register int c, n;
                    124:        FILE *fp;
                    125:        int bsize, ch, nused;
                    126:        BUF bufs[2];
                    127:
1.4     ! millert   128:        while ((ch = getopt(argc, argv, "dlq")) != -1)
1.1       deraadt   129:                switch (ch) {
                    130:                case 'd':
                    131:                        debug = 1;
                    132:                        break;
                    133:                case 'l':
                    134:                        longest = 1;
                    135:                        break;
1.2       deraadt   136:                case 'q':
                    137:                        quiet = 1;
                    138:                        break;
1.1       deraadt   139:                case '?':
                    140:                default:
                    141:                        usage();
                    142:                }
                    143:        argc -= optind;
                    144:        argv += optind;
                    145:
                    146:        switch (argc) {
                    147:        case 0:
                    148:                fp = stdin;
                    149:                break;
                    150:        case 1:
                    151:                if ((fp = fopen(*argv, "r")) == NULL)
                    152:                        err(1, "%s", *argv);
                    153:                break;
                    154:        default:
                    155:                usage();
                    156:        }
                    157:
                    158:        for (b = bufs, n = 2; --n >= 0; b++)
                    159:                b->b_buf = grow_buf(NULL, b->b_bsize = 1024);
                    160:
                    161:        /* parse input and build the graph */
                    162:        for (n = 0, c = getc(fp);;) {
                    163:                while (c != EOF && isspace(c))
                    164:                        c = getc(fp);
                    165:                if (c == EOF)
                    166:                        break;
                    167:
                    168:                nused = 0;
                    169:                b = &bufs[n];
                    170:                bsize = b->b_bsize;
                    171:                do {
                    172:                        b->b_buf[nused++] = c;
                    173:                        if (nused == bsize)
                    174:                                b->b_buf = grow_buf(b->b_buf, bsize *= 2);
                    175:                        c = getc(fp);
                    176:                } while (c != EOF && !isspace(c));
                    177:
                    178:                b->b_buf[nused] = '\0';
                    179:                b->b_bsize = bsize;
                    180:                if (n)
                    181:                        add_arc(bufs[0].b_buf, bufs[1].b_buf);
                    182:                n = !n;
                    183:        }
                    184:        (void)fclose(fp);
                    185:        if (n)
                    186:                errx(1, "odd data count");
                    187:
                    188:        /* do the sort */
                    189:        tsort();
                    190:        exit(0);
                    191: }
                    192:
                    193: /* double the size of oldbuf and return a pointer to the new buffer. */
                    194: void *
                    195: grow_buf(bp, size)
                    196:        void *bp;
                    197:        int size;
                    198: {
                    199:        if ((bp = realloc(bp, (u_int)size)) == NULL)
                    200:                err(1, NULL);
                    201:        return (bp);
                    202: }
                    203:
                    204: /*
                    205:  * add an arc from node s1 to node s2 in the graph.  If s1 or s2 are not in
                    206:  * the graph, then add them.
                    207:  */
                    208: void
                    209: add_arc(s1, s2)
                    210:        char *s1, *s2;
                    211: {
                    212:        register NODE *n1;
                    213:        NODE *n2;
                    214:        int bsize, i;
                    215:
                    216:        n1 = get_node(s1);
                    217:
                    218:        if (!strcmp(s1, s2))
                    219:                return;
                    220:
                    221:        n2 = get_node(s2);
                    222:
                    223:        /*
                    224:         * Check if this arc is already here.
                    225:         */
                    226:        for (i = 0; i < n1->n_narcs; i++)
                    227:                if (n1->n_arcs[i] == n2)
                    228:                        return;
                    229:        /*
                    230:         * Add it.
                    231:         */
                    232:        if (n1->n_narcs == n1->n_arcsize) {
                    233:                if (!n1->n_arcsize)
                    234:                        n1->n_arcsize = 10;
                    235:                bsize = n1->n_arcsize * sizeof(*n1->n_arcs) * 2;
                    236:                n1->n_arcs = grow_buf(n1->n_arcs, bsize);
                    237:                n1->n_arcsize = bsize / sizeof(*n1->n_arcs);
                    238:        }
                    239:        n1->n_arcs[n1->n_narcs++] = n2;
                    240:        ++n2->n_refcnt;
                    241: }
                    242:
                    243: /* Find a node in the graph (insert if not found) and return a pointer to it. */
                    244: NODE *
                    245: get_node(name)
                    246:        char *name;
                    247: {
                    248:        DBT data, key;
                    249:        NODE *n;
                    250:
                    251:        if (db == NULL &&
                    252:            (db = dbopen(NULL, O_RDWR, 0, DB_HASH, NULL)) == NULL)
                    253:                err(1, "db: %s", name);
                    254:
                    255:        key.data = name;
                    256:        key.size = strlen(name) + 1;
                    257:
                    258:        switch ((*db->get)(db, &key, &data, 0)) {
                    259:        case 0:
                    260:                bcopy(data.data, &n, sizeof(n));
                    261:                return (n);
                    262:        case 1:
                    263:                break;
                    264:        default:
                    265:        case -1:
                    266:                err(1, "db: %s", name);
                    267:        }
                    268:
                    269:        if ((n = malloc(sizeof(NODE) + key.size)) == NULL)
                    270:                err(1, NULL);
                    271:
                    272:        n->n_narcs = 0;
                    273:        n->n_arcsize = 0;
                    274:        n->n_arcs = NULL;
                    275:        n->n_refcnt = 0;
                    276:        n->n_flags = 0;
                    277:        bcopy(name, n->n_name, key.size);
                    278:
                    279:        /* Add to linked list. */
                    280:        if ((n->n_next = graph) != NULL)
                    281:                graph->n_prevp = &n->n_next;
                    282:        n->n_prevp = &graph;
                    283:        graph = n;
                    284:
                    285:        /* Add to hash table. */
                    286:        data.data = &n;
                    287:        data.size = sizeof(n);
                    288:        if ((*db->put)(db, &key, &data, 0))
                    289:                err(1, "db: %s", name);
                    290:        return (n);
                    291: }
                    292:
                    293:
                    294: /*
                    295:  * Clear the NODEST flag from all nodes.
                    296:  */
                    297: void
                    298: clear_cycle()
                    299: {
                    300:        NODE *n;
                    301:
                    302:        for (n = graph; n != NULL; n = n->n_next)
                    303:                n->n_flags &= ~NF_NODEST;
                    304: }
                    305:
                    306: /* do topological sort on graph */
                    307: void
                    308: tsort()
                    309: {
                    310:        register NODE *n, *next;
                    311:        register int cnt, i;
                    312:
                    313:        while (graph != NULL) {
                    314:                /*
                    315:                 * Keep getting rid of simple cases until there are none left,
                    316:                 * if there are any nodes still in the graph, then there is
                    317:                 * a cycle in it.
                    318:                 */
                    319:                do {
                    320:                        for (cnt = 0, n = graph; n != NULL; n = next) {
                    321:                                next = n->n_next;
                    322:                                if (n->n_refcnt == 0) {
                    323:                                        remove_node(n);
                    324:                                        ++cnt;
                    325:                                }
                    326:                        }
                    327:                } while (graph != NULL && cnt);
                    328:
                    329:                if (graph == NULL)
                    330:                        break;
                    331:
                    332:                if (!cycle_buf) {
                    333:                        /*
                    334:                         * Allocate space for two cycle logs - one to be used
                    335:                         * as scratch space, the other to save the longest
                    336:                         * cycle.
                    337:                         */
                    338:                        for (cnt = 0, n = graph; n != NULL; n = n->n_next)
                    339:                                ++cnt;
                    340:                        cycle_buf = malloc((u_int)sizeof(NODE *) * cnt);
                    341:                        longest_cycle = malloc((u_int)sizeof(NODE *) * cnt);
                    342:                        if (cycle_buf == NULL || longest_cycle == NULL)
                    343:                                err(1, NULL);
                    344:                }
                    345:                for (n = graph; n != NULL; n = n->n_next)
                    346:                        if (!(n->n_flags & NF_ACYCLIC))
                    347:                                if (cnt = find_cycle(n, n, 0, 0)) {
1.2       deraadt   348:                                        if (!quiet) {
                    349:                                                warnx("cycle in data");
                    350:                                                for (i = 0; i < cnt; i++)
                    351:                                                        warnx("%s",
                    352:                                                            longest_cycle[i]->n_name);
                    353:                                        }
1.1       deraadt   354:                                        remove_node(n);
                    355:                                        clear_cycle();
                    356:                                        break;
                    357:                                } else {
                    358:                                        /* to avoid further checks */
                    359:                                        n->n_flags  |= NF_ACYCLIC;
                    360:                                        clear_cycle();
                    361:                                }
                    362:
                    363:                if (n == NULL)
                    364:                        errx(1, "internal error -- could not find cycle");
                    365:        }
                    366: }
                    367:
                    368: /* print node and remove from graph (does not actually free node) */
                    369: void
                    370: remove_node(n)
                    371:        register NODE *n;
                    372: {
                    373:        register NODE **np;
                    374:        register int i;
                    375:
                    376:        (void)printf("%s\n", n->n_name);
                    377:        for (np = n->n_arcs, i = n->n_narcs; --i >= 0; np++)
                    378:                --(*np)->n_refcnt;
                    379:        n->n_narcs = 0;
                    380:        *n->n_prevp = n->n_next;
                    381:        if (n->n_next)
                    382:                n->n_next->n_prevp = n->n_prevp;
                    383: }
                    384:
                    385:
                    386: /* look for the longest? cycle from node from to node to. */
                    387: int
                    388: find_cycle(from, to, longest_len, depth)
                    389:        NODE *from, *to;
                    390:        int depth, longest_len;
                    391: {
                    392:        register NODE **np;
                    393:        register int i, len;
                    394:
                    395:        /*
                    396:         * avoid infinite loops and ignore portions of the graph known
                    397:         * to be acyclic
                    398:         */
                    399:        if (from->n_flags & (NF_NODEST|NF_MARK|NF_ACYCLIC))
                    400:                return (0);
                    401:        from->n_flags |= NF_MARK;
                    402:
                    403:        for (np = from->n_arcs, i = from->n_narcs; --i >= 0; np++) {
                    404:                cycle_buf[depth] = *np;
                    405:                if (*np == to) {
                    406:                        if (depth + 1 > longest_len) {
                    407:                                longest_len = depth + 1;
                    408:                                (void)memcpy((char *)longest_cycle,
                    409:                                    (char *)cycle_buf,
                    410:                                    longest_len * sizeof(NODE *));
                    411:                        }
                    412:                } else {
                    413:                        if ((*np)->n_flags & (NF_MARK|NF_ACYCLIC|NF_NODEST))
                    414:                                continue;
                    415:                        len = find_cycle(*np, to, longest_len, depth + 1);
                    416:
                    417:                        if (debug)
                    418:                                (void)printf("%*s %s->%s %d\n", depth, "",
                    419:                                    from->n_name, to->n_name, len);
                    420:
                    421:                        if (len == 0)
                    422:                                (*np)->n_flags |= NF_NODEST;
                    423:
                    424:                        if (len > longest_len)
                    425:                                longest_len = len;
                    426:
                    427:                        if (len > 0 && !longest)
                    428:                                break;
                    429:                }
                    430:        }
                    431:        from->n_flags &= ~NF_MARK;
                    432:        return (longest_len);
                    433: }
                    434:
                    435: void
                    436: usage()
                    437: {
1.2       deraadt   438:        (void)fprintf(stderr, "usage: tsort [-lq] [file]\n");
1.1       deraadt   439:        exit(1);
                    440: }