home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-387-Vol-3of3.iso / c / cpio11as.zip / UTIL.C < prev    next >
C/C++ Source or Header  |  1992-02-22  |  20KB  |  740 lines

  1. /* util.c - Several utility routines for cpio.
  2.    Copyright (C) 1988, 1989, 1990 Free Software Foundation, Inc.
  3.  
  4.    This program is free software; you can redistribute it and/or modify
  5.    it under the terms of the GNU General Public License as published by
  6.    the Free Software Foundation; either version 1, or (at your option)
  7.    any later version.
  8.  
  9.    This program is distributed in the hope that it will be useful,
  10.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.    GNU General Public License for more details.
  13.  
  14.    You should have received a copy of the GNU General Public License
  15.    along with this program; if not, write to the Free Software
  16.    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  17.  
  18. /* MS-DOS port (c) 1990 by Thorsten Ohl, ohl@gnu.ai.mit.edu
  19.    This port is also distributed under the terms of the
  20.    GNU General Public License as published by the
  21.    Free Software Foundation.
  22.  
  23.    Please note that this file is not identical to the
  24.    original GNU release, you should have received this
  25.    code as patch to the official release.
  26.  
  27.    $Header: e:/gnu/cpio/RCS/util.c 1.1.0.2 90/09/23 23:11:12 tho Exp $
  28.  */
  29.  
  30. #include <stdio.h>
  31. #include <pwd.h>
  32. #ifndef MSDOS
  33. #include <grp.h>
  34. #endif
  35. #include <sys/types.h>
  36. #include <sys/stat.h>
  37. #include <errno.h>
  38. #ifndef MSDOS            /* sigh, it's `volatile' !!! */
  39. extern int errno;
  40. #include <sys/ioctl.h>
  41. #endif
  42. #ifdef USG
  43. #include <string.h>
  44. #else
  45. #include <strings.h>
  46. #endif
  47. #ifndef MTIO_MISSING
  48. #include <sys/mtio.h>
  49. #endif
  50. #include "cpio.h"
  51. #include "extern.h"
  52.  
  53. #ifdef MSDOS
  54. #include <assert.h>
  55. extern int mkdir (char *, int);    /* we're cheating! */
  56. extern void fill_input_buffer (int in_des, int num_bytes);
  57. extern void tape_offline (int tape_des);
  58. extern char *xrealloc (char *ptr, unsigned int size);
  59. #endif /* MSDOS */
  60.  
  61. /* TRUE if the last buffer written by `empty_output_buffer' was a block
  62.    of zeros spaced forward with lseek. */
  63. static char last_write_made_hole;
  64.  
  65. /* Write `output_size' bytes of `output_buffer' to file
  66.    descriptor OUT_DES and reset `output_size' and `out_buff'.
  67.    Use lseek if possible.  */
  68.  
  69. void
  70. empty_output_buffer (out_des)
  71.      int out_des;
  72. {
  73.   LONG bytes_written;
  74.   char *cp;
  75.   int *ip;
  76.  
  77.   last_write_made_hole = FALSE;
  78.   if (output_is_seekable)
  79.     {
  80.       output_buffer[output_size] = 1; /* Sentinel to stop loop.  */
  81.  
  82.       /* Find first non-zero *word*, or the word with the sentinel.  */
  83.       ip = (int *) output_buffer;
  84.       while (*ip++ == 0)
  85.     ;
  86.  
  87.       /* Find the first non-zero *byte*, or the sentinel.  */
  88.       cp = (char *) (ip - 1);
  89.       while (*cp++ == '\0')
  90.     ;
  91.  
  92.       /* If we found the sentinel, the whole input block was zero,
  93.      and we can make a hole.  */
  94.       if (cp > &output_buffer[output_size])
  95.     {
  96.       last_write_made_hole = TRUE;
  97.       if (lseek (out_des, (long) output_size, 1) < 0L)
  98.         error (1, errno, "cannot seek on output");
  99.     }
  100.     }
  101.   if (last_write_made_hole == FALSE)
  102.     {
  103. #ifdef MSDOS
  104.       assert (output_size < 0xffffL);
  105.       bytes_written = write (out_des, output_buffer, (size_t) output_size);
  106. #else
  107.       bytes_written = write (out_des, output_buffer, output_size);
  108. #endif
  109.       if (bytes_written != output_size)
  110.     {
  111.       error (0, errno, "write error");
  112.       if (bytes_written == 0 && output_is_special)
  113.         {
  114.           get_next_reel (out_des);
  115. #ifdef MSDOS
  116.           bytes_written = write (out_des, output_buffer,
  117.                      (size_t) output_size);
  118. #else
  119.           bytes_written = write (out_des, output_buffer, output_size);
  120. #endif
  121.         }
  122.       if (bytes_written != output_size)
  123.         exit (1);
  124.     }
  125.     }
  126.   output_bytes += output_size;
  127.   out_buff = output_buffer;
  128.   output_size = 0;
  129. }
  130.  
  131. /* Read at most NUM_BYTES or `io_block_size' bytes, whichever is smaller,
  132.    into the start of `input_buffer' from file descriptor IN_DES.
  133.    Set `input_size' to the number of bytes read and reset `in_buff'.
  134.    Exit with an error if end of file is reached.  */
  135.  
  136. void
  137. fill_input_buffer (in_des, num_bytes)
  138.      int in_des;
  139.      int num_bytes;
  140. {
  141.   in_buff = input_buffer;
  142.   num_bytes = (num_bytes < io_block_size) ? num_bytes : io_block_size;
  143.   input_size = read (in_des, input_buffer, num_bytes);
  144.   if (input_size == 0)
  145.     {
  146.       error (0, 0, "premature end of file");
  147.       if (input_is_special)
  148.     {
  149.       get_next_reel (in_des);
  150.       /* Try again with a new tape. */
  151.       input_size = read (in_des, input_buffer, num_bytes);
  152.     }
  153.     }
  154.   if (input_size < 0)
  155.     error (1, errno, "read error");
  156.   if (input_size == 0)
  157.     exit (1);
  158.   input_bytes += input_size;
  159. }
  160.  
  161. /* If the file ends with a `hole', something needs to be written at
  162.    the end.  Otherwise the kernel would truncate the file at the end
  163.    of the last write operation.  */
  164.  
  165. void
  166. finish_output_file (path, out_des)
  167.      char *path;
  168.      int out_des;
  169. {
  170.   if (last_write_made_hole)
  171.     {
  172.       /* Seek backwards one character and write a null.  */
  173.       if (lseek (out_des, (off_t) -1, 1) < 0L
  174.       || write (out_des, "", 1) != 1)
  175.     {
  176.       error (0, errno, "%s", path);
  177.     }
  178.     }
  179. }
  180.  
  181. /* Copy NUM_BYTES of buffer IN_BUF to `out_buff', which may be partly full.
  182.    When `out_buff' fills up, flush it to file descriptor OUT_DES.  */
  183.  
  184. void
  185. copy_buf_out (in_buf, out_des, num_bytes)
  186.      char *in_buf;
  187.      int out_des;
  188.      LONG num_bytes;
  189. {
  190.   /* Copy the bytes one at a time, and empty the output buffer
  191.      when it is full.  */
  192.   while (num_bytes-- > 0)
  193.     {
  194.       if (output_size == io_block_size)
  195.     empty_output_buffer (out_des);
  196.       *(out_buff++) = *(in_buf++);
  197.       output_size++;
  198.     }
  199. }
  200.  
  201. /* Copy NUM_BYTES of buffer `in_buff' into IN_BUF.
  202.    `in_buff' may be partly full.
  203.    When `in_buff' is exhausted, refill it from file descriptor IN_DES.  */
  204.  
  205. void
  206. copy_in_buf (in_buf, in_des, num_bytes)
  207.      char *in_buf;
  208.      int in_des;
  209.      LONG num_bytes;
  210. {
  211.   while (num_bytes-- > 0)
  212.     {
  213.       if (input_size == 0)
  214.     fill_input_buffer (in_des, io_block_size);
  215.       *(in_buf++) = *(in_buff++);
  216.       input_size--;
  217.     }
  218. }
  219.  
  220. /*  Skip the next NUM_BYTES bytes of file descriptor IN_DES.
  221.     If possible, use lseek to keep from reading all the bytes;
  222.     otherwise (as from tapes), read all bytes. */
  223.  
  224. void
  225. toss_input (in_des, num_bytes)
  226.      int in_des;
  227.      LONG num_bytes;
  228. {
  229.   int num_toss;            /* Number of io_block_size blocks to toss.  */
  230.   int i;            /* Index for loop.  */
  231.  
  232.   if (num_bytes > input_size)
  233.     {
  234.       /* We need to toss more than what is currently in the input buffer.
  235.          Calculate the number of full blocks to toss and either seek past
  236.      them or read them from tape.  The last partial block is then read
  237.      and prepared for a toss of less than one full block.  */
  238.       num_bytes -= input_size;
  239. #ifdef MSDOS
  240.       num_toss = (int) (num_bytes / io_block_size);
  241. #else
  242.       num_toss = num_bytes / io_block_size;
  243. #endif
  244.       if (input_is_seekable)
  245.     {
  246.       /* We can seek past the blocks, saving input disk reads. */
  247. #ifdef MSDOS
  248.       if (lseek (in_des, (long) num_toss * (long) io_block_size, 1) < 0L)
  249. #else
  250.       if (lseek (in_des, (long) (num_toss * io_block_size), 1) < 0L)
  251. #endif
  252.         error (1, errno, "cannot seek on input");
  253.       input_bytes += num_toss * io_block_size;
  254.     }
  255.       else
  256.     {
  257.       /* The input is something that must be read serially. */
  258.       for (i = 1; i <= num_toss; i++)
  259.         fill_input_buffer (in_des, io_block_size);
  260.     }
  261. #ifdef MSDOS
  262.       num_bytes -= (long) num_toss * (long) io_block_size;
  263. #else
  264.       num_bytes -= num_toss * io_block_size;
  265. #endif
  266.       /* Get next block containing last "few" bytes to toss.  */
  267.       fill_input_buffer (in_des, io_block_size);
  268.     }
  269.  
  270.   /* Adjust to correct place in the current buffer full.  */
  271.   in_buff += num_bytes;
  272.   input_size -= num_bytes;
  273. }
  274.  
  275. /* Copy a file using the input and output buffers, which may start out
  276.    partly full.  After the copy, the files are not closed nor the last
  277.    block flushed to output, and the input buffer may still be partly
  278.    full.
  279.    IN_DES is the file descriptor for input;
  280.    OUT_DES is the file descriptor for output;
  281.    NUM_BYTES is the number of bytes to copy.  */
  282.  
  283. void
  284. copy_files (in_des, out_des, num_bytes)
  285.      int in_des;
  286.      int out_des;
  287.      LONG num_bytes;
  288. {
  289.   LONG size;
  290.  
  291.   while (num_bytes > 0)
  292.     {
  293.       if (input_size == 0)
  294.     fill_input_buffer (in_des, io_block_size);
  295.       size = (input_size < num_bytes) ? input_size : num_bytes;
  296.       copy_buf_out (in_buff, out_des, size);
  297.       num_bytes -= size;
  298.       input_size -= size;
  299.       in_buff += size;
  300.     }
  301. }
  302.  
  303. /* Create all directories up to but not including the last part of NAME.
  304.    Do not destroy any nondirectories while creating directories.  */
  305.  
  306. void
  307. create_all_directories (name)
  308.      char *name;
  309. {
  310.   char *dir;            /* Working directory name.  */
  311.   int i;            /* Index into NAME.  */
  312.   int name_len;            /* Length of NAME.  */
  313.   struct stat f_stat;        /* File stat for directory.  */
  314.  
  315.   name_len = strlen (name);
  316.   for (i = name_len - 1; i >= 0 && name[i] != '/'; i--)
  317.     /* Do nothing. */ ;
  318.  
  319.   /* If there are no directories in the string, just return. */
  320.   if (i < 0)
  321.     return;
  322.  
  323.   dir = copystring (name);
  324.   dir[i + 1] = '\0';
  325.   name_len = i + 1;
  326.  
  327.   /* Process the directories starting with the first one after the root:
  328.      replace sucessive '/' with a zero byte while making sure that that
  329.      directory exists.  Then set the zero byte back to '/'.  */
  330.   for (i = 1; i < name_len; i++)
  331.     {
  332.       if (dir[i] == '/')
  333.     {
  334.       dir[i] = '\0';
  335.       /* Use stat instead of lstat because symlinks to directories
  336.          are just as good as directories here.  */
  337.       if (stat (dir, &f_stat) < 0)
  338.         {
  339.           /* File does not exist, create the directory. */
  340.           if (mkdir (dir, 0777) < 0)
  341.         {
  342.           error (0, errno, "%s", dir);
  343.           free (dir);
  344.           return;
  345.         }
  346.           else if (verbose_flag)
  347.         error (0, 0, "created directory %s", dir);
  348.         }
  349.       else if ((f_stat.st_mode & S_IFMT) != S_IFDIR)
  350.         {
  351.           error (0, 0, "%s exists but is not a directory", dir);
  352.           free (dir);
  353.           return;
  354.         }
  355.       dir[i] = '/';
  356.     }
  357.     }
  358.  
  359.   free (dir);
  360. }
  361.  
  362. /* Return a newly allocated copy of STRING. */
  363.  
  364. char *
  365. copystring (string)
  366.      char *string;
  367. {
  368.   return strcpy ((char *) xmalloc (strlen (string) + 1), string);
  369. }
  370.  
  371. struct userid
  372. {
  373.   int uid;
  374.   char *name;
  375.   struct userid *next;
  376. };
  377.  
  378. struct userid *user_alist;
  379.  
  380. /* Translate `uid' to a login name, with cache.  */
  381.  
  382. char *
  383. getuser (uid)
  384.      int uid;
  385. {
  386.   register struct userid *tail;
  387.   struct passwd *pwent;
  388.   char usernum_string[20];
  389.  
  390.   for (tail = user_alist; tail; tail = tail->next)
  391.     if (tail->uid == uid)
  392.       return tail->name;
  393.  
  394.   pwent = getpwuid (uid);
  395.   tail = (struct userid *) xmalloc (sizeof (struct userid));
  396.   tail->uid = uid;
  397.   tail->next = user_alist;
  398.   if (pwent == 0)
  399.     {
  400.       sprintf (usernum_string, "%d", uid);
  401.       tail->name = copystring (usernum_string);
  402.     }
  403.   else
  404.     tail->name = copystring (pwent->pw_name);
  405.   user_alist = tail;
  406.   return tail->name;
  407. }
  408.  
  409. /* We use the same struct as for userids.  */
  410. struct userid *group_alist;
  411.  
  412. /* Translate `gid' to a group name, with cache.  */
  413.  
  414. char *
  415. getgroup (gid)
  416.      int gid;
  417. {
  418.   register struct userid *tail;
  419.   struct group *grent;
  420.   char groupnum_string[20];
  421.  
  422.   for (tail = group_alist; tail; tail = tail->next)
  423.     if (tail->uid == gid)
  424.       return tail->name;
  425.  
  426.   grent = getgrgid (gid);
  427.   tail = (struct userid *) xmalloc (sizeof (struct userid));
  428.   tail->uid = gid;
  429.   tail->next = group_alist;
  430.   if (grent == 0)
  431.     {
  432.       sprintf (groupnum_string, "%u", gid);
  433.       tail->name = copystring (groupnum_string);
  434.     }
  435.   else
  436.     tail->name = copystring (grent->gr_name);
  437.   group_alist = tail;
  438.   return tail->name;
  439. }
  440.  
  441. /* Support for remembering inodes with multiple links.  Used in the
  442.    "copy in" and "copy pass" modes for making links instead of copying
  443.    the file.  */
  444.  
  445. struct inode_val
  446. {
  447.   unsigned inode;
  448.   char *file_name;
  449. };
  450.  
  451. /* Inode hash table.  Allocated by first call to add_inode. */
  452. static struct inode_val **hash_table = NULL;
  453.  
  454. /* Size of current hash table.  Initial size is 47.  (47 = 2*22 + 3) */
  455. static int hash_size = 22;
  456.  
  457. /* Number of elements in current hash table. */
  458. static int hash_num;
  459.  
  460. /* Find the file name associated with NODE_NUM.  If there is no file
  461.    associated with NODE_NUM, return NULL. */
  462.  
  463. char *
  464. find_inode_file (node_num)
  465.      unsigned node_num;
  466. {
  467.   int start;            /* Initial hash location.  */
  468.   int temp;            /* Rehash search variable.  */
  469.  
  470.   if (hash_table != NULL)
  471.     {
  472.       /* Hash function is node number modulo the table size. */
  473.       start = node_num % hash_size;
  474.  
  475.       /* Initial look into the table.  */
  476.       if (hash_table[start] == NULL)
  477.     return NULL;
  478.       if (hash_table[start]->inode == node_num)
  479.     return hash_table[start]->file_name;
  480.  
  481.       /* The home position is full with a different inode record.
  482.      Do a linear search terminated by a NULL pointer.  */
  483.       for (temp = start + 1; hash_table[temp] != NULL && temp != start;
  484.        temp = (temp + 1) % hash_size)
  485.     {
  486.       if (hash_table[temp]->inode == node_num)
  487.         return hash_table[temp]->file_name;
  488.     }
  489.     }
  490.  
  491.   return NULL;
  492. }
  493.  
  494. /* Associate FILE_NAME with the inode NODE_NUM.  (Insert into hash table.) */
  495.  
  496. void
  497. add_inode (node_num, file_name)
  498.      unsigned short node_num;
  499.      char *file_name;
  500. {
  501.   struct inode_val *temp;
  502.  
  503.   /* Create new inode record.  */
  504.   temp = (struct inode_val *) xmalloc (sizeof (struct inode_val));
  505.   temp->inode = node_num;
  506.   temp->file_name = copystring (file_name);
  507.  
  508.   /* Do we have to increase the size of (or initially allocate)
  509.      the hash table?  */
  510.   if (hash_num == hash_size || hash_table == NULL)
  511.     {
  512.       struct inode_val **old_table;    /* Pointer to old table. */
  513.       int i;            /* Index for re-insert loop. */
  514.  
  515.       /* Save old table.  */
  516.       old_table = hash_table;
  517.       if (old_table == NULL)
  518.     hash_num = 0;
  519.  
  520.       /* Calculate new size of table and allocate it.
  521.          Sequence of table sizes is 47, 97, 197, 397, 797, 1597, 3197, 6397 ...
  522.      where 3197 and most of the sizes after 6397 are not prime.  The other
  523.      numbers listed are prime.  */
  524.       hash_size = 2 * hash_size + 3;
  525.       hash_table = (struct inode_val **)
  526.     xmalloc (hash_size * sizeof (struct inode_val *));
  527.  
  528.       /* Insert the values from the old table into the new table.  */
  529.       for (i = 0; i < hash_num; i++)
  530.     hash_insert (old_table[i]);
  531.  
  532.       if (old_table != NULL)
  533.     free (old_table);
  534.     }
  535.  
  536.   /* Insert the new record and increment the count of elements in the
  537.       hash table. */
  538.   hash_insert (temp);
  539.   hash_num++;
  540. }
  541.  
  542. /* Do the hash insert.  Used in normal inserts and resizing the hash
  543.    table.  It is guaranteed that there is room to insert the item.
  544.    NEW_VALUE is the pointer to the previously allocated inode, file
  545.    name association record.  */
  546.  
  547. void
  548. hash_insert (new_value)
  549.      struct inode_val *new_value;
  550. {
  551.   int start;            /* Home position for the value.  */
  552.   int temp;            /* Used for rehashing. */
  553.  
  554.   /* Hash function is node number modulo the table size. */
  555.   start = new_value->inode % hash_size;
  556.  
  557.   /* Do the initial look into the table.  */
  558.   if (hash_table[start] == NULL)
  559.     {
  560.       hash_table[start] = new_value;
  561.       return;
  562.     }
  563.  
  564.   /* If we get to here, the home position is full with a different inode
  565.      record.  Do a linear search for the first NULL pointer and insert
  566.      the new item there.  */
  567.   temp = (start + 1) % hash_size;
  568.   while (hash_table[temp] != NULL)
  569.     temp = (temp + 1) % hash_size;
  570.  
  571.   /* Insert at the NULL. */
  572.   hash_table[temp] = new_value;
  573. }
  574.  
  575. /* Attempt to rewind the tape drive on file descriptor TAPE_DES
  576.    and take it offline.  */
  577.  
  578. void
  579. tape_offline (tape_des)
  580.      int tape_des;
  581. {
  582. #ifndef MTIO_MISSING
  583.   struct mtop control;
  584.  
  585.   control.mt_op = MTOFFL;
  586.   control.mt_count = 1;
  587.   ioctl (tape_des, MTIOCTOP, &control);    /* Don't care if it fails.  */
  588. #endif
  589. }
  590.  
  591. /* The file on file descriptor TAPE_DESC is assumed to be magnetic tape
  592.    and the end of the tape has been reached.
  593.    Ask the user for to mount a new tape to continue the processing.  */
  594.  
  595. void
  596. get_next_reel (tape_des)
  597.      int tape_des;
  598. {
  599.   FILE *tty_in;            /* File for interacting with user.  */
  600.   FILE *tty_out;        /* File for interacting with user.  */
  601.   int c;
  602.  
  603.   /* Open files for interactive communication.  */
  604. #ifdef MSDOS
  605.   tty_in = fopen ("con", "r");
  606.   if (tty_in == NULL)
  607.     error (2, errno, "/dev/tty");
  608.   tty_out = fopen ("con", "w");
  609.   if (tty_out == NULL)
  610.     error (2, errno, "/dev/tty");
  611. #else /* not MSDOS */
  612.   tty_in = fopen ("/dev/tty", "r");
  613.   if (tty_in == NULL)
  614.     error (2, errno, "/dev/tty");
  615.   tty_out = fopen ("/dev/tty", "w");
  616.   if (tty_out == NULL)
  617.     error (2, errno, "/dev/tty");
  618. #endif /* not MSDOS */
  619.  
  620.   /* Give message and wait for carrage return.  User should hit carrage return
  621.      only after loading the next tape.  */
  622.   fprintf (tty_out, "Found end of tape.  Load next tape and press RETURN. ");
  623.   fflush (tty_out);
  624.  
  625.   tape_offline (tape_des);
  626.  
  627.   do
  628.     c = getc (tty_in);
  629.   while (c != EOF && c != '\n');
  630.  
  631.   fclose (tty_in);
  632.   fclose (tty_out);
  633. }
  634.  
  635. /* Like malloc but get fatal error if memory is exhausted.  */
  636.  
  637. char *
  638. xmalloc (size)
  639.      unsigned size;
  640. {
  641.   char *ptr = malloc (size);
  642.  
  643.   if (ptr == 0)
  644.     error (2, 0, "virtual memory exhausted");
  645.   return ptr;
  646. }
  647.  
  648. char *
  649. xrealloc (ptr, size)
  650.      char *ptr;
  651.      unsigned size;
  652. {
  653.   ptr = realloc (ptr, size);
  654.   if (ptr == 0)
  655.     error (2, 0, "virtual memory exhausted");
  656.   return ptr;
  657. }
  658.  
  659. #ifdef MKDIR_MISSING
  660. /* mkdir adapted from GNU tar. */
  661.  
  662. /* Make directory DPATH, with permission mode DMODE.
  663.  
  664.    Written by Robert Rother, Mariah Corporation, August 1985
  665.    (sdcsvax!rmr or rmr@uscd).  If you want it, it's yours.
  666.  
  667.    Severely hacked over by John Gilmore to make a 4.2BSD compatible
  668.    subroutine.    11Mar86; hoptoad!gnu
  669.  
  670.    Modified by rmtodd@uokmax 6-28-87 -- when making an already existing dir,
  671.    subroutine didn't return EEXIST.  It does now. */
  672.  
  673. int
  674. mkdir (dpath, dmode)
  675.      char *dpath;
  676.      int dmode;
  677. {
  678.   int cpid, status;
  679.   struct stat statbuf;
  680.  
  681.   if (stat (dpath, &statbuf) == 0)
  682.     {
  683.       errno = EEXIST;        /* stat worked, so it already exists. */
  684.       return -1;
  685.     }
  686.  
  687.   /* If stat fails for a reason other than non-existence, return error. */
  688.   if (errno != ENOENT)
  689.     return -1;
  690.  
  691.   cpid = fork ();
  692.   switch (cpid)
  693.     {
  694.     case -1:            /* Cannot fork. */
  695.       return -1;        /* errno is set already. */
  696.  
  697.     case 0:            /* Child process. */
  698.       /* Cheap hack to set mode of new directory.  Since this child
  699.      process is going away anyway, we zap its umask.
  700.      This won't suffice to set SUID, SGID, etc. on this
  701.      directory, so the parent process calls chmod afterward. */
  702.       status = umask (0);    /* Get current umask. */
  703.       umask (status | (0777 & ~dmode));    /* Set for mkdir. */
  704.       execl ("/bin/mkdir", "mkdir", dpath, (char *) 0);
  705.       _exit (1);
  706.  
  707.     default:            /* Parent process. */
  708.       while (wait (&status) != cpid) /* Wait for kid to finish. */
  709.     /* Do nothing. */ ;
  710.  
  711.       if (status & 0xFFFF)
  712.     {
  713.       errno = EIO;        /* /bin/mkdir failed. */
  714.       return -1;
  715.     }
  716.       return chmod (dpath, dmode);
  717.     }
  718. }
  719. #endif
  720.  
  721. #ifdef BCOPY_MISSING
  722. /* Copy COUNT bytes from SOURCE to DEST, handling overlaps properly.
  723.    Do not null-terminate. */
  724.  
  725. void
  726. bcopy (source, dest, count)
  727.      char *source, *dest;
  728.      unsigned count;
  729. {
  730.   if (source < dest)
  731.     /* Moving from low to high memory; start at end. */
  732.     for (source += count, dest += count; count > 0; --count)
  733.       *--dest = *--source;
  734.   else if (source != dest)
  735.     /* Moving from high to low memory; start at beginning. */
  736.     for (; count > 0; --count)
  737.       *dest++ = *source++;
  738. }
  739. #endif
  740.