1 | /* $Id: shfile.h 2243 2009-01-10 02:24:02Z bird $ */
|
---|
2 | /** @file
|
---|
3 | *
|
---|
4 | * File management.
|
---|
5 | *
|
---|
6 | * Copyright (c) 2007-2009 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 | # ifdef _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 | int flags; /**< Open flags. */
|
---|
105 | intptr_t native; /**< The native file descriptor number. */
|
---|
106 | } shfile;
|
---|
107 |
|
---|
108 | /**
|
---|
109 | * The file descriptor table for a shell.
|
---|
110 | */
|
---|
111 | typedef struct shfdtab
|
---|
112 | {
|
---|
113 | shmtx mtx; /**< Mutex protecting any operations on the table and it's handles. */
|
---|
114 | char *cwd; /**< The current directory for this shell instance. */
|
---|
115 | unsigned size; /**< The size of the table (number of entries). */
|
---|
116 | shfile *tab; /**< Pointer to the table. */
|
---|
117 | } shfdtab;
|
---|
118 |
|
---|
119 | int shfile_open(shfdtab *, const char *, unsigned, mode_t);
|
---|
120 | int shfile_pipe(shfdtab *, int [2]);
|
---|
121 | int shfile_close(shfdtab *, unsigned);
|
---|
122 | long shfile_read(shfdtab *, int, void *, size_t);
|
---|
123 | long shfile_write(shfdtab *, int, const void *, size_t);
|
---|
124 | long shfile_lseek(shfdtab *, int, long, int);
|
---|
125 | int shfile_fcntl(shfdtab *, int fd, int cmd, int arg);
|
---|
126 | int shfile_dup(shfdtab *, int fd);
|
---|
127 |
|
---|
128 | int shfile_stat(shfdtab *, const char *, struct stat *);
|
---|
129 | int shfile_lstat(shfdtab *, const char *, struct stat *);
|
---|
130 | int shfile_chdir(shfdtab *, const char *);
|
---|
131 | char *shfile_getcwd(shfdtab *, char *, int);
|
---|
132 | int shfile_access(shfdtab *, const char *, int);
|
---|
133 | int shfile_isatty(shfdtab *, int);
|
---|
134 | int shfile_cloexec(shfdtab *, int, int);
|
---|
135 | int shfile_ioctl(shfdtab *, int, unsigned long, void *);
|
---|
136 | #ifdef _MSC_VER
|
---|
137 | # define TIOCGWINSZ 0x4201
|
---|
138 | typedef struct sh_winsize
|
---|
139 | {
|
---|
140 | unsigned ws_row; /**< Rows, in characters. */
|
---|
141 | unsigned ws_col; /**< Columns, in characters. */
|
---|
142 | unsigned ws_xpixel; /**< Horizontal size, pixels. */
|
---|
143 | unsigned ws_ypixel; /**< Vertical size, pixels. */
|
---|
144 | } sh_winsize;
|
---|
145 | #else
|
---|
146 | typedef struct winsize sh_winsize;
|
---|
147 | #endif
|
---|
148 | mode_t shfile_get_umask(shfdtab *);
|
---|
149 |
|
---|
150 |
|
---|
151 | typedef struct sh_dirent
|
---|
152 | {
|
---|
153 | char name[260];
|
---|
154 | } shdirent;
|
---|
155 |
|
---|
156 | typedef struct shdir
|
---|
157 | {
|
---|
158 | shfdtab *shfdtab;
|
---|
159 | void *native;
|
---|
160 | shdirent ent;
|
---|
161 | } shdir;
|
---|
162 |
|
---|
163 | shdir *shfile_opendir(shfdtab *, const char *);
|
---|
164 | shdirent *shfile_readdir(struct shdir *);
|
---|
165 | void shfile_closedir(struct shdir *);
|
---|
166 |
|
---|
167 | #endif
|
---|
168 |
|
---|