Exemple #1
0
char *
tempnam(const char *dir, const char *pfx)
{
	int sverrno, len;
	char *f, *name;

	if (!(name = malloc(PATH_MAX)))
		return(NULL);

	if (!pfx)
		pfx = "tmp.";

	if (issetugid() == 0 && (f = getenv("TMPDIR")) && *f != '\0') {
		len = snprintf(name, PATH_MAX, "%s%s%sXXXXXXXXXX", f,
		    f[strlen(f) - 1] == '/' ? "" : "/", pfx);
		if (len < 0 || len >= PATH_MAX) {
			errno = ENAMETOOLONG;
			return(NULL);
		}
		if ((f = _mktemp(name)))
			return(f);
	}

	if (dir != NULL) {
		f = *dir ? (char *)dir : ".";
		len = snprintf(name, PATH_MAX, "%s%s%sXXXXXXXXXX", f,
		    f[strlen(f) - 1] == '/' ? "" : "/", pfx);
		if (len < 0 || len >= PATH_MAX) {
			errno = ENAMETOOLONG;
			return(NULL);
		}
		if ((f = _mktemp(name)))
			return(f);
	}

	f = P_tmpdir;
	len = snprintf(name, PATH_MAX, "%s%sXXXXXXXXX", f, pfx);
	if (len < 0 || len >= PATH_MAX) {
		errno = ENAMETOOLONG;
		return(NULL);
	}
	if ((f = _mktemp(name)))
		return(f);

	f = _PATH_TMP;
	len = snprintf(name, PATH_MAX, "%s%sXXXXXXXXX", f, pfx);
	if (len < 0 || len >= PATH_MAX) {
		errno = ENAMETOOLONG;
		return(NULL);
	}
	if ((f = _mktemp(name)))
		return(f);

	sverrno = errno;
	free(name);
	errno = sverrno;
	return(NULL);
}
CDemoRecorder::CDemoRecorder()
{
	// We want this folder to exist
	if (!filesystem.CreateDirectory("demos"))
		return;
	
	char buf[500] = "demos/XXXXXX";
#ifndef _WIN32
	mkstemp(buf);
#else
	_mktemp(buf);
#endif

	
	demoName = wantedName = buf;
	recordDemo=SAFE_NEW std::ofstream(filesystem.LocateFile(demoName, FileSystem::WRITE).c_str(), std::ios::out|std::ios::binary);

	if (gameSetup)
	{
		// add a TDF section containing the game version to the startup script
		std::string scriptText = MakeDemoStartScript (gameSetup->gameSetupText, gameSetup->gameSetupTextLength);
	
		char c=1;
		recordDemo->write(&c,1);
		int len = scriptText.length();
		recordDemo->write((char*)&len, sizeof(int));
		recordDemo->write(scriptText.c_str(), scriptText.length());
	}
	else
	{
		char c=0;
		recordDemo->write(&c,1);
	}
}
Exemple #3
0
	char *tmpnam(

/*  SYNOPSIS */
	char *s)

