grub2: bring back build of aros-side grub2 tools
[AROS.git] / arch / all-mingw32 / filesys / emul_handler / filenames.c
blob690738d1fb345f3961f7fa08b90f33f6777f4c48
1 /*
2 Copyright © 1995-2014, The AROS Development Team. All rights reserved.
3 $Id$
4 */
6 #include <aros/debug.h>
7 #include <dos/dos.h>
9 #include <string.h>
11 /*********************************************************************************************/
14 * Create a plain path out of the supplied filename.
15 * Eg 'path1/path2//path3/' becomes 'path1/path3'.
17 BOOL shrink(char *filename)
19 char *s, *s1,*s2;
20 unsigned long len;
22 /* We skip the first slash because it separates volume root prefix and the actual pathname */
23 s = filename;
24 if (*s == '\\')
25 s++;
27 for(;;)
29 /* leading slashes? --> return FALSE. */
30 if(*s == '\\') return FALSE;
32 /* remove superflous paths (ie paths that are followed by '\') */
33 s1 = strstr(s, "\\\\");
34 if(s1==NULL)
35 break;
36 s2=s1;
37 while(s2 > s)
39 if (s2[-1] == '\\') break;
40 s2--;
43 memmove(s2,s1+2,strlen(s1+1));
46 /* strip trailing slash */
47 len = strlen(filename);
48 if (len && (filename[len-1] == '\\'))
49 filename[len-1]=0;
51 return TRUE;
54 /* Validate file name */
55 ULONG validate(char *filename)
57 char *s = filename;
59 while (*s)
61 if (*s == '.') {
62 do {
63 s++;
64 } while (*s == '.');
65 if ((*s == '/') || (!*s)) {
66 D(bug("[emul] Bad file name, contains dots-only component\n"));
67 return ERROR_INVALID_COMPONENT_NAME;
70 do {
71 if (*s == '\\') {
72 D(bug("[emul] Bad file name, contains backslash\n"));
73 return ERROR_INVALID_COMPONENT_NAME;
75 s++;
76 } while ((*s != '/') && *s);
77 while (*s == '/')
78 s++;
81 return 0;
84 /* Append file name to an existing path */
85 char *append(char *c, char *filename)
87 char *s;
89 *c++ = '\\';
91 /* We are on Windows, so we have to revert slashes while copying filename */
92 for (s = filename; *s; s++)
93 *c++ = (*s == '/') ? '\\' : *s;
95 return c;
98 /* Find start position of the file name in path string */
99 long startpos(char *name, long i)
101 /* look for the first '\' in the filename starting at the end */
102 while (i != 0 && name[i] != '\\')
103 i--;
105 return i;
108 /* Copy file name with possible conversion */
109 void copyname(char *result, char *name, long i)
111 long c;
113 /* We want to get AROS filename, so we revert slashes back */
114 for (c = 0; c < i; c++)
115 result[c] = (name[c] == '\\') ? '/' : name[c];
117 result[i]=0x0;
120 /* Go to next part in path string */
121 char *nextpart(char *sp)
123 char *sp_end;
125 for(sp_end = sp + 1; sp_end[0] != '\0' && sp_end[0] != '\\'; sp_end++);
127 return sp_end;