1 | /* $Id: $ */
|
---|
2 | /** @file
|
---|
3 | *
|
---|
4 | * File management.
|
---|
5 | *
|
---|
6 | * Copyright (c) 2007 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 <fcntl.h>
|
---|
32 | #include <sys/stat.h>
|
---|
33 | #ifdef _MSC_VER
|
---|
34 | # define _PATH_DEVNULL "nul"
|
---|
35 | # define _PATH_DEFPATH "."
|
---|
36 | #else
|
---|
37 | # if !defined(__sun__)
|
---|
38 | # include <paths.h>
|
---|
39 | # endif
|
---|
40 | # ifdef _PATH_DEVNULL
|
---|
41 | # define _PATH_DEVNULL "/dev/null"
|
---|
42 | # endif
|
---|
43 | # ifndef _PATH_DEFPATH
|
---|
44 | # define _PATH_DEFPATH "/bin:/usr/bin:/sbin:/usr/sbin"
|
---|
45 | # endif
|
---|
46 | #endif
|
---|
47 | #ifndef _MSC_VER
|
---|
48 | # include <sys/fcntl.h>
|
---|
49 | # include <unistd.h>
|
---|
50 | # ifndef O_BINARY
|
---|
51 | # define O_BINARY 0
|
---|
52 | # endif
|
---|
53 | # ifndef O_TEXT
|
---|
54 | # define O_TEXT 0
|
---|
55 | # endif
|
---|
56 |
|
---|
57 | #else
|
---|
58 | # include <io.h>
|
---|
59 | # include <direct.h>
|
---|
60 |
|
---|
61 | # define S_ISDIR(m) (((m) & _S_IFMT) == _S_IFDIR)
|
---|
62 | # define S_ISREG(m) (((m) & _S_IFMT) == _S_IFREG)
|
---|
63 | # define S_ISLNK(m) 0
|
---|
64 | # define S_IRWXU (_S_IREAD | _S_IWRITE | _S_IEXEC)
|
---|
65 | # define S_IXUSR _S_IEXEC
|
---|
66 | # define S_IWUSR _S_IWRITE
|
---|
67 | # define S_IRUSR _S_IREAD
|
---|
68 | # define S_IRWXG 0000070
|
---|
69 | # define S_IRGRP 0000040
|
---|
70 | # define S_IWGRP 0000020
|
---|
71 | # define S_IXGRP 0000010
|
---|
72 | # define S_IRWXO 0000007
|
---|
73 | # define S_IROTH 0000004
|
---|
74 | # define S_IWOTH 0000002
|
---|
75 | # define S_IXOTH 0000001
|
---|
76 | # define S_ISUID 0004000
|
---|
77 | # define S_ISGID 0002000
|
---|
78 | # define ALLPERMS 0000777
|
---|
79 |
|
---|
80 | # define F_DUPFD 0
|
---|
81 | # define F_GETFD 1
|
---|
82 | # define F_SETFD 2
|
---|
83 | # define F_GETFL 3
|
---|
84 | # define F_SETFL 4
|
---|
85 | # define FD_CLOEXEC 1
|
---|
86 |
|
---|
87 | # define F_OK 0
|
---|
88 | # define X_OK 1
|
---|
89 | # define W_OK 2
|
---|
90 | # define R_OK 4
|
---|
91 |
|
---|
92 | # define O_NONBLOCK 0 /// @todo
|
---|
93 |
|
---|
94 | #endif
|
---|
95 |
|
---|
96 |
|
---|
97 | /**
|
---|
98 | * One file.
|
---|
99 | */
|
---|
100 | typedef struct shfile
|
---|
101 | {
|
---|
102 | int fd; /**< The shell file descriptor number. */
|
---|
103 | int flags; /**< Open flags. */
|
---|
104 | intptr_t native; /**< The native file descriptor number. */
|
---|
105 | } shfile;
|
---|
106 |
|
---|
107 | /**
|
---|
108 | * The file descriptor table for a shell.
|
---|
109 | */
|
---|
110 | typedef struct shfdtab
|
---|
111 | {
|
---|
112 | shmtx mtx; /**< Mutex protecting any operations on the table and it's handles. */
|
---|
113 | char *cwd; /**< The current directory for this shell instance. */
|
---|
114 | unsigned size; /**< The size of the table (number of entries). */
|
---|
115 | shfile *tab; /**< Pointer to the table. */
|
---|
116 | } shfdtab;
|
---|
117 |
|
---|
118 | int shfile_open(shfdtab *, const char *, unsigned);
|
---|
119 | int shfile_pipe(shfdtab *, int [2]);
|
---|
120 | int shfile_close(shfdtab *, unsigned);
|
---|
121 | long shfile_read(shfdtab *, int, void *, size_t);
|
---|
122 | long shfile_write(shfdtab *, int, const void *, size_t);
|
---|
123 | long shfile_lseek(shfdtab *, int, long, int);
|
---|
124 | int shfile_fcntl(shfdtab *, int fd, int cmd, int arg);
|
---|
125 | int shfile_dup(shfdtab *, int fd);
|
---|
126 |
|
---|
127 | int shfile_stat(shfdtab *, const char *, struct stat *);
|
---|
128 | int shfile_lstat(shfdtab *, const char *, struct stat *);
|
---|
129 | int shfile_chdir(shfdtab *, const char *);
|
---|
130 | char *shfile_getcwd(shfdtab *, char *, int);
|
---|
131 | int shfile_isatty(shfdtab *, int);
|
---|
132 | int shfile_ioctl(shfdtab *, int, unsigned long, void *);
|
---|
133 | int shfile_access(shfdtab *, const char *, int);
|
---|
134 | #ifdef _MSC_VER
|
---|
135 | # define TIOCGWINSZ 0x4201
|
---|
136 | typedef struct sh_winsize
|
---|
137 | {
|
---|
138 | unsigned ws_row; /**< Rows, in characters. */
|
---|
139 | unsigned ws_col; /**< Columns, in characters. */
|
---|
140 | unsigned ws_xpixel; /**< Horizontal size, pixels. */
|
---|
141 | unsigned ws_ypixel; /**< Vertical size, pixels. */
|
---|
142 | } sh_winsize;
|
---|
143 | #else
|
---|
144 | typedef struct winsize sh_winsize;
|
---|
145 | #endif
|
---|
146 |
|
---|
147 | typedef struct sh_dirent
|
---|
148 | {
|
---|
149 | char name[260];
|
---|
150 | } shdirent;
|
---|
151 |
|
---|
152 | typedef struct shdir
|
---|
153 | {
|
---|
154 | shfdtab *shfdtab;
|
---|
155 | void *native;
|
---|
156 | shdirent ent;
|
---|
157 | } shdir;
|
---|
158 |
|
---|
159 | shdir *shfile_opendir(shfdtab *, const char *);
|
---|
160 | shdirent *shfile_readdir(struct shdir *);
|
---|
161 | void shfile_closedir(struct shdir *);
|
---|
162 |
|
---|
163 | #endif
|
---|
164 |
|
---|