/*  FUNCTION

    INPUTS

    RESULT

    NOTES

    EXAMPLE

    BUGS

    SEE ALSO

    INTERNALS

******************************************************************************/
{
	static u_long tmpcount;
	static char buf[L_tmpnam];

	if (s == NULL)
		s = buf;
	(void)snprintf(s, L_tmpnam, "%stmp.%lu.XXXXXX", P_tmpdir, tmpcount);
	++tmpcount;
	return (_mktemp(s));
}
Exemple #4
0
void gmx_tmpnam(char *buf)
{
    int i,len,fd;

    if ((len = strlen(buf)) < 7)
        gmx_fatal(FARGS,"Buf passed to gmx_tmpnam must be at least 7 bytes long");
    for(i=len-6; (i<len); i++) {
        buf[i] = 'X';
    }
    /* mktemp is dangerous and we should use mkstemp instead, but 
     * since windows doesnt support it we have to separate the cases.
     * 20090307: mktemp deprecated, use iso c++ _mktemp instead.
     */
#if ((defined WIN32 || defined _WIN32 || defined WIN64 || defined _WIN64) && !defined __CYGWIN__ && !defined __CYGWIN32__)
    _mktemp(buf);
#else
    fd = mkstemp(buf);

    switch (fd) {
        case EINVAL:
            gmx_fatal(FARGS,"Invalid template %s for mkstemp",buf);
            break;
        case EEXIST:
            gmx_fatal(FARGS,"mkstemp created existing file",buf);
            break;
        case EACCES: 
            gmx_fatal(FARGS,"Permission denied for opening %s",buf);
            break;
        default:
            break;
    }   
    close(fd);
#endif
    /* name in Buf should now be OK */
}
Exemple #5
0
static YAP_Bool p_mktemp(void) {
#if HAVE_MKSTEMP || HAVE_MKTEMP || defined(__MINGW32__) || _MSC_VER
  char *s, tmp[BUF_SIZE];
  s = (char *)YAP_AtomName(YAP_AtomOfTerm(YAP_ARG1));
#if HAVE_STRNCPY
  strncpy(tmp, s, BUF_SIZE);
#else
  strcpy(tmp, s);
#endif
#if defined(__MINGW32__) || _MSC_VER
  if ((s = _mktemp(tmp)) == NULL) {
    /* return an error number */
    return (YAP_Unify(YAP_ARG3, YAP_MkIntTerm(errno)));
  }
  return (YAP_Unify(YAP_ARG2, YAP_MkAtomTerm(YAP_LookupAtom(s))));
#elif HAVE_MKSTEMP
  strcpy(tmp, "/tmp/YAP_tmpXXXXXXXX");
  if (mkstemp(tmp) == -1) {
    /* return an error number */
    return (YAP_Unify(YAP_ARG3, YAP_MkIntTerm(errno)));
  }
  return YAP_Unify(YAP_ARG2, YAP_MkAtomTerm(YAP_LookupAtom(tmp)));
#else
  if ((s = mktemp(tmp)) == NULL) {
    /* return an error number */
    return (YAP_Unify(YAP_ARG3, YAP_MkIntTerm(errno)));
  }
  return YAP_Unify(YAP_ARG2, YAP_MkAtomTerm(YAP_LookupAtom(s)));
#endif
#else
  return FALSE;
#endif
  return (TRUE);
}
Exemple #6
0
void gmx_tmpnam(char *buf)
{
    int i, len;

    if ((len = strlen(buf)) < 7)
    {
        gmx_fatal(FARGS, "Buf passed to gmx_tmpnam must be at least 7 bytes long");
    }
    for (i = len-6; (i < len); i++)
    {
        buf[i] = 'X';
    }
    /* mktemp is dangerous and we should use mkstemp instead, but
     * since windows doesnt support it we have to separate the cases.
     * 20090307: mktemp deprecated, use iso c++ _mktemp instead.
     */
#ifdef GMX_NATIVE_WINDOWS
    _mktemp(buf);
#else
    int fd = mkstemp(buf);

    if (fd < 0)
    {
        gmx_fatal(FARGS, "Creating temporary file %s: %s", buf,
                  strerror(errno));
    }
    close(fd);
#endif
    /* name in Buf should now be OK */
}
Exemple #7
0
void
FUNCTION (test, binary) (const size_t M, const size_t N)
{
  TYPE (gsl_matrix) * m = FUNCTION (gsl_matrix, calloc) (M, N);

  size_t i, j;
  size_t k = 0;

  char filename[] = "test.XXXXXX";
#if !defined( _MSC_VER )
  int fd = mkstemp(filename);
#else
  char * fd = _mktemp(filename);
# define fdopen fopen
#endif

  {
    FILE *f = fdopen(fd, "wb");
    k = 0;
    for (i = 0; i < M; i++)
      {
        for (j = 0; j < N; j++)
          {
            k++;
            FUNCTION (gsl_matrix, set) (m, i, j, (BASE) k);
          }
      }

    FUNCTION (gsl_matrix, fwrite) (f, m);
    fclose (f);
  }

  {
    FILE *f = fopen (filename, "rb");
    TYPE (gsl_matrix) * mm = FUNCTION (gsl_matrix, alloc) (M, N);
    status = 0;

    FUNCTION (gsl_matrix, fread) (f, mm);
    k = 0;
    for (i = 0; i < M; i++)
      {
        for (j = 0; j < N; j++)
          {
            k++;
            if (mm->data[i * N + j] != (BASE) k)
              status = 1;
          }
      }

    gsl_test (status, NAME (gsl_matrix) "_write and read");

    fclose (f);
    FUNCTION (gsl_matrix, free) (mm);
  }

  unlink(filename);

  FUNCTION (gsl_matrix, free) (m);
}
inline int mkstemp(char *tmp)
{
  int fd;

  if (!_mktemp(tmp))
    return -1;
  fd = _open(tmp, _O_CREAT|_O_RDWR|_O_TEXT|_O_TRUNC, _S_IREAD|_S_IWRITE);
  return fd;
}
Exemple #9
0
//----------------------------------------------------------------------------------
// Opens a temporary file
std::string Gnuplot::create_tmpfile(std::ofstream &tmp)
{
#if defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(__TOS_WIN__)
    char name[] = "gnuplotiXXXXXX"; //tmp file in working directory
#elif defined(unix) || defined(__unix) || defined(__unix__) || defined(__APPLE__)
    char name[] = "/tmp/gnuplotiXXXXXX"; // tmp file in /tmp
#endif

    // check if maximum number of temporary files reached
    if (Gnuplot::tmpfile_num == GP_MAX_TMP_FILES - 1)
    {
        std::ostringstream except;
        except << "Maximum number of temporary files reached (" << GP_MAX_TMP_FILES
               << "): cannot open more files" << std::endl;

        throw GnuplotException( except.str() );
        return "";
    }

    // int mkstemp(char *name);
    // shall replace the contents of the string pointed to by "name" by a unique filename,
    // and return a file descriptor for the file open for reading and writing.
    // Otherwise, -1 shall be returned if no suitable file could be created.
    // The string in template should look like a filename with six trailing 'X' s;
    // mkstemp() replaces each 'X' with a character from the portable filename character set.
    // The characters are chosen such that the resulting name does not duplicate the name of an existing file at the time of a call to mkstemp()


    // open temporary files for output
#if defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(__TOS_WIN__)
		if (_mktemp(name) == nullptr)
#elif defined(unix) || defined(__unix) || defined(__unix__) || defined(__APPLE__)
    if (mkstemp(name) == -1)
#endif
    {
        std::ostringstream except;
        except << "Cannot create temporary file \"" << name << "\"";
        throw GnuplotException(except.str());
        return "";
    }

    tmp.open(name);
    if (tmp.bad())
    {
        std::ostringstream except;
        except << "Cannot create temporary file \"" << name << "\"";
        throw GnuplotException(except.str());
        return "";
    }

    // Save the temporary filename
    this->tmpfile_list.push_back(name);
    Gnuplot::tmpfile_num++;

    return name;
}
Exemple #10
0
int
_zip_mkstemp(char *path)
{
    int ret = -1;

    _mktemp(path);
    ret = _open(path, O_RDWR|O_BINARY|O_CREAT|O_EXCL|_O_SHORT_LIVED, _S_IREAD|_S_IWRITE); 

    return ret;
}
Exemple #11
0
char *
tempnam(const char *dir, const char *pfx)
{
	int sverrno;
	char *f, *name;

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

	if (!pfx)
		pfx = "tmp.";

	if (issetugid() == 0 && (f = getenv("TMPDIR"))) {
		(void)snprintf(name, MAXPATHLEN, "%s%s%sXXXXXX", f,
		    *(f + strlen(f) - 1) == '/'? "": "/", pfx);
		if ((f = _mktemp(name)))
			return(f);
	}

	if ((f = (char *)dir)) {
		(void)snprintf(name, MAXPATHLEN, "%s%s%sXXXXXX", f,
		    *(f + strlen(f) - 1) == '/'? "": "/", pfx);
		if ((f = _mktemp(name)))
			return(f);
	}

	f = P_tmpdir;
	(void)snprintf(name, MAXPATHLEN, "%s%sXXXXXX", f, pfx);
	if ((f = _mktemp(name)))
		return(f);

	f = _PATH_TMP;
	(void)snprintf(name, MAXPATHLEN, "%s%sXXXXXX", f, pfx);
	if ((f = _mktemp(name)))
		return(f);

	sverrno = errno;
	free(name);
	errno = sverrno;
	return(NULL);
}
Exemple #12
0
int p_mkstemp(char *tmp_path)
{
#if defined(_MSC_VER)
	if (_mktemp_s(tmp_path, strlen(tmp_path) + 1) != 0)
		return GIT_EOSERR;
#else
	if (_mktemp(tmp_path) == NULL)
		return GIT_EOSERR;
#endif

	return p_creat(tmp_path, 0744);
}
Exemple #13
0
char *
tmpnam(char *s)
{
  static long unsigned int tmpcount = 0;  // Must be long to satisfy the %lu in snprintf below.
  static char buf[L_tmpnam];

  if (s == NULL)
    s = buf;
  (void)snprintf(s, L_tmpnam, "%stmp_%lu_XXXX", _PATH_TMP, tmpcount);
  ++tmpcount;
  return (_mktemp(s));
}
Exemple #14
0
int mkstemp(char *input)
{
	input = _mktemp(input);
	if (input == NULL)
	{
		errno = EEXIST;
		return -1;
	}
	
	int fd = open(input, O_WRONLY | O_CREAT, S_IREAD | S_IWRITE);
	return fd;
}
Exemple #15
0
char *
tmpnam(char *s)
{
	static u_long tmpcount;
	static char buf[L_tmpnam];

	if (s == NULL)
		s = buf;
	(void)snprintf(s, L_tmpnam, "%stmp.%lu.XXXXXXXXX", P_tmpdir, tmpcount);
	++tmpcount;
	return (_mktemp(s));
}
Exemple #16
0
void
FUNCTION (test, file) (size_t stride, size_t N)
{
  TYPE (gsl_vector) * v = FUNCTION (create, vector) (stride, N);
  TYPE (gsl_vector) * w = FUNCTION (create, vector) (stride, N);

  size_t i;

  char filename[] = "test.XXXXXX";
#if !defined( _WIN32 )
  int fd = mkstemp(filename);
#else
  char * fd = _mktemp(filename);
#  define fdopen fopen
#endif
  {
    FILE *f = fdopen (fd, "wb");

    for (i = 0; i < N; i++)
      {
        FUNCTION (gsl_vector, set) (v, i, (ATOMIC) (N - i));
      };

    FUNCTION (gsl_vector, fwrite) (f, v);

    fclose (f);
  }

  {
    FILE *f = fopen (filename, "rb");

    FUNCTION (gsl_vector, fread) (f, w);

    status = 0;
    for (i = 0; i < N; i++)
      {
        if (w->data[i*stride] != (ATOMIC) (N - i))
          status = 1;
      };

    TEST (status, "_write and read");

    fclose (f);
  }

  unlink(filename);

  FUNCTION (gsl_vector, free) (v);      /* free whatever is in v */
  FUNCTION (gsl_vector, free) (w);      /* free whatever is in w */
}
Exemple #17
0
///////////////////////////////////////////////////////////////////////
// Function				:	osTempname
// Description			:	Create a temporary file
// Return Value			:	-
// Comments				:	The directory must end with / (or \)
void	osTempname(const char *directory,const char *prefix,char *result) {
	#ifdef _WINDOWS
		// avoid some windows shortcomings by extending count when we
		// start to get clashes
		static int i = 0;
		sprintf(result,"%s%s-%4xXXXXXXXX",directory,prefix,i);
		while(_mktemp(result) == NULL) {
			sprintf(result,"%s%s-%4xXXXXXXXX",directory,prefix,i++);
		}
	#else
		sprintf(result,"%s%s-XXXXXXXX",directory,prefix);
		mktemp(result);
	#endif
}
Exemple #18
0
void
FUNCTION (test, text) (size_t stride, size_t N)
{
  TYPE (gsl_vector) * v = FUNCTION (create, vector) (stride, N);
  TYPE (gsl_vector) * w = FUNCTION (create, vector) (stride, N);

  size_t i;

  char filename[] = "test.XXXXXX";
#if !defined( _WIN32 )
   int fd = mkstemp(filename);
#else
   char * fd = _mktemp(filename);
#  define fdopen fopen
#endif
  {
    FILE *f = fdopen (fd, "w");

    for (i = 0; i < N; i++)
      {
        FUNCTION (gsl_vector, set) (v, i, (ATOMIC) i);
      };

    FUNCTION (gsl_vector, fprintf) (f, v, OUT_FORMAT);

    fclose (f);
  }

  {
    FILE *f = fopen (filename, "r");

    FUNCTION (gsl_vector, fscanf) (f, w);

    status = 0;
    for (i = 0; i < N; i++)
      {
        if (w->data[i*stride] != (ATOMIC) i)
          status = 1;
      };

    gsl_test (status, NAME (gsl_vector) "_fprintf and fscanf");

    fclose (f);
  }

  unlink(filename);

  FUNCTION (gsl_vector, free) (v);
  FUNCTION (gsl_vector, free) (w);
}
Exemple #19
0
int
mkstemp(char *tpl)
{
  int fd = -1;
  char *p;
  int e = errno;

  errno = 0;
  p = _mktemp(tpl);
  if (*p && errno == 0)
    {
      errno = e;
      fd = _open(p, _O_RDWR | _O_CREAT | _O_EXCL | _O_BINARY,
		 _S_IREAD | _S_IWRITE);
    }
  return fd;
}
Exemple #20
0
char const * gnuplot_tmpfile(gnuplot_ctrl * handle)
{
    static char const * tmp_filename_template = "gnuplot_tmpdatafile_XXXXXX";
    char *              tmp_filename = NULL;
    int                 tmp_filelen = strlen(tmp_filename_template);

#ifndef _WIN32
    int                 unx_fd;
#endif // #ifndef _WIN32

    assert(handle->tmp_filename_tbl[handle->ntmp] == NULL);

    /* Open one more temporary file? */
    if (handle->ntmp == GP_MAX_TMP_FILES - 1) {
        fprintf(stderr,
                "maximum # of temporary files reached (%d): cannot open more",
                GP_MAX_TMP_FILES) ;
        return NULL;
    }

    tmp_filename = (char*) malloc(tmp_filelen+1);
    if (tmp_filename == NULL)
    {
        return NULL;
    }
    strcpy(tmp_filename, tmp_filename_template);

#ifdef _WIN32
    if (_mktemp(tmp_filename) == NULL)
    {
        return NULL;
    }
#else // #ifdef _WIN32
    unx_fd = mkstemp(tmp_filename);
    if (unx_fd == -1)
    {
        return NULL;
    }
    close(unx_fd);

#endif // #ifdef _WIN32

    handle->tmp_filename_tbl[handle->ntmp] = tmp_filename;
    handle->ntmp ++;
    return tmp_filename;
}
Exemple #21
0
FILE *gmx_fopen_temporary(char *buf)
{
    int   i, len;
    FILE *fpout = NULL;

    if ((len = strlen(buf)) < 7)
    {
        gmx_fatal(FARGS, "Buf passed to gmx_fopentmp must be at least 7 bytes long");
    }
    for (i = len-6; (i < len); i++)
    {
        buf[i] = 'X';
    }
    /* mktemp is dangerous and we should use mkstemp instead, but
     * since windows doesnt support it we have to separate the cases.
     * 20090307: mktemp deprecated, use iso c++ _mktemp instead.
     */
#ifdef GMX_NATIVE_WINDOWS
    _mktemp(buf);
    if (buf == NULL)
    {
        gmx_fatal(FARGS, "Error creating temporary file %s: %s", buf,
                  strerror(errno));
    }
    if ((fpout = fopen(buf, "w")) == NULL)
    {
        gmx_fatal(FARGS, "Cannot open temporary file %s", buf);
    }
#else
    int fd = mkstemp(buf);
    if (fd < 0)
    {
        gmx_fatal(FARGS, "Error creating temporary file %s: %s", buf,
                  strerror(errno));
    }
    if ((fpout = fdopen(fd, "w")) == NULL)
    {
        gmx_fatal(FARGS, "Cannot open temporary file %s", buf);
    }
#endif
    /* name in Buf should now be OK and file is open */

    return fpout;
}
Exemple #22
0
FILE *
open_printer()
{
    char *temp;

    if ((temp = getenv("TEMP")) == NULL)
	*win_prntmp = '\0';
    else  {
	safe_strncpy(win_prntmp, temp, MAX_PRT_LEN);
	/* stop X's in path being converted by _mktemp */
	for (temp = win_prntmp; *temp != NUL; temp++)
	    *temp = tolower((unsigned char)*temp);
	if ((strlen(win_prntmp) > 0) && (win_prntmp[strlen(win_prntmp) - 1] != '\\'))
	    strcat(win_prntmp, "\\");
    }
    strncat(win_prntmp, "_gptmp", MAX_PRT_LEN - strlen(win_prntmp));
    strncat(win_prntmp, "XXXXXX", MAX_PRT_LEN - strlen(win_prntmp));
    _mktemp(win_prntmp);
    return fopen(win_prntmp, "wb");
}
Exemple #23
0
/***	mktmpnam - make mktemp behave in a reasonable way
*
*	mktmpnam duplicates the pattern string, passed the duplicate into
*	mktemp (thus preserving the pattern string), and returns the address
*	of the new name string.  Use free to dispose of the new string.
*
*	mktmpnam will examine the contents of the environment variable
*	"TMPENV" (TMP on Xenix or Dos systems) to learn what directory
*	the temp file should go in.  If no such environment variable exists
*	or it is NULL, mktmpnam will use a system dependent default, specified
*	by the defined variable TMPDEF.
*
*	RETURN	address of file name string, NULL if failure
*
*/
char *mktmpnam()
{
	char *tmpbase, *pattstr, *cp;

	if ((pattstr = (char *)memalloc(MAXPATH)) == NULL)
		return NULL;
	if ((tmpbase = getenv(TMPENV)) == NULL)
		tmpbase = TMPDEF;
	strcpy(pattstr, tmpbase);

	cp = pattstr + strlen(pattstr) - 1;
	while (cp > pattstr) {
		if (*cp == PATHSEP)
			*(cp--) = '\0';
		else
			break;
	}

	strcat(pattstr, TMPPATT);
	return (char *)_mktemp(pattstr);
}
Exemple #24
0
void main()
  {
    char name[sizeof(TMPLTE)];
    char *mknm;
    int i;
    FILE *fp;

    for( i = 0; i < 30; i++ ) {
      strcpy( name, TMPLTE );
      mknm = _mktemp( name );
      if( mknm == NULL )
        printf( "Name is badly formed\n" );
      else {
        printf( "Name is %s\n", mknm );
        fp = fopen( mknm, "w" );
        if( fp != NULL ) {
          fprintf( fp, "Name is %s\n", mknm );
          fclose( fp );
        }
      }
    }
  }
