summaryrefslogtreecommitdiff
path: root/reference/C/CONTRIB/SNIP/flnorm.c
blob: 79337abe99cd99933e39573f3081f0c2abc64531 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
/*
**  FLNORM.C - Normalize DOS file names
**
**  Original Copyright 1988-1991 by Bob Stout as part of
**  the MicroFirm Function Library (MFL)
**
**  This subset version is functionally identical to the
**  version originally published by the author in Tech Specialist
**  magazine and is hereby donated to the public domain.
*/

#include <stdio.h>
#include <string.h>
#ifdef __TURBOC__
 #include <dir.h>
#else
 #include <direct.h>
#endif
#include <dos.h>
#include <io.h>

#define MAX_FLEN 67
#define LAST_CHAR(string) (((char *)string)[strlen(string)-1])

typedef enum {ERROR = -1, FALSE, TRUE} LOGICAL;

/*
**  NOTE: Uses the following functions, also in SNIPPETS!
*/

int chdrv(int);                           /* In DRVALID.C   */
char *unix2dos(char *);                   /* In UNIX2DOS.C  */
char *fln_fix(char *);                    /* In FLN_FIX.C   */

int flnorm(char *in_name, char *out_name)
{
      LOGICAL dir_flag = FALSE, new_drv = FALSE, root_flag;
      int status = 0, level = 0;
      char *p, *out;
      static char drive[2][3];
      static char file[14];
      static char I_am_here[MAX_FLEN];
      static char I_am_there[MAX_FLEN];
      static char remember[MAX_FLEN];

      getcwd(I_am_here, MAX_FLEN);
      if (!in_name || !in_name[0])
      {
            strcpy(out_name, I_am_here);
            goto ERRexit;
      }
      strncpy(drive[0], I_am_here, 2);
      drive[0][2] = '\0';
      if (':' == in_name[1])
      {     /* If a drive is specified                            */
            if (chdrv(in_name[0]))
            {     /* If the drive is invalid                      */
                  status = ERROR;
                  goto ERRexit;
            }
            new_drv = TRUE;
            getcwd(remember, MAX_FLEN);
            strncpy(drive[1], remember, 2);
            drive[1][2] = '\0';
      }
      else
      {     /* If a drive isn't specified                         */
            if (NULL != (p = strchr(in_name, ':')))
            {     /* If filename is illegal                       */
                  status = ERROR;
                  goto ERRexit;
            }
      }
      unix2dos(in_name);
      if (new_drv)
      {
            if ('\\' == in_name[2])
                  strcpy(out_name, drive[1]);
            else
            {
                  strcpy(out_name, remember);
                  if ('\\' != LAST_CHAR(remember))
                        strcat(out_name, "\\");
            }
      }
      else
      {
            strcpy(out_name, drive[0]);
            if ('\\' != *in_name)
            {
                  strcat(out_name, I_am_here);
                  if ('\\' != LAST_CHAR(I_am_here))
                        strcat(out_name, "\\");
            }
      }
      strcat(out_name, &in_name[(new_drv) ? 2 : 0]);
      fln_fix(out_name);
      out = &out_name[2];
      if (!(*out))
            goto ERRexit;
      while ('\\' == LAST_CHAR(out))
      {     /* Strip trailing `\'s                                */
            LAST_CHAR(out_name) = '\0';
            dir_flag = TRUE;
      }
      if (!(*out))
      {
            if (dir_flag)
            {
                  strcat(out, "\\");
                  goto ERRexit;
            }
            else  goto BADPATH;
      }
      if (NULL != (p = strrchr(out_name, '\\')))
            strcpy(file, p);        /* Save filename              */
      if (chdir(out))
      {     /* If can't move to path                              */
            if ((!dir_flag) && p)
            {     /* If there was a separate path                 */
                  *p = '\0';
                  if (!(*out))
                  {     /* Back at the root, handle it            */
                        strcpy(p, "\\");
                        strcpy(file, &file[1]);
                  }
                  if (chdir(out))
                  {     /* If we can't move to path               */
                        *p = '\\';
                        goto BADPATH;
                  }
                  ++level;          /* Flag we stripped name      */
            }
            else
            {     /* No path as specified                         */
                  if (p)
                  {
BADPATH:                status = ERROR;
                        goto ERRexit;
                  }
            }
      }
      getcwd(I_am_there, MAX_FLEN); /* Get normalized path        */
      strupr(I_am_there);
      strcpy(out_name, I_am_there);
      if (level)
            strcat(out_name, file);
ERRexit:
      if (new_drv)
      {
            chdir(remember);
            chdrv(I_am_here[0]);
      }
      chdir(I_am_here);
      if (status)
            out_name[0] = '\0';
      return status;
}