source: trunk/src/kash/shfile.h@ 3465

Last change on this file since 3465 was 3465, checked in by bird, 5 years ago

kash: Keep the filename around in debug builds. Added profiling of CloseHandle in debug builds, verifying that is is _slow_ when modifying (e.g. appending to) existing files.

  • Property svn:eol-style set to LF
  • Property svn:keywords set to Id
File size: 5.9 KB
Line 
1/* $Id: shfile.h 3465 2020-09-15 19:46:48Z bird $ */
2/** @file
3 * File management.
4 */
5
6/*
7 * Copyright (c) 2007-2010 knut st. osmundsen <bird-kBuild-spamx@anduin.net>
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#ifndef _MSC_VER
50# include <fcntl.h>
51# include <unistd.h>
52# ifndef O_BINARY
53# define O_BINARY 0
54# endif
55# ifndef O_TEXT
56# define O_TEXT 0
57# endif
58
59#else
60# include <io.h>
61# include <direct.h>
62
63# define S_ISDIR(m) (((m) & _S_IFMT) == _S_IFDIR)
64# define S_ISREG(m) (((m) & _S_IFMT) == _S_IFREG)
65# define S_ISLNK(m) 0
66# define S_IRWXU (_S_IREAD | _S_IWRITE | _S_IEXEC)
67# define S_IXUSR _S_IEXEC
68# define S_IWUSR _S_IWRITE
69# define S_IRUSR _S_IREAD
70# define S_IRWXG 0000070
71# define S_IRGRP 0000040
72# define S_IWGRP 0000020
73# define S_IXGRP 0000010
74# define S_IRWXO 0000007
75# define S_IROTH 0000004
76# define S_IWOTH 0000002
77# define S_IXOTH 0000001
78# define S_ISUID 0004000
79# define S_ISGID 0002000
80# define ALLPERMS 0000777
81
82# define F_DUPFD 0
83# define F_GETFD 1
84# define F_SETFD 2
85# define F_GETFL 3
86# define F_SETFL 4
87# define FD_CLOEXEC 1
88
89# define F_OK 0
90# define X_OK 1
91# define W_OK 2
92# define R_OK 4
93
94# define O_NONBLOCK 0 /** @todo */
95
96#endif
97
98
99/**
100 * One file.
101 */
102typedef struct shfile
103{
104 int fd; /**< The shell file descriptor number. */
105 unsigned oflags; /**< Open flags. */
106 unsigned shflags; /**< The shell file descriptor flags. */
107 intptr_t native; /**< The native file descriptor number. */
108#ifdef DEBUG
109 char *dbgname; /**< The name of the file, if applicable, debug builds only. */
110# define SHFILE_DBGNAME(a) a
111# else
112# define SHFILE_DBGNAME(a) NULL
113#endif
114} shfile;
115
116/** @name shfile::shflags values.
117 * @{
118 */
119#define SHFILE_FLAGS_CLOSE_ON_EXEC 0x0001
120#define SHFILE_FLAGS_TRACE 0x0002 /**< The 'trace' file, keep open after execve. */
121#define SHFILE_FLAGS_TYPE_MASK 0x00f0
122#define SHFILE_FLAGS_FILE 0x0000
123#define SHFILE_FLAGS_PIPE 0x0010
124#define SHFILE_FLAGS_DIR 0x0020
125#define SHFILE_FLAGS_TTY 0x0030
126/** @} */
127
128/**
129 * The file descriptor table for a shell.
130 */
131typedef struct shfdtab
132{
133 shmtx mtx; /**< Mutex protecting any operations on the table and it's handles. */
134 char *cwd; /**< The current directory for this shell instance. */
135 unsigned size; /**< The size of the table (number of entries). */
136 shfile *tab; /**< Pointer to the table. */
137} shfdtab;
138
139int shfile_init(shfdtab *, shfdtab *);
140void shfile_uninit(shfdtab *, int);
141void shfile_fork_win(shfdtab *pfdtab, int set, intptr_t *hndls);
142typedef struct shfdexecwin
143{
144 int inherithandles;
145 int startsuspended;
146 shmtxtmp tmp;
147 int replacehandles[3];
148 intptr_t handles[3];
149 void *strtinfo;
150} shfdexecwin;
151int shfile_exec_win(shfdtab *pfdtab, int prepare, shfdexecwin *info);
152int shfile_exec_unix(shfdtab *pfdtab);
153
154int shfile_open(shfdtab *, const char *, unsigned, mode_t);
155int shfile_pipe(shfdtab *, int [2]);
156int shfile_close(shfdtab *, unsigned);
157long shfile_read(shfdtab *, int, void *, size_t);
158long shfile_write(shfdtab *, int, const void *, size_t);
159long shfile_lseek(shfdtab *, int, long, int);
160int shfile_fcntl(shfdtab *, int fd, int cmd, int arg);
161int shfile_dup(shfdtab *, int fd);
162int shfile_movefd(shfdtab *, int fdfrom, int fdto);
163int shfile_movefd_above(shfdtab *, int fdfrom, int fdmin);
164
165int shfile_stat(shfdtab *, const char *, struct stat *);
166int shfile_lstat(shfdtab *, const char *, struct stat *);
167int shfile_chdir(shfdtab *, const char *);
168char *shfile_getcwd(shfdtab *, char *, int);
169int shfile_access(shfdtab *, const char *, int);
170int shfile_isatty(shfdtab *, int);
171int shfile_cloexec(shfdtab *, int, int);
172int shfile_set_trace(shfdtab *, int);
173int shfile_ioctl(shfdtab *, int, unsigned long, void *);
174#if defined(_MSC_VER) || defined(__OS2__)
175# define TIOCGWINSZ 0x4201
176typedef struct sh_winsize
177{
178 unsigned ws_row; /**< Rows, in characters. */
179 unsigned ws_col; /**< Columns, in characters. */
180 unsigned ws_xpixel; /**< Horizontal size, pixels. */
181 unsigned ws_ypixel; /**< Vertical size, pixels. */
182} sh_winsize;
183#else
184typedef struct winsize sh_winsize;
185#endif
186mode_t shfile_get_umask(shfdtab *);
187void shfile_set_umask(shfdtab *, mode_t);
188
189
190typedef struct sh_dirent
191{
192 char name[260];
193} shdirent;
194
195typedef struct shdir
196{
197 shfdtab *pfdtab;
198 void *native;
199 shdirent ent;
200#if K_OS == K_OS_WINDOWS
201 size_t off;
202 size_t cb;
203 char buf[32768 - sizeof(void *) * 2 - sizeof(shdirent) * 2];
204#endif
205} shdir;
206
207shdir *shfile_opendir(shfdtab *, const char *);
208shdirent *shfile_readdir(struct shdir *);
209void shfile_closedir(struct shdir *);
210
211#endif
212
Note: See TracBrowser for help on using the repository browser.