This is the mail archive of the ecos-patches@sources.redhat.com mailing list for the eCos project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: Misc generic code changes


>> +///////////////////////////////////////////////////////////////////////////
> +// tmpnam()
> +
> +__externC char *tmpnam( char *s ) __THROW
> +{
> +    STDIO_ENTRY();
> +    static char staticbuf[ L_tmpnam ];
> +#if (TMP_MAX < 256)
> +    typedef cyg_uint8 counttype;
> +#elif (TMP_MAX < 65536)
> +    typedef cyg_uint16 counttype;
> +#else
> +    typedef cyg_ucount32 counttype;
> +#endif
> +    static counttype count;
> +    counttype totaliters=0;
> +    int i;
> +
> +    if ( NULL != s )
> +        CYG_CHECK_DATA_PTR( s, "supplied string pointer invalid" );
> +    else
> +        s = staticbuf;
> +
> +    // start off by making it "tmp00000" etc. so we can fill backwards
> +    // from end without spaces
> +    s[0] = 't'; s[1] = 'm'; s[2] = 'p';
> +
> +    while (totaliters < TMP_MAX)
> +    {
> +        for (i=3; i < (L_tmpnam-1); i++)
> +        {
> +            s[i] = '0';
> +        }
> +        s[i] = '\0';
> +
> +        counttype counttmp = count;
> +        for (i=(L_tmpnam-1); i>2; i--)
> +        {
> +            const char tohex[] = "0123456789abcdef";
> +            s[i] = tohex[counttmp & 0xf];
> +            counttmp = counttmp >> 4;
> +        }
> +        count++;
> +        count %= TMP_MAX; // cycle round
> +        totaliters++;
> +
> +        // s now points to a name candidate
> +#ifdef CYGPKG_LIBC_STDIO_FILEIO
> +        int fd = open( s, O_RDONLY );
> +        close(fd);
> +        if (fd < 0 && ENOENT == errno) // we have a winner
> +            break;
> +#else
> +        break; // no real filesystem, so just go with what we've come up with
> +#endif

Wouldn't it be better to close just the winner and not close all the
loosers who should have an fd of -1 making the close pointless and a
waste of time.

        Andrew


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]