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