1 | /* $Id: shfile.h 3473 2020-09-16 21:12:58Z bird $ */
|
---|
2 | /** @file
|
---|
3 | * File management.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (c) 2007-2010 knut st. osmundsen <[email protected]>
|
---|
8 | *
|
---|
9 | *
|
---|
10 | * This file is part of kBuild.
|
---|
11 | *
|
---|
12 | * kBuild is free software; you can redistribute it and/or modify
|
---|
13 | * it under the terms of the GNU General Public License as published by
|
---|
14 | * the Free Software Foundation; either version 2 of the License, or
|
---|
15 | * (at your option) any later version.
|
---|
16 | *
|
---|
17 | * kBuild is distributed in the hope that it will be useful,
|
---|
18 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
20 | * GNU General Public License for more details.
|
---|
21 | *
|
---|
22 | * You should have received a copy of the GNU General Public License
|
---|
23 | * along with kBuild; if not, write to the Free Software
|
---|
24 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
---|
25 | *
|
---|
26 | */
|
---|
27 |
|
---|
28 | #ifndef ___shfile_h
|
---|
29 | #define ___shfile_h
|
---|
30 |
|
---|
31 | #include "shtypes.h"
|
---|
32 | #include "shthread.h"
|
---|
33 | #include <fcntl.h>
|
---|
34 | #include <sys/stat.h>
|
---|
35 | #ifdef _MSC_VER
|
---|
36 | # define _PATH_DEVNULL "nul"
|
---|
37 | # define _PATH_DEFPATH "."
|
---|
38 | #else
|
---|
39 | # if !defined(__sun__)
|
---|
40 | # include <paths.h>
|
---|
41 | # endif
|
---|
42 | # ifndef _PATH_DEVNULL
|
---|
43 | # define _PATH_DEVNULL "/dev/null"
|
---|
44 | # endif
|
---|
45 | # ifndef _PATH_DEFPATH
|
---|
46 | # define _PATH_DEFPATH "/bin:/usr/bin:/sbin:/usr/sbin"
|
---|
47 | # endif
|
---|
48 | #endif
|
---|
49 | #include <limits.h> /* for PIPE_BUF */
|
---|
50 | #ifndef _MSC_VER
|
---|
51 | # include <fcntl.h>
|
---|
52 | # include <unistd.h>
|
---|
53 | # ifndef O_BINARY
|
---|
54 | # define O_BINARY 0
|
---|
55 | # endif
|
---|
56 | # ifndef O_TEXT
|
---|
57 | # define O_TEXT 0
|
---|
58 | # endif
|
---|
59 |
|
---|
60 | #else
|
---|
61 | # include <io.h>
|
---|
62 | # include <direct.h>
|
---|
63 |
|
---|
64 | # define S_ISDIR(m) (((m) & _S_IFMT) == _S_IFDIR)
|
---|
65 | # define S_ISREG(m) (((m) & _S_IFMT) == _S_IFREG)
|
---|
66 | # define S_ISLNK(m) 0
|
---|
67 | # define S_IRWXU (_S_IREAD | _S_IWRITE | _S_IEXEC)
|
---|
68 | # define S_IXUSR _S_IEXEC
|
---|
69 | # define S_IWUSR _S_IWRITE
|
---|
70 | # define S_IRUSR _S_IREAD
|
---|
71 | # define S_IRWXG 0000070
|
---|
72 | # define S_IRGRP 0000040
|
---|
73 | # define S_IWGRP 0000020
|
---|
74 | # define S_IXGRP 0000010
|
---|
75 | # define S_IRWXO 0000007
|
---|
76 | # define S_IROTH 0000004
|
---|
77 | # define S_IWOTH 0000002
|
---|
78 | # define S_IXOTH 0000001
|
---|
79 | # define S_ISUID 0004000
|
---|
80 | # define S_ISGID 0002000
|
---|
81 | # define ALLPERMS 0000777
|
---|
82 |
|
---|
83 | # define F_DUPFD 0
|
---|
84 | # define F_GETFD 1
|
---|
85 | # define F_SETFD 2
|
---|
86 | # define F_GETFL 3
|
---|
87 | # define F_SETFL 4
|
---|
88 | # define FD_CLOEXEC 1
|
---|
89 |
|
---|
90 | # define F_OK 0
|
---|
91 | # define X_OK 1
|
---|
92 | # define W_OK 2
|
---|
93 | # define R_OK 4
|
---|
94 |
|
---|
95 | # define O_NONBLOCK 0 /** @todo */
|
---|
96 |
|
---|
97 | #endif
|
---|
98 | #if K_OS == K_OS_WINDOWS
|
---|
99 | # include "nt/ntstat.h"
|
---|
100 | #endif
|
---|
101 |
|
---|
102 |
|
---|
103 | /**
|
---|
104 | * One file.
|
---|
105 | */
|
---|
106 | typedef struct shfile
|
---|
107 | {
|
---|
108 | int fd; /**< The shell file descriptor number. */
|
---|
109 | unsigned oflags; /**< Open flags. */
|
---|
110 | unsigned shflags; /**< The shell file descriptor flags. */
|
---|
111 | intptr_t native; /**< The native file descriptor number. */
|
---|
112 | #ifdef DEBUG
|
---|
113 | char *dbgname; /**< The name of the file, if applicable, debug builds only. */
|
---|
114 | # define SHFILE_DBGNAME(a) a
|
---|
115 | # else
|
---|
116 | # define SHFILE_DBGNAME(a) NULL
|
---|
117 | #endif
|
---|
118 | } shfile;
|
---|
119 |
|
---|
120 | /** @name shfile::shflags values.
|
---|
121 | * @{
|
---|
122 | */
|
---|
123 | #define SHFILE_FLAGS_CLOSE_ON_EXEC 0x0001
|
---|
124 | #define SHFILE_FLAGS_TRACE 0x0002 /**< The 'trace' file, keep open after execve. */
|
---|
125 | #define SHFILE_FLAGS_TYPE_MASK 0x00f0
|
---|
126 | #define SHFILE_FLAGS_FILE 0x0000
|
---|
127 | #define SHFILE_FLAGS_PIPE 0x0010
|
---|
128 | #define SHFILE_FLAGS_DIR 0x0020
|
---|
129 | #define SHFILE_FLAGS_TTY 0x0030
|
---|
130 | #define SHFILE_FLAGS_DIRTY 0x0100 /**< The file has been written to. */
|
---|
131 | /** @} */
|
---|
132 |
|
---|
133 | /**
|
---|
134 | * The file descriptor table for a shell.
|
---|
135 | */
|
---|
136 | typedef struct shfdtab
|
---|
137 | {
|
---|
138 | shmtx mtx; /**< Mutex protecting any operations on the table and it's handles. */
|
---|
139 | char *cwd; /**< The current directory for this shell instance. */
|
---|
140 | unsigned size; /**< The size of the table (number of entries). */
|
---|
141 | shfile *tab; /**< Pointer to the table. */
|
---|
142 | } shfdtab;
|
---|
143 |
|
---|
144 | int shfile_init(shfdtab *, shfdtab *);
|
---|
145 | void shfile_uninit(shfdtab *, int);
|
---|
146 | void shfile_fork_win(shfdtab *pfdtab, int set, intptr_t *hndls);
|
---|
147 | typedef struct shfdexecwin
|
---|
148 | {
|
---|
149 | int inherithandles;
|
---|
150 | int startsuspended;
|
---|
151 | shmtxtmp tmp;
|
---|
152 | int replacehandles[3];
|
---|
153 | intptr_t handles[3];
|
---|
154 | void *strtinfo;
|
---|
155 | } shfdexecwin;
|
---|
156 | int shfile_exec_win(shfdtab *pfdtab, int prepare, shfdexecwin *info);
|
---|
157 | int shfile_exec_unix(shfdtab *pfdtab);
|
---|
158 | #if K_OS == K_OS_WINDOWS && defined(KASH_ASYNC_CLOSE_HANDLE)
|
---|
159 | void shfile_async_close_sync(void);
|
---|
160 | #endif
|
---|
161 |
|
---|
162 | int shfile_open(shfdtab *, const char *, unsigned, mode_t);
|
---|
163 | #if K_OS == K_OS_WINDOWS
|
---|
164 | # define SHFILE_PIPE_SIZE 65536
|
---|
165 | #elif defined(PIPE_BUF)
|
---|
166 | # define SHFILE_PIPE_SIZE PIPE_BUF
|
---|
167 | #else
|
---|
168 | # define SHFILE_PIPE_SIZE 4096
|
---|
169 | #endif
|
---|
170 | int shfile_pipe(shfdtab *, int [2]);
|
---|
171 | int shfile_close(shfdtab *, unsigned);
|
---|
172 | long shfile_read(shfdtab *, int, void *, size_t);
|
---|
173 | long shfile_write(shfdtab *, int, const void *, size_t);
|
---|
174 | long shfile_lseek(shfdtab *, int, long, int);
|
---|
175 | int shfile_fcntl(shfdtab *, int fd, int cmd, int arg);
|
---|
176 | int shfile_dup(shfdtab *, int fd);
|
---|
177 | int shfile_movefd(shfdtab *, int fdfrom, int fdto);
|
---|
178 | int shfile_movefd_above(shfdtab *, int fdfrom, int fdmin);
|
---|
179 |
|
---|
180 | int shfile_stat(shfdtab *, const char *, struct stat *);
|
---|
181 | int shfile_stat_isreg(shfdtab *, const char *); /**< returns -1, 0 or 1. */
|
---|
182 | int shfile_stat_exists(shfdtab *, const char *); /**< same as shfile_stat, but discards the stat data. */
|
---|
183 | int shfile_lstat(shfdtab *, const char *, struct stat *);
|
---|
184 | int shfile_chdir(shfdtab *, const char *);
|
---|
185 | char *shfile_getcwd(shfdtab *, char *, int);
|
---|
186 | int shfile_access(shfdtab *, const char *, int);
|
---|
187 | int shfile_isatty(shfdtab *, int);
|
---|
188 | int shfile_cloexec(shfdtab *, int, int);
|
---|
189 | int shfile_set_trace(shfdtab *, int);
|
---|
190 | int shfile_ioctl(shfdtab *, int, unsigned long, void *);
|
---|
191 | #if defined(_MSC_VER) || defined(__OS2__)
|
---|
192 | # define TIOCGWINSZ 0x4201
|
---|
193 | typedef struct sh_winsize
|
---|
194 | {
|
---|
195 | unsigned ws_row; /**< Rows, in characters. */
|
---|
196 | unsigned ws_col; /**< Columns, in characters. */
|
---|
197 | unsigned ws_xpixel; /**< Horizontal size, pixels. */
|
---|
198 | unsigned ws_ypixel; /**< Vertical size, pixels. */
|
---|
199 | } sh_winsize;
|
---|
200 | #else
|
---|
201 | typedef struct winsize sh_winsize;
|
---|
202 | #endif
|
---|
203 | mode_t shfile_get_umask(shfdtab *);
|
---|
204 | void shfile_set_umask(shfdtab *, mode_t);
|
---|
205 |
|
---|
206 |
|
---|
207 | typedef struct sh_dirent
|
---|
208 | {
|
---|
209 | char name[260];
|
---|
210 | } shdirent;
|
---|
211 |
|
---|
212 | typedef struct shdir
|
---|
213 | {
|
---|
214 | shfdtab *pfdtab;
|
---|
215 | void *native;
|
---|
216 | shdirent ent;
|
---|
217 | #if K_OS == K_OS_WINDOWS
|
---|
218 | size_t off;
|
---|
219 | size_t cb;
|
---|
220 | char buf[32768 - sizeof(void *) * 2 - sizeof(shdirent) * 2];
|
---|
221 | #endif
|
---|
222 | } shdir;
|
---|
223 |
|
---|
224 | shdir *shfile_opendir(shfdtab *, const char *);
|
---|
225 | shdirent *shfile_readdir(struct shdir *);
|
---|
226 | void shfile_closedir(struct shdir *);
|
---|
227 |
|
---|
228 | #endif
|
---|
229 |
|
---|