| 1 | /* $Id: shfile.h 2546 2011-10-01 19:49:54Z 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 | */
|
|---|
| 102 | typedef 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 | } shfile;
|
|---|
| 109 |
|
|---|
| 110 | /** @name shfile::shflags values.
|
|---|
| 111 | * @{
|
|---|
| 112 | */
|
|---|
| 113 | #define SHFILE_FLAGS_CLOSE_ON_EXEC 0x0001
|
|---|
| 114 | #define SHFILE_FLAGS_TYPE_MASK 0x00f0
|
|---|
| 115 | #define SHFILE_FLAGS_FILE 0x0000
|
|---|
| 116 | #define SHFILE_FLAGS_PIPE 0x0010
|
|---|
| 117 | #define SHFILE_FLAGS_DIR 0x0020
|
|---|
| 118 | #define SHFILE_FLAGS_TTY 0x0030
|
|---|
| 119 | /** @} */
|
|---|
| 120 |
|
|---|
| 121 | /**
|
|---|
| 122 | * The file descriptor table for a shell.
|
|---|
| 123 | */
|
|---|
| 124 | typedef struct shfdtab
|
|---|
| 125 | {
|
|---|
| 126 | shmtx mtx; /**< Mutex protecting any operations on the table and it's handles. */
|
|---|
| 127 | char *cwd; /**< The current directory for this shell instance. */
|
|---|
| 128 | unsigned size; /**< The size of the table (number of entries). */
|
|---|
| 129 | shfile *tab; /**< Pointer to the table. */
|
|---|
| 130 | } shfdtab;
|
|---|
| 131 |
|
|---|
| 132 | int shfile_init(shfdtab *, shfdtab *);
|
|---|
| 133 | void shfile_fork_win(shfdtab *pfdtab, int set, intptr_t *hndls);
|
|---|
| 134 | void *shfile_exec_win(shfdtab *pfdtab, int prepare, unsigned short *sizep, intptr_t *hndls);
|
|---|
| 135 | int shfile_exec_unix(shfdtab *pfdtab);
|
|---|
| 136 |
|
|---|
| 137 | int shfile_open(shfdtab *, const char *, unsigned, mode_t);
|
|---|
| 138 | int shfile_pipe(shfdtab *, int [2]);
|
|---|
| 139 | int shfile_close(shfdtab *, unsigned);
|
|---|
| 140 | long shfile_read(shfdtab *, int, void *, size_t);
|
|---|
| 141 | long shfile_write(shfdtab *, int, const void *, size_t);
|
|---|
| 142 | long shfile_lseek(shfdtab *, int, long, int);
|
|---|
| 143 | int shfile_fcntl(shfdtab *, int fd, int cmd, int arg);
|
|---|
| 144 | int shfile_dup(shfdtab *, int fd);
|
|---|
| 145 | int shfile_movefd(shfdtab *, int fdfrom, int fdto);
|
|---|
| 146 | int shfile_movefd_above(shfdtab *, int fdfrom, int fdmin);
|
|---|
| 147 |
|
|---|
| 148 | int shfile_stat(shfdtab *, const char *, struct stat *);
|
|---|
| 149 | int shfile_lstat(shfdtab *, const char *, struct stat *);
|
|---|
| 150 | int shfile_chdir(shfdtab *, const char *);
|
|---|
| 151 | char *shfile_getcwd(shfdtab *, char *, int);
|
|---|
| 152 | int shfile_access(shfdtab *, const char *, int);
|
|---|
| 153 | int shfile_isatty(shfdtab *, int);
|
|---|
| 154 | int shfile_cloexec(shfdtab *, int, int);
|
|---|
| 155 | int shfile_ioctl(shfdtab *, int, unsigned long, void *);
|
|---|
| 156 | #if defined(_MSC_VER) || defined(__OS2__)
|
|---|
| 157 | # define TIOCGWINSZ 0x4201
|
|---|
| 158 | typedef struct sh_winsize
|
|---|
| 159 | {
|
|---|
| 160 | unsigned ws_row; /**< Rows, in characters. */
|
|---|
| 161 | unsigned ws_col; /**< Columns, in characters. */
|
|---|
| 162 | unsigned ws_xpixel; /**< Horizontal size, pixels. */
|
|---|
| 163 | unsigned ws_ypixel; /**< Vertical size, pixels. */
|
|---|
| 164 | } sh_winsize;
|
|---|
| 165 | #else
|
|---|
| 166 | typedef struct winsize sh_winsize;
|
|---|
| 167 | #endif
|
|---|
| 168 | mode_t shfile_get_umask(shfdtab *);
|
|---|
| 169 | void shfile_set_umask(shfdtab *, mode_t);
|
|---|
| 170 |
|
|---|
| 171 |
|
|---|
| 172 | typedef struct sh_dirent
|
|---|
| 173 | {
|
|---|
| 174 | char name[260];
|
|---|
| 175 | } shdirent;
|
|---|
| 176 |
|
|---|
| 177 | typedef struct shdir
|
|---|
| 178 | {
|
|---|
| 179 | shfdtab *pfdtab;
|
|---|
| 180 | void *native;
|
|---|
| 181 | shdirent ent;
|
|---|
| 182 | #if K_OS == K_OS_WINDOWS
|
|---|
| 183 | size_t off;
|
|---|
| 184 | size_t cb;
|
|---|
| 185 | char buf[32768 - sizeof(void *) * 2 - sizeof(shdirent) * 2];
|
|---|
| 186 | #endif
|
|---|
| 187 | } shdir;
|
|---|
| 188 |
|
|---|
| 189 | shdir *shfile_opendir(shfdtab *, const char *);
|
|---|
| 190 | shdirent *shfile_readdir(struct shdir *);
|
|---|
| 191 | void shfile_closedir(struct shdir *);
|
|---|
| 192 |
|
|---|
| 193 | #endif
|
|---|
| 194 |
|
|---|