2 * Suffix appending for in-place editing under MS-DOS and OS/2.
6 * Style 0: Append the suffix exactly as standard perl would do it.
7 * If the filesystem groks it, use it. (HPFS will always
8 * grok it. FAT will rarely accept it.)
10 * Style 1: The suffix begins with a '.'. The extension is replaced.
11 * If the name matches the original name, use the fallback method.
13 * Style 2: The suffix is a single character, not a '.'. Try to add the
14 * suffix to the following places, using the first one that works.
15 * [1] Append to extension.
16 * [2] Append to filename,
17 * [3] Replace end of extension,
18 * [4] Replace end of filename.
19 * If the name matches the original name, use the fallback method.
21 * Style 3: Any other case: Ignore the suffix completely and use the
24 * Fallback method: Change the extension to ".$$$". If that matches the
25 * original name, then change the extension to ".~~~".
27 * If filename is more than 1000 characters long, we die a horrible
30 * The filename restriction is a cheat so that we can use buf[] to store
31 * assorted temporary goo.
33 * Examples, assuming style 0 failed.
35 * suffix = ".bak" (style 1)
37 * foo.bak => foo.$$$ (fallback)
38 * foo.$$$ => foo.~~~ (fallback)
39 * makefile => makefile.bak
41 * suffix = "~" (style 2)
45 * foo~.c~~ => foo~~.c~~
46 * foo~~~~~.c~~ => foo~~~~~.$$$ (fallback)
49 * makefile => makefile.~
50 * longname.fil => longname.fi~
51 * longname.fi~ => longnam~.fi~
52 * longnam~.fi~ => longnam~.$$$
59 #define INCL_DOSFILEMGR
60 #define INCL_DOSERRORS
64 static char suffix1[] = ".$$$";
65 static char suffix2[] = ".~~~";
67 #define ext (&buf[1000])
69 add_suffix(str,suffix)
71 register char *suffix;
78 if (!(str->str_pok)) (void)str_2ptr(str);
79 if (str->str_cur > 1000)
80 fatal("Cannot do inplace edit on long filename (%d characters)", str->str_cur);
86 if (valid_filename(str->str_ptr)) return;
88 /* Fooey, style 0 failed. Fix str before continuing. */
89 str->str_ptr[str->str_cur = slen] = '\0';
92 slen = strlen(suffix);
93 t = buf; baselen = 0; s = str->str_ptr;
94 while ( (*t = *s) && *s != '.') {
96 if (*s == '\\' || *s == '/') baselen = 0;
102 while (*t++ = *s++) extlen++;
103 if (extlen == 0) { ext[0] = '.'; ext[1] = 0; extlen++; }
105 if (*suffix == '.') { /* Style 1 */
106 if (strEQ(ext, suffix)) goto fallback;
108 } else if (suffix[1] == '\0') { /* Style 2 */
110 ext[extlen] = *suffix;
111 ext[++extlen] = '\0';
112 } else if (baselen < 8) {
114 } else if (ext[3] != *suffix) {
116 } else if (buf[7] != *suffix) {
118 } else goto fallback;
120 } else { /* Style 3: Panic */
122 (void)bcopy(strEQ(ext, suffix1) ? suffix2 : suffix1, p, 4+1);
135 switch(DosOpen(s, &hf, &usAction, 0L, 0, FILE_OPEN,
136 OPEN_ACCESS_READONLY | OPEN_SHARE_DENYNONE, 0L)) {
142 case ERROR_FILENAME_EXCED_RANGE: