1 /* $OpenBSD: unexpand.c,v 1.12 2015/11/11 02:52:46 deraadt Exp $ */ 2 /* $NetBSD: unexpand.c,v 1.5 1994/12/24 17:08:05 cgd Exp $ */ 3 4 /*- 5 * Copyright (c) 1980, 1993 6 * The Regents of the University of California. All rights reserved. 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. Neither the name of the University nor the names of its contributors 17 * may be used to endorse or promote products derived from this software 18 * without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30 * SUCH DAMAGE. 31 */ 32 33 /* 34 * unexpand - put tabs into a file replacing blanks 35 */ 36 #include <stdio.h> 37 #include <stdlib.h> 38 #include <string.h> 39 #include <unistd.h> 40 41 char genbuf[BUFSIZ]; 42 char linebuf[BUFSIZ]; 43 int all; 44 45 void tabify(char); 46 47 int 48 main(int argc, char *argv[]) 49 { 50 char *cp; 51 52 if (pledge("stdio rpath", NULL) == -1) { 53 perror("pledge"); 54 exit(1); 55 } 56 57 argc--, argv++; 58 if (argc > 0 && argv[0][0] == '-') { 59 if (strcmp(argv[0], "-a") != 0) { 60 fprintf(stderr, "usage: unexpand [-a] [file ...]\n"); 61 exit(1); 62 } 63 all++; 64 argc--, argv++; 65 } 66 do { 67 if (argc > 0) { 68 if (freopen(argv[0], "r", stdin) == NULL) { 69 perror(argv[0]); 70 exit(1); 71 } 72 argc--, argv++; 73 } 74 while (fgets(genbuf, BUFSIZ, stdin) != NULL) { 75 for (cp = linebuf; *cp; cp++) 76 continue; 77 if (cp > linebuf) 78 cp[-1] = 0; 79 tabify(all); 80 printf("%s", linebuf); 81 } 82 } while (argc > 0); 83 exit(0); 84 } 85 86 void 87 tabify(char c) 88 { 89 char *cp, *dp; 90 int dcol; 91 int ocol; 92 size_t len; 93 94 ocol = 0; 95 dcol = 0; 96 cp = genbuf; 97 dp = linebuf; 98 len = sizeof linebuf; 99 100 for (;;) { 101 switch (*cp) { 102 103 case ' ': 104 dcol++; 105 break; 106 107 case '\t': 108 dcol += 8; 109 dcol &= ~07; 110 break; 111 112 default: 113 while (((ocol + 8) &~ 07) <= dcol) { 114 if (ocol + 1 == dcol) 115 break; 116 if (len > 1) { 117 *dp++ = '\t'; 118 len--; 119 } 120 ocol += 8; 121 ocol &= ~07; 122 } 123 while (ocol < dcol) { 124 if (len > 1) { 125 *dp++ = ' '; 126 len--; 127 } 128 ocol++; 129 } 130 if (*cp == 0 || c == 0) { 131 strlcpy(dp, cp, len); 132 return; 133 } 134 *dp++ = *cp; 135 len--; 136 ocol++; 137 dcol++; 138 } 139 cp++; 140 } 141 } 142