home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume25 / trash / part01 / myldopen.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-03-22  |  2.0 KB  |  114 lines

  1. #include    <sys/param.h>
  2. #include    <sys/types.h>
  3. #include    <errno.h>
  4. #include    <fcntl.h>
  5. #include    <stdio.h>
  6. #include    <filehdr.h>
  7. #include    <scnhdr.h>
  8. #include    <syms.h>
  9. #include    <ldfcn.h>
  10. #include    <aouthdr.h>
  11.  
  12. extern void    couldnot();
  13.  
  14. extern int    errno;
  15.  
  16. /*
  17.  * Front-end for ldopen()
  18.  * that writes different
  19.  * error messages.
  20.  */
  21. LDFILE    *
  22. my_ldopen(adotout, ldfp)
  23. char    *adotout;
  24. LDFILE    *ldfp;
  25. {
  26.     int    new_fd;
  27.     int    need_cleanup;
  28.     LDFILE    *resultfp;
  29.     int    saved_errno;
  30.  
  31.     need_cleanup = 1;
  32.  
  33.     if ((new_fd = dup(2)) == -1)
  34.     {
  35.         if (errno == EBADF)
  36.         {
  37.             errno = 0;
  38.             need_cleanup = 0;
  39.         }
  40.         else
  41.         {
  42.             couldnot("dup fd 2 before ldopen(\"%s\", ..)", adotout);
  43.             return (LDFILE *)0;
  44.         }
  45.     }
  46.  
  47.     if (need_cleanup)
  48.     {
  49.         int    devnull_fd;
  50.  
  51.         if ((devnull_fd = open("/dev/null", O_WRONLY)) == -1)
  52.         {
  53.             couldnot("open /dev/null before ldopen(\"%s\", ..)", adotout);
  54.             (void)close(new_fd);
  55.             return (LDFILE *)0;
  56.         }
  57.  
  58.         if (dup2(devnull_fd, 2) != 2)
  59.         {
  60.             saved_errno = errno;
  61.             (void)dup2(new_fd, 2);
  62.             errno = saved_errno;
  63.             couldnot("dup2 /dev/null fd before ldopen(\"%s\", ..)", adotout);
  64.             (void)close(devnull_fd);
  65.             (void)close(new_fd);
  66.             return (LDFILE *)0;
  67.         }
  68.  
  69.         if (close(devnull_fd) == -1)
  70.         {
  71.             saved_errno = errno;
  72.             (void)dup2(new_fd, 2);
  73.             errno = saved_errno;
  74.             couldnot("close /dev/null fd before ldopen(\"%s\", ..)", adotout);
  75.             (void)close(new_fd);
  76.             return (LDFILE *)0;
  77.         }
  78.     }
  79.  
  80.     if ((resultfp = ldopen(adotout, ldfp)) == (LDFILE *)0)
  81.     {
  82.         if (need_cleanup)
  83.         {
  84.             saved_errno = errno;
  85.             (void)dup2(new_fd, 2);
  86.             errno = saved_errno;
  87.         }
  88.         couldnot("ldopen \"%s\"", adotout);
  89.         if (need_cleanup)
  90.             (void)close(new_fd);
  91.         return (LDFILE *)0;
  92.     }
  93.  
  94.     if (need_cleanup)
  95.     {
  96.         if (dup2(new_fd, 2) != 2)
  97.         {
  98.             couldnot("dup2 to restore fd 2 after ldopen(\"%s\", ..)", adotout);
  99.             (void)ldclose(resultfp);
  100.             (void)close(new_fd);
  101.             return (LDFILE *)0;
  102.         }
  103.  
  104.         if (close(new_fd) == -1)
  105.         {
  106.             couldnot("close new_fd after ldopen(\"%s\", ..)", adotout);
  107.             (void)ldclose(resultfp);
  108.             return (LDFILE *)0;
  109.         }
  110.     }
  111.  
  112.     return resultfp;
  113. }
  114.