home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / C / Applications / MacPerl 4.1.3 / Perl / util.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-12-12  |  37.7 KB  |  1,887 lines  |  [TEXT/MPS ]

  1. /* $RCSfile: util.c,v $$Revision: 4.0.1.6 $$Date: 92/06/11 21:18:47 $
  2.  *
  3.  *    Copyright (c) 1991, Larry Wall
  4.  *
  5.  *    You may distribute under the terms of the Perl Artistic License,
  6.  *    as specified in the README file.
  7.  *
  8.  * $Log:    util.c,v $
  9.  * Revision 4.0.1.6  92/06/11  21:18:47  lwall
  10.  * patch34: boneheaded typo in my_bcopy()
  11.  * 
  12.  * Revision 4.0.1.5  92/06/08  16:08:37  lwall
  13.  * patch20: removed implicit int declarations on functions
  14.  * patch20: Perl now distinguishes overlapped copies from non-overlapped
  15.  * patch20: fixed confusion between a *var's real name and its effective name
  16.  * patch20: bcopy() and memcpy() now tested for overlap safety
  17.  * patch20: added Atari ST portability
  18.  * 
  19.  * Revision 4.0.1.4  91/11/11  16:48:54  lwall
  20.  * patch19: study was busted by 4.018
  21.  * patch19: added little-endian pack/unpack options
  22.  * 
  23.  * Revision 4.0.1.3  91/11/05  19:18:26  lwall
  24.  * patch11: safe malloc code now integrated into Perl's malloc when possible
  25.  * patch11: index("little", "longer string") could visit faraway places
  26.  * patch11: warn '-' x 10000 dumped core
  27.  * patch11: forked exec on non-existent program now issues a warning
  28.  * 
  29.  * Revision 4.0.1.2  91/06/07  12:10:42  lwall
  30.  * patch4: new copyright notice
  31.  * patch4: made some allowances for "semi-standard" C
  32.  * patch4: index() could blow up searching for null string
  33.  * patch4: taintchecks could improperly modify parent in vfork()
  34.  * patch4: exec would close files even if you cleared close-on-exec flag
  35.  * 
  36.  * Revision 4.0.1.1  91/04/12  09:19:25  lwall
  37.  * patch1: random cleanup in cpp namespace
  38.  * 
  39.  * Revision 4.0  91/03/20  01:56:39  lwall
  40.  * 4.0 baseline.
  41.  * 
  42.  */
  43. /*SUPPRESS 112*/
  44.  
  45. #include "EXTERN.h"
  46. #include "perl.h"
  47.  
  48. #if !defined(NSIG) || defined(M_UNIX) || defined(M_XENIX)
  49. #include <signal.h>
  50. #endif
  51.  
  52. #ifdef I_VFORK
  53. #  include <vfork.h>
  54. #endif
  55.  
  56. #ifdef I_VARARGS
  57. #  include <varargs.h>
  58. #endif
  59.  
  60. #ifdef I_FCNTL
  61. #  include <fcntl.h>
  62. #endif
  63. #ifdef I_SYS_FILE
  64. #  include <sys/file.h>
  65. #endif
  66.  
  67. #define FLUSH
  68.  
  69. #ifndef safemalloc
  70.  
  71. static char nomem[] = "Out of memory!\n";
  72.  
  73. /* paranoid version of malloc */
  74.  
  75. #ifdef DEBUGGING
  76. static int an = 0;
  77. #endif
  78.  
  79. /* NOTE:  Do not call the next three routines directly.  Use the macros
  80.  * in handy.h, so that we can easily redefine everything to do tracking of
  81.  * allocated hunks back to the original New to track down any memory leaks.
  82.  */
  83.  
  84. #ifdef macintosh
  85. extern void * gSacrificialGoat;
  86. #endif
  87.  
  88. char *
  89. safemalloc(size)
  90. #ifdef MSDOS
  91. unsigned long size;
  92. #else
  93. MEM_SIZE size;
  94. #endif /* MSDOS */
  95. {
  96.     char *ptr;
  97. #ifndef STDMAC
  98.     char *malloc();
  99. #endif /* ! STDMAC */
  100.  
  101. #ifdef MSDOS
  102.     if (size > 0xffff) {
  103.         fprintf(stderr, "Allocation too large: %lx\n", size) FLUSH;
  104.         exit(1);
  105.     }
  106. #endif /* MSDOS */
  107. #ifdef DEBUGGING
  108.     if ((long)size < 0)
  109.     fatal("panic: malloc");
  110. #endif
  111.     ptr = malloc(size?size:1);    /* malloc(0) is NASTY on our system */
  112. #ifdef DEBUGGING
  113. #  if !(defined(I286) || defined(atarist))
  114. #ifdef macintosh
  115.     if (debug & 128)
  116.     fprintf(perldbg,"0x%x: (%05d) malloc %ld bytes\n",ptr,an++,(long)size);
  117. #else
  118.     if (debug & 128)
  119.     fprintf(stderr,"0x%x: (%05d) malloc %ld bytes\n",ptr,an++,(long)size);
  120. #endif
  121. #  else
  122.     if (debug & 128)
  123.     fprintf(stderr,"0x%lx: (%05d) malloc %ld bytes\n",ptr,an++,(long)size);
  124. #  endif
  125. #endif
  126. #ifdef macintosh
  127.     if (ptr != Nullch && gSacrificialGoat)
  128. #else
  129.     if (ptr != Nullch)
  130. #endif
  131.     return ptr;
  132.     else if (nomemok)
  133.     return Nullch;
  134.     else {
  135.     fputs(nomem,stderr) FLUSH;
  136.     exit(1);
  137.     }
  138.     /*NOTREACHED*/
  139. #ifdef lint
  140.     return ptr;
  141. #endif
  142. }
  143.  
  144. /* paranoid version of realloc */
  145.  
  146. char *
  147. saferealloc(where,size)
  148. char *where;
  149. #ifndef MSDOS
  150. MEM_SIZE size;
  151. #else
  152. unsigned long size;
  153. #endif /* MSDOS */
  154. {
  155.     char *ptr;
  156. #ifndef STDMAC
  157.     char *realloc();
  158. #endif /* ! STDMAC */
  159.  
  160. #ifdef MSDOS
  161.     if (size > 0xffff) {
  162.         fprintf(stderr, "Reallocation too large: %lx\n", size) FLUSH;
  163.         exit(1);
  164.     }
  165. #endif /* MSDOS */
  166.     if (!where)
  167.     fatal("Null realloc");
  168. #ifdef DEBUGGING
  169.     if ((long)size < 0)
  170.     fatal("panic: realloc");
  171. #endif
  172.     ptr = realloc(where,size?size:1);    /* realloc(0) is NASTY on our system */
  173. #ifdef DEBUGGING
  174. #  if !(defined(I286) || defined(atarist))
  175. #ifdef macintosh
  176.     if (debug & 128) {
  177.     fprintf(perldbg,"0x%x: (%05d) rfree\n",where,an++);
  178.     fprintf(perldbg,"0x%x: (%05d) realloc %ld bytes\n",ptr,an++,(long)size);
  179.     }
  180. #else
  181.     if (debug & 128) {
  182.     fprintf(stderr,"0x%x: (%05d) rfree\n",where,an++);
  183.     fprintf(stderr,"0x%x: (%05d) realloc %ld bytes\n",ptr,an++,(long)size);
  184.     }
  185. #endif
  186. #  else
  187.     if (debug & 128) {
  188.     fprintf(stderr,"0x%lx: (%05d) rfree\n",where,an++);
  189.     fprintf(stderr,"0x%lx: (%05d) realloc %ld bytes\n",ptr,an++,(long)size);
  190.     }
  191. #  endif
  192. #endif
  193. #ifdef macintosh
  194.     if (ptr != Nullch && gSacrificialGoat)
  195. #else
  196.     if (ptr != Nullch)
  197. #endif
  198.     return ptr;
  199.     else if (nomemok)
  200.     return Nullch;
  201.     else {
  202.     fputs(nomem,stderr) FLUSH;
  203.     exit(1);
  204.     }
  205.     /*NOTREACHED*/
  206. #ifdef lint
  207.     return ptr;
  208. #endif
  209. }
  210.  
  211. /* safe version of free */
  212.  
  213. void
  214. safefree(where)
  215. char *where;
  216. {
  217. #ifdef DEBUGGING
  218. #  if !(defined(I286) || defined(atarist))
  219.     if (debug & 128)
  220. #ifdef macintosh
  221.     fprintf(perldbg,"0x%x: (%05d) free\n",where,an++);
  222. #else
  223.     fprintf(stderr,"0x%x: (%05d) free\n",where,an++);
  224. #endif
  225. #  else
  226.     if (debug & 128)
  227.     fprintf(stderr,"0x%lx: (%05d) free\n",where,an++);
  228. #  endif
  229. #endif
  230.     if (where) {
  231.     /*SUPPRESS 701*/
  232.     free(where);
  233.     }
  234. }
  235.  
  236. #endif /* !safemalloc */
  237.  
  238. #ifdef LEAKTEST
  239.  
  240. #define ALIGN sizeof(long)
  241.  
  242. char *
  243. safexmalloc(x,size)
  244. int x;
  245. MEM_SIZE size;
  246. {
  247.     register char *where;
  248.  
  249.     where = safemalloc(size + ALIGN);
  250.     xcount[x]++;
  251.     where[0] = x % 100;
  252.     where[1] = x / 100;
  253.     return where + ALIGN;
  254. }
  255.  
  256. char *
  257. safexrealloc(where,size)
  258. char *where;
  259. MEM_SIZE size;
  260. {
  261.     return saferealloc(where - ALIGN, size + ALIGN) + ALIGN;
  262. }
  263.  
  264. void
  265. safexfree(where)
  266. char *where;
  267. {
  268.     int x;
  269.  
  270.     if (!where)
  271.     return;
  272.     where -= ALIGN;
  273.     x = where[0] + 100 * where[1];
  274.     xcount[x]--;
  275.     safefree(where);
  276. }
  277.  
  278. static void
  279. xstat()
  280. {
  281.     register int i;
  282.  
  283.     for (i = 0; i < MAXXCOUNT; i++) {
  284.     if (xcount[i] > lastxcount[i]) {
  285.         fprintf(stderr,"%2d %2d\t%ld\n", i / 100, i % 100, xcount[i]);
  286.         lastxcount[i] = xcount[i];
  287.     }
  288.     }
  289. }
  290.  
  291. #endif /* LEAKTEST */
  292.  
  293. /* copy a string up to some (non-backslashed) delimiter, if any */
  294.  
  295. char *
  296. cpytill(to,from,fromend,delim,retlen)
  297. register char *to;
  298. register char *from;
  299. register char *fromend;
  300. register int delim;
  301. int *retlen;
  302. {
  303.     char *origto = to;
  304.  
  305.     for (; from < fromend; from++,to++) {
  306.     if (*from == '\\') {
  307.         if (from[1] == delim)
  308.         from++;
  309.         else if (from[1] == '\\')
  310.         *to++ = *from++;
  311.     }
  312.     else if (*from == delim)
  313.         break;
  314.     *to = *from;
  315.     }
  316.     *to = '\0';
  317.     *retlen = to - origto;
  318.     return from;
  319. }
  320.  
  321. /* return ptr to little string in big string, NULL if not found */
  322. /* This routine was donated by Corey Satten. */
  323.  
  324. char *
  325. instr(big, little)
  326. register char *big;
  327. register char *little;
  328. {
  329.     register char *s, *x;
  330.     register int first;
  331.  
  332.     if (!little)
  333.     return big;
  334.     first = *little++;
  335.     if (!first)
  336.     return big;
  337.     while (*big) {
  338.     if (*big++ != first)
  339.         continue;
  340.     for (x=big,s=little; *s; /**/ ) {
  341.         if (!*x)
  342.         return Nullch;
  343.         if (*s++ != *x++) {
  344.         s--;
  345.         break;
  346.         }
  347.     }
  348.     if (!*s)
  349.         return big-1;
  350.     }
  351.     return Nullch;
  352. }
  353.  
  354. /* same as instr but allow embedded nulls */
  355.  
  356. char *
  357. ninstr(big, bigend, little, lend)
  358. register char *big;
  359. register char *bigend;
  360. char *little;
  361. char *lend;
  362. {
  363.     register char *s, *x;
  364.     register int first = *little;
  365.     register char *littleend = lend;
  366.  
  367.     if (!first && little > littleend)
  368.     return big;
  369.     if (bigend - big < littleend - little)
  370.     return Nullch;
  371.     bigend -= littleend - little++;
  372.     while (big <= bigend) {
  373.     if (*big++ != first)
  374.         continue;
  375.     for (x=big,s=little; s < littleend; /**/ ) {
  376.         if (*s++ != *x++) {
  377.         s--;
  378.         break;
  379.         }
  380.     }
  381.     if (s >= littleend)
  382.         return big-1;
  383.     }
  384.     return Nullch;
  385. }
  386.  
  387. /* reverse of the above--find last substring */
  388.  
  389. char *
  390. rninstr(big, bigend, little, lend)
  391. register char *big;
  392. char *bigend;
  393. char *little;
  394. char *lend;
  395. {
  396.     register char *bigbeg;
  397.     register char *s, *x;
  398.     register int first = *little;
  399.     register char *littleend = lend;
  400.  
  401.     if (!first && little > littleend)
  402.     return bigend;
  403.     bigbeg = big;
  404.     big = bigend - (littleend - little++);
  405.     while (big >= bigbeg) {
  406.     if (*big-- != first)
  407.         continue;
  408.     for (x=big+2,s=little; s < littleend; /**/ ) {
  409.         if (*s++ != *x++) {
  410.         s--;
  411.         break;
  412.         }
  413.     }
  414.     if (s >= littleend)
  415.         return big+1;
  416.     }
  417.     return Nullch;
  418. }
  419.  
  420. unsigned char fold[] = {
  421.     0,    1,    2,    3,    4,    5,    6,    7,
  422.     8,    9,    10,    11,    12,    13,    14,    15,
  423.     16,    17,    18,    19,    20,    21,    22,    23,
  424.     24,    25,    26,    27,    28,    29,    30,    31,
  425.     32,    33,    34,    35,    36,    37,    38,    39,
  426.     40,    41,    42,    43,    44,    45,    46,    47,
  427.     48,    49,    50,    51,    52,    53,    54,    55,
  428.     56,    57,    58,    59,    60,    61,    62,    63,
  429.     64,    'a',    'b',    'c',    'd',    'e',    'f',    'g',
  430.     'h',    'i',    'j',    'k',    'l',    'm',    'n',    'o',
  431.     'p',    'q',    'r',    's',    't',    'u',    'v',    'w',
  432.     'x',    'y',    'z',    91,    92,    93,    94,    95,
  433.     96,    'A',    'B',    'C',    'D',    'E',    'F',    'G',
  434.     'H',    'I',    'J',    'K',    'L',    'M',    'N',    'O',
  435.     'P',    'Q',    'R',    'S',    'T',    'U',    'V',    'W',
  436.     'X',    'Y',    'Z',    123,    124,    125,    126,    127,
  437.     128,    129,    130,    131,    132,    133,    134,    135,
  438.     136,    137,    138,    139,    140,    141,    142,    143,
  439.     144,    145,    146,    147,    148,    149,    150,    151,
  440.     152,    153,    154,    155,    156,    157,    158,    159,
  441.     160,    161,    162,    163,    164,    165,    166,    167,
  442.     168,    169,    170,    171,    172,    173,    174,    175,
  443.     176,    177,    178,    179,    180,    181,    182,    183,
  444.     184,    185,    186,    187,    188,    189,    190,    191,
  445.     192,    193,    194,    195,    196,    197,    198,    199,
  446.     200,    201,    202,    203,    204,    205,    206,    207,
  447.     208,    209,    210,    211,    212,    213,    214,    215,
  448.     216,    217,    218,    219,    220,    221,    222,    223,    
  449.     224,    225,    226,    227,    228,    229,    230,    231,
  450.     232,    233,    234,    235,    236,    237,    238,    239,
  451.     240,    241,    242,    243,    244,    245,    246,    247,
  452.     248,    249,    250,    251,    252,    253,    254,    255
  453. };
  454.  
  455. static unsigned char freq[] = {
  456.     1,    2,    84,    151,    154,    155,    156,    157,
  457.     165,    246,    250,    3,    158,    7,    18,    29,
  458.     40,    51,    62,    73,    85,    96,    107,    118,
  459.     129,    140,    147,    148,    149,    150,    152,    153,
  460.     255,    182,    224,    205,    174,    176,    180,    217,
  461.     233,    232,    236,    187,    235,    228,    234,    226,
  462.     222,    219,    211,    195,    188,    193,    185,    184,
  463.     191,    183,    201,    229,    181,    220,    194,    162,
  464.     163,    208,    186,    202,    200,    218,    198,    179,
  465.     178,    214,    166,    170,    207,    199,    209,    206,
  466.     204,    160,    212,    216,    215,    192,    175,    173,
  467.     243,    172,    161,    190,    203,    189,    164,    230,
  468.     167,    248,    227,    244,    242,    255,    241,    231,
  469.     240,    253,    169,    210,    245,    237,    249,    247,
  470.     239,    168,    252,    251,    254,    238,    223,    221,
  471.     213,    225,    177,    197,    171,    196,    159,    4,
  472.     5,    6,    8,    9,    10,    11,    12,    13,
  473.     14,    15,    16,    17,    19,    20,    21,    22,
  474.     23,    24,    25,    26,    27,    28,    30,    31,
  475.     32,    33,    34,    35,    36,    37,    38,    39,
  476.     41,    42,    43,    44,    45,    46,    47,    48,
  477.     49,    50,    52,    53,    54,    55,    56,    57,
  478.     58,    59,    60,    61,    63,    64,    65,    66,
  479.     67,    68,    69,    70,    71,    72,    74,    75,
  480.     76,    77,    78,    79,    80,    81,    82,    83,
  481.     86,    87,    88,    89,    90,    91,    92,    93,
  482.     94,    95,    97,    98,    99,    100,    101,    102,
  483.     103,    104,    105,    106,    108,    109,    110,    111,
  484.     112,    113,    114,    115,    116,    117,    119,    120,
  485.     121,    122,    123,    124,    125,    126,    127,    128,
  486.     130,    131,    132,    133,    134,    135,    136,    137,
  487.     138,    139,    141,    142,    143,    144,    145,    146
  488. };
  489.  
  490. void
  491. fbmcompile(str, iflag)
  492. STR *str;
  493. int iflag;
  494. {
  495.     register unsigned char *s;
  496.     register unsigned char *table;
  497.     register unsigned int i;
  498.     register unsigned int len = str->str_cur;
  499.     int rarest = 0;
  500.     unsigned int frequency = 256;
  501.  
  502.     Str_Grow(str,len+258);
  503. #ifndef lint
  504.     table = (unsigned char*)(str->str_ptr + len + 1);
  505. #else
  506.     table = Null(unsigned char*);
  507. #endif
  508.     s = table - 2;
  509.     for (i = 0; i < 256; i++) {
  510.     table[i] = len;
  511.     }
  512.     i = 0;
  513. #ifndef lint
  514.     while (s >= (unsigned char*)(str->str_ptr))
  515. #endif
  516.     {
  517.     if (table[*s] == len) {
  518. #ifndef pdp11
  519.         if (iflag)
  520.         table[*s] = table[fold[*s]] = i;
  521. #else
  522.         if (iflag) {
  523.         int j;
  524.         j = fold[*s];
  525.         table[j] = i;
  526.         table[*s] = i;
  527.         }
  528. #endif /* pdp11 */
  529.         else
  530.         table[*s] = i;
  531.     }
  532.     s--,i++;
  533.     }
  534.     str->str_pok |= SP_FBM;        /* deep magic */
  535.  
  536. #ifndef lint
  537.     s = (unsigned char*)(str->str_ptr);        /* deeper magic */
  538. #else
  539.     s = Null(unsigned char*);
  540. #endif
  541.     if (iflag) {
  542.     register unsigned int tmp, foldtmp;
  543.     str->str_pok |= SP_CASEFOLD;
  544.     for (i = 0; i < len; i++) {
  545.         tmp=freq[s[i]];
  546.         foldtmp=freq[fold[s[i]]];
  547.         if (tmp < frequency && foldtmp < frequency) {
  548.         rarest = i;
  549.         /* choose most frequent among the two */
  550.         frequency = (tmp > foldtmp) ? tmp : foldtmp;
  551.         }
  552.     }
  553.     }
  554.     else {
  555.     for (i = 0; i < len; i++) {
  556.         if (freq[s[i]] < frequency) {
  557.         rarest = i;
  558.         frequency = freq[s[i]];
  559.         }
  560.     }
  561.     }
  562.     str->str_rare = s[rarest];
  563.     str->str_state = rarest;
  564. #ifdef DEBUGGING
  565.     if (debug & 512)
  566. #ifdef macintosh
  567.     fprintf(perldbg,"rarest char %c at %d\n",str->str_rare, str->str_state);
  568. #else
  569.     fprintf(stderr,"rarest char %c at %d\n",str->str_rare, str->str_state);
  570. #endif
  571. #endif
  572. }
  573.  
  574. char *
  575. fbminstr(big, bigend, littlestr)
  576. unsigned char *big;
  577. register unsigned char *bigend;
  578. STR *littlestr;
  579. {
  580.     register unsigned char *s;
  581.     register int tmp;
  582.     register int littlelen;
  583.     register unsigned char *little;
  584.     register unsigned char *table;
  585.     register unsigned char *olds;
  586.     register unsigned char *oldlittle;
  587.  
  588. #ifndef lint
  589.     if (!(littlestr->str_pok & SP_FBM)) {
  590.     if (!littlestr->str_ptr)
  591.         return (char*)big;
  592.     return ninstr((char*)big,(char*)bigend,
  593.         littlestr->str_ptr, littlestr->str_ptr + littlestr->str_cur);
  594.     }
  595. #endif
  596.  
  597.     littlelen = littlestr->str_cur;
  598. #ifndef lint
  599.     if (littlestr->str_pok & SP_TAIL && !multiline) {    /* tail anchored? */
  600.     if (littlelen > bigend - big)
  601.         return Nullch;
  602.     little = (unsigned char*)littlestr->str_ptr;
  603.     if (littlestr->str_pok & SP_CASEFOLD) {    /* oops, fake it */
  604.         big = bigend - littlelen;        /* just start near end */
  605.         if (bigend[-1] == '\n' && little[littlelen-1] != '\n')
  606.         big--;
  607.     }
  608.     else {
  609.         s = bigend - littlelen;
  610.         if (*s == *little && bcmp(s,little,littlelen)==0)
  611.         return (char*)s;        /* how sweet it is */
  612.         else if (bigend[-1] == '\n' && little[littlelen-1] != '\n'
  613.           && s > big) {
  614.             s--;
  615.         if (*s == *little && bcmp(s,little,littlelen)==0)
  616.             return (char*)s;
  617.         }
  618.         return Nullch;
  619.     }
  620.     }
  621.     table = (unsigned char*)(littlestr->str_ptr + littlelen + 1);
  622. #else
  623.     table = Null(unsigned char*);
  624. #endif
  625.     if (--littlelen >= bigend - big)
  626.     return Nullch;
  627.     s = big + littlelen;
  628.     oldlittle = little = table - 2;
  629.     if (littlestr->str_pok & SP_CASEFOLD) {    /* case insensitive? */
  630.     if (s < bigend) {
  631.       top1:
  632.         /*SUPPRESS 560*/
  633.         if (tmp = table[*s]) {
  634. #ifdef POINTERRIGOR
  635.         if (bigend - s > tmp) {
  636.             s += tmp;
  637.             goto top1;
  638.         }
  639. #else
  640.         if ((s += tmp) < bigend)
  641.             goto top1;
  642. #endif
  643.         return Nullch;
  644.         }
  645.         else {
  646.         tmp = littlelen;    /* less expensive than calling strncmp() */
  647.         olds = s;
  648.         while (tmp--) {
  649.             if (*--s == *--little || fold[*s] == *little)
  650.             continue;
  651.             s = olds + 1;    /* here we pay the price for failure */
  652.             little = oldlittle;
  653.             if (s < bigend)    /* fake up continue to outer loop */
  654.             goto top1;
  655.             return Nullch;
  656.         }
  657. #ifndef lint
  658.         return (char *)s;
  659. #endif
  660.         }
  661.     }
  662.     }
  663.     else {
  664.     if (s < bigend) {
  665.       top2:
  666.         /*SUPPRESS 560*/
  667.         if (tmp = table[*s]) {
  668. #ifdef POINTERRIGOR
  669.         if (bigend - s > tmp) {
  670.             s += tmp;
  671.             goto top2;
  672.         }
  673. #else
  674.         if ((s += tmp) < bigend)
  675.             goto top2;
  676. #endif
  677.         return Nullch;
  678.         }
  679.         else {
  680.         tmp = littlelen;    /* less expensive than calling strncmp() */
  681.         olds = s;
  682.         while (tmp--) {
  683.             if (*--s == *--little)
  684.             continue;
  685.             s = olds + 1;    /* here we pay the price for failure */
  686.             little = oldlittle;
  687.             if (s < bigend)    /* fake up continue to outer loop */
  688.             goto top2;
  689.             return Nullch;
  690.         }
  691. #ifndef lint
  692.         return (char *)s;
  693. #endif
  694.         }
  695.     }
  696.     }
  697.     return Nullch;
  698. }
  699.  
  700. char *
  701. screaminstr(bigstr, littlestr)
  702. STR *bigstr;
  703. STR *littlestr;
  704. {
  705.     register unsigned char *s, *x;
  706.     register unsigned char *big;
  707.     register int pos;
  708.     register int previous;
  709.     register int first;
  710.     register unsigned char *little;
  711.     register unsigned char *bigend;
  712.     register unsigned char *littleend;
  713.  
  714.     if ((pos = screamfirst[littlestr->str_rare]) < 0) 
  715.     return Nullch;
  716. #ifndef lint
  717.     little = (unsigned char *)(littlestr->str_ptr);
  718. #else
  719.     little = Null(unsigned char *);
  720. #endif
  721.     littleend = little + littlestr->str_cur;
  722.     first = *little++;
  723.     previous = littlestr->str_state;
  724. #ifndef lint
  725.     big = (unsigned char *)(bigstr->str_ptr);
  726. #else
  727.     big = Null(unsigned char*);
  728. #endif
  729.     bigend = big + bigstr->str_cur;
  730.     while (pos < previous) {
  731. #ifndef lint
  732.     if (!(pos += screamnext[pos]))
  733. #endif
  734.         return Nullch;
  735.     }
  736. #ifdef POINTERRIGOR
  737.     if (littlestr->str_pok & SP_CASEFOLD) {    /* case insignificant? */
  738.     do {
  739.         if (big[pos-previous] != first && big[pos-previous] != fold[first])
  740.         continue;
  741.         for (x=big+pos+1-previous,s=little; s < littleend; /**/ ) {
  742.         if (x >= bigend)
  743.             return Nullch;
  744.         if (*s++ != *x++ && fold[*(s-1)] != *(x-1)) {
  745.             s--;
  746.             break;
  747.         }
  748.         }
  749.         if (s == littleend)
  750. #ifndef lint
  751.         return (char *)(big+pos-previous);
  752. #else
  753.         return Nullch;
  754. #endif
  755.     } while (
  756. #ifndef lint
  757.         pos += screamnext[pos]    /* does this goof up anywhere? */
  758. #else
  759.         pos += screamnext[0]
  760. #endif
  761.         );
  762.     }
  763.     else {
  764.     do {
  765.         if (big[pos-previous] != first)
  766.         continue;
  767.         for (x=big+pos+1-previous,s=little; s < littleend; /**/ ) {
  768.         if (x >= bigend)
  769.             return Nullch;
  770.         if (*s++ != *x++) {
  771.             s--;
  772.             break;
  773.         }
  774.         }
  775.         if (s == littleend)
  776. #ifndef lint
  777.         return (char *)(big+pos-previous);
  778. #else
  779.         return Nullch;
  780. #endif
  781.     } while (
  782. #ifndef lint
  783.         pos += screamnext[pos]
  784. #else
  785.         pos += screamnext[0]
  786. #endif
  787.         );
  788.     }
  789. #else /* !POINTERRIGOR */
  790.     big -= previous;
  791.     if (littlestr->str_pok & SP_CASEFOLD) {    /* case insignificant? */
  792.     do {
  793.         if (big[pos] != first && big[pos] != fold[first])
  794.         continue;
  795.         for (x=big+pos+1,s=little; s < littleend; /**/ ) {
  796.         if (x >= bigend)
  797.             return Nullch;
  798.         if (*s++ != *x++ && fold[*(s-1)] != *(x-1)) {
  799.             s--;
  800.             break;
  801.         }
  802.         }
  803.         if (s == littleend)
  804. #ifndef lint
  805.         return (char *)(big+pos);
  806. #else
  807.         return Nullch;
  808. #endif
  809.     } while (
  810. #ifndef lint
  811.         pos += screamnext[pos]    /* does this goof up anywhere? */
  812. #else
  813.         pos += screamnext[0]
  814. #endif
  815.         );
  816.     }
  817.     else {
  818.     do {
  819.         if (big[pos] != first)
  820.         continue;
  821.         for (x=big+pos+1,s=little; s < littleend; /**/ ) {
  822.         if (x >= bigend)
  823.             return Nullch;
  824.         if (*s++ != *x++) {
  825.             s--;
  826.             break;
  827.         }
  828.         }
  829.         if (s == littleend)
  830. #ifndef lint
  831.         return (char *)(big+pos);
  832. #else
  833.         return Nullch;
  834. #endif
  835.     } while (
  836. #ifndef lint
  837.         pos += screamnext[pos]
  838. #else
  839.         pos += screamnext[0]
  840. #endif
  841.         );
  842.     }
  843. #endif /* POINTERRIGOR */
  844.     return Nullch;
  845. }
  846.  
  847. /* copy a string to a safe spot */
  848.  
  849. char *
  850. savestr(str)
  851. char *str;
  852. {
  853.     register char *newaddr;
  854.  
  855.     New(902,newaddr,strlen(str)+1,char);
  856.     (void)strcpy(newaddr,str);
  857.     return newaddr;
  858. }
  859.  
  860. /* same thing but with a known length */
  861.  
  862. char *
  863. nsavestr(str, len)
  864. char *str;
  865. register int len;
  866. {
  867.     register char *newaddr;
  868.  
  869.     New(903,newaddr,len+1,char);
  870.     Copy(str,newaddr,len,char);        /* might not be null terminated */
  871.     newaddr[len] = '\0';        /* is now */
  872.     return newaddr;
  873. }
  874.  
  875. /* grow a static string to at least a certain length */
  876.  
  877. void
  878. growstr(strptr,curlen,newlen)
  879. char **strptr;
  880. int *curlen;
  881. int newlen;
  882. {
  883.     if (newlen > *curlen) {        /* need more room? */
  884.     if (*curlen)
  885.         Renew(*strptr,newlen,char);
  886.     else
  887.         New(905,*strptr,newlen,char);
  888.     *curlen = newlen;
  889.     }
  890. }
  891.  
  892. #ifdef macintosh
  893.  
  894. char gPseudoFileName[256];
  895.  
  896. char * MPWFileName(char * file)
  897. {
  898.     if (!strcmp(file, "Dev:Pseudo"))
  899.         return gPseudoFileName;
  900.     else if (!strncmp(file, "Dev:Pseudo:", 11))
  901.         return file + 11;
  902.     else
  903.         return file;
  904. }
  905.  
  906. char * MPWPosIndication(char * buf, char * file, long line)
  907. {
  908.     strcpy(buf, "File '"); 
  909.     buf += 6;
  910.     
  911.     file = MPWFileName(file);
  912.     
  913.     while (*file) 
  914.         if (*file == '\'') {
  915.         strcpy(buf, "'∂''");
  916.         buf += 3;
  917.         ++file;
  918.     } else
  919.         *buf++ = *file++;
  920.         
  921.     if (buf[-1] == '\'' && buf[-2] == '\'')
  922.         --buf;
  923.     else
  924.         *buf++ = '\'';
  925.  
  926.     return buf + sprintf(buf, "; Line %ld", line);
  927. }
  928. #endif
  929.  
  930. #ifndef I_VARARGS
  931. /*VARARGS1*/
  932. char *
  933. mess(pat,a1,a2,a3,a4)
  934. char *pat;
  935. long a1, a2, a3, a4;
  936. {
  937.     char *s;
  938.     int usermess = strEQ(pat,"%s");
  939.     STR *tmpstr;
  940.  
  941.     s = buf;
  942. #ifdef macintosh
  943.     if (usermess) {
  944.     tmpstr = str_mortal(&str_undef);
  945.     str_set(tmpstr, "# ");
  946.     str_cat(tmpstr, (char*)a1);
  947.     *s++ = tmpstr->str_ptr[tmpstr->str_cur-1];
  948.     }
  949.     else {
  950.         (void) sprintf(buf, "# ");
  951.         s += 2;
  952.     (void)sprintf(s,pat,a1,a2,a3,a4);
  953.     s += strlen(s);
  954.     }
  955. #else
  956.     if (usermess) {
  957.     tmpstr = str_mortal(&str_undef);
  958.     str_set(tmpstr, (char*)a1);
  959.     *s++ = tmpstr->str_ptr[tmpstr->str_cur-1];
  960.     }
  961.     else {
  962.     (void)sprintf(s,pat,a1,a2,a3,a4);
  963.     s += strlen(s);
  964.     }
  965. #endif
  966.  
  967.     if (s[-1] != '\n') {
  968. #ifdef macintosh
  969.     if (last_in_stab &&
  970.         stab_io(last_in_stab) &&
  971.         stab_io(last_in_stab)->lines ) {
  972.         if (s[-1] != '.')
  973.             *s++ = '.';
  974.         (void)sprintf(s," Input from <%s>, line %ld",
  975.           last_in_stab == argvstab ? "" : stab_name(last_in_stab),
  976.           (long)stab_io(last_in_stab)->lines);
  977.         s += strlen(s);
  978.     }
  979.     if (curcmd->c_line) {
  980.         s[0] = '\n';
  981.         s = MPWPosIndication(
  982.                 s+1, 
  983.             stab_val(curcmd->c_filestab)->str_ptr, 
  984.             (long)curcmd->c_line);
  985.     }
  986.     (void)strcpy(s,"\n");
  987. #else
  988.     if (curcmd->c_line) {
  989.         (void)sprintf(s," at %s line %ld",
  990.           stab_val(curcmd->c_filestab)->str_ptr, (long)curcmd->c_line);
  991.         s += strlen(s);
  992.     }
  993.     if (last_in_stab &&
  994.         stab_io(last_in_stab) &&
  995.         stab_io(last_in_stab)->lines ) {
  996.         (void)sprintf(s,", <%s> line %ld",
  997.           last_in_stab == argvstab ? "" : stab_ename(last_in_stab),
  998.           (long)stab_io(last_in_stab)->lines);
  999.         s += strlen(s);
  1000.     }
  1001.     (void)strcpy(s,".\n");
  1002. #endif
  1003.     if (usermess)
  1004.         str_cat(tmpstr,buf+1);
  1005.     }
  1006.     if (usermess)
  1007.     return tmpstr->str_ptr;
  1008.     else
  1009.     return buf;
  1010. }
  1011.  
  1012. /*VARARGS1*/
  1013. void fatal(pat,a1,a2,a3,a4)
  1014. char *pat;
  1015. long a1, a2, a3, a4;
  1016. {
  1017.     extern FILE *e_fp;
  1018.     extern char *e_tmpname;
  1019.     char *tmps;
  1020.     char *message;
  1021.  
  1022.     message = mess(pat,a1,a2,a3,a4);
  1023.     if (in_eval) {
  1024.     str_set(stab_val(stabent("@",TRUE)),message);
  1025.     tmps = "_EVAL_";
  1026.     while (loop_ptr >= 0 && (!loop_stack[loop_ptr].loop_label ||
  1027.       strNE(tmps,loop_stack[loop_ptr].loop_label) )) {
  1028. #ifdef DEBUGGING
  1029.         if (debug & 4) {
  1030.         deb("(Skipping label #%d %s)\n",loop_ptr,
  1031.             loop_stack[loop_ptr].loop_label);
  1032.         }
  1033. #endif
  1034.         loop_ptr--;
  1035.     }
  1036. #ifdef DEBUGGING
  1037.     if (debug & 4) {
  1038.         deb("(Found label #%d %s)\n",loop_ptr,
  1039.         loop_stack[loop_ptr].loop_label);
  1040.     }
  1041. #endif
  1042.     if (loop_ptr < 0) {
  1043.         in_eval = 0;
  1044.         fatal("Bad label: %s", tmps);
  1045.     }
  1046.     longjmp(loop_stack[loop_ptr].loop_env, 1);
  1047.     }
  1048.     fputs(message,stderr);
  1049.     (void)fflush(stderr);
  1050.     if (e_fp)
  1051.     (void)UNLINK(e_tmpname);
  1052.     statusvalue >>= 8;
  1053.     exit((int)((errno&255)?errno:((statusvalue&255)?statusvalue:255)));
  1054. }
  1055.  
  1056. /*VARARGS1*/
  1057. void warn(pat,a1,a2,a3,a4)
  1058. char *pat;
  1059. long a1, a2, a3, a4;
  1060. {
  1061.     char *message;
  1062.  
  1063.     message = mess(pat,a1,a2,a3,a4);
  1064.     fputs(message,stderr);
  1065. #ifdef LEAKTEST
  1066. #ifdef DEBUGGING
  1067.     if (debug & 4096)
  1068.     xstat();
  1069. #endif
  1070. #endif
  1071.     (void)fflush(stderr);
  1072. }
  1073. #else
  1074. /*VARARGS0*/
  1075. char *
  1076. mess(args)
  1077. va_list args;
  1078. {
  1079.     char *pat;
  1080.     char *s;
  1081.     STR *tmpstr;
  1082.     int usermess;
  1083. #ifndef HAS_VPRINTF
  1084. #ifdef CHARVSPRINTF
  1085.     char *vsprintf();
  1086. #else
  1087.     int vsprintf();
  1088. #endif
  1089. #endif
  1090.  
  1091. #ifdef lint
  1092.     pat = Nullch;
  1093. #else
  1094.     pat = va_arg(args, char *);
  1095. #endif
  1096.     s = buf;
  1097.     usermess = strEQ(pat, "%s");
  1098.     if (usermess) {
  1099.     tmpstr = str_mortal(&str_undef);
  1100.     str_set(tmpstr, va_arg(args, char *));
  1101.     *s++ = tmpstr->str_ptr[tmpstr->str_cur-1];
  1102.     }
  1103.     else {
  1104.     (void) vsprintf(s,pat,args);
  1105.     s += strlen(s);
  1106.     }
  1107.  
  1108.     if (s[-1] != '\n') {
  1109.     if (curcmd->c_line) {
  1110.         (void)sprintf(s," at %s line %ld",
  1111.           stab_val(curcmd->c_filestab)->str_ptr, (long)curcmd->c_line);
  1112.         s += strlen(s);
  1113.     }
  1114.     if (last_in_stab &&
  1115.         stab_io(last_in_stab) &&
  1116.         stab_io(last_in_stab)->lines ) {
  1117.         (void)sprintf(s,", <%s> line %ld",
  1118.           last_in_stab == argvstab ? "" : last_in_stab->str_magic->str_ptr,
  1119.           (long)stab_io(last_in_stab)->lines);
  1120.         s += strlen(s);
  1121.     }
  1122.     (void)strcpy(s,".\n");
  1123.     if (usermess)
  1124.         str_cat(tmpstr,buf+1);
  1125.     }
  1126.  
  1127.     if (usermess)
  1128.     return tmpstr->str_ptr;
  1129.     else
  1130.     return buf;
  1131. }
  1132.  
  1133. /*VARARGS0*/
  1134. void fatal(va_alist)
  1135. va_dcl
  1136. {
  1137.     va_list args;
  1138.     extern FILE *e_fp;
  1139.     extern char *e_tmpname;
  1140.     char *tmps;
  1141.     char *message;
  1142.  
  1143. #ifndef lint
  1144.     va_start(args);
  1145. #else
  1146.     args = 0;
  1147. #endif
  1148.     message = mess(args);
  1149.     va_end(args);
  1150.     if (in_eval) {
  1151.     str_set(stab_val(stabent("@",TRUE)),message);
  1152.     tmps = "_EVAL_";
  1153.     while (loop_ptr >= 0 && (!loop_stack[loop_ptr].loop_label ||
  1154.       strNE(tmps,loop_stack[loop_ptr].loop_label) )) {
  1155. #ifdef DEBUGGING
  1156.         if (debug & 4) {
  1157.         deb("(Skipping label #%d %s)\n",loop_ptr,
  1158.             loop_stack[loop_ptr].loop_label);
  1159.         }
  1160. #endif
  1161.         loop_ptr--;
  1162.     }
  1163. #ifdef DEBUGGING
  1164.     if (debug & 4) {
  1165.         deb("(Found label #%d %s)\n",loop_ptr,
  1166.         loop_stack[loop_ptr].loop_label);
  1167.     }
  1168. #endif
  1169.     if (loop_ptr < 0) {
  1170.         in_eval = 0;
  1171.         fatal("Bad label: %s", tmps);
  1172.     }
  1173.     longjmp(loop_stack[loop_ptr].loop_env, 1);
  1174.     }
  1175.     fputs(message,stderr);
  1176.     (void)fflush(stderr);
  1177.     if (e_fp)
  1178.     (void)UNLINK(e_tmpname);
  1179.     statusvalue >>= 8;
  1180.     exit((int)((errno&255)?errno:((statusvalue&255)?statusvalue:255)));
  1181. }
  1182.  
  1183. /*VARARGS0*/
  1184. void warn(va_alist)
  1185. va_dcl
  1186. {
  1187.     va_list args;
  1188.     char *message;
  1189.  
  1190. #ifndef lint
  1191.     va_start(args);
  1192. #else
  1193.     args = 0;
  1194. #endif
  1195.     message = mess(args);
  1196.     va_end(args);
  1197.  
  1198.     fputs(message,stderr);
  1199. #ifdef LEAKTEST
  1200. #ifdef DEBUGGING
  1201.     if (debug & 4096)
  1202.     xstat();
  1203. #endif
  1204. #endif
  1205.     (void)fflush(stderr);
  1206. }
  1207. #endif
  1208.  
  1209. void
  1210. my_setenv(nam,val)
  1211. char *nam, *val;
  1212. {
  1213.     register int i=envix(nam);        /* where does it go? */
  1214.  
  1215.     if (environ == origenviron) {    /* need we copy environment? */
  1216.     int j;
  1217.     int max;
  1218.     char **tmpenv;
  1219.  
  1220.     /*SUPPRESS 530*/
  1221.     for (max = i; environ[max]; max++) ;
  1222.     New(901,tmpenv, max+2, char*);
  1223.     for (j=0; j<max; j++)        /* copy environment */
  1224.         tmpenv[j] = savestr(environ[j]);
  1225.     tmpenv[max] = Nullch;
  1226.     environ = tmpenv;        /* tell exec where it is now */
  1227.     }
  1228.     if (!val) {
  1229.     while (environ[i]) {
  1230.         environ[i] = environ[i+1];
  1231.         i++;
  1232.     }
  1233.     return;
  1234.     }
  1235.     if (!environ[i]) {            /* does not exist yet */
  1236.     Renew(environ, i+2, char*);    /* just expand it a bit */
  1237.     environ[i+1] = Nullch;    /* make sure it's null terminated */
  1238.     }
  1239.     else
  1240.     Safefree(environ[i]);
  1241.     New(904, environ[i], strlen(nam) + strlen(val) + 2, char);
  1242. #ifndef MSDOS
  1243.     (void)sprintf(environ[i],"%s=%s",nam,val);/* all that work just for this */
  1244. #else
  1245.     /* MS-DOS requires environment variable names to be in uppercase */
  1246.     /* [Tom Dinger, 27 August 1990: Well, it doesn't _require_ it, but
  1247.      * some utilities and applications may break because they only look
  1248.      * for upper case strings. (Fixed strupr() bug here.)]
  1249.      */
  1250.     strcpy(environ[i],nam); strupr(environ[i]);
  1251.     (void)sprintf(environ[i] + strlen(nam),"=%s",val);
  1252. #endif /* MSDOS */
  1253. }
  1254.  
  1255. int
  1256. envix(nam)
  1257. char *nam;
  1258. {
  1259.     register int i, len = strlen(nam);
  1260.  
  1261.     for (i = 0; environ[i]; i++) {
  1262.     if (strnEQ(environ[i],nam,len) && environ[i][len] == '=')
  1263.         break;            /* strnEQ must come first to avoid */
  1264.     }                    /* potential SEGV's */
  1265.     return i;
  1266. }
  1267.  
  1268. #ifdef EUNICE
  1269. int
  1270. unlnk(f)    /* unlink all versions of a file */
  1271. char *f;
  1272. {
  1273.     int i;
  1274.  
  1275.     for (i = 0; unlink(f) >= 0; i++) ;
  1276.     return i ? 0 : -1;
  1277. }
  1278. #endif
  1279.  
  1280. #if !defined(HAS_BCOPY) || !defined(SAFE_BCOPY)
  1281. char *
  1282. my_bcopy(from,to,len)
  1283. register char *from;
  1284. register char *to;
  1285. register int len;
  1286. {
  1287.     char *retval = to;
  1288.  
  1289.     if (from - to >= 0) {
  1290.     while (len--)
  1291.         *to++ = *from++;
  1292.     }
  1293.     else {
  1294.     to += len;
  1295.     from += len;
  1296.     while (len--)
  1297.         *(--to) = *(--from);
  1298.     }
  1299.     return retval;
  1300. }
  1301. #endif
  1302.  
  1303. #if !defined(HAS_BZERO) && !defined(HAS_MEMSET)
  1304. char *
  1305. my_bzero(loc,len)
  1306. register char *loc;
  1307. register int len;
  1308. {
  1309.     char *retval = loc;
  1310.  
  1311.     while (len--)
  1312.     *loc++ = 0;
  1313.     return retval;
  1314. }
  1315. #endif
  1316.  
  1317. #ifndef HAS_MEMCMP
  1318. int
  1319. my_memcmp(s1,s2,len)
  1320. register unsigned char *s1;
  1321. register unsigned char *s2;
  1322. register int len;
  1323. {
  1324.     register int tmp;
  1325.  
  1326.     while (len--) {
  1327.     if (tmp = *s1++ - *s2++)
  1328.         return tmp;
  1329.     }
  1330.     return 0;
  1331. }
  1332. #endif /* HAS_MEMCMP */
  1333.  
  1334. #ifdef I_VARARGS
  1335. #ifndef HAS_VPRINTF
  1336.  
  1337. #ifdef CHARVSPRINTF
  1338. char *
  1339. #else
  1340. int
  1341. #endif
  1342. vsprintf(dest, pat, args)
  1343. char *dest, *pat, *args;
  1344. {
  1345.     FILE fakebuf;
  1346.  
  1347.     fakebuf._ptr = dest;
  1348.     fakebuf._cnt = 32767;
  1349. #ifndef _IOSTRG
  1350. #define _IOSTRG 0
  1351. #endif
  1352.     fakebuf._flag = _IOWRT|_IOSTRG;
  1353.     _doprnt(pat, args, &fakebuf);    /* what a kludge */
  1354.     (void)putc('\0', &fakebuf);
  1355. #ifdef CHARVSPRINTF
  1356.     return(dest);
  1357. #else
  1358.     return 0;        /* perl doesn't use return value */
  1359. #endif
  1360. }
  1361.  
  1362. #ifdef DEBUGGING
  1363. int
  1364. vfprintf(fd, pat, args)
  1365. FILE *fd;
  1366. char *pat, *args;
  1367. {
  1368.     _doprnt(pat, args, fd);
  1369.     return 0;        /* wrong, but perl doesn't use the return value */
  1370. }
  1371. #endif
  1372. #endif /* HAS_VPRINTF */
  1373. #endif /* I_VARARGS */
  1374.  
  1375. /*
  1376.  * I think my_swap(), htonl() and ntohl() have never been used.
  1377.  * perl.h contains last-chance references to my_swap(), my_htonl()
  1378.  * and my_ntohl().  I presume these are the intended functions;
  1379.  * but htonl() and ntohl() have the wrong names.  There are no
  1380.  * functions my_htonl() and my_ntohl() defined anywhere.
  1381.  * -DWS
  1382.  */
  1383. #ifdef MYSWAP
  1384. #if BYTEORDER != 0x4321
  1385. short
  1386. my_swap(s)
  1387. short s;
  1388. {
  1389. #if (BYTEORDER & 1) == 0
  1390.     short result;
  1391.  
  1392.     result = ((s & 255) << 8) + ((s >> 8) & 255);
  1393.     return result;
  1394. #else
  1395.     return s;
  1396. #endif
  1397. }
  1398.  
  1399. long
  1400. htonl(l)
  1401. register long l;
  1402. {
  1403.     union {
  1404.     long result;
  1405.     char c[sizeof(long)];
  1406.     } u;
  1407.  
  1408. #if BYTEORDER == 0x1234
  1409.     u.c[0] = (l >> 24) & 255;
  1410.     u.c[1] = (l >> 16) & 255;
  1411.     u.c[2] = (l >> 8) & 255;
  1412.     u.c[3] = l & 255;
  1413.     return u.result;
  1414. #else
  1415. #if ((BYTEORDER - 0x1111) & 0x444) || !(BYTEORDER & 0xf)
  1416.     fatal("Unknown BYTEORDER\n");
  1417. #else
  1418.     register int o;
  1419.     register int s;
  1420.  
  1421.     for (o = BYTEORDER - 0x1111, s = 0; s < (sizeof(long)*8); o >>= 4, s += 8) {
  1422.     u.c[o & 0xf] = (l >> s) & 255;
  1423.     }
  1424.     return u.result;
  1425. #endif
  1426. #endif
  1427. }
  1428.  
  1429. long
  1430. ntohl(l)
  1431. register long l;
  1432. {
  1433.     union {
  1434.     long l;
  1435.     char c[sizeof(long)];
  1436.     } u;
  1437.  
  1438. #if BYTEORDER == 0x1234
  1439.     u.c[0] = (l >> 24) & 255;
  1440.     u.c[1] = (l >> 16) & 255;
  1441.     u.c[2] = (l >> 8) & 255;
  1442.     u.c[3] = l & 255;
  1443.     return u.l;
  1444. #else
  1445. #if ((BYTEORDER - 0x1111) & 0x444) || !(BYTEORDER & 0xf)
  1446.     fatal("Unknown BYTEORDER\n");
  1447. #else
  1448.     register int o;
  1449.     register int s;
  1450.  
  1451.     u.l = l;
  1452.     l = 0;
  1453.     for (o = BYTEORDER - 0x1111, s = 0; s < (sizeof(long)*8); o >>= 4, s += 8) {
  1454.     l |= (u.c[o & 0xf] & 255) << s;
  1455.     }
  1456.     return l;
  1457. #endif
  1458. #endif
  1459. }
  1460.  
  1461. #endif /* BYTEORDER != 0x4321 */
  1462. #endif /* MYSWAP */
  1463.  
  1464. /*
  1465.  * Little-endian byte order functions - 'v' for 'VAX', or 'reVerse'.
  1466.  * If these functions are defined,
  1467.  * the BYTEORDER is neither 0x1234 nor 0x4321.
  1468.  * However, this is not assumed.
  1469.  * -DWS
  1470.  */
  1471.  
  1472. #define HTOV(name,type)                        \
  1473.     type                            \
  1474.     name (n)                        \
  1475.     register type n;                    \
  1476.     {                            \
  1477.         union {                        \
  1478.         type value;                    \
  1479.         char c[sizeof(type)];                \
  1480.         } u;                        \
  1481.         register int i;                    \
  1482.         register int s;                    \
  1483.         for (i = 0, s = 0; i < sizeof(u.c); i++, s += 8) {    \
  1484.         u.c[i] = (n >> s) & 0xFF;            \
  1485.         }                            \
  1486.         return u.value;                    \
  1487.     }
  1488.  
  1489. #define VTOH(name,type)                        \
  1490.     type                            \
  1491.     name (n)                        \
  1492.     register type n;                    \
  1493.     {                            \
  1494.         union {                        \
  1495.         type value;                    \
  1496.         char c[sizeof(type)];                \
  1497.         } u;                        \
  1498.         register int i;                    \
  1499.         register int s;                    \
  1500.         u.value = n;                    \
  1501.         n = 0;                        \
  1502.         for (i = 0, s = 0; i < sizeof(u.c); i++, s += 8) {    \
  1503.         n += (u.c[i] & 0xFF) << s;            \
  1504.         }                            \
  1505.         return n;                        \
  1506.     }
  1507.  
  1508. #if defined(HAS_HTOVS) && !defined(htovs)
  1509. HTOV(htovs,short)
  1510. #endif
  1511. #if defined(HAS_HTOVL) && !defined(htovl)
  1512. HTOV(htovl,long)
  1513. #endif
  1514. #if defined(HAS_VTOHS) && !defined(vtohs)
  1515. VTOH(vtohs,short)
  1516. #endif
  1517. #if defined(HAS_VTOHL) && !defined(vtohl)
  1518. VTOH(vtohl,long)
  1519. #endif
  1520.  
  1521. #ifndef MSMAC
  1522. FILE *
  1523. mypopen(cmd,mode)
  1524. char    *cmd;
  1525. char    *mode;
  1526. {
  1527.     int p[2];
  1528.     register int this, that;
  1529.     register int pid;
  1530.     STR *str;
  1531.     int doexec = strNE(cmd,"-");
  1532.  
  1533.     if (pipe(p) < 0)
  1534.     return Nullfp;
  1535.     this = (*mode == 'w');
  1536.     that = !this;
  1537. #ifdef TAINT
  1538.     if (doexec) {
  1539.     taintenv();
  1540.     taintproper("Insecure dependency in exec");
  1541.     }
  1542. #endif
  1543.     while ((pid = (doexec?vfork():fork())) < 0) {
  1544.     if (errno != EAGAIN) {
  1545.         close(p[this]);
  1546.         if (!doexec)
  1547.         fatal("Can't fork");
  1548.         return Nullfp;
  1549.     }
  1550.     sleep(5);
  1551.     }
  1552.     if (pid == 0) {
  1553. #define THIS that
  1554. #define THAT this
  1555.     close(p[THAT]);
  1556.     if (p[THIS] != (*mode == 'r')) {
  1557.         dup2(p[THIS], *mode == 'r');
  1558.         close(p[THIS]);
  1559.     }
  1560.     if (doexec) {
  1561. #if !defined(HAS_FCNTL) || !defined(F_SETFD)
  1562.         int fd;
  1563.  
  1564. #ifndef NOFILE
  1565. #define NOFILE 20
  1566. #endif
  1567.         for (fd = maxsysfd + 1; fd < NOFILE; fd++)
  1568.         close(fd);
  1569. #endif
  1570.         do_exec(cmd);    /* may or may not use the shell */
  1571.         warn("Can't exec \"%s\": %s", cmd, strerror(errno));
  1572.         _exit(1);
  1573.     }
  1574.     /*SUPPRESS 560*/
  1575.     if (tmpstab = stabent("$",allstabs))
  1576.         str_numset(STAB_STR(tmpstab),(double)getpid());
  1577.     forkprocess = 0;
  1578.     hclear(pidstatus, FALSE);    /* we have no children */
  1579.     return Nullfp;
  1580. #undef THIS
  1581. #undef THAT
  1582.     }
  1583.     do_execfree();    /* free any memory malloced by child on vfork */
  1584.     close(p[that]);
  1585.     if (p[that] < p[this]) {
  1586.     dup2(p[this], p[that]);
  1587.     close(p[this]);
  1588.     p[this] = p[that];
  1589.     }
  1590.     str = afetch(fdpid,p[this],TRUE);
  1591.     str->str_u.str_useful = pid;
  1592.     forkprocess = pid;
  1593.     return fdopen(p[this], mode);
  1594. }
  1595. #endif /* !MSMAC */
  1596.  
  1597. #ifdef NOTDEF
  1598. dumpfds(s)
  1599. char *s;
  1600. {
  1601.     int fd;
  1602.     struct stat tmpstatbuf;
  1603.  
  1604.     fprintf(stderr,"%s", s);
  1605.     for (fd = 0; fd < 32; fd++) {
  1606.     if (fstat(fd,&tmpstatbuf) >= 0)
  1607.         fprintf(stderr," %d",fd);
  1608.     }
  1609.     fprintf(stderr,"\n");
  1610. }
  1611. #endif
  1612.  
  1613. #ifndef HAS_DUP2
  1614. dup2(oldfd,newfd)
  1615. int oldfd;
  1616. int newfd;
  1617. {
  1618. #if defined(HAS_FCNTL) && defined(F_DUPFD)
  1619.     close(newfd);
  1620.     fcntl(oldfd, F_DUPFD, newfd);
  1621. #else
  1622.     int fdtmp[256];
  1623.     int fdx = 0;
  1624.     int fd;
  1625.  
  1626.     if (oldfd == newfd)
  1627.     return 0;
  1628.     close(newfd);
  1629.     while ((fd = dup(oldfd)) != newfd)    /* good enough for low fd's */
  1630.     fdtmp[fdx++] = fd;
  1631.     while (fdx > 0)
  1632.     close(fdtmp[--fdx]);
  1633. #endif
  1634. }
  1635. #endif
  1636.  
  1637. #ifndef MSMAC
  1638. int
  1639. mypclose(ptr)
  1640. FILE *ptr;
  1641. {
  1642. #ifdef VOIDSIG
  1643.     void (*hstat)(), (*istat)(), (*qstat)();
  1644. #else
  1645.     int (*hstat)(), (*istat)(), (*qstat)();
  1646. #endif
  1647.     int status;
  1648.     STR *str;
  1649.     int pid;
  1650.  
  1651.     str = afetch(fdpid,fileno(ptr),TRUE);
  1652.     pid = (int)str->str_u.str_useful;
  1653.     astore(fdpid,fileno(ptr),Nullstr);
  1654.     fclose(ptr);
  1655. #ifdef UTS
  1656.     if(kill(pid, 0) < 0) { return(pid); }   /* HOM 12/23/91 */
  1657. #endif
  1658.     hstat = signal(SIGHUP, SIG_IGN);
  1659.     istat = signal(SIGINT, SIG_IGN);
  1660.     qstat = signal(SIGQUIT, SIG_IGN);
  1661.     pid = wait4pid(pid, &status, 0);
  1662.     signal(SIGHUP, hstat);
  1663.     signal(SIGINT, istat);
  1664.     signal(SIGQUIT, qstat);
  1665.     return(pid < 0 ? pid : status);
  1666. }
  1667.  
  1668. int
  1669. wait4pid(pid,statusp,flags)
  1670. int pid;
  1671. int *statusp;
  1672. int flags;
  1673. {
  1674. #if !defined(HAS_WAIT4) && !defined(HAS_WAITPID)
  1675.     int result;
  1676.     STR *str;
  1677.     char spid[16];
  1678. #endif
  1679.  
  1680.     if (!pid)
  1681.     return -1;
  1682. #ifdef HAS_WAIT4
  1683.     return wait4((pid==-1)?0:pid,statusp,flags,Null(struct rusage *));
  1684. #else
  1685. #ifdef HAS_WAITPID
  1686.     return waitpid(pid,statusp,flags);
  1687. #else
  1688.     if (pid > 0) {
  1689.     sprintf(spid, "%d", pid);
  1690.     str = hfetch(pidstatus,spid,strlen(spid),FALSE);
  1691.     if (str != &str_undef) {
  1692.         *statusp = (int)str->str_u.str_useful;
  1693.         hdelete(pidstatus,spid,strlen(spid));
  1694.         return pid;
  1695.     }
  1696.     }
  1697.     else {
  1698.     HENT *entry;
  1699.  
  1700.     hiterinit(pidstatus);
  1701.     if (entry = hiternext(pidstatus)) {
  1702.         pid = atoi(hiterkey(entry,statusp));
  1703.         str = hiterval(pidstatus,entry);
  1704.         *statusp = (int)str->str_u.str_useful;
  1705.         sprintf(spid, "%d", pid);
  1706.         hdelete(pidstatus,spid,strlen(spid));
  1707.         return pid;
  1708.     }
  1709.     }
  1710.     if (flags)
  1711.     fatal("Can't do waitpid with flags");
  1712.     else {
  1713.     while ((result = wait(statusp)) != pid && pid > 0 && result >= 0)
  1714.         pidgone(result,*statusp);
  1715.     if (result < 0)
  1716.         *statusp = -1;
  1717.     }
  1718.     return result;
  1719. #endif
  1720. #endif
  1721. }
  1722. #endif /* !MSMAC */
  1723.  
  1724. void
  1725. /*SUPPRESS 590*/
  1726. pidgone(pid,status)
  1727. int pid;
  1728. int status;
  1729. {
  1730. #if defined(HAS_WAIT4) || defined(HAS_WAITPID)
  1731. #else
  1732.     register STR *str;
  1733.     char spid[16];
  1734.  
  1735.     sprintf(spid, "%d", pid);
  1736.     str = hfetch(pidstatus,spid,strlen(spid),TRUE);
  1737.     str->str_u.str_useful = status;
  1738. #endif
  1739.     return;
  1740. }
  1741.  
  1742. #ifndef HAS_MEMCMP
  1743. memcmp(s1,s2,len)
  1744. register unsigned char *s1;
  1745. register unsigned char *s2;
  1746. register int len;
  1747. {
  1748.     register int tmp;
  1749.  
  1750.     while (len--) {
  1751.     if (tmp = *s1++ - *s2++)
  1752.         return tmp;
  1753.     }
  1754.     return 0;
  1755. }
  1756. #endif /* HAS_MEMCMP */
  1757.  
  1758. void
  1759. repeatcpy(to,from,len,count)
  1760. register char *to;
  1761. register char *from;
  1762. int len;
  1763. register int count;
  1764. {
  1765.     register int todo;
  1766.     register char *frombase = from;
  1767.  
  1768.     if (len == 1) {
  1769.     todo = *from;
  1770.     while (count-- > 0)
  1771.         *to++ = todo;
  1772.     return;
  1773.     }
  1774.     while (count-- > 0) {
  1775.     for (todo = len; todo > 0; todo--) {
  1776.         *to++ = *from++;
  1777.     }
  1778.     from = frombase;
  1779.     }
  1780. }
  1781.  
  1782. #ifndef CASTNEGFLOAT
  1783. unsigned long
  1784. castulong(f)
  1785. double f;
  1786. {
  1787.     long along;
  1788.  
  1789. #if CASTFLAGS & 2
  1790. #   define BIGDOUBLE 2147483648.0
  1791.     if (f >= BIGDOUBLE)
  1792.     return (unsigned long)(f-(long)(f/BIGDOUBLE)*BIGDOUBLE)|0x80000000;
  1793. #endif
  1794.     if (f >= 0.0)
  1795.     return (unsigned long)f;
  1796.     along = (long)f;
  1797.     return (unsigned long)along;
  1798. }
  1799. #endif
  1800.  
  1801. #ifndef HAS_RENAME
  1802. int
  1803. same_dirent(a,b)
  1804. char *a;
  1805. char *b;
  1806. {
  1807.     char *fa = rindex(a,'/');
  1808.     char *fb = rindex(b,'/');
  1809.     struct stat tmpstatbuf1;
  1810.     struct stat tmpstatbuf2;
  1811. #ifndef MAXPATHLEN
  1812. #define MAXPATHLEN 1024
  1813. #endif
  1814.     char tmpbuf[MAXPATHLEN+1];
  1815.  
  1816.     if (fa)
  1817.     fa++;
  1818.     else
  1819.     fa = a;
  1820.     if (fb)
  1821.     fb++;
  1822.     else
  1823.     fb = b;
  1824.     if (strNE(a,b))
  1825.     return FALSE;
  1826.     if (fa == a)
  1827.     strcpy(tmpbuf,".");
  1828.     else
  1829.     strncpy(tmpbuf, a, fa - a);
  1830.     if (stat(tmpbuf, &tmpstatbuf1) < 0)
  1831.     return FALSE;
  1832.     if (fb == b)
  1833.     strcpy(tmpbuf,".");
  1834.     else
  1835.     strncpy(tmpbuf, b, fb - b);
  1836.     if (stat(tmpbuf, &tmpstatbuf2) < 0)
  1837.     return FALSE;
  1838.     return tmpstatbuf1.st_dev == tmpstatbuf2.st_dev &&
  1839.        tmpstatbuf1.st_ino == tmpstatbuf2.st_ino;
  1840. }
  1841. #endif /* !HAS_RENAME */
  1842.  
  1843. unsigned long
  1844. scanoct(start, len, retlen)
  1845. char *start;
  1846. int len;
  1847. int *retlen;
  1848. {
  1849.     register char *s = start;
  1850.     register unsigned long retval = 0;
  1851.  
  1852.     while (len-- && *s >= '0' && *s <= '7') {
  1853.     retval <<= 3;
  1854.     retval |= *s++ - '0';
  1855.     }
  1856.     *retlen = s - start;
  1857.     return retval;
  1858. }
  1859.  
  1860. unsigned long
  1861. scanhex(start, len, retlen)
  1862. char *start;
  1863. int len;
  1864. int *retlen;
  1865. {
  1866.     register char *s = start;
  1867.     register unsigned long retval = 0;
  1868.     char *tmp;
  1869.  
  1870.     while (len-- && *s && (tmp = index(hexdigit, *s))) {
  1871.     retval <<= 4;
  1872.     retval |= (tmp - hexdigit) & 15;
  1873.     s++;
  1874.     }
  1875.     *retlen = s - start;
  1876.     return retval;
  1877. }
  1878.  
  1879. void init_util()
  1880. {
  1881. #ifndef safemalloc
  1882. #ifdef DEBUGGING
  1883.     an = 0;
  1884. #endif
  1885. #endif
  1886.     gPseudoFileName[0] = 0;
  1887. }