1/*
2 * gawkmisc.c --- miscellanious gawk routines that are OS specific.
3 */
4
5/*
6 * Copyright (C) 1986, 1988, 1989, 1991 - 95 the Free Software Foundation, Inc.
7 *
8 * This file is part of GAWK, the GNU implementation of the
9 * AWK Progamming Language.
10 *
11 * GAWK is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * GAWK is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA
24 */
25
26#ifndef O_RDONLY
27#include <fcntl.h>
28#endif
29#ifndef O_ACCMODE
30#define O_ACCMODE	(O_RDONLY | O_WRONLY | O_RDWR)
31#endif
32
33#ifndef __LIBC__
34#include <jctype.h>
35#endif
36
37extern char *_toslash P((char *));
38
39char quote = '\'';
40#ifdef DEFPATH
41char *defpath = DEFPATH;
42#else
43char *defpath = ".;/usr/lib/awk;/usr/local/lib/awk";
44#endif
45char envsep  = ';';
46
47/* gawk_name --- pull out the "gawk" part from how the OS called us */
48
49char *
50gawk_name(filespec)
51const char *filespec;
52{
53	char *p, *q;
54
55	p = (char *) filespec;  /* Sloppy... */
56
57	_toslash(p);
58	if ((q = strrchr(p, '/')) != NULL)
59		p = q + 1;
60	if ((q = strrchr(p, '.')) != NULL && strcasecmp(q, ".x") == 0)
61		*q = '\0';
62	return strlwr(p);
63}
64
65/* os_arg_fixup --- fixup the command line */
66
67void
68os_arg_fixup(argcp, argvp)
69int *argcp;
70char ***argvp;
71{
72	/* no-op */
73	return;
74}
75
76/* os_devopen --- open special per-OS devices */
77
78int
79os_devopen(name, flag)
80const char *name;
81int flag;
82{
83	int openfd = -1;
84
85	if (STREQ(name, "/dev/null"))
86		openfd = open("nul", flag, 0666);
87	else if (STREQ(name, "/dev/tty"))
88		openfd = open("con", flag, 0666);
89
90	if (openfd >= 0 && (flag & O_ACCMODE) == O_WRONLY && !isatty(openfd))
91		setmode(openfd, O_BINARY);
92
93	return openfd;
94}
95
96/* optimal_bufsize --- determine optimal buffer size */
97
98int
99optimal_bufsize(fd, stb)
100int fd;
101struct stat *stb;
102{
103	/* force all members to zero in case OS doesn't use all of them. */
104	memset(stb, '\0', sizeof(struct stat));
105
106	/*
107	 * System V.n, n < 4, doesn't have the file system block size in the
108	 * stat structure. So we have to make some sort of reasonable
109	 * guess. We use stdio's BUFSIZ, since that is what it was
110	 * meant for in the first place.
111	 */
112#ifdef HAVE_ST_BLKSIZE
113#define DEFBLKSIZE	(stb->st_blksize ? stb->st_blksize : BUFSIZ)
114#else
115#define	DEFBLKSIZE	BUFSIZ
116#endif
117
118	if (isatty(fd))
119		return BUFSIZ;
120	if (fstat(fd, stb) == -1)
121		fatal("can't stat fd %d (%s)", fd, strerror(errno));
122	if (lseek(fd, (off_t)0, 0) == -1)	/* not a regular file */
123		return DEFBLKSIZE;
124	if (stb->st_size > 0 && stb->st_size < DEFBLKSIZE) /* small file */
125		return stb->st_size;
126	return DEFBLKSIZE;
127}
128
129/* ispath --- return true if path has directory components */
130
131int
132ispath(file)
133const char *file;
134{
135	_toslash((char *) file); /* Sloppy... */
136	for (; *file; file++) {
137		switch (*file) {
138		case '/':
139		case ':':
140			return 1;
141		}
142	}
143	return 0;
144}
145
146/* isdirpunct --- return true if char is a directory separator */
147
148int
149isdirpunct(c)
150int c;
151{
152	return (strchr(":\\/", c) != NULL);
153}
154
155#ifndef __LIBC__
156char *
157_toslash (path)
158char *path;
159{
160	char *p;
161
162	p = path;
163	while (*p != '\0') {
164		if (iskanji ((unsigned char) *p))
165			p++;
166		else if (*p == '\\')
167			*p = '/';
168		p++;
169	}
170
171	return path;
172}
173
174int
175getegid ()
176{
177	return 0;
178}
179
180int
181geteuid ()
182{
183	return 0;
184}
185
186int
187getgid ()
188{
189	return 0;
190}
191
192int
193getuid ()
194{
195	return 0;
196}
197
198int
199getpgrp ()
200{
201	return 0;
202}
203
204int
205getppid ()
206{
207	return 0;
208}
209#endif
210