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

  1. *****Listing 2*****
  2.  
  3. /*
  4.  *    CDE.C: Extended "cd" command for MS-DOS.
  5.  *    Written by Leor Zolman, 9/20/89
  6.  *
  7.  *    Features:
  8.  *        1)    Allows changing to another drive and dirctory in one step
  9.  *        2)    Supports directory aliasing through environment variables
  10.  *        3)    With no arguments, optionally switches to 'home' directory
  11.  *            (if the HOME environment variable is currently defined)
  12.  *        3)    Manages a "previous directory" stack through environment
  13.  *            variables. The number of entries in the stack is dynamically
  14.  *            configurable through a special controlling environment variable.
  15.  *        4)    For special case of stack size = 1, toggling back and forth
  16.  *            between two directories is supported
  17.  *          
  18.  *     Usage:
  19.  *        cde [d:][path]        (changes to given drive/directory)
  20.  *        cde <env-var-name>    (indirect dir change on environment variable)
  21.  *        cde                    (changes to HOME directory, if defined, or
  22.  *                                returns current working directory otherwise)
  23.  *
  24.  *    Compile/Link:
  25.  *        cl /Ox cde.c util.c envlib    (where ENVLIB.OBJ is Master Env. Pkg.)
  26.  *
  27.  *    Uses the Master Environment library from CUJ 7/89.
  28.  *
  29.  */
  30.  
  31. #include    <stdio.h>
  32. #include    <dos.h>
  33. #include    <string.h>
  34. #include    <stdlib.h>
  35. #include    "util.h"
  36.     
  37. main(int argc, char **argv)
  38. {
  39.     char    *pathp;
  40.     char    cwdbuf[MAX_DIRNAME_SIZE];        /* buffer for current dir name    */
  41.  
  42.     int        chaincnt;                        /* size of dir stack            */
  43.     char    chaincnt_txt[10], *chaincntp;
  44.     char    chnevar1[MAX_EVARNAME_SIZE],    /* env var names built here        */
  45.             chnevar2[MAX_EVARNAME_SIZE];
  46.     char    chndname_save[MAX_DIRNAME_SIZE], *chndname;
  47.     char    itoabuf[10];                    /* used by itoa() function        */
  48.     int        i;
  49.     
  50.                                 /* Get current dir. name and current drive:    */
  51.     getcwd(cwdbuf, MAX_DIRNAME_SIZE);
  52.     
  53.     if (argc == 1)                        /* if no args given,                */
  54.         if (pathp = m_getenv(HOME_NAME))    /* if HOME directory defined,    */
  55.         {
  56.             change_dir(pathp);              /* then try to change to it.    */
  57.             strcpy(chnevar1, CHAIN_BASE);   /* set top-stack env var        */
  58.             strcat(chnevar1, "1");
  59.             if (m_putenv(chnevar1, cwdbuf))   /* to old dir                */
  60.                 error("Error setting environment variable");
  61.             return 0;
  62.         }
  63.         else
  64.         {                                /* just print current working dir    */
  65.             cputs(cwdbuf);
  66.             putch('\n');
  67.             return 0;
  68.         }
  69.  
  70.     if (argc != 2)
  71.         error("Usage: cde [d:][newpath] or <environment-var-name>\n");
  72.     
  73.     pathp = argv[1];                    /* skip whitespace in pathname    */
  74.     
  75.     if (chndname = m_getenv(pathp))        /* if env-var-name given,        */
  76.         pathp = chndname;                /* use its value as new path    */
  77.  
  78.     change_dir(pathp);
  79.  
  80.                     /* Read or initialize master chain length variable:    */
  81.     if ((chaincntp = m_getenv(CHAINS_VAR)) == NULL)
  82.         if (m_putenv(CHAINS_VAR,
  83.                     strcpy(chaincntp = chaincnt_txt, DEFAULT_CHAINS)))
  84.             error("Error creating environment variable");
  85.     
  86.                             /* Update the environment directory chain:    */
  87.     chaincnt = atoi(chaincntp);
  88.     for (i = chaincnt; i > 0; i--)
  89.     {                    /* construct name of previous dirname variable:    */
  90.         if (i != 1)
  91.         {
  92.             strcpy(chnevar2, CHAIN_BASE);
  93.             strcat(chnevar2, itoa(i-1, itoabuf, 10));
  94.         }
  95.  
  96.         if (chndname = ((i != 1) ? m_getenv(chnevar2) : cwdbuf))
  97.         {                            /* copy value of prev. to current    */
  98.             strcpy(chndname_save, chndname);    /* m_putenv() bashes it    */
  99.             strcpy(chnevar1, CHAIN_BASE);
  100.             strcat(chnevar1, itoa(i, itoabuf, 10));
  101.             if (m_putenv(chnevar1, chndname_save))
  102.                 error("Error setting environment variable");
  103.         }
  104.     }
  105.     return 0;
  106. }
  107.  
  108.