source: trunk/sys/dietlibc/mkstemp.c @ 171

Last change on this file since 171 was 1, checked in by alain, 8 years ago

First import

File size: 788 bytes
RevLine 
[1]1#include <unistd.h>
2#include <fcntl.h>
3#include <string.h>
4#include <stdlib.h>
5#include <errno.h>
6#include "dietfeatures.h"
7
8#ifndef O_NOFOLLOW
9#define O_NOFOLLOW 0
10#endif
11
12int mkstemp(char* template) {
13  char *tmp=template+strlen(template)-6;
14  int randfd;
15  int i,res;
16  unsigned int random;
17  if (tmp<template) goto error;
18  for (i=0; i<6; ++i) if (tmp[i]!='X') { error: errno=EINVAL; return -1; }
19  //randfd=open("/dev/urandom",O_RDONLY);
20  for (;;) {
21    //read(randfd,&random,sizeof(random));
22    random = rand();
23    for (i=0; i<6; ++i) {
24      int hexdigit=(random>>(i*5))&0x1f;
25      tmp[i]=hexdigit>9?hexdigit+'a'-10:hexdigit+'0';
26    }
27    res=open(template,O_CREAT|O_RDWR|O_EXCL|O_NOFOLLOW,0600);
28    if (res>=0 || errno!=EEXIST) break;
29  }
30  //close(randfd);
31  return res;
32}
Note: See TracBrowser for help on using the repository browser.