lynx-dev
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

freopen (was: Re: LYNX-DEV 4DOS)


From: Laura Eaves
Subject: freopen (was: Re: LYNX-DEV 4DOS)
Date: Thu, 8 May 1997 03:53:15 -0400 (EDT)

> Date: Wed, 7 May 1997 23:41:51 -0400 (EDT)
> From: ELCHONON EDELSON <address@hidden>
>...
> On Wed, 7 May 1997, Foteos Macrides wrote:
>
> > ELCHONON EDELSON <address@hidden> wrote:
> > >how hard would it be to allow a command-line option to
> > >freopen() stderr onto a specified output file? After
> > >appropriate access checks, of course.
> > 
> >     Duhhh, of course!!!!  I just tried it, and it works fine.
> > What do you have in mind for access checks?
> > 
> >                             Fote
>
> You would probably want to check for permission to write to the
> specified file, so you don't lose your stderr if the open fails.
> I I recall correctly, freopen will close the given descriptor
> before attempting the open, so if the open fails you've lost
> your stderr and any code that writes to it (such as all the debugging
> you have scattered through the whole source base) will cause
> a possibly fatal error.

The following test program using access() seems to work and
could be adapted for freopening stderr to a fiilename specified by the user.

I still think prompting the user for a file name when ^T toggles trace to ON
is better than using a command line option.
This would also allow a user to specify multiple output files for different
traces without exiting lynx, and it's not much harder to implement than a
command line option.
--le

#include <stdio.h>
#include <unistd.h>

main(argc, argv)
int argc; char** argv;
{
        int i;
        for ( i = 1;  i < argc;  ++i ) {
            int ok = 1;
            char* filename = argv[i];
            printf("checking '%s'\n",filename);
            if ( access(filename,F_OK) == -1 ) {
                /* file does not exist or is not accessible.  Check for write
                 * permission on parent dir
                 */
                char* dir;
                int x = strlen(filename);
                int c = 0;
                while ( x > 0 && filename[x] != '/' ) --x;
                if ( filename[x] == '/' ) {
                    c = filename[++x]; filename[x] = '\0';
                }
                dir = (x==0) ? "." : filename;
                printf(" - file does not exist or is inaccessible\n");
                printf(" - checking parent directory %s\n",dir);
                if ( access(dir,W_OK) == -1 ) ok = 0;
                if ( !ok )
                    printf(" - parent dir does not exist or is not writable\n");
                if ( c ) filename[x] = c;
            } else if ( access(filename,R_OK|W_OK) == -1 ) {
                printf(" - file exists but does not have RW permission\n");
                ok = 0;
            }
            if ( ok ) {
                /* file either exists and has RW permission, or does not exist
                 * but the parent directory is writable
                 */
                FILE* fd = freopen(filename,"a+",stderr);
                printf("freopen(%s,\"a+\",stderr)\n",filename);
                if ( fd == NULL ) {
                    printf("%s: FAILED TO REOPEN STDERR!!!\n",filename);
                    /* exit(1); */
                } else
                    fprintf(stderr,"testing %s\n",filename);
            }
        }
        return 0;
}

;
; To UNSUBSCRIBE:  Send a mail message to address@hidden
;                  with "unsubscribe lynx-dev" (without the
;                  quotation marks) on a line by itself.
;

reply via email to

[Prev in Thread] Current Thread [Next in Thread]