home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 230.lha / PopCli_IV / copyproc.c < prev    next >
C/C++ Source or Header  |  1989-04-06  |  5KB  |  138 lines

  1. /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
  2. /* |_o_o|\\ Copyright (c) 1986 The Software Distillery.  All Rights Reserved */
  3. /* |. o.| || This program may not be distributed without the permission of   */
  4. /* | .  | || the authors.                                                    */
  5. /* | o  | ||    Dave Baker     Ed Burnette  Stan Chow    Jay Denebeim        */
  6. /* |  . |//     Gordon Keener  Jack Rouse   John Toebes  Doug Walker         */
  7. /* ======          BBS:(919)-471-6436      VOICE:(919)-469-4210              */ 
  8. /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
  9. /*
  10.  * VERY loosely based on the input.device example by Rob Peck, 12/1/85
  11.  */
  12.  
  13. /*
  14.  * copyproc() copies over the process information from one process (typically
  15.  * a parent) to another process (typically a child). Pass it the pointers
  16.  * to the two processes, and let copyproc() do the rest.
  17.  */
  18.  
  19. #include "popcli.h"
  20. #include <rexx/storage.h>
  21.  
  22. /* Dos constants. Don't ask about the 64. */
  23. #define CLISIZE      sizeof(struct CommandLineInterface)
  24. #define STRINGSIZE   64
  25.  
  26. /* BE SURE TO CALL termproc() WHEN PROCESS "to" TERMINATES!!!!! */
  27.  
  28. static BPTR DosAlloc( GLOBAL_DATA *, long );
  29.  
  30. void copyproc( gptr, to, from )
  31. GLOBAL_DATA *gptr;
  32. register struct Process *to;
  33. register struct Process *from;
  34. {
  35.    register struct CommandLineInterface  *tocli;
  36.    register struct CommandLineInterface  *fromcli;
  37.  
  38.    struct CDIR *current, *new;
  39.  
  40.    long         tostr;
  41.  
  42.    /* We need to copy over the information from the process which started */
  43.    /* us. Currently we will copy:                                         */
  44.    /*   Stack size                                                        */
  45.    /*   Path                                                              */
  46.    /*   Prompt                                                            */
  47.    /*   Default Directory                                                 */
  48.    /*   Fail Level                                                        */
  49.  
  50.    /* First, the current directory. Easy-peasy. */
  51.    CurrentDir( DupLock( from->pr_CurrentDir ) );
  52.  
  53.    /* See if we have any CLI info to worry about. */
  54.    fromcli = (struct CommandLineInterface *)(from->pr_CLI << 2);
  55.    if (fromcli == NULL)
  56.    {
  57.       to->pr_CLI = NULL;
  58.       return;
  59.    }
  60.  
  61.    /* Allocate the new CLI structure and prompt structure */
  62.    tocli = (struct CommandLineInterface *)
  63.                      (DosAlloc( gptr, CLISIZE + (2 * STRINGSIZE) ) << 2);
  64.    memset( (char *)tocli, 0, CLISIZE );
  65.    to->pr_CLI = ((long)tocli) >> 2;
  66.  
  67.    /* Copy the prompt structure to the new block if the old CLI structure */
  68.    /* had a prompt field.                                                 */
  69.    if (fromcli->cli_Prompt)
  70.    {
  71.       tostr = ((long)tocli) + CLISIZE;
  72.       memcpy( (char *)tostr, (char *)(fromcli->cli_Prompt << 2), STRINGSIZE );
  73.       tocli->cli_Prompt = tostr >> 2;
  74.    }
  75.  
  76.    /* Copy the Current Working Directory Name block, if it exists */
  77.    if (fromcli->cli_SetName)
  78.    {
  79.       tostr = ((long)tocli) + CLISIZE + STRINGSIZE;
  80.       memcpy( (char *)tostr, (char *)(fromcli->cli_SetName << 2), STRINGSIZE );
  81.       tocli->cli_SetName = tostr >> 2;
  82.    }
  83.       
  84.    tocli->cli_FailLevel    = fromcli->cli_FailLevel;
  85.    tocli->cli_DefaultStack = fromcli->cli_DefaultStack;
  86.  
  87.    /* Now, copy the path. Hardy-pardy. These things are chained with */
  88.    /* (ugh) BPTRs in a strange arrangement described in the CDIR     */
  89.    /* structure above. We merely go thru, allocating new CDIRs and   */
  90.    /* DupLock()ing.                                                  */
  91.  
  92.    for (current = (struct CDIR *)(fromcli->cli_CommandDir << 2), new = NULL;
  93.         current != NULL;
  94.         current = (struct CDIR *)(current->next << 2))
  95.    {
  96.       if (new == NULL)
  97.       {
  98.          /* Put the first block in the CLI structure. */
  99.          tocli->cli_CommandDir = DosAlloc( gptr, sizeof(struct CDIR) );
  100.          new = (struct CDIR *)(tocli->cli_CommandDir << 2);
  101.       }
  102.       else
  103.       {
  104.          /* Thereafter, new blocks get chained on the previous one.  */
  105.          new->next = DosAlloc( gptr, sizeof(struct CDIR) );
  106.          new = (struct CDIR *)(new->next << 2);
  107.       }
  108.  
  109.       new->lock = DupLock( current->lock );
  110.       new->next = NULL;  /* just in case this is the last one */
  111.    }
  112. }
  113.  
  114. /*
  115.  * Allocate block & remember the length.
  116.  * Blocks should be freed with DosFree, in termproc.c
  117.  */
  118.  
  119. static BPTR DosAlloc( gptr, size )
  120. GLOBAL_DATA *gptr;
  121. register long size;
  122. {
  123.    register long    *new;
  124.  
  125.    size += 4;
  126.  
  127.    new = (long *)AllocMem( size, 0 );
  128.  
  129. /*
  130.    if (new == NULL)
  131.       barf();
  132. */
  133.  
  134.    *new++ = size;
  135.  
  136.    return( ((long)new) >> 2 );
  137. }
  138.