home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / listings / v_08_02 / 8n02118a < prev    next >
Text File  |  1990-03-01  |  2KB  |  82 lines

  1. *****Listing 3*****
  2.  
  3. /*
  4.  *    RET.C: Return to previous working directory
  5.  *  Written by Leor Zolman, 9/89
  6.  *
  7.  *        (companion to CDE.C)
  8.  *    Uses the Master Environment package from CUJ 7/89
  9.  *
  10.  *  Usage:
  11.  *    ret                    (returns to previous directory)
  12.  *
  13.  *    Compile/Link:
  14.  *        cl /Ox ret.c util.c envlib    (ENVLIB.OBJ is Master Environment pkg)
  15.  */
  16.  
  17. #include    <stdio.h>
  18. #include    <string.h>
  19. #include    <stdlib.h>
  20. #include    <dos.h>
  21. #include    "util.h"
  22.  
  23. main(int argc, char **argv)
  24. {
  25.     char *pathp;
  26.     char cwdbuf[MAX_DIRNAME_SIZE];
  27.  
  28.     int        chaincnt;
  29.     char    chnevar1[MAX_EVARNAME_SIZE],    /* env var names built here        */
  30.             chnevar2[MAX_EVARNAME_SIZE];
  31.     char    chndname_save[MAX_DIRNAME_SIZE], *chndname;
  32.     char    itoabuf[10];                    /* used by itoa() function        */
  33.     int i;
  34.  
  35.                                 /* Get current dir. name and current drive:    */
  36.     getcwd(cwdbuf, MAX_DIRNAME_SIZE);
  37.  
  38.     if (argc != 1)
  39.         error("Usage: ret     (returns to last dir cde'd from)");
  40.  
  41.     if ((pathp = m_getenv(CHAINS_VAR)) == NULL)
  42.         error("cde hasn't been run yet");
  43.     else
  44.         chaincnt = atoi(pathp);
  45.  
  46.                                     /* See if CDE has created any entries: */
  47.     strcpy(chnevar1, CHAIN_BASE);
  48.     strcat(chnevar1, "1");
  49.     if (!(pathp = m_getenv(chnevar1)))    /* if so, pathp points to last dir    */
  50.         error("No previous directory");    /* else no previous dir                */
  51.  
  52.     change_dir(pathp);                    /* change to previous directory:    */
  53.  
  54.                         /* Update the environment directory chain: */
  55.     if (chaincnt == 1)        /* special case: record old dir */
  56.     {
  57.         if (m_putenv(chnevar1, cwdbuf))
  58.             error("Error setting environment variable");
  59.         return 0;
  60.     }
  61.  
  62.     for (i = 1; ; i++)
  63.     {                /* get name of current dirname variable */
  64.         strcpy(chnevar1, CHAIN_BASE);
  65.         strcat(chnevar1, itoa(i, itoabuf, 10));
  66.         
  67.         strcpy(chnevar2, CHAIN_BASE);
  68.         strcat(chnevar2, itoa(i + 1, itoabuf, 10));
  69.  
  70.         if (!(chndname = m_getenv(chnevar2)))
  71.             break;            /* found end of saved chain */
  72.         
  73.                 /* copy value of next higher to current */
  74.         strcpy(chndname_save, chndname);    /* m_putenv() bashes it */
  75.         strcpy(chnevar1, CHAIN_BASE);
  76.         strcat(chnevar1, itoa(i, itoabuf, 10));
  77.         if (m_putenv(chnevar1, chndname_save))
  78.             error("Error setting environment variable");
  79.     }
  80.     return 0;
  81. }
  82.