Mercurial > ~dholland > hg > ag > index.cgi
comparison 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 |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:13d2b8934445 |
---|---|
1 /* | |
2 * AnaGram, A System for Syntax Directed Programming | |
3 * Copyright 1993-1999 Parsifal Software. All Rights Reserved. | |
4 * Copyright 2006 David A. Holland. All Rights Reserved. | |
5 * See the file COPYING for license and usage terms. | |
6 * | |
7 * file.cpp | |
8 */ | |
9 | |
10 #include <sys/types.h> | |
11 #include <sys/stat.h> | |
12 #include <fcntl.h> | |
13 #include <string.h> | |
14 #include <limits.h> | |
15 | |
16 #ifdef AG_ON_WINDOWS | |
17 #include <direct.h> | |
18 #include <share.h> | |
19 #endif | |
20 | |
21 #ifdef AG_ON_UNIX | |
22 #define SH_DENYNO 0 | |
23 #define SH_DENYWR 0 | |
24 #endif | |
25 | |
26 #include "port.h" | |
27 | |
28 #include "agstring.h" | |
29 #include "file.h" | |
30 | |
31 //#define INCLUDE_LOGGING | |
32 #include "log.h" | |
33 | |
34 | |
35 AgString work_dir_name; | |
36 | |
37 //////////////////////////////////////////////////////////// | |
38 | |
39 /* | |
40 * Open files, compatibly with DOS file sharing. | |
41 * (Does this even mean anything in win32?) | |
42 */ | |
43 | |
44 static int open_shared(const char *path, int openflags, int sharingmode) { | |
45 int fd; | |
46 | |
47 LOGSECTION("open_shared"); | |
48 LOGV(path); | |
49 | |
50 #ifdef AG_ON_UNIX | |
51 /* | |
52 * Unix filehandles are always shared, like it or not. We could | |
53 * conceivably use mandatory locking for the denywrite case... but | |
54 * (1) mandatory locking isn't universally supported, (2) it'll | |
55 * likely cause permission problems, and (3) there's really not a | |
56 * whole lot of point. | |
57 */ | |
58 (void)sharingmode; | |
59 fd = open(path, openflags, 0664 /* default mode */); | |
60 #endif | |
61 | |
62 #ifdef AG_ON_WINDOWS | |
63 fd = sopen(path, openflags, sharingmode); | |
64 #endif | |
65 | |
66 LOGV(fd); | |
67 if (fd < 0) { | |
68 LOGV(strerror(errno)); | |
69 } | |
70 | |
71 return fd; | |
72 } | |
73 | |
74 int open_shared_denywrite(const char *path, int openflags) { | |
75 LOGSECTION("open_shared_denywrite"); | |
76 return open_shared(path, openflags, SH_DENYWR); | |
77 } | |
78 | |
79 int open_shared_any(const char *path, int openflags) { | |
80 LOGSECTION("open_shared_any"); | |
81 return open_shared(path, openflags, SH_DENYNO); | |
82 } | |
83 | |
84 //////////////////////////////////////////////////////////// | |
85 | |
86 time_t getFileTimestamp(const char *name) { | |
87 struct stat statbuf; | |
88 | |
89 stat(name, &statbuf); | |
90 return statbuf.st_mtime; | |
91 } | |
92 | |
93 //////////////////////////////////////////////////////////// | |
94 | |
95 void set_work_dir(void) { | |
96 LOGSECTION("set_work_dir"); | |
97 | |
98 // in unix, this only needs to be PATH_MAX, but who knows about windows... | |
99 char temp[PATH_MAX+1]; | |
100 getcwd(temp, PATH_MAX); | |
101 work_dir_name = AgString(temp); | |
102 | |
103 LOGV(work_dir_name); | |
104 } |