From 7e0f021a9aec35fd8e6725e87e3313b101d26f5e Mon Sep 17 00:00:00 2001 From: Tobias Klauser Date: Sun, 27 Jan 2008 11:37:44 +0100 Subject: Initial import (2.0.2-6) --- reference/C/CONTRIB/SNIP/fln_fix.c | 142 +++++++++++++++++++++++++++++++++++++ 1 file changed, 142 insertions(+) create mode 100755 reference/C/CONTRIB/SNIP/fln_fix.c (limited to 'reference/C/CONTRIB/SNIP/fln_fix.c') diff --git a/reference/C/CONTRIB/SNIP/fln_fix.c b/reference/C/CONTRIB/SNIP/fln_fix.c new file mode 100755 index 0000000..e1ded2e --- /dev/null +++ b/reference/C/CONTRIB/SNIP/fln_fix.c @@ -0,0 +1,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 +#include +#include +#include + +#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; +} -- cgit v1.2.3-54-g00ecf