home *** CD-ROM | disk | FTP | other *** search
/ The Developer Connection…ice Driver Kit for OS/2 3 / DEV3-D1.ISO / source / util2src / cdir.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-04-30  |  9.5 KB  |  220 lines

  1. /*============================================================================*
  2.  * main() module: cdir.c - Change directory.
  3.  *
  4.  * (C)Copyright IBM Corporation, 1991.                           Brian E. Yoder
  5.  *
  6.  * This program is a loose adaptation of the AIX 'cd' command.  See the
  7.  * CDX.DOC file for usage information.
  8.  *
  9.  * On OS/2, a program cannot change the current drive and path.  Therefore,
  10.  * we only write the d:path (and d:, if specified) to stdout.  The cdx
  11.  * command uses this information to change the current working drive and path.
  12.  *
  13.  * 04/17/91 - Created.
  14.  * 04/15/91 - Initial version 1.0.
  15.  * 04/30/91 - Ported to OS/2.  Is called by cdx.cmd -- will not work directly.
  16.  *            Also, if we are changing to a relative directory that is not
  17.  *            within our current path, we no longer write the name to stdout.
  18.  *            AIX's cd command did, but with OS/2's 'prompt $p$g', it should
  19.  *            be obvious where we are.
  20.  *============================================================================*/
  21.  
  22. static char copr[] = "(c)Copyright IBM Corporation, 1991.  All rights reserved.";
  23. static char ver[]  = "Version 1.0,  4/30/91";
  24.  
  25. #include <stdio.h>
  26. #include <stdlib.h>
  27. #include <string.h>
  28. #include <sys/types.h>
  29.  
  30. #include "util.h"
  31.  
  32. #define NO       0
  33. #define YES      1
  34.  
  35. #define  MAX_CPY 1024               /* Max length of strings, during strcpy() */
  36. #define  MAX_LEN 2048               /* Max buffer lengths */
  37.  
  38. /*----------------------------------------------------------------------------*
  39.  * Buffers used by this program
  40.  *----------------------------------------------------------------------------*/
  41.  
  42. static char cdbuff[MAX_LEN+1];      /* Buffer for contents of CDPATH var */
  43. static char cdname[MAX_LEN+1];      /* Buffer for contents of a name */
  44.  
  45. /*============================================================================*
  46.  * Define internal functions to allow forward access
  47.  *============================================================================*/
  48.  
  49. static int        gopath       ( char  *);
  50.  
  51. /*============================================================================*
  52.  * Main Program Entry Point
  53.  *============================================================================*/
  54.  
  55. main(argc, argv)
  56.  
  57. int argc;                           /* arg count */
  58. char *argv[];                       /* arg pointers */
  59.  
  60. {
  61.   int     rc;                       /* Return code store area */
  62.   long    lrc;                      /* Long return code */
  63.   char   *path;                     /* Pointer to drive/path specification */
  64.   char   *home;                     /* Pointer to HOME var string */
  65.   char   *cdpath;                   /* Pointer to CDPATH var string */
  66.   char   *pstr;                     /* Pointer to generic string */
  67.  
  68.   argc--;                           /* Ignore 1st argument (program name) */
  69.   argv++;
  70.  
  71.  /*---------------------------------------------------------------------------*
  72.   * Store pointers to environment variables:
  73.   *---------------------------------------------------------------------------*/
  74.  
  75.   home = getenv("HOME");
  76.   cdpath = getenv("CDPATH");
  77.  
  78.  /*---------------------------------------------------------------------------*
  79.   * If no arguments entered, try to find/go to HOME directory
  80.   *---------------------------------------------------------------------------*/
  81.  
  82.   if (argc == 0)                    /* If no arguments entered: */
  83.   {
  84.      if (home != NULL)                   /* If we have a HOME path: */
  85.      {
  86.         strncpy(cdname, home, MAX_CPY);       /* Copy it to buffer */
  87.         rc = gopath(cdname);                  /* Go to it */
  88.         if (rc != 0)
  89.            fprintf(stderr, "'%s': No such directory.\n", cdname);
  90.      }
  91.      else                                /* Else we don't have a home path: */
  92.      {                                        /* Display error */
  93.         fprintf(stderr, "No HOME path defined.\n");
  94.         rc = 2;                               /* Set error return code */
  95.      }
  96.      return(rc);                         /* Return to caller */
  97.   }
  98.  
  99.   path = *argv;                     /* Point path to argument string */
  100.   if (*path == '\0')                /* If path string is zero length: */
  101.      return(0);                     /* Then it's ok to stay where we are */
  102.  
  103.  /*---------------------------------------------------------------------------*
  104.   * If we have a drive specification or an absolute path,
  105.   * then attempt to go to drive/path
  106.   *---------------------------------------------------------------------------*/
  107.  
  108.   if ( hasdrive(path)  ||           /* If we have a drive spec in the path: */
  109.        (*path == '\\') ||           /* Or the path is absolute: */
  110.        (*path == '/') )
  111.   {
  112.      rc = gopath(path);                  /* Go to it */
  113.      if (rc != 0)                        /* See if it worked */
  114.         fprintf(stderr, "'%s': No such directory.\n", path);
  115.      return(rc);                         /* Return */
  116.   }
  117.  
  118.  /*---------------------------------------------------------------------------*
  119.   * We have a relative path:  first try to go directly to it
  120.   *---------------------------------------------------------------------------*/
  121.  
  122.   rc = gopath(path);                /* Try to go to it */
  123.   if (rc == 0)                      /* If we succeed: then return */
  124.      return(0);                     /* Otherwise, we'll go check cdpath */
  125.  
  126.  /*---------------------------------------------------------------------------*
  127.   * We have no drive spec:  If we have a relative path, look in current
  128.   * dir and then CDPATH for it
  129.   *---------------------------------------------------------------------------*/
  130.  
  131.   if (cdpath != NULL)               /* If we have a CDPATH path list: */
  132.      strncpy(cdbuff, cdpath,        /* Copy it to the buffer */
  133.              MAX_CPY);              /* Otherwise, we're out of luck */
  134.   else
  135.   {
  136.      fprintf(stderr, "'%s': No such directory.\n", path);
  137.      return(2);
  138.   }
  139.  
  140.   pstr = strtok(cdbuff, ";");       /* Point to 1st path in list, and */
  141.   while (pstr != NULL)              /* Loop to check paths: */
  142.   {
  143.      strncpy(cdname, pstr, MAX_CPY);     /* Copy CDPATH path to buffer */
  144.      if (cdname[0] != '\0')              /* If it has a non-zero length: */
  145.      {
  146.         pathcat(cdname, path);                /* Append our path string to it */
  147.         if (isdir(cdname))                    /* If it's a valid directory: */
  148.         {
  149.            rc = gopath(cdname);                    /* Go to it */
  150.            if (rc == 0)                            /* If ok: */
  151.            {
  152. // OS/2 ver:  printf("%s\n", cdname);                   /* Print the new dir name */
  153.               return(0);                                /* Return */
  154.            }
  155.         }                                     /* Endif */
  156.      }                                   /* Either: 0-length, not a dir, or */
  157.      pstr = strtok(NULL, ";");           /* failed gopath(): Point to next path */
  158.   }                                 /* End of path check loop */
  159.  
  160.  /*---------------------------------------------------------------------------*
  161.   * We have looked everywhere, but the path cannot be found
  162.   *---------------------------------------------------------------------------*/
  163.  
  164.   fprintf(stderr, "'%s': No such directory.\n", path);
  165.   return(2);                        /* Return */
  166.  
  167. } /* end of main() */
  168.  
  169. /*============================================================================*
  170.  * gopath() - Go to a specified drive and path.
  171.  *
  172.  * REMARKS:
  173.  *   This subroutine changes the current directory on a specified drive,
  174.  *   and changes the current drive to the specified drive.  If the drive
  175.  *   is missing, then the current drive is not changed.
  176.  *
  177.  *   The best we can do for error checking is to verify that the specified
  178.  *   drive and path exists.  If the system() command to change the working
  179.  *   drive fails, we're out of luck -- DOS won't tell us.
  180.  *
  181.  *   For OS/2, this subroutine only writes the pathname (and drive, if
  182.  *   applicable) to stdout.  It's up to the calling 'cdx' command to
  183.  *   use this output to change the session's current path and drive.
  184.  *
  185.  * RETURNS:
  186.  *   0: Successful.
  187.  *   2: Failed.
  188.  *============================================================================*/
  189.  
  190. static int  gopath(path)
  191.  
  192. char    *path;                      /* Pointer to [d:][path] string */
  193.  
  194. {
  195.   int  rc;                          /* Return code storage */
  196.   char drv[3];                      /* To store drive string */
  197.  
  198.   if (*path == '\0')                /* If path is a null string: */
  199.      return(0);                     /* Then it's ok to stay where we are */
  200.  
  201.   if (!isdir(path))                 /* If it's not a path, then we can't */
  202.      return(2);                     /* possibly go to it */
  203.  
  204. //chdir(path);   /* DOS method */   /* Set new current working directory */
  205.  
  206.   printf("%s", path);               /* Write path to stdout */
  207.  
  208.   if (hasdrive(path))               /* If the path has a drive spec */
  209.   {
  210.      memcpy(drv, path, 2);               /* Store drive and colon string */
  211.      drv[2] = '\0';                      /* in the drv[] buffer.  Then: */
  212. //   system(drv);                        /* Invoke DOS to set new default drive */
  213.      printf(" %s", drv);                 /* Write path to stdout */
  214.   }
  215.  
  216.   printf("\n");                     /* Add newline */
  217.   fflush(stdout);                   /* Flush stdout buffer */
  218.   return(0);                        /* Ok, to the best of our knowledge */
  219. }
  220.