1 | /* $Id: shfile.h 2498 2011-07-22 12:05:57Z bird $ */
|
---|
2 | /** @file
|
---|
3 | *
|
---|
4 | * File management.
|
---|
5 | *
|
---|
6 | * Copyright (c) 2007-2010 knut st. osmundsen <[email protected]>
|
---|
7 | *
|
---|
8 | *
|
---|
9 | * This file is part of kBuild.
|
---|
10 | *
|
---|
11 | * kBuild 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 | * kBuild 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 kBuild; if not, write to the Free Software
|
---|
23 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
---|
24 | *
|
---|
25 | */
|
---|
26 |
|
---|
27 | #ifndef ___shfile_h
|
---|
28 | #define ___shfile_h
|
---|
29 |
|
---|
30 | #include "shtypes.h"
|
---|
31 | #include "shthread.h"
|
---|
32 | #include <fcntl.h>
|
---|
33 | #include <sys/stat.h>
|
---|
34 | #ifdef _MSC_VER
|
---|
35 | # define _PATH_DEVNULL "nul"
|
---|
36 | # define _PATH_DEFPATH "."
|
---|
37 | #else
|
---|
38 | # if !defined(__sun__)
|
---|
39 | # include <paths.h>
|
---|
40 | # endif
|
---|
41 | # ifndef _PATH_DEVNULL
|
---|
42 | # define _PATH_DEVNULL "/dev/null"
|
---|
43 | # endif
|
---|
44 | # ifndef _PATH_DEFPATH
|
---|
45 | # define _PATH_DEFPATH "/bin:/usr/bin:/sbin:/usr/sbin"
|
---|
46 | # endif
|
---|
47 | #endif
|
---|
48 | #ifndef _MSC_VER
|
---|
49 | # include <sys/fcntl.h>
|
---|
50 | # include <unistd.h>
|
---|
51 | # ifndef O_BINARY
|
---|
52 | # define O_BINARY 0
|
---|
53 | # endif
|
---|
54 | # ifndef O_TEXT
|
---|
55 | # define O_TEXT 0
|
---|
56 | # endif
|
---|
57 |
|
---|
58 | #else
|
---|
59 | # include <io.h>
|
---|
60 | # include <direct.h>
|
---|
61 |
|
---|
62 | # define S_ISDIR(m) (((m) & _S_IFMT) == _S_IFDIR)
|
---|
63 | # define S_ISREG(m) (((m) & _S_IFMT) == _S_IFREG)
|
---|
64 | # define S_ISLNK(m) 0
|
---|
65 | # define S_IRWXU (_S_IREAD | _S_IWRITE | _S_IEXEC)
|
---|
66 | # define S_IXUSR _S_IEXEC
|
---|
67 | # define S_IWUSR _S_IWRITE
|
---|
68 | # define S_IRUSR _S_IREAD
|
---|
69 | # define S_IRWXG 0000070
|
---|
70 | # define S_IRGRP 0000040
|
---|
71 | # define S_IWGRP 0000020
|
---|
72 | # define S_IXGRP 0000010
|
---|
73 | # define S_IRWXO 0000007
|
---|
74 | # define S_IROTH 0000004
|
---|
75 | # define S_IWOTH 0000002
|
---|
76 | # define S_IXOTH 0000001
|
---|
77 | # define S_ISUID 0004000
|
---|
78 | # define S_ISGID 0002000
|
---|
79 | # define ALLPERMS 0000777
|
---|
80 |
|
---|
81 | # define F_DUPFD 0
|
---|
82 | # define F_GETFD 1
|
---|
83 | # define F_SETFD 2
|
---|
84 | # define F_GETFL 3
|
---|
85 | # define F_SETFL 4
|
---|
86 | # define FD_CLOEXEC 1
|
---|
87 |
|
---|
88 | # define F_OK 0
|
---|
89 | # define X_OK 1
|
---|
90 | # define W_OK 2
|
---|
91 | # define R_OK 4
|
---|
92 |
|
---|
93 | # define O_NONBLOCK 0 /** @todo */
|
---|
94 |
|
---|
95 | #endif
|
---|
96 |
|
---|
97 |
|
---|
98 | /**
|
---|
99 | * One file.
|
---|
100 | */
|
---|
101 | typedef struct shfile
|
---|
102 | {
|
---|
103 | int fd; /**< The shell file descriptor number. */
|
---|
104 | unsigned oflags; /**< Open flags. */
|
---|
105 | unsigned shflags; /**< The shell file descriptor flags. */
|
---|
106 | intptr_t native; /**< The native file descriptor number. */
|
---|
107 | } shfile;
|
---|
108 |
|
---|
109 | /** @name shfile::shflags values.
|
---|
110 | * @{
|
---|
111 | */
|
---|
112 | #define SHFILE_FLAGS_CLOSE_ON_EXEC 0x0001
|
---|
113 | #define SHFILE_FLAGS_TYPE_MASK 0x00f0
|
---|
114 | #define SHFILE_FLAGS_FILE 0x0000
|
---|
115 | #define SHFILE_FLAGS_PIPE 0x0010
|
---|
116 | #define SHFILE_FLAGS_DIR 0x0020
|
---|
117 | #define SHFILE_FLAGS_TTY 0x0030
|
---|
118 | /** @} */
|
---|
119 |
|
---|
120 | /**
|
---|
121 | * The file descriptor table for a shell.
|
---|
122 | */
|
---|
123 | typedef struct shfdtab
|
---|
124 | {
|
---|
125 | shmtx mtx; /**< Mutex protecting any operations on the table and it's handles. */
|
---|
126 | char *cwd; /**< The current directory for this shell instance. */
|
---|
127 | unsigned size; /**< The size of the table (number of entries). */
|
---|
128 | shfile *tab; /**< Pointer to the table. */
|
---|
129 | } shfdtab;
|
---|
130 |
|
---|
131 | int shfile_init(shfdtab *, shfdtab *);
|
---|
132 | void shfile_fork_win(shfdtab *pfdtab, int set, intptr_t *hndls);
|
---|
133 | void *shfile_exec_win(shfdtab *pfdtab, int prepare, unsigned short *sizep, intptr_t *hndls);
|
---|
134 | int shfile_exec_unix(shfdtab *pfdtab);
|
---|
135 |
|
---|
136 | int shfile_open(shfdtab *, const char *, unsigned, mode_t);
|
---|
137 | int shfile_pipe(shfdtab *, int [2]);
|
---|
138 | int shfile_close(shfdtab *, unsigned);
|
---|
139 | long shfile_read(shfdtab *, int, void *, size_t);
|
---|
140 | long shfile_write(shfdtab *, int, const void *, size_t);
|
---|
141 | long shfile_lseek(shfdtab *, int, long, int);
|
---|
142 | int shfile_fcntl(shfdtab *, int fd, int cmd, int arg);
|
---|
143 | int shfile_dup(shfdtab *, int fd);
|
---|
144 | int shfile_movefd(shfdtab *, int fdfrom, int fdto);
|
---|
145 | int shfile_movefd_above(shfdtab *, int fdfrom, int fdmin);
|
---|
146 |
|
---|
147 | int shfile_stat(shfdtab *, const char *, struct stat *);
|
---|
148 | int shfile_lstat(shfdtab *, const char *, struct stat *);
|
---|
149 | int shfile_chdir(shfdtab *, const char *);
|
---|
150 | char *shfile_getcwd(shfdtab *, char *, int);
|
---|
151 | int shfile_access(shfdtab *, const char *, int);
|
---|
152 | int shfile_isatty(shfdtab *, int);
|
---|
153 | int shfile_cloexec(shfdtab *, int, int);
|
---|
154 | int shfile_ioctl(shfdtab *, int, unsigned long, void *);
|
---|
155 | #if defined(_MSC_VER) || defined(__OS2__)
|
---|
156 | # define TIOCGWINSZ 0x4201
|
---|
157 | typedef struct sh_winsize
|
---|
158 | {
|
---|
159 | unsigned ws_row; /**< Rows, in characters. */
|
---|
160 | unsigned ws_col; /**< Columns, in characters. */
|
---|
161 | unsigned ws_xpixel; /**< Horizontal size, pixels. */
|
---|
162 | unsigned ws_ypixel; /**< Vertical size, pixels. */
|
---|
163 | } sh_winsize;
|
---|
164 | #else
|
---|
165 | typedef struct winsize sh_winsize;
|
---|
166 | #endif
|
---|
167 | mode_t shfile_get_umask(shfdtab *);
|
---|
168 | void shfile_set_umask(shfdtab *, mode_t);
|
---|
169 |
|
---|
170 |
|
---|
171 | typedef struct sh_dirent
|
---|
172 | {
|
---|
173 | char name[260];
|
---|
174 | } shdirent;
|
---|
175 |
|
---|
176 | typedef struct shdir
|
---|
177 | {
|
---|
178 | shfdtab *pfdtab;
|
---|
179 | void *native;
|
---|
180 | shdirent ent;
|
---|
181 | #if K_OS == K_OS_WINDOWS
|
---|
182 | size_t off;
|
---|
183 | size_t cb;
|
---|
184 | char buf[32768 - sizeof(void *) * 2 - sizeof(shdirent) * 2];
|
---|
185 | #endif
|
---|
186 | } shdir;
|
---|
187 |
|
---|
188 | shdir *shfile_opendir(shfdtab *, const char *);
|
---|
189 | shdirent *shfile_readdir(struct shdir *);
|
---|
190 | void shfile_closedir(struct shdir *);
|
---|
191 |
|
---|
192 | #endif
|
---|
193 |
|
---|