summaryrefslogtreecommitdiff
path: root/reference/C/CONTRIB/SNIP/fln_fix.c
blob: e1ded2ec253aa08e2e7358057d42442ee8beced5 (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
/*
**  FLN_FIX.C
**
**  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>
#include <dos.h>
#include <io.h>

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

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

char *unix2dos(char *path);

/****************************************************************/
/*                                                              */
/*  Function to `crunch' dot directories and check for          */
/*  DOS-valid path strings. Drive specifiers in the path        */
/*  ignored.                                                    */
/*                                                              */
/****************************************************************/

char *fln_fix(char *path)
{
      LOGICAL dir_flag = FALSE, root_flag = FALSE;
      char *r, *p, *q, *s;

      if (path)
            strupr(path);

      /* Ignore leading drive specs   */

      if (NULL == (r = strrchr(path, ':')))
            r = path;
      else  ++r;

      unix2dos(r);                      /* Convert Unix to DOS style    */

      while ('\\' == *r)                /* Ignore leading backslashes   */
      {
            if ('\\' == r[1])
                  strcpy(r, &r[1]);
            else
            {
                  root_flag = TRUE;
                  ++r;
            }
      }

      p = r;                            /* Change "\\" to "\"           */
      while (NULL != (p = strchr(p, '\\')))
      {
            if ('\\' ==  p[1])
                  strcpy(p, &p[1]);
            else  ++p;
      }

      while ('.' == *r)                 /* Scrunch leading ".\"         */
      {
            if ('.' == r[1])
            {
                  /* Ignore leading ".."                                */

                  for (p = (r += 2); *p && (*p != '\\'); ++p)
                        ;
            }
            else
            {
                  for (p = r + 1 ;*p && (*p != '\\'); ++p)
                        ;
            }
            strcpy(r, p + ((*p) ? 1 : 0));
      }

      while ('\\' == LAST_CHAR(path))   /* Strip trailing backslash     */
      {
            dir_flag = TRUE;
            LAST_CHAR(path) = '\0';
      }

      s = r;

      /* Look for "\." in path        */

      while (NULL != (p = strstr(s, "\\.")))
      {
            if ('.' == p[2])
            {
                  /* Execute this section if ".." found                 */

                  q = p - 1;
                  while (q > r)           /* Backup one level           */
                  {
                        if (*q == '\\')
                              break;
                        --q;
                  }
                  if (q > r)
                  {
                        strcpy(q, p + 3);
                        s = q;
                  }
                  else if ('.' != *q)
                  {
                        strcpy(q + ((*q == '\\') ? 1 : 0),
                              p + 3 + ((*(p + 3)) ? 1 : 0));
                        s = q;
                  }
                  else  s = ++p;

            }
            else
            {
                  /* Execute this section if "." found                  */

                  q = p + 2;
                  for ( ;*q && (*q != '\\'); ++q)
                        ;
                  strcpy (p, q);
            }
      }

      if (root_flag)  /* Embedded ".." could have bubbled up to root  */
      {
            for (p = r; *p && ('.' == *p || '\\' == *p); ++p)
                  ;
            if (r != p)
                  strcpy(r, p);
      }

      if (dir_flag)
            strcat(path, "\\");
      return path;
}