1 /* 2 * Copyright (C) 2015-2021 Alibaba Group Holding Limited 3 */ 4 5 #include <stdio.h> 6 #include <string.h> 7 #include <pthread.h> 8 9 #define TEMP_PATH_MAX 64 10 #define TEMP_FILE_NAME_MAGIC "/tmp/du2s5sz3" 11 12 pthread_mutex_t g_tmpnam_lock = PTHREAD_MUTEX_INITIALIZER; 13 tmpnam(char * s)14char *tmpnam(char *s) 15 { 16 int ret; 17 static int temp_name_series = 0; 18 char *temp_name_prefix = TEMP_FILE_NAME_MAGIC; 19 char temp_name_series_buf[8] = {0}; 20 21 if (temp_name_series >= TMP_MAX) { 22 return NULL; 23 } 24 25 ret = pthread_mutex_lock(&g_tmpnam_lock); 26 if (ret != 0) { 27 return NULL; 28 } 29 30 strncpy(s, temp_name_prefix, TEMP_PATH_MAX); 31 snprintf(temp_name_series_buf, sizeof(temp_name_series_buf) - 1, "_%d", temp_name_series); 32 strncat(s, temp_name_series_buf, TEMP_PATH_MAX - strlen(s) - 1); 33 34 temp_name_series++; 35 36 pthread_mutex_unlock(&g_tmpnam_lock); 37 38 return s; 39 } 40 tmpfile(void)41FILE *tmpfile(void) 42 { 43 char path[TEMP_PATH_MAX] = {0}; 44 45 if (tmpnam(path) == NULL) { 46 return NULL; 47 } 48 49 return fopen(path, "w+"); 50 } 51