Skip to content

Commit

Permalink
Revert "Avoid using deprecated tmp functions (#11)" (#44)
Browse files Browse the repository at this point in the history
This reverts commit db1e477.
  • Loading branch information
hensldm authored Mar 18, 2023
1 parent a64576b commit d14d08e
Showing 1 changed file with 23 additions and 71 deletions.
94 changes: 23 additions & 71 deletions libc_impl.c
Original file line number Diff line number Diff line change
Expand Up @@ -2496,90 +2496,42 @@ void wrapper_longjmp(uint8_t* mem, uint32_t addr, int status) {
assert(0 && "longjmp not implemented");
}

uint32_t wrapper_tempnam(uint8_t* mem, uint32_t dir_addr, uint32_t pfx_addr) {
char template[1024];
int fd;
size_t len;
uint32_t ret_addr;
const char* tmpdir;

tmpdir = getenv("TMPDIR");

if (tmpdir != NULL) {
strcpy(template, tmpdir);
} else if (dir_addr != 0) {
STRING(dir)

if (dir[0] != '\0') {
strcpy(template, dir);
} else {
strcpy(template, "/tmp");
}
} else {
strcpy(template, "/tmp");
}

strcat(template, "/");

if (pfx_addr != 0) {
STRING(pfx)

if (strlen(pfx) <= 5) {
strcat(template, pfx);
}
}

strcat(template, "_ido_tempnam_XXXXXX");

fd = mkstemp(template);
if (fd == -1) {
uint32_t wrapper_tempnam(uint8_t *mem, uint32_t dir_addr, uint32_t pfx_addr) {
STRING(dir)
STRING(pfx)
char *ret = tempnam(dir, pfx);
char *ret_saved = ret;
if (ret == NULL) {
MEM_U32(ERRNO_ADDR) = errno;
return 0;
} else {
// close the file descriptor to mimic tempnam's behaviour
close(fd);
}

len = strlen(template) + 1;
ret_addr = wrapper_malloc(mem, len);

strcpy1(mem, ret_addr, template);
size_t len = strlen(ret) + 1;
uint32_t ret_addr = wrapper_malloc(mem, len);
uint32_t pos = ret_addr;
while (len--) {
MEM_S8(pos) = *ret;
++pos;
++ret;
}
free(ret_saved);
return ret_addr;
}

uint32_t wrapper_tmpnam(uint8_t* mem, uint32_t str_addr) {
char template[1024];
int fd;

uint32_t wrapper_tmpnam(uint8_t *mem, uint32_t str_addr) {
char buf[1024];
assert(str_addr != 0 && "s NULL not implemented for tmpnam");

strcpy(template, "/tmp/ido_tmpnam_XXXXXX");

fd = mkstemp(template);
if (fd == -1) {
char *ret = tmpnam(buf);
if (ret == NULL) {
return 0;
} else {
// close the file descriptor to mimic tmpnam's behaviour
close(fd);
strcpy1(mem, str_addr, ret);
return str_addr;
}

strcpy1(mem, str_addr, template);
return str_addr;
}

uint32_t wrapper_mktemp(uint8_t* mem, uint32_t template_addr) {
int fd;
uint32_t wrapper_mktemp(uint8_t *mem, uint32_t template_addr) {
STRING(template)

fd = mkstemp(template);
if (fd == -1) {
MEM_U32(ERRNO_ADDR) = errno;
template[0] = '\0';
} else {
// close the file descriptor to mimic mktemp's behaviour
close(fd);
}

mktemp(template);
strcpy1(mem, template_addr, template);
return template_addr;
}
Expand Down

0 comments on commit d14d08e

Please sign in to comment.