1 /* $OpenBSD: delete.c,v 1.8 2009/10/27 23:59:47 deraadt Exp $ */ 2 3 /*- 4 * Copyright (c) 1992, 1993, 1994 5 * The Regents of the University of California. All rights reserved. 6 * Copyright (c) 1992, 1993, 1994, 1995, 1996 7 * Keith Bostic. All rights reserved. 8 * 9 * See the LICENSE file for redistribution information. 10 */ 11 12 #include "config.h" 13 14 #include <sys/types.h> 15 #include <sys/queue.h> 16 17 #include <bitstring.h> 18 #include <errno.h> 19 #include <limits.h> 20 #include <stdio.h> 21 #include <stdlib.h> 22 #include <string.h> 23 24 #include "common.h" 25 26 /* 27 * del -- 28 * Delete a range of text. 29 * 30 * PUBLIC: int del(SCR *, MARK *, MARK *, int); 31 */ 32 int 33 del(sp, fm, tm, lmode) 34 SCR *sp; 35 MARK *fm, *tm; 36 int lmode; 37 { 38 recno_t lno; 39 size_t blen, len, nlen, tlen; 40 char *bp, *p; 41 int eof, rval; 42 43 bp = NULL; 44 45 /* Case 1 -- delete in line mode. */ 46 if (lmode) { 47 for (lno = tm->lno; lno >= fm->lno; --lno) { 48 if (db_delete(sp, lno)) 49 return (1); 50 ++sp->rptlines[L_DELETED]; 51 if (lno % INTERRUPT_CHECK == 0 && INTERRUPTED(sp)) 52 break; 53 } 54 goto done; 55 } 56 57 /* 58 * Case 2 -- delete to EOF. This is a special case because it's 59 * easier to pick it off than try and find it in the other cases. 60 */ 61 if (db_last(sp, &lno)) 62 return (1); 63 if (tm->lno >= lno) { 64 if (tm->lno == lno) { 65 if (db_get(sp, lno, DBG_FATAL, &p, &len)) 66 return (1); 67 eof = tm->cno != -1 && tm->cno >= len ? 1 : 0; 68 } else 69 eof = 1; 70 if (eof) { 71 for (lno = tm->lno; lno > fm->lno; --lno) { 72 if (db_delete(sp, lno)) 73 return (1); 74 ++sp->rptlines[L_DELETED]; 75 if (lno % 76 INTERRUPT_CHECK == 0 && INTERRUPTED(sp)) 77 break; 78 } 79 if (db_get(sp, fm->lno, DBG_FATAL, &p, &len)) 80 return (1); 81 GET_SPACE_RET(sp, bp, blen, fm->cno); 82 memcpy(bp, p, fm->cno); 83 if (db_set(sp, fm->lno, bp, fm->cno)) 84 return (1); 85 goto done; 86 } 87 } 88 89 /* Case 3 -- delete within a single line. */ 90 if (tm->lno == fm->lno) { 91 if (db_get(sp, fm->lno, DBG_FATAL, &p, &len)) 92 return (1); 93 GET_SPACE_RET(sp, bp, blen, len); 94 if (fm->cno != 0) 95 memcpy(bp, p, fm->cno); 96 memcpy(bp + fm->cno, p + (tm->cno + 1), len - (tm->cno + 1)); 97 if (db_set(sp, fm->lno, 98 bp, len - ((tm->cno - fm->cno) + 1))) 99 goto err; 100 goto done; 101 } 102 103 /* 104 * Case 4 -- delete over multiple lines. 105 * 106 * Copy the start partial line into place. 107 */ 108 if ((tlen = fm->cno) != 0) { 109 if (db_get(sp, fm->lno, DBG_FATAL, &p, NULL)) 110 return (1); 111 GET_SPACE_RET(sp, bp, blen, tlen + 256); 112 memcpy(bp, p, tlen); 113 } 114 115 /* Copy the end partial line into place. */ 116 if (db_get(sp, tm->lno, DBG_FATAL, &p, &len)) 117 goto err; 118 if (len != 0 && tm->cno != len - 1) { 119 /* 120 * XXX 121 * We can overflow memory here, if the total length is greater 122 * than SIZE_T_MAX. The only portable way I've found to test 123 * is depending on the overflow being less than the value. 124 */ 125 nlen = (len - (tm->cno + 1)) + tlen; 126 if (tlen > nlen) { 127 msgq(sp, M_ERR, "002|Line length overflow"); 128 goto err; 129 } 130 if (tlen == 0) { 131 GET_SPACE_RET(sp, bp, blen, nlen); 132 } else 133 ADD_SPACE_RET(sp, bp, blen, nlen); 134 135 memcpy(bp + tlen, p + (tm->cno + 1), len - (tm->cno + 1)); 136 tlen += len - (tm->cno + 1); 137 } 138 139 /* Set the current line. */ 140 if (db_set(sp, fm->lno, bp, tlen)) 141 goto err; 142 143 /* Delete the last and intermediate lines. */ 144 for (lno = tm->lno; lno > fm->lno; --lno) { 145 if (db_delete(sp, lno)) 146 goto err; 147 ++sp->rptlines[L_DELETED]; 148 if (lno % INTERRUPT_CHECK == 0 && INTERRUPTED(sp)) 149 break; 150 } 151 152 done: rval = 0; 153 if (0) 154 err: rval = 1; 155 if (bp != NULL) 156 FREE_SPACE(sp, bp, blen); 157 return (rval); 158 } 159