X-Git-Url: https://pintos-os.org/cgi-bin/gitweb.cgi?a=blobdiff_plain;ds=sidebyside;f=src%2Ffilename.c;h=aa92a323c83946e9686c84f46595dc70b4c67bb9;hb=cf89e411db41c05c39753b05cf144c8b26a44d96;hp=4aaf7d17f916d5c92aca4d4d2aad8f11d07376e9;hpb=4944c86a9318bc5b5578ab145a95c116ffd2c9fd;p=pspp diff --git a/src/filename.c b/src/filename.c index 4aaf7d17f9..aa92a323c8 100644 --- a/src/filename.c +++ b/src/filename.c @@ -17,42 +17,23 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ -/* AIX requires this to be the first thing in the file. */ #include -#if __GNUC__ -#define alloca __builtin_alloca -#else -#if HAVE_ALLOCA_H -#include -#else -#ifdef _AIX -#pragma alloca -#else -#ifndef alloca /* predefined by HP cc +Olibcalls */ -char *alloca (); -#endif -#endif -#endif -#endif - -#include +#include "error.h" +#include "filename.h" #include #include #include #include "alloc.h" #include "error.h" -#include "filename.h" #include "settings.h" #include "str.h" #include "version.h" -#undef DEBUGGING -/*#define DEBUGGING 1 */ #include "debug-print.h" /* PORTME: Everything in this file is system dependent. */ -#if unix +#ifdef unix #include #if HAVE_UNISTD_H #include @@ -60,7 +41,7 @@ char *alloca (); #include "stat.h" #endif -#if __WIN32__ +#ifdef __WIN32__ #define NOGDI #define NOUSER #define NONLS @@ -148,7 +129,7 @@ fn_interp_vars (const char *input, const char *(*getenv) (const char *)) } } -#if unix +#ifdef unix /* Expands csh tilde notation from the path INPUT into a malloc()'d returned string. */ char * @@ -205,7 +186,7 @@ fn_tilde_expand (const char *input) } #else /* !unix */ char * -fn_tilde_expand (char *input) +fn_tilde_expand (const char *input) { return xstrdup (input); } @@ -220,7 +201,7 @@ fn_tilde_expand (char *input) If PREPEND is non-NULL, then it is prepended to each filename; i.e., it looks like PREPEND/PATH_COMPONENT/NAME. This is not done with absolute directories in the path. */ -#if unix || __MSDOS__ || __WIN32__ +#if defined (unix) || defined (__MSDOS__) || defined (__WIN32__) char * fn_search_path (const char *basename, const char *path, const char *prepend) { @@ -336,7 +317,7 @@ fn_prepend_dir (const char *file, const char *dir) existing file. Returns a malloc()'d copy of the canonical name. This function must always succeed; if it needs to bail out then it should return xstrdup(FN1). */ -#if unix +#ifdef unix char * fn_normalize (const char *filename) { @@ -373,16 +354,10 @@ fn_normalize (const char *filename) else { errno = 0; -#if __CHECKER__ - memset (dest, 0, maxlen); -#endif while (getcwd (dest, maxlen - (dest - fn2)) == NULL && errno == ERANGE) { maxlen *= 2; dest = fn2 = xrealloc (fn2, maxlen + 1); -#if __CHECKER__ - memset (dest, 0, maxlen); -#endif errno = 0; } if (errno) @@ -452,7 +427,7 @@ fn_normalize (const char *filename) } } } -#elif __WIN32__ +#elif defined (__WIN32__) char * fn_normalize (const char *fn1) { @@ -550,6 +525,7 @@ fn_basename (const char *filename) } #endif +#if unix /* Returns the current working directory, as a malloc()'d string. From libc.info. */ char * @@ -569,6 +545,22 @@ fn_get_cwd (void) buffer = xmalloc (size); } } +#else +char * +fn_get_cwd (void) +{ + int size = 2; + char *buffer = xmalloc (size); + if ( buffer) + { + buffer[0]='.'; + buffer[1]='\0'; + } + + return buffer; + +} +#endif /* Find out information about files. */ @@ -576,13 +568,13 @@ fn_get_cwd (void) int fn_absolute_p (const char *name) { -#if unix +#ifdef unix if (name[0] == '/' || !strncmp (name, "./", 2) || !strncmp (name, "../", 3) || name[0] == '~') return 1; -#elif __MSDOS__ +#elif defined (__MSDOS__) if (name[0] == '\\' || !strncmp (name, ".\\", 2) || !strncmp (name, "..\\", 3) @@ -600,7 +592,7 @@ fn_special_p (const char *filename) { if (!strcmp (filename, "-") || !strcmp (filename, "stdin") || !strcmp (filename, "stdout") || !strcmp (filename, "stderr") -#if unix +#ifdef unix || filename[0] == '|' || (*filename && filename[strlen (filename) - 1] == '|') #endif @@ -614,7 +606,7 @@ fn_special_p (const char *filename) int fn_exists_p (const char *name) { -#if unix +#ifdef unix struct stat temp; return stat (name, &temp) == 0; @@ -627,7 +619,7 @@ fn_exists_p (const char *name) #endif } -#if unix +#ifdef unix /* Stolen from libc.info but heavily modified, this is a wrapper around readlink() that allows for arbitrary filename length. */ char * @@ -717,10 +709,10 @@ fn_open (const char *fn, const char *mode) else if (mode[0] == 'w' && !strcmp (fn, "stderr")) return stderr; -#if unix +#ifdef unix if (fn[0] == '|') { - if (set_safer) + if (safer_mode()) return safety_violation (fn); return popen (&fn[1], mode); @@ -730,7 +722,7 @@ fn_open (const char *fn, const char *mode) char *s; FILE *f; - if (set_safer) + if (safer_mode()) return safety_violation (fn); s = local_alloc (strlen (fn)); @@ -763,7 +755,7 @@ fn_close (const char *fn, FILE *f) { if (!strcmp (fn, "-")) return 0; -#if unix +#ifdef unix else if (fn[0] == '|' || (*fn && fn[strlen (fn) - 1] == '|')) { pclose (f); @@ -879,3 +871,93 @@ fn_close_ext (struct file_ext *f) } return 1; } + +#ifdef unix +/* A file's identity. */ +struct file_identity + { + dev_t device; /* Device number. */ + ino_t inode; /* Inode number. */ + }; + +/* Returns a pointer to a dynamically allocated structure whose + value can be used to tell whether two files are actually the + 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. */ +struct file_identity * +fn_get_identity (const char *filename) +{ + struct stat s; + + if (stat (filename, &s) == 0) + { + struct file_identity *identity = xmalloc (sizeof *identity); + identity->device = s.st_dev; + identity->inode = s.st_ino; + return identity; + } + else + return NULL; +} + +/* Frees IDENTITY obtained from fn_get_identity(). */ +void +fn_free_identity (struct file_identity *identity) +{ + free (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) +{ + assert (a != NULL); + assert (b != NULL); + if (a->device != b->device) + return a->device < b->device ? -1 : 1; + else + return a->inode < b->inode ? -1 : a->inode > b->inode; +} +#else /* not unix */ +/* A file's identity. */ +struct file_identity + { + char *normalized_filename; /* File's normalized name. */ + }; + +/* Returns a pointer to a dynamically allocated structure whose + value can be used to tell whether two files are actually the + 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. */ +struct file_identity * +fn_get_identity (const char *filename) +{ + struct file_identity *identity = xmalloc (sizeof *identity); + identity->normalized_filename = fn_normalize (filename); + return identity; +} + +/* Frees IDENTITY obtained from fn_get_identity(). */ +void +fn_free_identity (struct file_identity *identity) +{ + if (identity != NULL) + { + free (identity->normalized_filename); + free (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) +{ + return strcmp (a->normalized_filename, b->normalized_filename); +} +#endif /* not unix */