Mercurial > ~dholland > hg > ag > index.cgi
diff anagram/support/file.cpp @ 0:13d2b8934445
Import AnaGram (near-)release tree into Mercurial.
author | David A. Holland |
---|---|
date | Sat, 22 Dec 2007 17:52:45 -0500 |
parents | |
children |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/anagram/support/file.cpp Sat Dec 22 17:52:45 2007 -0500 @@ -0,0 +1,104 @@ +/* + * AnaGram, A System for Syntax Directed Programming + * Copyright 1993-1999 Parsifal Software. All Rights Reserved. + * Copyright 2006 David A. Holland. All Rights Reserved. + * See the file COPYING for license and usage terms. + * + * file.cpp + */ + +#include <sys/types.h> +#include <sys/stat.h> +#include <fcntl.h> +#include <string.h> +#include <limits.h> + +#ifdef AG_ON_WINDOWS +#include <direct.h> +#include <share.h> +#endif + +#ifdef AG_ON_UNIX +#define SH_DENYNO 0 +#define SH_DENYWR 0 +#endif + +#include "port.h" + +#include "agstring.h" +#include "file.h" + +//#define INCLUDE_LOGGING +#include "log.h" + + +AgString work_dir_name; + +//////////////////////////////////////////////////////////// + +/* + * Open files, compatibly with DOS file sharing. + * (Does this even mean anything in win32?) + */ + +static int open_shared(const char *path, int openflags, int sharingmode) { + int fd; + + LOGSECTION("open_shared"); + LOGV(path); + +#ifdef AG_ON_UNIX + /* + * Unix filehandles are always shared, like it or not. We could + * conceivably use mandatory locking for the denywrite case... but + * (1) mandatory locking isn't universally supported, (2) it'll + * likely cause permission problems, and (3) there's really not a + * whole lot of point. + */ + (void)sharingmode; + fd = open(path, openflags, 0664 /* default mode */); +#endif + +#ifdef AG_ON_WINDOWS + fd = sopen(path, openflags, sharingmode); +#endif + + LOGV(fd); + if (fd < 0) { + LOGV(strerror(errno)); + } + + return fd; +} + +int open_shared_denywrite(const char *path, int openflags) { + LOGSECTION("open_shared_denywrite"); + return open_shared(path, openflags, SH_DENYWR); +} + +int open_shared_any(const char *path, int openflags) { + LOGSECTION("open_shared_any"); + return open_shared(path, openflags, SH_DENYNO); +} + +//////////////////////////////////////////////////////////// + +time_t getFileTimestamp(const char *name) { + struct stat statbuf; + + stat(name, &statbuf); + return statbuf.st_mtime; +} + +//////////////////////////////////////////////////////////// + +void set_work_dir(void) { + LOGSECTION("set_work_dir"); + + // in unix, this only needs to be PATH_MAX, but who knows about windows... + char temp[PATH_MAX+1]; + getcwd(temp, PATH_MAX); + work_dir_name = AgString(temp); + + LOGV(work_dir_name); +}