Changeset 2424 in kBuild
- Timestamp:
- Oct 18, 2010 8:52:03 AM (14 years ago)
- Location:
- trunk/src/kash
- Files:
-
- 6 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/kash/eval.c
r2423 r2424 511 511 INTON; 512 512 if (prevfd > 0) { 513 shfile_close(&psh->fdtab, 0); 514 copyfd(psh, prevfd, 0); 515 shfile_close(&psh->fdtab, prevfd); 513 movefd(psh, prevfd, 0); 516 514 } 517 515 if (pip[1] >= 0) { 518 516 shfile_close(&psh->fdtab, pip[0]); 519 517 if (pip[1] != 1) { 520 shfile_close(&psh->fdtab, 1); 521 copyfd(psh, pip[1], 1); 522 shfile_close(&psh->fdtab, pip[1]); 518 movefd(psh, pip[1], 1); 523 519 } 524 520 } … … 582 578 shfile_close(&psh->fdtab, pip[0]); 583 579 if (pip[1] != 1) { 584 shfile_close(&psh->fdtab, 1); 585 copyfd(psh, pip[1], 1); 586 shfile_close(&psh->fdtab, pip[1]); 580 movefd(psh, pip[1], 1); 587 581 } 588 582 eflag(psh) = 0; … … 911 905 shfile_close(&psh->fdtab, pip[0]); 912 906 if (pip[1] != 1) { 913 shfile_close(&psh->fdtab, 1); 914 copyfd(psh, pip[1], 1); 915 shfile_close(&psh->fdtab, pip[1]); 907 movefd(psh, pip[1], 1); 916 908 } 917 909 } -
trunk/src/kash/input.c
r2310 r2424 408 408 error(psh, "Can't open %s", fname); 409 409 if (fd < 10) { 410 fd2 = copyfd(psh, fd, 10); 411 shfile_close(&psh->fdtab, fd); 410 fd2 = movefd_above(psh, fd, 10); 412 411 if (fd2 < 0) 413 412 error(psh, "Out of file descriptors"); -
trunk/src/kash/redir.c
r2423 r2424 233 233 234 234 if (f != fd) { 235 copyfd(psh, f, fd); 236 shfile_close(&psh->fdtab, f); 235 movefd(psh, f, fd); 237 236 } 238 237 INTON; … … 302 301 if (i == 0) 303 302 psh->fd0_redirected--; 304 shfile_close(&psh->fdtab, i);305 303 if (rp->renamed[i] >= 0) { 306 copyfd(psh, rp->renamed[i], i); 307 shfile_close(&psh->fdtab, rp->renamed[i]); 304 movefd(psh, rp->renamed[i], i); 305 } else { 306 shfile_close(&psh->fdtab, i); 308 307 } 309 308 } … … 378 377 if (errno == EMFILE) 379 378 return EMPTY; 380 else 381 error(psh, "%d: %s", from, strerror(errno)); 379 error(psh, "%d: %s", from, strerror(errno)); 382 380 } 383 381 return newfd; … … 386 384 387 385 /* 388 * Copy a file descriptor to be= to. Returns -1386 * Move a file descriptor to be == to. Returns -1 389 387 * if the source file descriptor is closed, EMPTY if there are no unused 390 388 * file descriptors left. … … 392 390 393 391 int 394 copyfd2(shinstance *psh, int from, int to)392 movefd(shinstance *psh, int from, int to) 395 393 { 396 394 int newfd; 397 395 398 /** @todo Use dup2()... */ 399 newfd = shfile_fcntl(&psh->fdtab, from, F_DUPFD, to); 396 newfd = shfile_movefd(&psh->fdtab, from, to); 400 397 if (newfd < 0) { 401 398 if (errno == EMFILE) 402 399 return EMPTY; 403 else 404 error(psh, "%d: %s", from, strerror(errno)); 405 } 406 else if (newfd != to) { 407 error(psh, "%d: F_DUPFD returned %d, expected %d", from, newfd, to); 408 close(newfd); 409 newfd = -1; 410 } 411 400 error(psh, "%d: %s", from, strerror(errno)); 401 } 412 402 return newfd; 413 403 } 414 404 405 406 /* 407 * Move a file descriptor to be >= to. Returns -1 408 * if the source file descriptor is closed, EMPTY if there are no unused 409 * file descriptors left. 410 */ 411 412 int 413 movefd_above(shinstance *psh, int from, int to) 414 { 415 int newfd; 416 417 newfd = shfile_movefd_above(&psh->fdtab, from, to); 418 if (newfd < 0) { 419 if (errno == EMFILE) 420 return EMPTY; 421 error(psh, "%d: %s", from, strerror(errno)); 422 } 423 return newfd; 424 } 425 -
trunk/src/kash/redir.h
r1233 r2424 46 46 void clearredir(struct shinstance *, int); 47 47 int copyfd(struct shinstance *, int, int); 48 int movefd(struct shinstance *, int, int); 49 int movefd_above(struct shinstance *, int, int); 48 50 -
trunk/src/kash/shfile.c
r2423 r2424 65 65 # define SHFILE_IN_USE 66 66 #endif 67 # define SHFILE_IN_USE 67 68 /** The max file table size. */ 68 69 #define SHFILE_MAX 1024 … … 176 177 177 178 /** 179 * Grows the descriptor table, making sure that it can hold @a fdMin, 180 * 181 * @returns The max(fdMin, fdFirstNew) on success, -1 on failure. 182 * @param pfdtab The table to grow. 183 * @param fdMin Grow to include this index. 184 */ 185 static int shfile_grow_tab_locked(shfdtab *pfdtab, int fdMin) 186 { 187 /* 188 * Grow the descriptor table. 189 */ 190 int fdRet = -1; 191 shfile *new_tab; 192 int new_size = pfdtab->size + SHFILE_GROW; 193 while (new_size < fdMin) 194 new_size += SHFILE_GROW; 195 new_tab = sh_realloc(shthread_get_shell(), pfdtab->tab, new_size * sizeof(shfile)); 196 if (new_tab) 197 { 198 int i; 199 for (i = pfdtab->size; i < new_size; i++) 200 { 201 new_tab[i].fd = -1; 202 new_tab[i].oflags = 0; 203 new_tab[i].shflags = 0; 204 new_tab[i].native = -1; 205 } 206 207 fdRet = pfdtab->size; 208 if (fdRet < fdMin) 209 fdRet = fdMin; 210 211 pfdtab->tab = new_tab; 212 pfdtab->size = new_size; 213 } 214 215 return fdRet; 216 } 217 218 /** 178 219 * Inserts the file into the descriptor table. 179 220 * … … 219 260 */ 220 261 fd = -1; 221 for (i = 0; (unsigned)i < pfdtab->size; i++) 222 if ( i >= fdMin 223 && pfdtab->tab[i].fd == -1) 262 for (i = fdMin >= 0 ? fdMin : 0; (unsigned)i < pfdtab->size; i++) 263 if (pfdtab->tab[i].fd == -1) 224 264 { 225 265 fd = i; … … 227 267 } 228 268 if (fd == -1) 229 { 230 /* 231 * Grow the descriptor table. 232 */ 233 shfile *new_tab; 234 int new_size = pfdtab->size + SHFILE_GROW; 235 while (new_size < fdMin) 236 new_size += SHFILE_GROW; 237 new_tab = sh_realloc(shthread_get_shell(), pfdtab->tab, new_size * sizeof(shfile)); 238 if (new_tab) 239 { 240 for (i = pfdtab->size; i < new_size; i++) 241 { 242 new_tab[i].fd = -1; 243 new_tab[i].oflags = 0; 244 new_tab[i].shflags = 0; 245 new_tab[i].native = -1; 246 } 247 248 fd = pfdtab->size; 249 if (fd < fdMin) 250 fd = fdMin; 251 252 pfdtab->tab = new_tab; 253 pfdtab->size = new_size; 254 } 255 } 269 fd = shfile_grow_tab_locked(pfdtab, fdMin); 256 270 257 271 /* … … 1134 1148 1135 1149 /** 1150 * Move the file descriptor, closing any existing descriptor at @a fdto. 1151 * 1152 * @returns fdto on success, -1 and errno on failure. 1153 * @param pfdtab The file descriptor table. 1154 * @param fdfrom The descriptor to move. 1155 * @param fdto Where to move it. 1156 */ 1157 int shfile_movefd(shfdtab *pfdtab, int fdfrom, int fdto) 1158 { 1159 #ifdef SHFILE_IN_USE 1160 int rc; 1161 shmtxtmp tmp; 1162 shfile *file = shfile_get(pfdtab, fdfrom, &tmp); 1163 if (file) 1164 { 1165 /* prepare the new entry */ 1166 if (fdto >= (int)pfdtab->size) 1167 shfile_grow_tab_locked(pfdtab, fdto); 1168 if (fdto < (int)pfdtab->size) 1169 { 1170 if (pfdtab->tab[fdto].fd != -1) 1171 shfile_native_close(pfdtab->tab[fdto].native, pfdtab->tab[fdto].oflags); 1172 1173 /* setup the target. */ 1174 pfdtab->tab[fdto].fd = fdto; 1175 pfdtab->tab[fdto].oflags = file->oflags; 1176 pfdtab->tab[fdto].shflags = file->shflags; 1177 pfdtab->tab[fdto].native = file->native; 1178 1179 /* close the source. */ 1180 file->fd = -1; 1181 file->oflags = 0; 1182 file->shflags = 0; 1183 file->native = -1; 1184 1185 rc = fdto; 1186 } 1187 else 1188 { 1189 errno = EMFILE; 1190 rc = -1; 1191 } 1192 1193 shfile_put(pfdtab, file, &tmp); 1194 } 1195 else 1196 rc = -1; 1197 return rc; 1198 1199 #else 1200 return dup2(fdfrom, fdto); 1201 #endif 1202 } 1203 1204 /** 1205 * Move the file descriptor to somewhere at @a fdMin or above. 1206 * 1207 * @returns the new file descriptor success, -1 and errno on failure. 1208 * @param pfdtab The file descriptor table. 1209 * @param fdfrom The descriptor to move. 1210 * @param fdMin The minimum descriptor. 1211 */ 1212 int shfile_movefd_above(shfdtab *pfdtab, int fdfrom, int fdMin) 1213 { 1214 #ifdef SHFILE_IN_USE 1215 int fdto; 1216 shmtxtmp tmp; 1217 shfile *file = shfile_get(pfdtab, fdfrom, &tmp); 1218 if (file) 1219 { 1220 /* find a new place */ 1221 int i; 1222 fdto = -1; 1223 for (i = fdMin; (unsigned)i < pfdtab->size; i++) 1224 if (pfdtab->tab[i].fd == -1) 1225 { 1226 fdto = i; 1227 break; 1228 } 1229 if (fdto == -1) 1230 fdto = shfile_grow_tab_locked(pfdtab, fdMin); 1231 if (fdto != -1) 1232 { 1233 /* setup the target. */ 1234 pfdtab->tab[fdto].fd = fdto; 1235 pfdtab->tab[fdto].oflags = file->oflags; 1236 pfdtab->tab[fdto].shflags = file->shflags; 1237 pfdtab->tab[fdto].native = file->native; 1238 1239 /* close the source. */ 1240 file->fd = -1; 1241 file->oflags = 0; 1242 file->shflags = 0; 1243 file->native = -1; 1244 } 1245 else 1246 { 1247 errno = EMFILE; 1248 fdto = -1; 1249 } 1250 1251 shfile_put(pfdtab, file, &tmp); 1252 } 1253 else 1254 fdto = -1; 1255 return fdto; 1256 1257 #else 1258 int fdnew = fcntl(fdfrom, F_DUPFD, fdMin); 1259 if (fdnew >= 0) 1260 close(fdfrom); 1261 return fdnew; 1262 #endif 1263 } 1264 1265 /** 1136 1266 * close(). 1137 1267 */ … … 1158 1288 1159 1289 #else 1160 fsync(fd);1161 {1162 struct stat s;1163 fstat(fd, &s);1164 TRACE2((NULL, "shfile_close(%d) - %lu bytes\n", fd, (long)s.st_size));1165 }1166 1290 rc = close(fd); 1167 1291 #endif … … 1430 1554 int shfile_chdir(shfdtab *pfdtab, const char *path) 1431 1555 { 1432 shinstance *psh = shthread_get_shell();1433 1556 int rc; 1434 1557 #ifdef SHFILE_IN_USE 1558 shinstance *psh = shthread_get_shell(); 1435 1559 char abspath[SHFILE_MAX_PATH]; 1436 1560 … … 1459 1583 #endif 1460 1584 1461 TRACE2(( psh, "shfile_chdir(,%s) -> %d [%d]\n", path, rc, errno));1585 TRACE2((NULL, "shfile_chdir(,%s) -> %d [%d]\n", path, rc, errno)); 1462 1586 return rc; 1463 1587 } -
trunk/src/kash/shfile.h
r2423 r2424 142 142 int shfile_fcntl(shfdtab *, int fd, int cmd, int arg); 143 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); 144 146 145 147 int shfile_stat(shfdtab *, const char *, struct stat *);
Note:
See TracChangeset
for help on using the changeset viewer.