Exemple #25
0
/**
 * Create a name for a temporary file. The function calls <code>mkstemp</code>.
 * mktemp is used if no mkstemp is available (see platform dependent define
 * <code>HAVE_NO_MKSTEMP</code> in dlp_base.h).
 * The filename is returned in an internal static buffer. Thus any subsequent
 * calls destroy the value.<code>free</code> does not need to be called to
 * deallocate this pointer.
 *
 * @param lpsDir
 *          Target directory to be used. If lpsDir is NULL, the function tries
 *       to determine the temporary directory by evaluation of the environment
 *       variables TEMP and TMP in this order. If none of these is set then
 *       the current directory is used.
 * @param lpsPfx
 *          Filename prefix
 * @return A pointer to the generated name or <code>NULL</code> if no temporary
 *         name could be created.
 */
char* dlp_tempnam(const char* lpsDir, const char* lpsPfx)
{
  static const char *lpsTempDir = NULL;
  char         lpsBuf[L_PATH];
  static char  lpsTemname[L_PATH];

  sprintf(lpsBuf, "%sXXXXXX",lpsPfx);
  if(lpsDir==NULL)
  {
    if(!lpsTempDir) lpsTempDir = getenv("TEMP");
    if(!lpsTempDir) lpsTempDir = getenv("TMP");
#ifdef __LINUX
#ifndef __TMS
    if(!lpsTempDir)
    {
      if(getcwd(lpsTemname,L_PATH) == NULL) return NULL;
      if(chdir("/var/tmp")==0) lpsTempDir = "/var/tmp";
      else if(chdir("/tmp")==0) lpsTempDir = "/tmp";
      if(chdir(lpsTemname)!=0) return NULL;
    }
#endif
#endif
    if(!lpsTempDir) lpsTempDir = ".";
    sprintf(lpsTemname,"%s%c%s",lpsTempDir,C_DIR,lpsBuf);
  }
  else sprintf(lpsTemname,"%s%c%s",lpsDir,C_DIR,lpsBuf);

#ifndef __TMS
#ifdef HAVE_NO_MKSTEMP
  dlp_strncpy(lpsTemname,_mktemp(lpsTemname),L_PATH-1);
#else
  close(mkstemp(lpsTemname));  /* mkstemp opens the file and returns the file descriptor. */
                                             /* Lets close it immediately, since we just need the name. */
#endif
#endif
  return lpsTemname;
}
int temporary_file(char *name) {
    char *temp;
    size_t i, len = 0;

    /* Get the length of the name */
    len = strlen(name);
    if (len <= 6) return 1;

    /* Allocate the memory to hold name */
    temp = (char*) malloc((len + 1) * sizeof (char));
    strcpy(temp, name);
    for (i = (len - 7); i < len; i++) temp[i] = 'X';

    /*    strcpy (temp, "cgnsXXXXXX"); */
#ifdef _WIN32
    if (_mktemp(temp) == NULL)
        error("temporary_file", "failed to create temporary filename\n");
#else
    if (mkstemp(temp) == -1)
        error("temporary_file", "failed to create temporary filename\n");
#endif
    strcpy(name, temp);
    return 0;
}
Exemple #27
0
/*
 * Returns 1 for failure, 0 for success
 */
