Merge from vendor branch OPENSSH:
[dragonfly.git] / contrib / awk / posix / gawkmisc.c
1 /* gawkmisc.c --- miscellanious gawk routines that are OS specific.
2  
3    Copyright (C) 1986, 1988, 1989, 1991 - 96 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
18
19 char quote = '\'';
20 char *defpath = DEFPATH;
21 char envsep = ':';
22
23 /* gawk_name --- pull out the "gawk" part from how the OS called us */
24
25 char *
26 gawk_name(filespec)
27 const char *filespec;
28 {
29         char *p;
30     
31         /* "path/name" -> "name" */
32         p = strrchr(filespec, '/');
33         return (p == NULL ? (char *) filespec : p + 1);
34 }
35
36 /* os_arg_fixup --- fixup the command line */
37
38 void
39 os_arg_fixup(argcp, argvp)
40 int *argcp;
41 char ***argvp;
42 {
43         /* no-op */
44         return;
45 }
46
47 /* os_devopen --- open special per-OS devices */
48
49 int
50 os_devopen(name, flag)
51 const char *name;
52 int flag;
53 {
54         /* no-op */
55         return INVALID_HANDLE;
56 }
57
58 /* optimal_bufsize --- determine optimal buffer size */
59
60 int
61 optimal_bufsize(fd, stb)
62 int fd;
63 struct stat *stb;
64 {
65         /* force all members to zero in case OS doesn't use all of them. */
66         memset(stb, '\0', sizeof(struct stat));
67
68         /*
69          * System V.n, n < 4, doesn't have the file system block size in the
70          * stat structure. So we have to make some sort of reasonable
71          * guess. We use stdio's BUFSIZ, since that is what it was
72          * meant for in the first place.
73          */
74 #ifdef HAVE_ST_BLKSIZE
75 #define DEFBLKSIZE      (stb->st_blksize ? stb->st_blksize : BUFSIZ)
76 #else
77 #define DEFBLKSIZE      BUFSIZ
78 #endif
79
80         if (isatty(fd))
81                 return BUFSIZ;
82         if (fstat(fd, stb) == -1)
83                 fatal("can't stat fd %d (%s)", fd, strerror(errno));
84         if (lseek(fd, (off_t)0, 0) == -1)       /* not a regular file */
85                 return DEFBLKSIZE;
86         if (stb->st_size > 0 && stb->st_size < DEFBLKSIZE) /* small file */
87                 return stb->st_size;
88         return DEFBLKSIZE;
89 }
90
91 /* ispath --- return true if path has directory components */
92
93 int
94 ispath(file)
95 const char *file;
96 {
97         return (strchr(file, '/') != NULL);
98 }
99
100 /* isdirpunct --- return true if char is a directory separator */
101
102 int
103 isdirpunct(c)
104 int c;
105 {
106         return (c == '/');
107 }
108