home *** CD-ROM | disk | FTP | other *** search
/ minnie.tuhs.org / 2014.11.minnie.tuhs.org.tar / minnie.tuhs.org / UnixArchive / PDP-11 / Trees / V6 / usr / source / s1 / dd.c < prev    next >
C/C++ Source or Header  |  1975-05-14  |  8KB  |  459 lines

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