delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1992/05/02/18:06:30

Date: Sat, 2 May 92 23:03:18 +0100
P1-Message-Id: DE*DBP*RUHR-UNI-BOCHUM; 920502230054045-MTARUBA
Ua-Content-Id: 920502230054045-
From: Torsten DOT Porwol AT RUBA DOT RZ DOT RUHR-UNI-BOCHUM DOT DE
Subject: bug in tmpfile() replacement for tmpnam.c
Importance: high
To: djgpp AT sun DOT soe DOT clarkson DOT edu (Receipt Notification Requested)
(Reply Requested)
Status: O

To whom it may concern,
        the function tmpfile() of the dj contibution 1.06 does not
work correctly. Just try this small program:

#############cut here######################
#include <stdio.h>

void main()
{FILE *t;
t=tmpfile();
exit(0);
}
#############cut here######################

First of all tmpnam() returns always NULL therefore tmpfile() is
unable to open the file. Second of all in the original version
the file is opened with the status w+.
However, I am used to the status w+b. The enclosed file is a
substitution for the original file tmpnam.c. It should
be mentioned that the two functions (itoa() and tmpnam()) are bassicly taken from
the emx contribution of Eberhard Mattes, due to fact that I have been too
lazy to find the bug in the original version.

The functions provided here work without larger difficulties but

I figured out that one should run chkdsk /f after a run of a program
which uses the function tmpfile().

Has anybody got an idea why ?????

If anybody should be interested in getting the f2c libraries for djgpp
he should get in touch with me.



##############################################################################
# Torsten Porwol                     Ruhr-Universitaet Bochum                #
#                                    Lehrstuhl fuer Physikalische Chemie I   #
#                                    Universitaetsstr. 150                   #
#                                    Postfach 10 21 48                       #
#                                    D-4630 Bochum 1                         #
#                                    FRG                                     #
|----------------------------------------------------------------------------|
##############################################################################
|----------------------------------------------------------------------------|
#                                    Phone: (49) - 234 - 700 4219 (office)   #
#                                    Phone: (49) - 201 - 22 74 26            #
#                                    FAX:   (49) - 234 - 700 7231            #
#                       email: torsten DOT porwol AT ruba DOT rz DOT ruhr-uni-bochum DOT de     #
##############################################################################
#### filename: tmpnam.c
#############cut here######################
#include <stdlib.h>

char *itoa (int value, char *string, int radix)
    {
    char *dst;
    char digits[32];
    unsigned x;
    int i, n;

    dst = string;
    if (radix < 2 || radix > 36)
        {
        *dst = 0;
        return (string);
        }
    if (radix == 10 && value < 0)
        {
        *dst++ = '-';
        x = -value;
        }
    else
        x = value;
    i = 0;
    do
        {
        n = x % (unsigned)radix;
        digits[i++] = (n < 10 ? (char)n+'0' : (char)n-10+'a');
        x /= (unsigned)radix;
        } while (x != 0);
    while (i > 0)
        *dst++ = digits[--i];
    *dst = 0;
    return (string);
    }
/* This is file TMPNAM.C */

/*
 * Copyright (c) 1988 Regents of the University of California.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms are permitted provided
 * that: (1) source distributions retain this entire copyright notice and
 * comment, and (2) distributions including binaries display the following
 * acknowledgement:  ``This product includes software developed by the
 * University of California, Berkeley and its contributors'' in the
 * documentation or other materials provided with the distribution and in
 * all advertising materials mentioning features or use of this software.
 * Neither the name of the University nor the names of its contributors may
 * be used to endorse or promote products derived from this software without
 * specific prior written permission.
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
 * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
 */

#if defined(LIBC_SCCS) && !defined(lint)
static char sccsid[] = "@(#)tmpnam.c 4.8 (Berkeley) 6/22/90";
#endif /* LIBC_SCCS and not lint */

#define IDX_LO 10000000
#define IDX_HI 99999999

static int _tmpidx = IDX_LO;


#include <sys/param.h>
#include <sys/types.h>
#include <stdio.h>
#include <errno.h>

#ifndef P_tmpdir
#define P_tmpdir "/tmp"
#endif

FILE *
tmpfile()
{
 FILE *fp;
 char *f, *tmpnam();

 if (!(f = tmpnam((char *)NULL)) || !(fp = fopen(f, "w+b"))) {
  fprintf(stderr, "tmpfile: cannot open %s.\n", f);
  return(NULL);
 }
 (void)unlink(f);
 return(fp);
}
char *tmpnam (char *string)
    {
    char *p;
    int saved_errno, idx_start;
    static char buf[L_tmpnam];

    if (string == NULL) string = buf;
    (void)strcpy (string, P_tmpdir);
    p = strchr (string, 0);
    if (string[0] != 0 && p[-1] != '\\' && p[-1] != '/' && p[-1] != ':')
        *p++ = '/';
    saved_errno = errno;
    idx_start = _tmpidx;
    for (;;)
        {
        if (_tmpidx >= IDX_HI)
            _tmpidx = IDX_LO;
        else
            ++_tmpidx;
        if (_tmpidx == idx_start)
            {
            errno = EINVAL;
            return (NULL);
            }
        (void)itoa (_tmpidx, p, 10);
        (void)strcat (p, ".tmp");
        errno = 0;
        if (access (string, 0) != 0 && errno == ENOENT)
            break;
        }
    errno = saved_errno;
    return (string);
    }

char *
tempnam(dir, pfx)
 const char *dir, *pfx;
{
 char *f, *name, *getenv(), /* *malloc(),*/ *mktemp();

 if (!(name = malloc((u_int)MAXPATHLEN)))
  return(NULL);

 if (f = getenv("TMPDIR")) {
  (void)sprintf(name, "%s/%sXXXXXX", f, pfx ? "" : pfx);
  if (f = mktemp(name))
   return(f);
 }
 if (dir) {
  (void)sprintf(name, "%s/%sXXXXXX", dir, pfx ? "" : pfx);
  if (f = mktemp(name))
   return(f);
 }
 (void)sprintf(name, "%s/%sXXXXXX", P_tmpdir, pfx ? "" : pfx);
 if (f = mktemp(name))
  return(f);
 (void)sprintf(name, "/tmp/%sXXXXXX", pfx ? "" : pfx);
 if (!(f = mktemp(name)))
  (void)free(name);
 return(f);
}
#############cut here######################
############################ EOF tmpnam.c #############################

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019