- register int i;
- struct chan chans[NCHANS];
- struct schan schans[NPORTS];
-
- lseek(fc, (long)nl[SCHANS].n_value, 0);
- read(fc, chans, sizeof chans);
- lseek(fc, (long)nl[SSCHANS].n_value, 0);
- read(fc, schans, sizeof schans);
-
- printf("CHAN FLAGS INDEX LINE GROUP FILE TTYP CTLX PGRP OTTYP OLINE DATQ CTLY\n");
- for (i = 0; i < NCHANS; i++) {
- printf("%3d ", i);
- putf(chans[i].c_flags&INUSE, 'I');
- putf(chans[i].c_flags&SIOCTL, 'S');
- putf(chans[i].c_flags&XGRP, 'X');
- putf(chans[i].c_flags&YGRP, 'Y');
- putf(chans[i].c_flags&WCLOSE, 'W');
- putf(chans[i].c_flags&ISGRP, 'i');
- putf(chans[i].c_flags&BLOCK, 'B');
- putf(chans[i].c_flags&EOTMARK, 'E');
- putf(chans[i].c_flags&SIGBLK, 's');
- putf(chans[i].c_flags&BLKMSG, 'b');
- putf(chans[i].c_flags&ENAMSG, 'e');
- putf(chans[i].c_flags&WFLUSH, 'w');
- putf(chans[i].c_flags&NMBUF, 'N');
- putf(chans[i].c_flags&PORT, 'P');
- putf(chans[i].c_flags&ALT, 'A');
- putf(chans[i].c_flags&FBLOCK, 'F');
- printf("%8x ", chans[i].c_index);
- printf("%3d ", chans[i].c_line);
- printf("%8x ", chans[i].c_group);
- printf("%8x ", chans[i].c_fy);
- printf("%8x ", chans[i].c_ttyp);
- printf("%8x ", chans[i].c_ctlx);
- printf("%6d ", chans[i].c_pgrp);
- printf("%8x ", chans[i].c_ottyp);
- printf("%3d ", chans[i].c_oline);
- printf("%8x ", chans[i].cx.datq);
- printf("%8x\n", chans[i].c_ctly);
- }
-
- printf("\nCHAN FLAGS INDEX LINE GROUP FILE TTYP CTLX PGRP\n");
- for (i = 0; i < NPORTS; i++) {
- printf("%3d ", i);
- putf(schans[i].c_flags&INUSE, 'I');
- putf(schans[i].c_flags&SIOCTL, 'S');
- putf(schans[i].c_flags&XGRP, 'X');
- putf(schans[i].c_flags&YGRP, 'Y');
- putf(schans[i].c_flags&WCLOSE, 'W');
- putf(schans[i].c_flags&ISGRP, 'i');
- putf(schans[i].c_flags&BLOCK, 'B');
- putf(schans[i].c_flags&EOTMARK, 'E');
- putf(schans[i].c_flags&SIGBLK, 's');
- putf(schans[i].c_flags&BLKMSG, 'b');
- putf(schans[i].c_flags&ENAMSG, 'e');
- putf(schans[i].c_flags&WFLUSH, 'w');
- putf(schans[i].c_flags&NMBUF, 'N');
- putf(schans[i].c_flags&PORT, 'P');
- putf(schans[i].c_flags&ALT, 'A');
- putf(schans[i].c_flags&FBLOCK, 'F');
- printf("%8x ", schans[i].c_index);
- printf("%3d ", schans[i].c_line);
- printf("%8x ", schans[i].c_group);
- printf("%8x ", schans[i].c_fy);
- printf("%8x ", schans[i].c_ttyp);
- printf("%8x ", schans[i].c_ctlx);
- printf("%6d\n", schans[i].c_pgrp);
+ struct mapent *firstbp;
+ register struct mapent *bp;
+ register int t;
+
+ /*
+ * Both address and size must be
+ * positive, or the protocol has broken down.
+ */
+ if (addr <= 0 || size <= 0)
+ goto badrmfree;
+ /*
+ * Locate the piece of the map which starts after the
+ * returned space (or the end of the map).
+ */
+ firstbp = bp = (struct mapent *)(mp + 1);
+ for (; bp->m_addr <= addr && bp->m_size != 0; bp++)
+ continue;
+ /*
+ * If the piece on the left abuts us,
+ * then we should combine with it.
+ */
+ if (bp > firstbp && (bp-1)->m_addr+(bp-1)->m_size >= addr) {
+ /*
+ * Check no overlap (internal error).
+ */
+ if ((bp-1)->m_addr+(bp-1)->m_size > addr)
+ goto badrmfree;
+ /*
+ * Add into piece on the left by increasing its size.
+ */
+ (bp-1)->m_size += size;
+ /*
+ * If the combined piece abuts the piece on
+ * the right now, compress it in also,
+ * by shifting the remaining pieces of the map over.
+ */
+ if (bp->m_addr && addr+size >= bp->m_addr) {
+ if (addr+size > bp->m_addr)
+ goto badrmfree;
+ (bp-1)->m_size += bp->m_size;
+ while (bp->m_size) {
+ bp++;
+ (bp-1)->m_addr = bp->m_addr;
+ (bp-1)->m_size = bp->m_size;
+ }
+ }
+ goto done;
+ }
+ /*
+ * Don't abut on the left, check for abutting on
+ * the right.
+ */
+ if (addr+size >= bp->m_addr && bp->m_size) {
+ if (addr+size > bp->m_addr)
+ goto badrmfree;
+ bp->m_addr -= size;
+ bp->m_size += size;
+ goto done;