int main(int argc, char **argv) {
    char *dosdir,
         *fdauto,
         *fdconfig,
         dd[_MAX_PATH],
         fa[_MAX_PATH],
         fc[_MAX_PATH],
         temp[12],
         executestring[_MAX_PATH];
    int ret = -1;
    FILE *fp;
    kittenopen("FDPKG");
    if((argc&&argv[1][0]=='/'||argv[1][0]=='-')&&(argv[1][1]=='?'||tolower(argv[1][1])=='h'))
    {
        help();
        kittenclose();
        return 1;
    }
    sprintf(temp, "%s.BAT", _mktemp("XXXXXX"));
    while(access(temp, 0) == 0) sprintf(temp, "%s.BAT", _mktemp("XXXXXX"));
    if((fp = fopen(temp, "w")) == NULL) {
        kitten_printf(5,4,"Could not open temp files\n");
        return -1;
    }
    while(kbhit()) getch();
    sprintf(executestring, "%s\r", temp);
    kb_stuff(executestring);
    fprintf(fp, "@echo off\n");
    if((dosdir = getenv("DOSDIR")) == NULL) {
        if		(access("C:\\FDOS"   , 0) == 0) {
            ret = find_dir("C:\\FDOS");
            if(ret == 0) strcpy(dd, "C:\\FDOS");
        }
        if(ret != 0 && access("C:\\FREEDOS", 0) == 0) {
            ret = find_dir("C:\\FREEDOS");
            if(ret == 0) strcpy(dd, "C:\\FREEDOS");
        }
        if(ret != 0 && access("C:\\DOS"    , 0) == 0) {
            ret = find_dir("C:\\DOS");
            if(ret == 0) strcpy(dd, "C:\\DOS");
        }
        if(ret != 0 && access("D\\FDOS"    , 0) == 0) {
            ret = find_dir("D:\\FDOS");
            if(ret == 0) strcpy(dd, "D:\\FDOS");
        }
        if(ret != 0 && access("D\\FREEDOS" , 0) == 0) {
            ret = find_dir("D:\\FREEDOS");
            if(ret == 0) strcpy(dd, "D:\\FREEDOS");
        }
        if(ret != 0 && access("D\\DOS"     , 0) == 0) {
            ret = find_dir("D:\\DOS");
            if(ret == 0) strcpy(dd, "D:\\DOS");
        }
        if(ret != 0) {
            kitten_printf(5,5,"Could not find suitable directory for %%DOSDIR%%\n");
            fclose(fp);
            return -1;
        } else fprintf(fp, "SET DOSDIR=%s\n", dd);
    }
    if((fdauto = getenv("AUTOFILE")) == NULL) {
        if(	access("C:\\FDAUTO.BAT",   0) == 0) strcpy(fa, "C:\\FDAUTO.BAT");
        else if(access("C:\\AUTOEXEC.BAT", 0) == 0) strcpy(fa, "C:\\AUTOEXEC.BAT");
        else {
            kitten_printf(5,6,"Could not find suitable autoexec.bat\n");
            fprintf(fp, "DEL %s\x1a", temp);
            fclose(fp);
            return -1;
        }
        fprintf(fp, "SET AUTOFILE=%s\necho SET AUTOFILE=%s >> %s\n", fa, fa, fa);
        if(dosdir == NULL) fprintf(fp, "echo SET DOSDIR=%s >> %s\n", dd, fa);
    } else if(dosdir == NULL) fprintf(fp, "echo SET DOSDIR=%s >> %s\n", dd, fdauto);
    if((fdconfig = getenv("CFGFILE")) == NULL) {
        if(	access("C:\\FDCONFIG.SYS", 0) == 0) strcpy(fc, "C:\\FDCONFIG.SYS");
        else if(access("C:\\CONFIG.SYS"  , 0) == 0) strcpy(fc, "C:\\CONFIG.SYS");
        else {
            kitten_printf(5,7,"Could not find suitable config.sys\n");
            fprintf(fp, "DEL %s\x1a", temp);
            fclose(fp);
            return -1;
        }
        fprintf(fp, "SET CFGFILE=%s\necho SET CFGFILE=%s >> %s\n",
                fc, fc, (fdauto == NULL) ? fa : fdauto);
    }
    fprintf(fp, "DEL %s\x1a", temp);
    fclose(fp);
    return 0;
}
Exemple #28
0
/* exported as 'mktemp' */
char *
nss_mktemp(char *path)
{
    return _mktemp(path);
}
Exemple #29
0
char* mktemp(char* tn) {
  return _mktemp(tn);
}
Exemple #30
0
int
_zip_mkstemp(char *path)
{
#ifdef _WIN32
	int ret;
	ret = _creat(_mktemp(path), _S_IREAD|_S_IWRITE);
	if (ret == -1) {
		return 0;
	} else {
		return ret;
	}
#else
	int fd;   
	char *start, *trv;
	struct stat sbuf;
	pid_t pid;

	/* To guarantee multiple calls generate unique names even if
	   the file is not created. 676 different possibilities with 7
	   or more X's, 26 with 6 or less. */
	static char xtra[2] = "aa";
	int xcnt = 0;

	pid = getpid();

	/* Move to end of path and count trailing X's. */
	for (trv = path; *trv; ++trv)
		if (*trv == 'X')
			xcnt++;
		else
			xcnt = 0;	

	/* Use at least one from xtra.  Use 2 if more than 6 X's. */
	if (*(trv - 1) == 'X')
		*--trv = xtra[0];
	if (xcnt > 6 && *(trv - 1) == 'X')
		*--trv = xtra[1];

	/* Set remaining X's to pid digits with 0's to the left. */
	while (*--trv == 'X') {
		*trv = (pid % 10) + '0';
		pid /= 10;
	}

	/* update xtra for next call. */
	if (xtra[0] != 'z')
		xtra[0]++;
	else {
		xtra[0] = 'a';
		if (xtra[1] != 'z')
			xtra[1]++;
		else
			xtra[1] = 'a';
	}

	/*
	 * check the target directory; if you have six X's and it
	 * doesn't exist this runs for a *very* long time.
	 */
	for (start = trv + 1;; --trv) {
		if (trv <= path)
			break;
		if (*trv == '/') {
			*trv = '\0';
			if (stat(path, &sbuf))
				return (0);
			if (!S_ISDIR(sbuf.st_mode)) {
				errno = ENOTDIR;
				return (0);
			}
			*trv = '/';
			break;
		}
	}

	for (;;) {
		if ((fd=open(path, O_CREAT|O_EXCL|O_RDWR|O_BINARY, 0600)) >= 0)
			return (fd);
		if (errno != EEXIST)
			return (0);

		/* tricky little algorithm for backward compatibility */
		for (trv = start;;) {
			if (!*trv)
				return (0);
			if (*trv == 'z')
				*trv++ = 'a';
			else {
				if (isdigit((unsigned char)*trv))
					*trv = 'a';
				else
					++*trv;
				break;
			}
		}
	}
	/*NOTREACHED*/
#endif
}