1: #include <stdio.h> 2: #include <signal.h> 3: 4: #define BIG 32767 5: #define LCASE 01 6: #define UCASE 02 7: #define SWAB 04 8: #define NERR 010 9: #define SYNC 020 10: int cflag; 11: int fflag; 12: int skip; 13: int seekn; 14: int count; 15: int files = 1; 16: char *string; 17: char *ifile; 18: char *ofile; 19: char *ibuf; 20: char *obuf; 21: char *sbrk(); 22: int ibs = 512; 23: int obs = 512; 24: int bs; 25: int cbs; 26: int ibc; 27: int obc; 28: int cbc; 29: int nifr; 30: int nipr; 31: int nofr; 32: int nopr; 33: int ntrunc; 34: int ibf; 35: int obf; 36: 37: /*! rtrim and rfill options added by PLW 2/11/80*/ 38: int rtrim; 39: int rfill; 40: 41: char *op; 42: int nspace; 43: char etoa[] = { 44: 0000,0001,0002,0003,0234,0011,0206,0177, 45: 0227,0215,0216,0013,0014,0015,0016,0017, 46: 0020,0021,0022,0023,0235,0205,0010,0207, 47: 0030,0031,0222,0217,0034,0035,0036,0037, 48: 0200,0201,0202,0203,0204,0012,0027,0033, 49: 0210,0211,0212,0213,0214,0005,0006,0007, 50: 0220,0221,0026,0223,0224,0225,0226,0004, 51: 0230,0231,0232,0233,0024,0025,0236,0032, 52: 0040,0240,0241,0242,0243,0244,0245,0246, 53: 0247,0250,0133,0056,0074,0050,0053,0041, 54: 0046,0251,0252,0253,0254,0255,0256,0257, 55: 0260,0261,0135,0044,0052,0051,0073,0136, 56: 0055,0057,0262,0263,0264,0265,0266,0267, 57: 0270,0271,0174,0054,0045,0137,0076,0077, 58: 0272,0273,0274,0275,0276,0277,0300,0301, 59: 0302,0140,0072,0043,0100,0047,0075,0042, 60: 0303,0141,0142,0143,0144,0145,0146,0147, 61: 0150,0151,0304,0305,0306,0307,0310,0311, 62: 0312,0152,0153,0154,0155,0156,0157,0160, 63: 0161,0162,0313,0314,0315,0316,0317,0320, 64: 0321,0176,0163,0164,0165,0166,0167,0170, 65: 0171,0172,0322,0323,0324,0325,0326,0327, 66: 0330,0331,0332,0333,0334,0335,0336,0337, 67: 0340,0341,0342,0343,0344,0345,0346,0347, 68: 0173,0101,0102,0103,0104,0105,0106,0107, 69: 0110,0111,0350,0351,0352,0353,0354,0355, 70: 0175,0112,0113,0114,0115,0116,0117,0120, 71: 0121,0122,0356,0357,0360,0361,0362,0363, 72: 0134,0237,0123,0124,0125,0126,0127,0130, 73: 0131,0132,0364,0365,0366,0367,0370,0371, 74: 0060,0061,0062,0063,0064,0065,0066,0067, 75: 0070,0071,0372,0373,0374,0375,0376,0377, 76: }; 77: char atoe[] = { 78: 0000,0001,0002,0003,0067,0055,0056,0057, 79: 0026,0005,0045,0013,0014,0015,0016,0017, 80: 0020,0021,0022,0023,0074,0075,0062,0046, 81: 0030,0031,0077,0047,0034,0035,0036,0037, 82: 0100,0117,0177,0173,0133,0154,0120,0175, 83: 0115,0135,0134,0116,0153,0140,0113,0141, 84: 0360,0361,0362,0363,0364,0365,0366,0367, 85: 0370,0371,0172,0136,0114,0176,0156,0157, 86: 0174,0301,0302,0303,0304,0305,0306,0307, 87: 0310,0311,0321,0322,0323,0324,0325,0326, 88: 0327,0330,0331,0342,0343,0344,0345,0346, 89: 0347,0350,0351,0112,0340,0132,0137,0155, 90: 0171,0201,0202,0203,0204,0205,0206,0207, 91: 0210,0211,0221,0222,0223,0224,0225,0226, 92: 0227,0230,0231,0242,0243,0244,0245,0246, 93: 0247,0250,0251,0300,0152,0320,0241,0007, 94: 0040,0041,0042,0043,0044,0025,0006,0027, 95: 0050,0051,0052,0053,0054,0011,0012,0033, 96: 0060,0061,0032,0063,0064,0065,0066,0010, 97: 0070,0071,0072,0073,0004,0024,0076,0341, 98: 0101,0102,0103,0104,0105,0106,0107,0110, 99: 0111,0121,0122,0123,0124,0125,0126,0127, 100: 0130,0131,0142,0143,0144,0145,0146,0147, 101: 0150,0151,0160,0161,0162,0163,0164,0165, 102: 0166,0167,0170,0200,0212,0213,0214,0215, 103: 0216,0217,0220,0232,0233,0234,0235,0236, 104: 0237,0240,0252,0253,0254,0255,0256,0257, 105: 0260,0261,0262,0263,0264,0265,0266,0267, 106: 0270,0271,0272,0273,0274,0275,0276,0277, 107: 0312,0313,0314,0315,0316,0317,0332,0333, 108: 0334,0335,0336,0337,0352,0353,0354,0355, 109: 0356,0357,0372,0373,0374,0375,0376,0377, 110: }; 111: char atoibm[] = 112: { 113: 0000,0001,0002,0003,0067,0055,0056,0057, 114: 0026,0005,0045,0013,0014,0015,0016,0017, 115: 0020,0021,0022,0023,0074,0075,0062,0046, 116: 0030,0031,0077,0047,0034,0035,0036,0037, 117: 0100,0132,0177,0173,0133,0154,0120,0175, 118: 0115,0135,0134,0116,0153,0140,0113,0141, 119: 0360,0361,0362,0363,0364,0365,0366,0367, 120: 0370,0371,0172,0136,0114,0176,0156,0157, 121: 0174,0301,0302,0303,0304,0305,0306,0307, 122: 0310,0311,0321,0322,0323,0324,0325,0326, 123: 0327,0330,0331,0342,0343,0344,0345,0346, 124: 0347,0350,0351,0255,0340,0275,0137,0155, 125: 0171,0201,0202,0203,0204,0205,0206,0207, 126: 0210,0211,0221,0222,0223,0224,0225,0226, 127: 0227,0230,0231,0242,0243,0244,0245,0246, 128: 0247,0250,0251,0300,0117,0320,0241,0007, 129: 0040,0041,0042,0043,0044,0025,0006,0027, 130: 0050,0051,0052,0053,0054,0011,0012,0033, 131: 0060,0061,0032,0063,0064,0065,0066,0010, 132: 0070,0071,0072,0073,0004,0024,0076,0341, 133: 0101,0102,0103,0104,0105,0106,0107,0110, 134: 0111,0121,0122,0123,0124,0125,0126,0127, 135: 0130,0131,0142,0143,0144,0145,0146,0147, 136: 0150,0151,0160,0161,0162,0163,0164,0165, 137: 0166,0167,0170,0200,0212,0213,0214,0215, 138: 0216,0217,0220,0232,0233,0234,0235,0236, 139: 0237,0240,0252,0253,0254,0255,0256,0257, 140: 0260,0261,0262,0263,0264,0265,0266,0267, 141: 0270,0271,0272,0273,0274,0275,0276,0277, 142: 0312,0313,0314,0315,0316,0317,0332,0333, 143: 0334,0335,0336,0337,0352,0353,0354,0355, 144: 0356,0357,0372,0373,0374,0375,0376,0377, 145: }; 146: 147: 148: main(argc, argv) 149: int argc; 150: char **argv; 151: { 152: int (*conv)(); 153: register char *ip; 154: register c; 155: int ebcdic(), ibm(), ascii(), null(), cnull(), term(); 156: int a; 157: 158: conv = null; 159: for(c=1; c<argc; c++) { 160: string = argv[c]; 161: if(match("ibs=")) { 162: ibs = number(BIG); 163: continue; 164: } 165: if(match("obs=")) { 166: obs = number(BIG); 167: continue; 168: } 169: if(match("cbs=")) { 170: cbs = number(BIG); 171: continue; 172: } 173: if (match("bs=")) { 174: bs = number(BIG); 175: continue; 176: } 177: if(match("if=")) { 178: ifile = string; 179: continue; 180: } 181: if(match("of=")) { 182: ofile = string; 183: continue; 184: } 185: if(match("skip=")) { 186: skip = number(BIG); 187: continue; 188: } 189: if(match("seek=")) { 190: seekn = number(BIG); 191: continue; 192: } 193: if(match("count=")) { 194: count = number(BIG); 195: continue; 196: } 197: if(match("files=")) { 198: files = number(BIG); 199: continue; 200: } 201: if(match("conv=")) { 202: cloop: 203: if(match(",")) 204: goto cloop; 205: if(*string == '\0') 206: continue; 207: if(match("ebcdic")) { 208: conv = ebcdic; 209: rfill=0; 210: goto cloop; 211: } 212: if(match("ibm")) { 213: conv = ibm; 214: goto cloop; 215: } 216: if(match("ascii")) { 217: conv = ascii; 218: rtrim=0; 219: goto cloop; 220: } 221: if(match("lcase")) { 222: cflag |= LCASE; 223: goto cloop; 224: } 225: if(match("ucase")) { 226: cflag |= UCASE; 227: goto cloop; 228: } 229: if(match("swab")) { 230: cflag |= SWAB; 231: goto cloop; 232: } 233: if(match("noerror")) { 234: cflag |= NERR; 235: goto cloop; 236: } 237: if(match("sync")) { 238: cflag |= SYNC; 239: goto cloop; 240: } 241: if(match("rtrim")) { 242: rtrim=1; 243: conv=ascii; 244: goto cloop; 245: } 246: if(match("rfill")) { 247: rfill=1; 248: conv=ebcdic; 249: goto cloop; 250: } 251: } 252: fprintf(stderr,"bad arg: %s\n", string); 253: exit(0); 254: } 255: if(conv == null && cflag&(LCASE|UCASE)) 256: conv = cnull; 257: if (ifile) 258: ibf = open(ifile, 0); 259: else 260: ibf = dup(0); 261: if(ibf < 0) { 262: fprintf(stderr,"cannot open: %s\n", ifile); 263: exit(0); 264: } 265: if (ofile) 266: obf = creat(ofile, 0666); 267: else 268: obf = dup(1); 269: if(obf < 0) { 270: fprintf(stderr,"cannot create: %s\n", ofile); 271: exit(0); 272: } 273: if (bs) { 274: ibs = obs = bs; 275: if (conv == null) 276: fflag++; 277: } 278: if(ibs == 0 || obs == 0) { 279: fprintf(stderr,"counts: cannot be zero\n"); 280: exit(0); 281: } 282: ibuf = sbrk(ibs); 283: if (fflag) 284: obuf = ibuf; 285: else 286: obuf = sbrk(obs); 287: sbrk(64); /* For good measure */ 288: if(ibuf == (char *)-1 || obuf == (char *)-1) { 289: fprintf(stderr, "not enough memory\n"); 290: exit(0); 291: } 292: ibc = 0; 293: obc = 0; 294: cbc = 0; 295: op = obuf; 296: 297: if (signal(SIGINT, SIG_IGN) != SIG_IGN) 298: signal(SIGINT, term); 299: while(skip) { 300: read(ibf, ibuf, ibs); 301: skip--; 302: } 303: while(seekn) { 304: lseek(obf, (long)obs, 1); 305: seekn--; 306: } 307: 308: loop: 309: if(ibc-- == 0) { 310: ibc = 0; 311: if(count==0 || nifr+nipr!=count) { 312: if(cflag&(NERR|SYNC)) 313: for(ip=ibuf+ibs; ip>ibuf;) 314: *--ip = 0; 315: ibc = read(ibf, ibuf, ibs); 316: } 317: if(ibc == -1) { 318: perror("read"); 319: if((cflag&NERR) == 0) { 320: flsh(); 321: term(); 322: } 323: ibc = 0; 324: for(c=0; c<ibs; c++) 325: if(ibuf[c] != 0) 326: ibc = c; 327: stats(); 328: } 329: if(ibc == 0 && --files<=0) { 330: flsh(); 331: term(); 332: } 333: if(ibc != ibs) { 334: nipr++; 335: if(cflag&SYNC) 336: ibc = ibs; 337: } else 338: nifr++; 339: ip = ibuf; 340: c = ibc >> 1; 341: if(cflag&SWAB && c) 342: do { 343: a = *ip++; 344: ip[-1] = *ip; 345: *ip++ = a; 346: } while(--c); 347: ip = ibuf; 348: if (fflag) { 349: obc = ibc; 350: flsh(); 351: ibc = 0; 352: } 353: goto loop; 354: } 355: c = 0; 356: c |= *ip++; 357: c &= 0377; 358: (*conv)(c); 359: goto loop; 360: } 361: 362: flsh() 363: { 364: register c; 365: 366: if(obc) { 367: if(obc == obs) 368: nofr++; else 369: nopr++; 370: c = write(obf, obuf, obc); 371: if(c != obc) { 372: perror("write"); 373: term(); 374: } 375: obc = 0; 376: } 377: } 378: 379: match(s) 380: char *s; 381: { 382: register char *cs; 383: 384: cs = string; 385: while(*cs++ == *s) 386: if(*s++ == '\0') 387: goto true; 388: if(*s != '\0') 389: return(0); 390: 391: true: 392: cs--; 393: string = cs; 394: return(1); 395: } 396: 397: number(big) 398: { 399: register char *cs; 400: long n; 401: 402: cs = string; 403: n = 0; 404: while(*cs >= '0' && *cs <= '9') 405: n = n*10 + *cs++ - '0'; 406: for(;;) 407: switch(*cs++) { 408: 409: case 'k': 410: n *= 1024; 411: continue; 412: 413: case 'w': 414: n *= sizeof(int); 415: continue; 416: 417: case 'b': 418: n *= 512; 419: continue; 420: 421: case '*': 422: case 'x': 423: string = cs; 424: n *= number(BIG); 425: 426: case '\0': 427: if (n>=big || n<0) { 428: fprintf(stderr, "dd: argument %D out of range\n", n); 429: exit(1); 430: } 431: return(n); 432: } 433: /* never gets here */ 434: } 435: 436: cnull(cc) 437: { 438: register c; 439: 440: c = cc; 441: if(cflag&UCASE && c>='a' && c<='z') 442: c += 'A'-'a'; 443: if(cflag&LCASE && c>='A' && c<='Z') 444: c += 'a'-'A'; 445: null(c); 446: } 447: 448: null(c) 449: { 450: 451: *op = c; 452: op++; 453: if(++obc >= obs) { 454: flsh(); 455: op = obuf; 456: } 457: } 458: 459: ascii(cc) 460: { 461: register c; 462: 463: c = rtrim ? cc : ( etoa[cc] & 0377); 464: if(cbs == 0) { 465: cnull(c); 466: return; 467: } 468: if(c == ' ') { 469: nspace++; 470: goto out; 471: } 472: while(nspace > 0) { 473: null(' '); 474: nspace--; 475: } 476: cnull(c); 477: 478: out: 479: if(++cbc >= cbs) { 480: null('\n'); 481: cbc = 0; 482: nspace = 0; 483: } 484: } 485: 486: ebcdic(cc) 487: { 488: register c; 489: 490: c = cc; 491: if(cflag&UCASE && c>='a' && c<='z') 492: c += 'A'-'a'; 493: if(cflag&LCASE && c>='A' && c<='Z') 494: c += 'a'-'A'; 495: if(rfill == 0) c = atoe[c] & 0377; 496: if(cbs == 0) { 497: null(c); 498: return; 499: } 500: if(cc == '\n') { 501: while(cbc < cbs) { 502: null(rfill ? ' ' : atoe[' ']); 503: cbc++; 504: } 505: cbc = 0; 506: return; 507: } 508: if(cbc == cbs) 509: ntrunc++; 510: cbc++; 511: if(cbc <= cbs) 512: null(c); 513: } 514: 515: ibm(cc) 516: { 517: register c; 518: 519: c = cc; 520: if(cflag&UCASE && c>='a' && c<='z') 521: c += 'A'-'a'; 522: if(cflag&LCASE && c>='A' && c<='Z') 523: c += 'a'-'A'; 524: c = atoibm[c] & 0377; 525: if(cbs == 0) { 526: null(c); 527: return; 528: } 529: if(cc == '\n') { 530: while(cbc < cbs) { 531: null(atoibm[' ']); 532: cbc++; 533: } 534: cbc = 0; 535: return; 536: } 537: if(cbc == cbs) 538: ntrunc++; 539: cbc++; 540: if(cbc <= cbs) 541: null(c); 542: } 543: 544: term() 545: { 546: 547: stats(); 548: exit(0); 549: } 550: 551: stats() 552: { 553: 554: fprintf(stderr,"%u+%u records in\n", nifr, nipr); 555: fprintf(stderr,"%u+%u records out\n", nofr, nopr); 556: if(ntrunc) 557: fprintf(stderr,"%u truncated records\n", ntrunc); 558: }