diff options
Diffstat (limited to 'util.c')
-rw-r--r-- | util.c | 180 |
1 files changed, 1 insertions, 179 deletions
@@ -2,7 +2,7 @@ util.c - - $Author: kosaki $ + $Author: usa $ created at: Fri Mar 10 17:22:34 JST 1995 Copyright (C) 1993-2008 Yukihiro Matsumoto @@ -184,184 +184,6 @@ ruby_strtoul(const char *str, char **endptr, int base) # define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR) #endif -#if defined(__CYGWIN32__) || defined(_WIN32) -/* - * Copyright (c) 1993, Intergraph Corporation - * - * You may distribute under the terms of either the GNU General Public - * License or the Artistic License, as specified in the perl README file. - * - * Various Unix compatibility functions and NT specific functions. - * - * Some of this code was derived from the MSDOS port(s) and the OS/2 port. - * - */ - - -/* - * Suffix appending for in-place editing under MS-DOS and OS/2 (and now NT!). - * - * Here are the rules: - * - * Style 0: Append the suffix exactly as standard perl would do it. - * If the filesystem groks it, use it. (HPFS will always - * grok it. So will NTFS. FAT will rarely accept it.) - * - * Style 1: The suffix begins with a '.'. The extension is replaced. - * If the name matches the original name, use the fallback method. - * - * Style 2: The suffix is a single character, not a '.'. Try to add the - * suffix to the following places, using the first one that works. - * [1] Append to extension. - * [2] Append to filename, - * [3] Replace end of extension, - * [4] Replace end of filename. - * If the name matches the original name, use the fallback method. - * - * Style 3: Any other case: Ignore the suffix completely and use the - * fallback method. - * - * Fallback method: Change the extension to ".$$$". If that matches the - * original name, then change the extension to ".~~~". - * - * If filename is more than 1000 characters long, we die a horrible - * death. Sorry. - * - * The filename restriction is a cheat so that we can use buf[] to store - * assorted temporary goo. - * - * Examples, assuming style 0 failed. - * - * suffix = ".bak" (style 1) - * foo.bar => foo.bak - * foo.bak => foo.$$$ (fallback) - * makefile => makefile.bak - * suffix = ".$$$" (style 1) - * foo.$$$ => foo.~~~ (fallback) - * - * suffix = "~" (style 2) - * foo.c => foo.c~ - * foo.c~ => foo.c~~ - * foo.c~~ => foo~.c~~ - * foo~.c~~ => foo~~.c~~ - * foo~~~~~.c~~ => foo~~~~~.$$$ (fallback) - * - * foo.pas => foo~.pas - * makefile => makefile.~ - * longname.fil => longname.fi~ - * longname.fi~ => longnam~.fi~ - * longnam~.fi~ => longnam~.$$$ - * - */ - - -static int valid_filename(const char *s); - -static const char suffix1[] = ".$$$"; -static const char suffix2[] = ".~~~"; - -#define strEQ(s1,s2) (strcmp((s1),(s2)) == 0) - -void -ruby_add_suffix(VALUE str, const char *suffix) -{ - long baselen; - long extlen = strlen(suffix); - long slen; - char buf[1024]; - const char *name; - const char *ext; - long len; - - name = StringValueCStr(str); - slen = strlen(name); - if (slen > (long)(sizeof(buf) - 1)) - rb_fatal("Cannot do inplace edit on long filename (%ld characters)", - slen); - - /* Style 0 */ - rb_str_cat(str, suffix, extlen); - if (valid_filename(RSTRING_PTR(str))) return; - - /* Fooey, style 0 failed. Fix str before continuing. */ - rb_str_resize(str, slen); - name = StringValueCStr(str); - ext = ruby_find_extname(name, &len); - - if (*suffix == '.') { /* Style 1 */ - if (ext) { - if (strEQ(ext, suffix)) { - extlen = sizeof(suffix1) - 1; /* suffix2 must be same length */ - suffix = strEQ(suffix, suffix1) ? suffix2 : suffix1; - } - slen = ext - name; - } - rb_str_resize(str, slen); - rb_str_cat(str, suffix, extlen); - } - else { - char *p = buf, *q; - strncpy(buf, name, slen); - if (ext) - p += (ext - name); - else - p += slen; - p[len] = '\0'; - if (suffix[1] == '\0') { /* Style 2 */ - q = (char *)ruby_find_basename(buf, &baselen, 0); - if (len <= 3) { - if (len == 0 && baselen >= 8 && p + 3 <= buf + sizeof(buf)) p[len++] = '.'; /* DOSISH */ - p[len] = *suffix; - p[++len] = '\0'; - } - else if (q && baselen < 8) { - q += baselen; - *q++ = *suffix; - if (ext) { - strncpy(q, ext, ext - name); - q[ext - name + 1] = '\0'; - } - else - *q = '\0'; - } - else if (len == 4 && p[3] != *suffix) - p[3] = *suffix; - else if (baselen == 8 && q[7] != *suffix) - q[7] = *suffix; - else - goto fallback; - } - else { /* Style 3: Panic */ - fallback: - (void)memcpy(p, !ext || strEQ(ext, suffix1) ? suffix2 : suffix1, 5); - } - rb_str_resize(str, strlen(buf)); - memcpy(RSTRING_PTR(str), buf, RSTRING_LEN(str)); - } -} - -static int -valid_filename(const char *s) -{ - int fd; - - /* - // It doesn't exist, so see if we can open it. - */ - - if ((fd = open(s, O_CREAT|O_EXCL, 0666)) >= 0) { - close(fd); - unlink(s); /* don't leave it laying around */ - return 1; - } - else if (errno == EEXIST) { - /* if the file exists, then it's a valid filename! */ - return 1; - } - return 0; -} -#endif - /* mm.c */ |