/* * ep - extended pipelining * * fd.c - filedescriptor structure implementation * ____________________________________________________________ * * Developed by Ondrej Jombik * and Lubomir Host * Copyright (c) 2000-2003 Platon SDG, http://platon.sk/ * All rights reserved. * * See README file for more information about this software. * See COPYING file for license information. * * Download the latest version from * http://platon.sk/projects/ep/ */ /* $Platon: ep/src/fd.c,v 1.15 2003/05/03 09:58:19 nepto Exp $ */ #ifdef HAVE_CONFIG_H # include "config.h" #endif #include #include #if HAVE_UNISTD_H # include # include #endif #ifdef STDC_HEADERS # include #endif #include "fd.h" #include "message.h" static char *fd_str[N_FD_STR][N_FD] = { {"input", "output", "errput"}, {"stdin", "stdout", "stderr"}, {"in", "out", "err"}, {"i", "o", "e"}, }; static int my_close(fd) int fd; { #if DEBUG msg_print("fd.c: my_close(%d)\n", fd); #endif if (fd == 0 || fd == 1 || fd == 2) return 0; return close(fd); } char * fd_id_to_fd_str(id) int id; { return fd_str[0][id]; } int fd_str_to_fd_id(str) char *str; { register int i, j; for (i = 0; i < N_FD_STR; i++) for (j = 0; j < N_FD; j++) { if (! strcasecmp(fd_str[i][j], str)) return j; } return -1; } int fd_add_proclink(fd, link) struct fd *fd; const struct proclink *link; { #if DEBUG msg_debug_print("fd_add_proclink(): link = (%d.%d)\n", link->proc_id, link->fd_id); #endif fd->link = (struct proclink *) realloc(fd->link, (fd->n_link + 1) * sizeof(struct proclink)); if (fd->link == NULL) { msg_error_print("fd_add_proclink(): realloc() failure\n"); return 0; } /* Simple structure assignment. */ fd->link[fd->n_link] = *link; fd->n_link++; return 1; } int fd_remove_proclink(fd, link) struct fd *fd; const struct proclink *link; { register int ord; #if DEBUG msg_debug_print("fd_remove_proclink(): link = (%d.%d)\n", link->proc_id, link->fd_id); #endif while ((ord = fd_is_proclink(fd, link)) != 0) /* ord is index + 1 */ if (fd_remove_proclink_by_index(fd, ord - 1) != 1) return 0; return 1; } int fd_remove_proclink2(fd, link_proc_id, link_fd_id) struct fd *fd; int link_proc_id; int link_fd_id; { register int ord; #if DEBUG msg_debug_print("fd_remove_proclink2(): link = (%d.%d)\n", link_proc_id, link_fd_id); #endif while ((ord = fd_is_proclink2(fd, link_proc_id, link_fd_id)) != 0) if (fd_remove_proclink_by_index(fd, ord - 1) != 1) return 0; return 1; } int fd_remove_proclink_by_index(fd, index) struct fd *fd; const int index; { register int i; #if DEBUG msg_debug_print("fd_remove_proclink_by_index(): index = %d\n", index); msg_debug_print("fd_remove_proclink_by_index(): n_link before = %d\n", fd->n_link); #endif /* Copying proclinks down by 1. */ for (i = index + 1; i < fd->n_link; i++) fd->link[i - 1] = fd->link[i]; if (fd->n_link > 1) { /* Reallocating array. */ fd->link = (struct proclink *)realloc(fd->link, (fd->n_link - 1) * sizeof(struct proclink)); if (fd->link == NULL) { msg_error_print("fd_remove_proclink_by_index(): " "realloc() failure\n"); return 0; } fd->n_link--; } else { my_close(fd->pipe[0]); my_close(fd->pipe[1]); free(fd->link); fd->link = NULL; fd->n_link = 0; } #if DEBUG msg_debug_print("fd_remove_proclink_by_index(): n_link after = %d\n", fd->n_link); #endif return 1; } int fd_is_proclink(fd, link) struct fd *fd; const struct proclink *link; { register int i; for (i = 0; i < fd->n_link; i++) /* * Why here cannot be this: if (fd->link[i] == *link) * Does somebody know it?!? */ if (fd->link[i].proc_id == link->proc_id && fd->link[i].fd_id == link->fd_id) return i + 1; return 0; } int fd_is_proclink2(fd, link_proc_id, link_fd_id) struct fd *fd; int link_proc_id; int link_fd_id; { register int i; for (i = 0; i < fd->n_link; i++) if (fd->link[i].proc_id == link_proc_id && fd->link[i].fd_id == link_fd_id) return i + 1; return 0; }