--- bzip2.c	Fri Aug 29 23:32:15 1997
+++ bzip2.acorn	Fri Mar 13 15:25:45 1998
@@ -1,4 +1,3 @@
-
 /*-----------------------------------------------------------*/
 /*--- A block-sorting, lossless compressor        bzip2.c ---*/
 /*-----------------------------------------------------------*/
@@ -94,16 +93,20 @@
   Generic 32-bit Unix.
   Also works on 64-bit Unix boxes.
 --*/
-#define BZ_UNIX      1
+#define BZ_UNIX		0
 
 /*--
   Win32, as seen by Jacob Navia's excellent
   port of (Chris Fraser & David Hanson)'s excellent
   lcc compiler.
 --*/
-#define BZ_LCCWIN32  0
+#define BZ_LCCWIN32	0
 
+/*--
+  Acorn's RISC OS (currently requires unixlib & gcc)
+--*/
 
+#define BZ_RISCOS	1
 
 /*---------------------------------------------*/
 /*--
@@ -225,6 +228,44 @@
 
 #endif
 
+#if BZ_RISCOS
+   #define BZ_FILETYPE 0x16E
+
+   #include <sys/types.h>
+   #include <stdio.h>		/* required by tmpnam() */
+   #include <sys/os.h>		/* required by os_file() */
+   #include <utime.h>
+   #include <unistd.h>		/* can i narrow this down & remove it? */
+   #include <malloc.h>
+   #include <sys/stat.h>
+   #include <sys/times.h>
+
+
+   #define Int32   int
+   #define UInt32  unsigned int
+   #define Char    char
+   #define UChar   unsigned char
+   #define Int16   short
+   #define UInt16  unsigned short
+
+   #define PATH_SEP    '.'
+   #define MY_LSTAT    lstat
+   #define MY_S_IFREG  S_ISREG
+   #define MY_STAT     stat
+
+   #define APPEND_FILESPEC(root, name) \
+      root=snocString((root), (name))
+
+   #define SET_BINARY_MODE(fd) /**/
+
+   /*--
+      You should try very hard to persuade your C compiler
+      to inline the bits marked INLINE.  Otherwise bzip2 will
+      run rather slowly.  gcc version 2.x is recommended.
+   --*/
+   #define INLINE   inline
+   #define NORETURN __attribute__ ((noreturn))
+#endif
 
 /*---------------------------------------------*/
 /*--
@@ -408,7 +449,7 @@
 --*/
 UChar    *block;    /*-- compress   --*/
 UInt16   *quadrant; /*-- compress   --*/
-Int32    *zptr;     /*-- compress   --*/ 
+Int32    *zptr;     /*-- compress   --*/
 UInt16   *szptr;    /*-- overlays zptr ---*/
 Int32    *ftab;     /*-- compress   --*/
 
@@ -448,7 +489,7 @@
 
 /*--
   Used when sorting.  If too many long comparisons
-  happen, we stop sorting, randomise the block 
+  happen, we stop sorting, randomise the block
   slightly, and try again.
 --*/
 
@@ -821,7 +862,7 @@
 
 
 /*---------------------------------------------*/
-void hbMakeCodeLengths ( UChar *len, 
+void hbMakeCodeLengths ( UChar *len,
                          Int32 *freq,
                          Int32 alphaSize,
                          Int32 maxLen )
@@ -835,7 +876,7 @@
 
    Int32 heap   [ MAX_ALPHA_SIZE + 2 ];
    Int32 weight [ MAX_ALPHA_SIZE * 2 ];
-   Int32 parent [ MAX_ALPHA_SIZE * 2 ]; 
+   Int32 parent [ MAX_ALPHA_SIZE * 2 ];
 
    for (i = 0; i < alphaSize; i++)
       weight[i+1] = (freq[i] == 0 ? 1 : freq[i]) << 8;
@@ -855,9 +896,9 @@
          heap[nHeap] = i;
          UPHEAP(nHeap);
       }
-      if (!(nHeap < (MAX_ALPHA_SIZE+2))) 
+      if (!(nHeap < (MAX_ALPHA_SIZE+2)))
          panic ( "hbMakeCodeLengths(1)" );
-   
+
       while (nHeap > 1) {
          n1 = heap[1]; heap[1] = heap[nHeap]; nHeap--; DOWNHEAP(1);
          n2 = heap[1]; heap[1] = heap[nHeap]; nHeap--; DOWNHEAP(1);
@@ -880,7 +921,7 @@
          len[i-1] = j;
          if (j > maxLen) tooLong = True;
       }
