X-Git-Url: https://pintos-os.org/cgi-bin/gitweb.cgi?a=blobdiff_plain;ds=sidebyside;f=src%2Fdata%2Ffile-name.c;h=f3b168176c0c63918d741ac9637fb9e81e8f6657;hb=a1d8ce0ad176357cf250ec065e0bf1a4520a19e3;hp=acf27c3ce882e67a7e549354d026d547c602fa31;hpb=44326932c8227c64a87f7a92ef16ce83c2fba2d4;p=pspp-builds.git diff --git a/src/data/file-name.c b/src/data/file-name.c index acf27c3c..f3b16817 100644 --- a/src/data/file-name.c +++ b/src/data/file-name.c @@ -1,38 +1,35 @@ -/* PSPP - computes sample statistics. - Copyright (C) 1997-9, 2000, 2006 Free Software Foundation, Inc. +/* PSPP - a program for statistical analysis. + Copyright (C) 1997-9, 2000, 2006, 2007 Free Software Foundation, Inc. - This program is free software; you can redistribute it and/or - modify it under the terms of the GNU General Public License as - published by the Free Software Foundation; either version 2 of the - License, or (at your option) any later version. + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. - This program is distributed in the hope that it will be useful, but - WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - General Public License for more details. + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. You should have received a copy of the GNU General Public License - along with this program; if not, write to the Free Software - Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA - 02110-1301, USA. */ + along with this program. If not, see . */ #include -#include "file-name.h" +#include #include #include #include #include -#include "canonicalize.h" #include "intprops.h" #include "minmax.h" -#include "settings.h" +#include "dirname.h" #include #include -#include +#include #include #include #include @@ -42,6 +39,11 @@ #include #include + +#if defined _WIN32 || defined __WIN32__ +#define WIN32_LEAN_AND_MEAN /* avoid including junk */ +#include +#endif /* Initialization. */ @@ -60,7 +62,7 @@ fn_init (void) properly handling the case where SRC is a substring of DST. Variables are as defined by GETENV. Supports $var and ${var} syntaxes; $$ substitutes as $. */ -void +void fn_interp_vars (struct substring src, const char *(*getenv) (const char *), struct string *dst_) { @@ -68,7 +70,7 @@ fn_interp_vars (struct substring src, const char *(*getenv) (const char *), int c; while ((c = ss_get_char (&src)) != EOF) - if (c != '$') + if (c != '$') ds_put_char (&dst, c); else { @@ -85,7 +87,7 @@ fn_interp_vars (struct substring src, const char *(*getenv) (const char *), else if (ss_match_char (&src, '{')) ss_get_until (&src, '}', &var_name); else - ss_get_chars (&src, MIN (1, ss_span (src, ss_cstr (CC_ALNUM))), + ss_get_chars (&src, MAX (1, ss_span (src, ss_cstr (CC_ALNUM))), &var_name); start = ds_length (&dst); @@ -94,7 +96,7 @@ fn_interp_vars (struct substring src, const char *(*getenv) (const char *), ds_truncate (&dst, start); ds_put_cstr (&dst, value); - } + } } ds_swap (&dst, dst_); @@ -128,7 +130,7 @@ fn_search_path (const char *base_name, const char *path_) /* Construct file name. */ ds_clear (&file); ds_put_substring (&file, dir); - if (!ds_is_empty (&file) && ds_last (&file) != '/') + if (!ds_is_empty (&file) && !ISSLASH (ds_last (&file))) ds_put_char (&file, '/'); ds_put_cstr (&file, base_name); @@ -153,32 +155,14 @@ fn_search_path (const char *base_name, const char *path_) char * fn_dir_name (const char *file_name) { - const char *p; - char *s; - size_t len; - - len = strlen (file_name); - if (len == 1 && file_name[0] == '/') - p = file_name + 1; - else if (len && file_name[len - 1] == '/') - p = buf_find_reverse (file_name, len - 1, file_name + len - 1, 1); - else - p = strrchr (file_name, '/'); - if (p == NULL) - p = file_name; - - s = xmalloc (p - file_name + 1); - memcpy (s, file_name, p - file_name); - s[p - file_name] = 0; - - return s; + return dir_name (file_name); } /* Returns the extension part of FILE_NAME as a malloc()'d string. If FILE_NAME does not have an extension, returns an empty string. */ char * -fn_extension (const char *file_name) +fn_extension (const char *file_name) { const char *extension = strrchr (file_name, '.'); if (extension == NULL) @@ -192,7 +176,7 @@ fn_extension (const char *file_name) bool fn_is_absolute (const char *name) { - return name[0] == '/'; + return IS_ABSOLUTE_FILE_NAME (name); } /* Returns true if FILE_NAME is a virtual file that doesn't @@ -268,22 +252,22 @@ safety_violation (const char *fn) FILE * fn_open (const char *fn, const char *mode) { - assert (mode[0] == 'r' || mode[0] == 'w'); + assert (mode[0] == 'r' || mode[0] == 'w' || mode[0] == 'a'); - if (mode[0] == 'r' && (!strcmp (fn, "stdin") || !strcmp (fn, "-"))) + if (mode[0] == 'r' && (!strcmp (fn, "stdin") || !strcmp (fn, "-"))) return stdin; else if (mode[0] == 'w' && (!strcmp (fn, "stdout") || !strcmp (fn, "-"))) return stdout; else if (mode[0] == 'w' && !strcmp (fn, "stderr")) return stderr; - + #if HAVE_POPEN if (fn[0] == '|') { if (get_safer_mode ()) return safety_violation (fn); - return popen (&fn[1], mode); + return popen (&fn[1], mode[0] == 'r' ? "r" : "w"); } else if (*fn && fn[strlen (fn) - 1] == '|') { @@ -292,12 +276,12 @@ fn_open (const char *fn, const char *mode) if (get_safer_mode ()) return safety_violation (fn); - + s = local_alloc (strlen (fn)); memcpy (s, fn, strlen (fn) - 1); s[strlen (fn) - 1] = 0; - - f = popen (s, mode); + + f = popen (s, mode[0] == 'r' ? "r" : "w"); local_free (s); @@ -321,7 +305,9 @@ fn_open (const char *fn, const char *mode) int fn_close (const char *fn, FILE *f) { - if (!strcmp (fn, "-")) + if (fileno (f) == STDIN_FILENO + || fileno (f) == STDOUT_FILENO + || fileno (f) == STDERR_FILENO) return 0; #if HAVE_POPEN else if (fn[0] == '|' || (*fn && fn[strlen (fn) - 1] == '|')) @@ -334,9 +320,9 @@ fn_close (const char *fn, FILE *f) return fclose (f); } -#ifdef unix +#if !(defined _WIN32 || defined __WIN32__) /* A file's identity. */ -struct file_identity +struct file_identity { dev_t device; /* Device number. */ ino_t inode; /* Inode number. */ @@ -347,13 +333,13 @@ struct file_identity same file. Returns a null pointer if no information about the file is available, perhaps because it does not exist. The caller is responsible for freeing the structure with - fn_free_identity() when finished. */ + fn_free_identity() when finished. */ struct file_identity * -fn_get_identity (const char *file_name) +fn_get_identity (const char *file_name) { struct stat s; - if (stat (file_name, &s) == 0) + if (stat (file_name, &s) == 0) { struct file_identity *identity = xmalloc (sizeof *identity); identity->device = s.st_dev; @@ -366,7 +352,7 @@ fn_get_identity (const char *file_name) /* Frees IDENTITY obtained from fn_get_identity(). */ void -fn_free_identity (struct file_identity *identity) +fn_free_identity (struct file_identity *identity) { free (identity); } @@ -374,7 +360,7 @@ fn_free_identity (struct file_identity *identity) /* Compares A and B, returning a strcmp()-type result. */ int fn_compare_file_identities (const struct file_identity *a, - const struct file_identity *b) + const struct file_identity *b) { assert (a != NULL); assert (b != NULL); @@ -383,9 +369,9 @@ fn_compare_file_identities (const struct file_identity *a, else return a->inode < b->inode ? -1 : a->inode > b->inode; } -#else /* not unix */ +#else /* Windows */ /* A file's identity. */ -struct file_identity +struct file_identity { char *normalized_file_name; /* File's normalized name. */ }; @@ -395,23 +381,26 @@ struct file_identity same file. Returns a null pointer if no information about the file is available, perhaps because it does not exist. The caller is responsible for freeing the structure with - fn_free_identity() when finished. */ + fn_free_identity() when finished. */ struct file_identity * -fn_get_identity (const char *file_name) +fn_get_identity (const char *file_name) { struct file_identity *identity = xmalloc (sizeof *identity); - char *cname = canonicalize_filename_mode (file_name, CAN_MISSING); - if (cname == NULL) - cname = xstrdup (file_name); - identity->normalized_file_name = cname; + char cname[PATH_MAX]; + + if (GetFullPathName (file_name, sizeof cname, cname, NULL)) + identity->normalized_file_name = xstrdup (cname); + else + identity->normalized_file_name = xstrdup (file_name); + return identity; } /* Frees IDENTITY obtained from fn_get_identity(). */ void -fn_free_identity (struct file_identity *identity) +fn_free_identity (struct file_identity *identity) { - if (identity != NULL) + if (identity != NULL) { free (identity->normalized_file_name); free (identity); @@ -421,8 +410,8 @@ fn_free_identity (struct file_identity *identity) /* Compares A and B, returning a strcmp()-type result. */ int fn_compare_file_identities (const struct file_identity *a, - const struct file_identity *b) + const struct file_identity *b) { - return strcmp (a->normalized_file_name, b->normalized_file_name); + return strcasecmp (a->normalized_file_name, b->normalized_file_name); } -#endif /* not unix */ +#endif /* Windows */