1 | /* gawkmisc.c --- miscellaneous gawk routines that are OS specific.
|
---|
2 |
|
---|
3 | Copyright (C) 1986, 1988, 1989, 1991 - 1998, 2001 - 2004 the Free Software Foundation, Inc.
|
---|
4 |
|
---|
5 | This program is free software; you can redistribute it and/or modify
|
---|
6 | it under the terms of the GNU General Public License as published by
|
---|
7 | the Free Software Foundation; either version 2, or (at your option)
|
---|
8 | any later version.
|
---|
9 |
|
---|
10 | This program is distributed in the hope that it will be useful,
|
---|
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
13 | GNU General Public License for more details.
|
---|
14 |
|
---|
15 | You should have received a copy of the GNU General Public License
|
---|
16 | along with this program; if not, write to the Free Software Foundation,
|
---|
17 | Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
|
---|
18 |
|
---|
19 | char quote = '\'';
|
---|
20 | char *defpath = DEFPATH;
|
---|
21 | char envsep = ':';
|
---|
22 |
|
---|
23 | #ifndef INVALID_HANDLE
|
---|
24 | /* FIXME: is this value for INVALID_HANDLE correct? */
|
---|
25 | #define INVALID_HANDLE -1
|
---|
26 | #endif
|
---|
27 |
|
---|
28 | /* gawk_name --- pull out the "gawk" part from how the OS called us */
|
---|
29 |
|
---|
30 | char *
|
---|
31 | gawk_name(filespec)
|
---|
32 | const char *filespec;
|
---|
33 | {
|
---|
34 | char *p;
|
---|
35 |
|
---|
36 | /* "path/name" -> "name" */
|
---|
37 | p = strrchr(filespec, '/');
|
---|
38 | return (p == NULL ? (char *) filespec : p + 1);
|
---|
39 | }
|
---|
40 |
|
---|
41 | /* os_arg_fixup --- fixup the command line */
|
---|
42 |
|
---|
43 | void
|
---|
44 | os_arg_fixup(argcp, argvp)
|
---|
45 | int *argcp;
|
---|
46 | char ***argvp;
|
---|
47 | {
|
---|
48 | /* no-op */
|
---|
49 | return;
|
---|
50 | }
|
---|
51 |
|
---|
52 | /* os_devopen --- open special per-OS devices */
|
---|
53 |
|
---|
54 | int
|
---|
55 | os_devopen(name, flag)
|
---|
56 | const char *name;
|
---|
57 | int flag;
|
---|
58 | {
|
---|
59 | /* no-op */
|
---|
60 | return INVALID_HANDLE;
|
---|
61 | }
|
---|
62 |
|
---|
63 | /* optimal_bufsize --- determine optimal buffer size */
|
---|
64 |
|
---|
65 | /*
|
---|
66 | * Enhance this for debugging purposes, as follows:
|
---|
67 | *
|
---|
68 | * Always stat the file, stat buffer is used by higher-level code.
|
---|
69 | *
|
---|
70 | * if (AWKBUFSIZE == "exact")
|
---|
71 | * return the file size
|
---|
72 | * else if (AWKBUFSIZE == a number)
|
---|
73 | * always return that number
|
---|
74 | * else
|
---|
75 | * if the size is < default_blocksize
|
---|
76 | * return the size
|
---|
77 | * else
|
---|
78 | * return default_blocksize
|
---|
79 | * end if
|
---|
80 | * endif
|
---|
81 | *
|
---|
82 | * Hair comes in an effort to only deal with AWKBUFSIZE
|
---|
83 | * once, the first time this routine is called, instead of
|
---|
84 | * every time. Performance, dontyaknow.
|
---|
85 | */
|
---|
86 |
|
---|
87 | size_t
|
---|
88 | optimal_bufsize(fd, stb)
|
---|
89 | int fd;
|
---|
90 | struct stat *stb;
|
---|
91 | {
|
---|
92 | char *val;
|
---|
93 | static size_t env_val = 0;
|
---|
94 | static short first = TRUE;
|
---|
95 | static short exact = FALSE;
|
---|
96 |
|
---|
97 | /* force all members to zero in case OS doesn't use all of them. */
|
---|
98 | memset(stb, '\0', sizeof(struct stat));
|
---|
99 |
|
---|
100 | /* always stat, in case stb is used by higher level code. */
|
---|
101 | if (fstat(fd, stb) == -1)
|
---|
102 | fatal("can't stat fd %d (%s)", fd, strerror(errno));
|
---|
103 |
|
---|
104 | if (first) {
|
---|
105 | first = FALSE;
|
---|
106 |
|
---|
107 | if ((val = getenv("AWKBUFSIZE")) != NULL) {
|
---|
108 | if (strcmp(val, "exact") == 0)
|
---|
109 | exact = TRUE;
|
---|
110 | else if (ISDIGIT(*val)) {
|
---|
111 | for (; *val && ISDIGIT(*val); val++)
|
---|
112 | env_val = (env_val * 10) + *val - '0';
|
---|
113 |
|
---|
114 | return env_val;
|
---|
115 | }
|
---|
116 | }
|
---|
117 | } else if (! exact && env_val > 0)
|
---|
118 | return env_val;
|
---|
119 | /* else
|
---|
120 | fall through */
|
---|
121 |
|
---|
122 | /*
|
---|
123 | * System V.n, n < 4, doesn't have the file system block size in the
|
---|
124 | * stat structure. So we have to make some sort of reasonable
|
---|
125 | * guess. We use stdio's BUFSIZ, since that is what it was
|
---|
126 | * meant for in the first place.
|
---|
127 | */
|
---|
128 | #ifdef HAVE_ST_BLKSIZE
|
---|
129 | #define DEFBLKSIZE (stb->st_blksize > 0 ? stb->st_blksize : BUFSIZ)
|
---|
130 | #else
|
---|
131 | #define DEFBLKSIZE BUFSIZ
|
---|
132 | #endif
|
---|
133 |
|
---|
134 | if (S_ISREG(stb->st_mode) /* regular file */
|
---|
135 | && 0 < stb->st_size /* non-zero size */
|
---|
136 | && (stb->st_size < DEFBLKSIZE /* small file */
|
---|
137 | || exact)) /* or debugging */
|
---|
138 | return stb->st_size; /* use file size */
|
---|
139 |
|
---|
140 | return DEFBLKSIZE;
|
---|
141 | }
|
---|
142 |
|
---|
143 | /* ispath --- return true if path has directory components */
|
---|
144 |
|
---|
145 | int
|
---|
146 | ispath(file)
|
---|
147 | const char *file;
|
---|
148 | {
|
---|
149 | return (strchr(file, '/') != NULL);
|
---|
150 | }
|
---|
151 |
|
---|
152 | /* isdirpunct --- return true if char is a directory separator */
|
---|
153 |
|
---|
154 | int
|
---|
155 | isdirpunct(c)
|
---|
156 | int c;
|
---|
157 | {
|
---|
158 | return (c == '/');
|
---|
159 | }
|
---|
160 |
|
---|
161 | /* os_close_on_exec --- set close on exec flag, print warning if fails */
|
---|
162 |
|
---|
163 | void
|
---|
164 | os_close_on_exec(fd, name, what, dir)
|
---|
165 | int fd;
|
---|
166 | const char *name, *what, *dir;
|
---|
167 | {
|
---|
168 | if (fd <= 2) /* sanity */
|
---|
169 | return;
|
---|
170 |
|
---|
171 | if (fcntl(fd, F_SETFD, 1) < 0)
|
---|
172 | warning(_("%s %s `%s': could not set close-on-exec: (fcntl: %s)"),
|
---|
173 | what, dir, name, strerror(errno));
|
---|
174 | }
|
---|
175 |
|
---|
176 | /* os_isdir --- is this an fd on a directory? */
|
---|
177 |
|
---|
178 | #if ! defined(S_ISDIR) && defined(S_IFDIR)
|
---|
179 | #define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
|
---|
180 | #endif
|
---|
181 |
|
---|
182 | int
|
---|
183 | os_isdir(fd)
|
---|
184 | int fd;
|
---|
185 | {
|
---|
186 | struct stat sbuf;
|
---|
187 |
|
---|
188 | return (fstat(fd, &sbuf) == 0 && S_ISDIR(sbuf.st_mode));
|
---|
189 | }
|
---|
190 |
|
---|
191 | /* os_is_setuid --- true if running setuid root */
|
---|
192 |
|
---|
193 | int
|
---|
194 | os_is_setuid()
|
---|
195 | {
|
---|
196 | long uid, euid;
|
---|
197 |
|
---|
198 | uid = getuid();
|
---|
199 | euid = geteuid();
|
---|
200 |
|
---|
201 | return (euid == 0 && euid != uid);
|
---|
202 | }
|
---|
203 |
|
---|
204 | /* os_setbinmode --- set binary mode on file */
|
---|
205 |
|
---|
206 | int
|
---|
207 | os_setbinmode(fd, mode)
|
---|
208 | int fd, mode;
|
---|
209 | {
|
---|
210 | return 0;
|
---|
211 | }
|
---|
212 |
|
---|
213 | /* os_restore_mode --- restore the original mode of the console device */
|
---|
214 |
|
---|
215 | void
|
---|
216 | os_restore_mode (fd)
|
---|
217 | int fd;
|
---|
218 | {
|
---|
219 | /* no-op */
|
---|
220 | return;
|
---|
221 | }
|
---|
222 |
|
---|
223 | #ifdef __CYGWIN__
|
---|
224 | #include <sys/cygwin.h>
|
---|
225 |
|
---|
226 | extern int _fmode;
|
---|
227 | void
|
---|
228 | cygwin_premain0 (int argc, char **argv, struct per_process *myself)
|
---|
229 | {
|
---|
230 | static struct __cygwin_perfile pf[] =
|
---|
231 | {
|
---|
232 | {"", O_RDONLY | O_TEXT},
|
---|
233 | /*{"", O_WRONLY | O_BINARY},*/
|
---|
234 | {NULL, 0}
|
---|
235 | };
|
---|
236 | cygwin_internal (CW_PERFILE, pf);
|
---|
237 | }
|
---|
238 | #endif
|
---|