-      
+
       if (! tooLong) break;
 
       for (i = 1; i < alphaSize; i++) {
@@ -1015,7 +1056,7 @@
       The back end needs a place to store the MTF values
       whilst it calculates the coding tables.  We could
       put them in the zptr array.  However, these values
-      will fit in a short, so we overlay szptr at the 
+      will fit in a short, so we overlay szptr at the
       start of zptr, in the hope of reducing the number
       of cache misses induced by the multiple traversals
       of the MTF values when calculating coding tables.
@@ -1109,7 +1150,7 @@
    wr = 0;
    zPend = 0;
    for (i = 0; i < nInUse; i++) yy[i] = (UChar) i;
-   
+
 
    for (i = 0; i <= last; i++) {
       UChar ll_i;
@@ -1195,8 +1236,8 @@
    Int32  fave[N_GROUPS];
 
    if (verbosity >= 3)
-      fprintf ( stderr, 
-                "      %d in block, %d after MTF & 1-2 coding, %d+2 syms in use\n", 
+      fprintf ( stderr,
+                "      %d in block, %d after MTF & 1-2 coding, %d+2 syms in use\n",
                 last+1, nMTF, nInUse );
 
    alphaSize = nInUse+2;
@@ -1211,7 +1252,7 @@
                    nGroups = 6;
 
    /*--- Generate an initial set of coding tables ---*/
-   { 
+   {
       Int32 nPart, remF, tFreq, aFreq;
 
       nPart = nGroups;
@@ -1226,31 +1267,31 @@
             aFreq += mtfFreq[ge];
          }
 
-         if (ge > gs 
-             && nPart != nGroups && nPart != 1 
+         if (ge > gs
+             && nPart != nGroups && nPart != 1
              && ((nGroups-nPart) % 2 == 1)) {
             aFreq -= mtfFreq[ge];
             ge--;
          }
 
          if (verbosity >= 3)
-            fprintf ( stderr, 
+            fprintf ( stderr,
                       "      initial group %d, [%d .. %d], has %d syms (%4.1f%%)\n",
-                              nPart, gs, ge, aFreq, 
+                              nPart, gs, ge, aFreq,
                               (100.0 * (float)aFreq) / (float)nMTF );
- 
+
          for (v = 0; v < alphaSize; v++)
-            if (v >= gs && v <= ge) 
+            if (v >= gs && v <= ge)
                len[nPart-1][v] = LESSER_ICOST; else
                len[nPart-1][v] = GREATER_ICOST;
- 
+
          nPart--;
          gs = ge+1;
          remF -= aFreq;
       }
    }
 
-   /*--- 
+   /*---
       Iterate up to N_ITERS times to improve the tables.
    ---*/
    for (iter = 0; iter < N_ITERS; iter++) {
@@ -1268,10 +1309,10 @@
 
          /*--- Set group start & end marks. --*/
          if (gs >= nMTF) break;
-         ge = gs + G_SIZE - 1; 
+         ge = gs + G_SIZE - 1;
          if (ge >= nMTF) ge = nMTF-1;
 
-         /*-- 
+         /*--
             Calculate the cost of this group as coded
             by each of the coding tables.
          --*/
@@ -1280,7 +1321,7 @@
          if (nGroups == 6) {
             register UInt16 cost0, cost1, cost2, cost3, cost4, cost5;
             cost0 = cost1 = cost2 = cost3 = cost4 = cost5 = 0;
-            for (i = gs; i <= ge; i++) { 
+            for (i = gs; i <= ge; i++) {
                UInt16 icv = szptr[i];
                cost0 += len[0][icv];
                cost1 += len[1][icv];
@@ -1292,13 +1333,13 @@
             cost[0] = cost0; cost[1] = cost1; cost[2] = cost2;
             cost[3] = cost3; cost[4] = cost4; cost[5] = cost5;
          } else {
-            for (i = gs; i <= ge; i++) { 
+            for (i = gs; i <= ge; i++) {
                UInt16 icv = szptr[i];
                for (t = 0; t < nGroups; t++) cost[t] += len[t][icv];
             }
          }
- 
-         /*-- 
+
+         /*--
             Find the coding table which is best for this group,
             and record its identity in the selector table.
          --*/
@@ -1310,7 +1351,7 @@
          selector[nSelectors] = bt;
          nSelectors++;
 
-         /*-- 
+         /*--
             Increment the symbol frequencies for the selected table.
           --*/
          for (i = gs; i <= ge; i++)
@@ -1319,8 +1360,8 @@
          gs = ge+1;
       }
       if (verbosity >= 3) {
-         fprintf ( stderr, 
-                   "      pass %d: size is %d, grp uses are ", 
+         fprintf ( stderr,
+                   "      pass %d: size is %d, grp uses are ",
                    iter+1, totc/8 );
          for (t = 0; t < nGroups; t++)
             fprintf ( stderr, "%d ", fave[t] );
@@ -1370,19 +1411,19 @@
       }
       if (maxLen > 20) panic ( "sendMTFValues(3)" );
       if (minLen < 1)  panic ( "sendMTFValues(4)" );
-      hbAssignCodes ( &code[t][0], &len[t][0], 
+      hbAssignCodes ( &code[t][0], &len[t][0],
                       minLen, maxLen, alphaSize );
    }
 
    /*--- Transmit the mapping table. ---*/
-   { 
+   {
       Bool inUse16[16];
       for (i = 0; i < 16; i++) {
           inUse16[i] = False;
           for (j = 0; j < 16; j++)
              if (inUse[i * 16 + j]) inUse16[i] = True;
       }
-     
+
       nBytes = bytesOut;
       for (i = 0; i < 16; i++)
          if (inUse16[i]) bsW(1,1); else bsW(1,0);
@@ -1392,7 +1433,7 @@
             for (j = 0; j < 16; j++)
                if (inUse[i * 16 + j]) bsW(1,1); else bsW(1,0);
 
-      if (verbosity >= 3) 
+      if (verbosity >= 3)
          fprintf ( stderr, "      bytes: mapping %d, ", bytesOut-nBytes );
    }
 
@@ -1400,7 +1441,7 @@
    nBytes = bytesOut;
    bsW ( 3, nGroups );
    bsW ( 15, nSelectors );
-   for (i = 0; i < nSelectors; i++) { 
+   for (i = 0; i < nSelectors; i++) {
       for (j = 0; j < selectorMtf[i]; j++) bsW(1,1);
       bsW(1,0);
    }
@@ -1429,9 +1470,9 @@
    gs = 0;
    while (True) {
       if (gs >= nMTF) break;
-      ge = gs + G_SIZE - 1; 
+      ge = gs + G_SIZE - 1;
       if (ge >= nMTF) ge = nMTF-1;
-      for (i = gs; i <= ge; i++) { 
+      for (i = gs; i <= ge; i++) {
          #if DEBUG
             assert (selector[selCtr] < nGroups);
          #endif
@@ -1467,8 +1508,8 @@
 
    /*--- Receive the mapping table ---*/
    for (i = 0; i < 16; i++)
-      if (bsR(1) == 1) 
-         inUse16[i] = True; else 
+      if (bsR(1) == 1)
+         inUse16[i] = True; else
          inUse16[i] = False;
 
    for (i = 0; i < 256; i++) inUse[i] = False;
@@ -1494,7 +1535,7 @@
    {
       UChar pos[N_GROUPS], tmp, v;
       for (v = 0; v < nGroups; v++) pos[v] = v;
-   
+
       for (i = 0; i < nSelectors; i++) {
          v = selectorMtf[i];
          tmp = pos[v];
@@ -1523,7 +1564,7 @@
          if (len[t][i] > maxLen) maxLen = len[t][i];
          if (len[t][i] < minLen) minLen = len[t][i];
       }
-      hbCreateDecodeTables ( 
+      hbCreateDecodeTables (
          &limit[t][0], &base[t][0], &perm[t][0], &len[t][0],
          minLen, maxLen, alphaSize
       );
@@ -1603,7 +1644,7 @@
 
          if (smallMode)
             while (s > 0) {
-               last++; 
+               last++;
                ll16[last] = ch;
                s--;
             }
@@ -1782,7 +1823,7 @@
 
    for (; hp >= 0; hp--) {
       h = incs[hp];
-      if (verbosity >= 5) 
+      if (verbosity >= 5)
          fprintf ( stderr, "          shell increment %d\n", h );
 
       i = lo + h;
@@ -2154,58 +2195,58 @@
 /*---------------------------------------------------*/
 
 /*---------------------------------------------*/
-Int32 rNums[512] = { 
-   619, 720, 127, 481, 931, 816, 813, 233, 566, 247, 
-   985, 724, 205, 454, 863, 491, 741, 242, 949, 214, 
-   733, 859, 335, 708, 621, 574, 73, 654, 730, 472, 
-   419, 436, 278, 496, 867, 210, 399, 680, 480, 51, 
-   878, 465, 811, 169, 869, 675, 611, 697, 867, 561, 
-   862, 687, 507, 283, 482, 129, 807, 591, 733, 623, 
-   150, 238, 59, 379, 684, 877, 625, 169, 643, 105, 
-   170, 607, 520, 932, 727, 476, 693, 425, 174, 647, 
-   73, 122, 335, 530, 442, 853, 695, 249, 445, 515, 
-   909, 545, 703, 919, 874, 474, 882, 500, 594, 612, 
-   641, 801, 220, 162, 819, 984, 589, 513, 495, 799, 
-   161, 604, 958, 533, 221, 400, 386, 867, 600, 782, 
-   382, 596, 414, 171, 516, 375, 682, 485, 911, 276, 
-   98, 553, 163, 354, 666, 933, 424, 341, 533, 870, 
-   227, 730, 475, 186, 263, 647, 537, 686, 600, 224, 
-   469, 68, 770, 919, 190, 373, 294, 822, 808, 206, 
-   184, 943, 795, 384, 383, 461, 404, 758, 839, 887, 
-   715, 67, 618, 276, 204, 918, 873, 777, 604, 560, 
-   951, 160, 578, 722, 79, 804, 96, 409, 713, 940, 
-   652, 934, 970, 447, 318, 353, 859, 672, 112, 785, 
-   645, 863, 803, 350, 139, 93, 354, 99, 820, 908, 
-   609, 772, 154, 274, 580, 184, 79, 626, 630, 742, 
-   653, 282, 762, 623, 680, 81, 927, 626, 789, 125, 
-   411, 521, 938, 300, 821, 78, 343, 175, 128, 250, 
-   170, 774, 972, 275, 999, 639, 495, 78, 352, 126, 
-   857, 956, 358, 619, 580, 124, 737, 594, 701, 612, 
-   669, 112, 134, 694, 363, 992, 809, 743, 168, 974, 
-   944, 375, 748, 52, 600, 747, 642, 182, 862, 81, 
-   344, 805, 988, 739, 511, 655, 814, 334, 249, 515, 
-   897, 955, 664, 981, 649, 113, 974, 459, 893, 228, 
-   433, 837, 553, 268, 926, 240, 102, 654, 459, 51, 
-   686, 754, 806, 760, 493, 403, 415, 394, 687, 700, 
-   946, 670, 656, 610, 738, 392, 760, 799, 887, 653, 
-   978, 321, 576, 617, 626, 502, 894, 679, 243, 440, 
-   680, 879, 194, 572, 640, 724, 926, 56, 204, 700, 
-   707, 151, 457, 449, 797, 195, 791, 558, 945, 679, 
-   297, 59, 87, 824, 713, 663, 412, 693, 342, 606, 
-   134, 108, 571, 364, 631, 212, 174, 643, 304, 329, 
-   343, 97, 430, 751, 497, 314, 983, 374, 822, 928, 
-   140, 206, 73, 263, 980, 736, 876, 478, 430, 305, 
-   170, 514, 364, 692, 829, 82, 855, 953, 676, 246, 
-   369, 970, 294, 750, 807, 827, 150, 790, 288, 923, 
-   804, 378, 215, 828, 592, 281, 565, 555, 710, 82, 
-   896, 831, 547, 261, 524, 462, 293, 465, 502, 56, 
-   661, 821, 976, 991, 658, 869, 905, 758, 745, 193, 
-   768, 550, 608, 933, 378, 286, 215, 979, 792, 961, 
-   61, 688, 793, 644, 986, 403, 106, 366, 905, 644, 
-   372, 567, 466, 434, 645, 210, 389, 550, 919, 135, 
-   780, 773, 635, 389, 707, 100, 626, 958, 165, 504, 
-   920, 176, 193, 713, 857, 265, 203, 50, 668, 108, 
-   645, 990, 626, 197, 510, 357, 358, 850, 858, 364, 
+Int32 rNums[512] = {
+   619, 720, 127, 481, 931, 816, 813, 233, 566, 247,
+   985, 724, 205, 454, 863, 491, 741, 242, 949, 214,
+   733, 859, 335, 708, 621, 574, 73, 654, 730, 472,
+   419, 436, 278, 496, 867, 210, 399, 680, 480, 51,
+   878, 465, 811, 169, 869, 675, 611, 697, 867, 561,
+   862, 687, 507, 283, 482, 129, 807, 591, 733, 623,
+   150, 238, 59, 379, 684, 877, 625, 169, 643, 105,
+   170, 607, 520, 932, 727, 476, 693, 425, 174, 647,
+   73, 122, 335, 530, 442, 853, 695, 249, 445, 515,
+   909, 545, 703, 919, 874, 474, 882, 500, 594, 612,
+   641, 801, 220, 162, 819, 984, 589, 513, 495, 799,
+   161, 604, 958, 533, 221, 400, 386, 867, 600, 782,
+   382, 596, 414, 171, 516, 375, 682, 485, 911, 276,
+   98, 553, 163, 354, 666, 933, 424, 341, 533, 870,
+   227, 730, 475, 186, 263, 647, 537, 686, 600, 224,
+   469, 68, 770, 919, 190, 373, 294, 822, 808, 206,
+   184, 943, 795, 384, 383, 461, 404, 758, 839, 887,
+   715, 67, 618, 276, 204, 918, 873, 777, 604, 560,
+   951, 160, 578, 722, 79, 804, 96, 409, 713, 940,
+   652, 934, 970, 447, 318, 353, 859, 672, 112, 785,
+   645, 863, 803, 350, 139, 93, 354, 99, 820, 908,
+   609, 772, 154, 274, 580, 184, 79, 626, 630, 742,
+   653, 282, 762, 623, 680, 81, 927, 626, 789, 125,
+   411, 521, 938, 300, 821, 78, 343, 175, 128, 250,
+   170, 774, 972, 275, 999, 639, 495, 78, 352, 126,
+   857, 956, 358, 619, 580, 124, 737, 594, 701, 612,
+   669, 112, 134, 694, 363, 992, 809, 743, 168, 974,
+   944, 375, 748, 52, 600, 747, 642, 182, 862, 81,
+   344, 805, 988, 739, 511, 655, 814, 334, 249, 515,
+   897, 955, 664, 981, 649, 113, 974, 459, 893, 228,
+   433, 837, 553, 268, 926, 240, 102, 654, 459, 51,
+   686, 754, 806, 760, 493, 403, 415, 394, 687, 700,
+   946, 670, 656, 610, 738, 392, 760, 799, 887, 653,
+   978, 321, 576, 617, 626, 502, 894, 679, 243, 440,
+   680, 879, 194, 572, 640, 724, 926, 56, 204, 700,
+   707, 151, 457, 449, 797, 195, 791, 558, 945, 679,
+   297, 59, 87, 824, 713, 663, 412, 693, 342, 606,
+   134, 108, 571, 364, 631, 212, 174, 643, 304, 329,
+   343, 97, 430, 751, 497, 314, 983, 374, 822, 928,
+   140, 206, 73, 263, 980, 736, 876, 478, 430, 305,
+   170, 514, 364, 692, 829, 82, 855, 953, 676, 246,
+   369, 970, 294, 750, 807, 827, 150, 790, 288, 923,
+   804, 378, 215, 828, 592, 281, 565, 555, 710, 82,
+   896, 831, 547, 261, 524, 462, 293, 465, 502, 56,
+   661, 821, 976, 991, 658, 869, 905, 758, 745, 193,
+   768, 550, 608, 933, 378, 286, 215, 979, 792, 961,
+   61, 688, 793, 644, 986, 403, 106, 366, 905, 644,
+   372, 567, 466, 434, 645, 210, 389, 550, 919, 135,
+   780, 773, 635, 389, 707, 100, 626, 958, 165, 504,
+   920, 176, 193, 713, 857, 265, 203, 50, 668, 108,
+   645, 990, 626, 197, 510, 357, 358, 850, 858, 364,
    936, 638
 };
 
@@ -2401,12 +2442,12 @@
                ch2 ^= (UInt32)RAND_MASK;
             }
             i2++;
-   
+
             if (dst)
                retVal = putc ( ch2, dst );
-   
+
             UPDATE_CRC ( localCrc, (UChar)ch2 );
-   
+
             if (ch2 != chPrev) {
                count = 1;
             } else {
@@ -2504,10 +2545,10 @@
             RAND_UPD_MASK;
             ch2 ^= (UInt32)RAND_MASK;
             i2++;
-   
+
             retVal = putc ( ch2, dst );
             UPDATE_CRC ( localCrc, (UChar)ch2 );
-   
+
             if (ch2 != chPrev) {
                count = 1;
             } else {
@@ -2534,10 +2575,10 @@
             chPrev = ch2;
             GET_FAST(ch2);
             i2++;
-   
+
             retVal = putc ( ch2, dst );
             UPDATE_CRC ( localCrc, (UChar)ch2 );
-   
+
             if (ch2 != chPrev) {
                count = 1;
             } else {
@@ -2754,7 +2795,7 @@
    }
 
    if (verbosity >= 2 && nBlocksRandomised > 0)
-      fprintf ( stderr, "    %d block%s needed randomisation\n", 
+      fprintf ( stderr, "    %d block%s needed randomisation\n",
                         nBlocksRandomised,
                         nBlocksRandomised == 1 ? "" : "s" );
 
@@ -3083,10 +3124,10 @@
                    progName );
    }
    if (numFileNames > 0 && numFilesProcessed < numFileNames) {
-      fprintf ( stderr, 
+      fprintf ( stderr,
                 "%s: WARNING: some files have not been processed:\n"
                 "\t%d specified on command line, %d not processed yet.\n\n",
-                progName, numFileNames, 
+                progName, numFileNames,
                           numFileNames - numFilesProcessed );
    }
    exit ( ec );
@@ -3276,10 +3317,16 @@
 /*---------------------------------------------*/
 Bool fileExists ( Char* name )
 {
+#ifdef BZ_RISCOS
+   int regs[6];
+   os_file( 17, name, regs );
+   return regs[0] & 1;
+#else
    FILE *tmp   = fopen ( name, "rb" );
    Bool exists = (tmp != NULL);
    if (tmp != NULL) fclose ( tmp );
    return exists;
+#endif
 }
 
 
@@ -3302,7 +3349,7 @@
 /*---------------------------------------------*/
 void copyDateAndPermissions ( Char *srcName, Char *dstName )
 {
-   #if BZ_UNIX
+#if BZ_UNIX
    IntNative      retVal;
    struct MY_STAT statBuf;
    struct utimbuf uTimBuf;
@@ -3316,13 +3363,22 @@
    ERROR_IF_NOT_ZERO ( retVal );
    retVal = utime ( dstName, &uTimBuf );
    ERROR_IF_NOT_ZERO ( retVal );
-   #endif
+#elif BZ_RISCOS
+   int regs [6];
+   os_file(17, srcName, regs);
+   os_file(1, dstName, regs);
+#endif
 }
 
 
 /*---------------------------------------------*/
 Bool endsInBz2 ( Char* name )
 {
+#ifdef BZ_RISCOS
+   int regs[6];
+   os_file(17, name, regs);
+   return (((regs[2] & 0x000fff00) >> 8) == BZ_FILETYPE);
+#else
    Int32 n = strlen ( name );
    if (n <= 4) return False;
    return
@@ -3330,6 +3386,7 @@
        name[n-3] == 'b' &&
        name[n-2] == 'z' &&
        name[n-1] == '2');
+#endif
 }
 
 
@@ -3348,18 +3405,29 @@
 {
    FILE *inStr;
    FILE *outStr;
+   char *off;
 
    if (name == NULL && srcMode != SM_I2O)
       panic ( "compress: bad modes\n" );
 
    switch (srcMode) {
       case SM_I2O: strcpy ( inName, "(stdin)" );
-                   strcpy ( outName, "(stdout)" ); break;
+                   strcpy ( outName, "(stdout)" );
+                   break;
       case SM_F2F: strcpy ( inName, name );
                    strcpy ( outName, name );
-                   strcat ( outName, ".bz2" ); break;
+#ifdef BZ_RISCOS
+		   off = strrchr ( outName, '.' );
+		   if (off == NULL) off = outName-1;
+		   memcpy ( off+1, "bzXXXXXX", 9);
+		   mktemp ( outName );
+#else
+                   strcat ( outName, ".bz2" );
+#endif
+		   break;
       case SM_F2O: strcpy ( inName, name );
-                   strcpy ( outName, "(stdout)" ); break;
+                   strcpy ( outName, "(stdout)" );
+                   break;
    }
 
    if ( srcMode != SM_I2O && containsDubiousChars ( inName ) ) {
@@ -3377,11 +3445,13 @@
                 progName, inName );
       return;
    }
+#ifndef BZ_RISCOS
    if ( srcMode != SM_I2O && notABogStandardFile ( inName )) {
       fprintf ( stderr, "%s: Input file %s is not a normal file, skipping.\n",
                 progName, inName );
       return;
    }
+#endif
    if ( srcMode == SM_F2F && fileExists ( outName ) ) {
       fprintf ( stderr, "%s: Output file %s already exists, skipping.\n",
                 progName, outName );
@@ -3455,9 +3525,19 @@
    /*--- If there was an I/O error, we won't get here. ---*/
    if ( srcMode == SM_F2F ) {
       copyDateAndPermissions ( inName, outName );
+#ifdef BZ_RISCOS
+      {
+         int regs[3];
+         regs[2] = BZ_FILETYPE;
+         os_file( 18, outName, regs);
+      }
+#endif
       if ( !keepInputFiles ) {
          IntNative retVal = remove ( inName );
          ERROR_IF_NOT_ZERO ( retVal );
+#ifdef BZ_RISCOS
+	 rename( outName, inName );
+#endif
       }
    }
 }
@@ -3469,20 +3549,30 @@
    FILE *inStr;
    FILE *outStr;
    Bool magicNumberOK;
+   char *off;
 
    if (name == NULL && srcMode != SM_I2O)
       panic ( "uncompress: bad modes\n" );
 
    switch (srcMode) {
       case SM_I2O: strcpy ( inName, "(stdin)" );
-                   strcpy ( outName, "(stdout)" ); break;
+                   strcpy ( outName, "(stdout)" );
+                   break;
       case SM_F2F: strcpy ( inName, name );
                    strcpy ( outName, name );
+#ifdef BZ_RISCOS
+		   off = strrchr ( outName, '.' );
+		   if (off == NULL) off = outName-1;
+		   memcpy ( off+1, "bzXXXXXX", 9);
+		   mktemp ( outName );
+#else
                    if (endsInBz2 ( outName ))
                       outName [ strlen ( outName ) - 4 ] = '\0';
+#endif
                    break;
       case SM_F2O: strcpy ( inName, name );
-                   strcpy ( outName, "(stdout)" ); break;
+                   strcpy ( outName, "(stdout)" );
+                   break;
    }
 
    if ( srcMode != SM_I2O && containsDubiousChars ( inName ) ) {
@@ -3501,11 +3591,13 @@
                 progName, inName );
       return;
    }
+#ifndef BZ_RISCOS
    if ( srcMode != SM_I2O && notABogStandardFile ( inName )) {
       fprintf ( stderr, "%s: Input file %s is not a normal file, skipping.\n",
                 progName, inName );
       return;
    }
+#endif
    if ( srcMode == SM_F2F && fileExists ( outName ) ) {
       fprintf ( stderr, "%s: Output file %s already exists, skipping.\n",
                 progName, outName );
@@ -3576,6 +3668,14 @@
             IntNative retVal = remove ( inName );
             ERROR_IF_NOT_ZERO ( retVal );
          }
+#ifdef BZ_RISCOS
+	 {
+	    int regs[3];
+	    rename( outName, inName );
+	    regs[2] = 0xffd;
+	    os_file( 18, inName, regs );
+	 }
+#endif
       }
    } else {
       if ( srcMode == SM_F2F ) {
@@ -3630,11 +3730,13 @@
                 progName, inName );
       return;
    }
+#ifndef BZ_RISCOS
    if ( srcMode != SM_I2O && notABogStandardFile ( inName )) {
       fprintf ( stderr, "%s: Input file %s is not a normal file, skipping.\n",
                 progName, inName );
       return;
    }
+#endif
 
    switch ( srcMode ) {
 
@@ -3833,6 +3935,10 @@
    #if DEBUG
       fprintf ( stderr, "bzip2: *** compiled with debugging ON ***\n" );
    #endif
+
+#if BZ_RISCOS
+   __uname_control = __UNAME_NO_PROCESS; /* Stop Unixlib trying to mangle filenames */
+#endif
 
    /*-- Be really really really paranoid :-) --*/
    if (sizeof(Int32) != 4 || sizeof(UInt32) != 4  ||
