home *** CD-ROM | disk | FTP | other *** search
- #include <sys/param.h>
- #include <sys/types.h>
- #include <errno.h>
- #include <fcntl.h>
- #include <stdio.h>
- #include <filehdr.h>
- #include <scnhdr.h>
- #include <syms.h>
- #include <ldfcn.h>
- #include <aouthdr.h>
-
- extern void couldnot();
-
- extern int errno;
-
- /*
- * Front-end for ldopen()
- * that writes different
- * error messages.
- */
- LDFILE *
- my_ldopen(adotout, ldfp)
- char *adotout;
- LDFILE *ldfp;
- {
- int new_fd;
- int need_cleanup;
- LDFILE *resultfp;
- int saved_errno;
-
- need_cleanup = 1;
-
- if ((new_fd = dup(2)) == -1)
- {
- if (errno == EBADF)
- {
- errno = 0;
- need_cleanup = 0;
- }
- else
- {
- couldnot("dup fd 2 before ldopen(\"%s\", ..)", adotout);
- return (LDFILE *)0;
- }
- }
-
- if (need_cleanup)
- {
- int devnull_fd;
-
- if ((devnull_fd = open("/dev/null", O_WRONLY)) == -1)
- {
- couldnot("open /dev/null before ldopen(\"%s\", ..)", adotout);
- (void)close(new_fd);
- return (LDFILE *)0;
- }
-
- if (dup2(devnull_fd, 2) != 2)
- {
- saved_errno = errno;
- (void)dup2(new_fd, 2);
- errno = saved_errno;
- couldnot("dup2 /dev/null fd before ldopen(\"%s\", ..)", adotout);
- (void)close(devnull_fd);
- (void)close(new_fd);
- return (LDFILE *)0;
- }
-
- if (close(devnull_fd) == -1)
- {
- saved_errno = errno;
- (void)dup2(new_fd, 2);
- errno = saved_errno;
- couldnot("close /dev/null fd before ldopen(\"%s\", ..)", adotout);
- (void)close(new_fd);
- return (LDFILE *)0;
- }
- }
-
- if ((resultfp = ldopen(adotout, ldfp)) == (LDFILE *)0)
- {
- if (need_cleanup)
- {
- saved_errno = errno;
- (void)dup2(new_fd, 2);
- errno = saved_errno;
- }
- couldnot("ldopen \"%s\"", adotout);
- if (need_cleanup)
- (void)close(new_fd);
- return (LDFILE *)0;
- }
-
- if (need_cleanup)
- {
- if (dup2(new_fd, 2) != 2)
- {
- couldnot("dup2 to restore fd 2 after ldopen(\"%s\", ..)", adotout);
- (void)ldclose(resultfp);
- (void)close(new_fd);
- return (LDFILE *)0;
- }
-
- if (close(new_fd) == -1)
- {
- couldnot("close new_fd after ldopen(\"%s\", ..)", adotout);
- (void)ldclose(resultfp);
- return (LDFILE *)0;
- }
- }
-
- return resultfp;
- }
-