home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / snip9707.zip / FLNORM.C < prev    next >
C/C++ Source or Header  |  1997-07-05  |  5KB  |  156 lines

  1. /* +++Date last modified: 05-Jul-1997 */
  2.  
  3. /*
  4. **  FLNORM.C - Normalize DOS file names
  5. **
  6. **  Original Copyright 1988-1991 by Bob Stout as part of
  7. **  the MicroFirm Function Library (MFL)
  8. **
  9. **  The user is granted a free limited license to use this source file
  10. **  to create royalty-free programs, subject to the terms of the
  11. **  license restrictions specified in the LICENSE.MFL file.
  12. */
  13.  
  14. #include <stdio.h>
  15. #include <string.h>
  16. #ifdef __TURBOC__
  17.  #include <dir.h>
  18. #else
  19.  #include <direct.h>
  20. #endif
  21. #include <dos.h>
  22. #include <io.h>
  23. #include "sniptype.h"
  24. #include "filnames.h"
  25.  
  26. #if defined(_WIN32) || defined(WIN32) || defined(__NT__)
  27.  #define chdrv _chdrive
  28. #else
  29.  #include "dosfiles.h"
  30. #endif
  31.  
  32. int flnorm(char *in_name, char *out_name)
  33. {
  34.       Boolean_T dir_flag = False_, new_drv = False_;
  35.       int status = 0, level = 0;
  36.       char *p, *out;
  37.       static char drive[2][3];
  38.       static char file[14];
  39.       static char I_am_here[FILENAME_MAX];
  40.       static char I_am_there[FILENAME_MAX];
  41.       static char remember[FILENAME_MAX];
  42.  
  43.       getcwd(I_am_here, FILENAME_MAX);
  44.       if (!in_name || !in_name[0])
  45.       {
  46.             strcpy(out_name, I_am_here);
  47.             goto ERRexit;
  48.       }
  49.       strncpy(drive[0], I_am_here, 2);
  50.       drive[0][2] = '\0';
  51.       if (':' == in_name[1])
  52.       {     /* If a drive is specified                            */
  53.             if (chdrv(in_name[0]))
  54.             {     /* If the drive is invalid                      */
  55.                   status = Error_;
  56.                   goto ERRexit;
  57.             }
  58.             new_drv = True_;
  59.             getcwd(remember, FILENAME_MAX);
  60.             strncpy(drive[1], remember, 2);
  61.             drive[1][2] = '\0';
  62.       }
  63.       else
  64.       {     /* If a drive isn't specified                         */
  65.             if (NULL != (p = strchr(in_name, ':')))
  66.             {     /* If filename is illegal                       */
  67.                   status = Error_;
  68.                   goto ERRexit;
  69.             }
  70.       }
  71.       unix2dos(in_name);
  72.       if (new_drv)
  73.       {
  74.             if ('\\' == in_name[2])
  75.                   strcpy(out_name, drive[1]);
  76.             else
  77.             {
  78.                   strcpy(out_name, remember);
  79.                   if ('\\' != LAST_CHAR(remember))
  80.                         strcat(out_name, "\\");
  81.             }
  82.       }
  83.       else
  84.       {
  85.             strcpy(out_name, drive[0]);
  86.             if ('\\' != *in_name)
  87.             {
  88.                   strcat(out_name, I_am_here);
  89.                   if ('\\' != LAST_CHAR(I_am_here))
  90.                         strcat(out_name, "\\");
  91.             }
  92.       }
  93.       strcat(out_name, &in_name[(new_drv) ? 2 : 0]);
  94.       fln_fix(out_name);
  95.       out = &out_name[2];
  96.       if (!(*out))
  97.             goto ERRexit;
  98.       while ('\\' == LAST_CHAR(out))
  99.       {     /* Strip trailing `\'s                                */
  100.             LAST_CHAR(out_name) = '\0';
  101.             dir_flag = True_;
  102.       }
  103.       if (!(*out))
  104.       {
  105.             if (dir_flag)
  106.             {
  107.                   strcat(out, "\\");
  108.                   goto ERRexit;
  109.             }
  110.             else  goto BADPATH;
  111.       }
  112.       if (NULL != (p = strrchr(out_name, '\\')))
  113.             strcpy(file, p);        /* Save filename              */
  114.       if (chdir(out))
  115.       {     /* If can't move to path                              */
  116.             if ((!dir_flag) && p)
  117.             {     /* If there was a separate path                 */
  118.                   *p = '\0';
  119.                   if (!(*out))
  120.                   {     /* Back at the root, handle it            */
  121.                         strcpy(p, "\\");
  122.                         strcpy(file, &file[1]);
  123.                   }
  124.                   if (chdir(out))
  125.                   {     /* If we can't move to path               */
  126.                         *p = '\\';
  127.                         goto BADPATH;
  128.                   }
  129.                   ++level;          /* Flag we stripped name      */
  130.             }
  131.             else
  132.             {     /* No path as specified                         */
  133.                   if (p)
  134.                   {
  135. BADPATH:                status = Error_;
  136.                         goto ERRexit;
  137.                   }
  138.             }
  139.       }
  140.       getcwd(I_am_there, FILENAME_MAX); /* Get normalized path        */
  141.       strupr(I_am_there);
  142.       strcpy(out_name, I_am_there);
  143.       if (level)
  144.             strcat(out_name, file);
  145. ERRexit:
  146.       if (new_drv)
  147.       {
  148.             chdir(remember);
  149.             chdrv(I_am_here[0]);
  150.       }
  151.       chdir(I_am_here);
  152.       if (status)
  153.             out_name[0] = '\0';
  154.       return status;
  155. }
  156.