summaryrefslogtreecommitdiff
path: root/posix/gawkmisc.c
blob: 2e70cf9cc41de69485aa2e06b45c4118e57ee39b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
/* gawkmisc.c --- miscellaneous gawk routines that are OS specific.
 
   Copyright (C) 1986, 1988, 1989, 1991 - 1998, 2001 - 2004 the 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, 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.

   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.  */

#ifdef __CYGWIN__
#include <stdio.h>
#include <sys/cygwin.h>
#include <io.h>
#endif

char quote = '\'';
char *defpath = DEFPATH;
char envsep = ':';

#ifndef INVALID_HANDLE
/* FIXME: is this value for INVALID_HANDLE correct? */
#define INVALID_HANDLE -1
#endif

/* gawk_name --- pull out the "gawk" part from how the OS called us */

char *
gawk_name(const char *filespec)
{
	char *p;
    
	/* "path/name" -> "name" */
	p = strrchr(filespec, '/');
	return (p == NULL ? (char *) filespec : p + 1);
}

/* os_arg_fixup --- fixup the command line */

void
os_arg_fixup(int *argcp, char ***argvp)
{
	/* no-op */
	return;
}

/* os_devopen --- open special per-OS devices */

int
os_devopen(const char *name, int flag)
{
	/* no-op */
	return INVALID_HANDLE;
}

/* optimal_bufsize --- determine optimal buffer size */

/*
 * Enhance this for debugging purposes, as follows:
 *
 * Always stat the file, stat buffer is used by higher-level code.
 *
 * if (AWKBUFSIZE == "exact")
 * 	return the file size
 * else if (AWKBUFSIZE == a number)
 * 	always return that number
 * else
 * 	if the size is < default_blocksize
 * 		return the size
 *	else
 *		return default_blocksize
 *	end if
 * endif
 *
 * Hair comes in an effort to only deal with AWKBUFSIZE
 * once, the first time this routine is called, instead of
 * every time.  Performance, dontyaknow.
 */

size_t
optimal_bufsize(int fd, struct stat *stb)
{
	char *val;
	static size_t env_val = 0;
	static short first = TRUE;
	static short exact = FALSE;

	/* force all members to zero in case OS doesn't use all of them. */
	memset(stb, '\0', sizeof(struct stat));

	/* always stat, in case stb is used by higher level code. */
	if (fstat(fd, stb) == -1)
		fatal("can't stat fd %d (%s)", fd, strerror(errno));

	if (first) {
		first = FALSE;

		if ((val = getenv("AWKBUFSIZE")) != NULL) {
			if (strcmp(val, "exact") == 0)
				exact = TRUE;
			else if (isdigit((unsigned char) *val)) {
				for (; *val && isdigit((unsigned char) *val); val++)
					env_val = (env_val * 10) + *val - '0';

				return env_val;
			}
		}
	} else if (! exact && env_val > 0)
		return env_val;
	/* else
	  	fall through */

	/*
	 * System V.n, n < 4, doesn't have the file system block size in the
	 * stat structure. So we have to make some sort of reasonable
	 * guess. We use stdio's BUFSIZ, since that is what it was
	 * meant for in the first place.
	 */
#ifdef HAVE_ST_BLKSIZE
#define DEFBLKSIZE	(stb->st_blksize > 0 ? stb->st_blksize : BUFSIZ)
#else
#define DEFBLKSIZE	BUFSIZ
#endif

	if (S_ISREG(stb->st_mode)		/* regular file */
	    && 0 < stb->st_size			/* non-zero size */
	    && (stb->st_size < DEFBLKSIZE	/* small file */
		|| exact))			/* or debugging */
		return stb->st_size;		/* use file size */

	return DEFBLKSIZE;
}

/* ispath --- return true if path has directory components */

int
ispath(const char *file)
{
	return (strchr(file, '/') != NULL);
}

/* isdirpunct --- return true if char is a directory separator */

int
isdirpunct(int c)
{
	return (c == '/');
}

/* os_close_on_exec --- set close on exec flag, print warning if fails */

void
os_close_on_exec(int fd, const char *name, const char *what, const char *dir)
{
	int curflags = 0;

	if (fd <= 2)	/* sanity */
		return;

	/*
	 * Per POSIX, use Read/Modify/Write - get the flags,
	 * add FD_CLOEXEC, set the flags back.
	 */

	if ((curflags = fcntl(fd, F_GETFD)) < 0) {
		warning(_("%s %s `%s': could not get fd flags: (fcntl F_GETFD: %s)"),
			what, dir, name, strerror(errno));
		return;
	}

#ifndef FD_CLOEXEC
#define FD_CLOEXEC	1
#endif

	curflags |= FD_CLOEXEC;

	if (fcntl(fd, F_SETFD, curflags) < 0)
		warning(_("%s %s `%s': could not set close-on-exec: (fcntl F_SETFD: %s)"),
			what, dir, name, strerror(errno));
}

/* os_isdir --- is this an fd on a directory? */

#if ! defined(S_ISDIR) && defined(S_IFDIR)
#define	S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
#endif

int
os_isdir(int fd)
{
	struct stat sbuf;

	return (fstat(fd, &sbuf) == 0 && S_ISDIR(sbuf.st_mode));
}

/* os_is_setuid --- true if running setuid root */

int
os_is_setuid()
{
	long uid, euid;

	uid = getuid();
	euid = geteuid();

	return (euid == 0 && euid != uid);
}

/* os_setbinmode --- set binary mode on file */

int
os_setbinmode(int fd, int mode)
{
#ifdef __CYGWIN__
	setmode (fd, mode);
#endif
	return 0;
}

/* os_restore_mode --- restore the original mode of the console device */

void
os_restore_mode(int fd)
{
	/* no-op */
	return;
}
 
/* files_are_same --- return true if files are identical */

int
files_are_same(struct stat *f1, struct stat *f2)
{
	return (f1->st_dev == f2->st_dev && f1->st_ino == f2->st_ino);
}

#ifdef __CYGWIN__
void
cygwin_premain0(int argc, char **argv, void *myself)
{
	static struct __cygwin_perfile pf[] = {
		{ "", O_RDONLY | O_TEXT },
		/*{ "", O_WRONLY | O_BINARY },*/
		{ NULL, 0 }
	};
	cygwin_internal(CW_PERFILE, pf);
}

void
cygwin_premain2(int argc, char **argv, void *myself)
{
	setmode(fileno (stdin), O_TEXT);
}
#endif