SDDSlib
Loading...
Searching...
No Matches
tmpname.c File Reference

Provides functions to generate unique temporary filenames. More...

#include "mdb.h"
#include <time.h>
#include <unistd.h>
#include <errno.h>

Go to the source code of this file.

Macros

#define struct_stat64   struct stat
 
#define __getpid   getpid
 
#define __lxstat64(version, path, buf)
 
#define __set_errno(Val)
 
#define ATTEMPTS_MIN   (62 * 62 * 62)
 

Functions

char * tmpname (char *s)
 Supplies a unique temporary filename.
 
char * mktempOAG (char *template)
 Generates a unique temporary filename based on a template.
 

Detailed Description

Provides functions to generate unique temporary filenames.

License
This file is distributed under the terms of the Software License Agreement found in the file LICENSE included with this distribution.
Author
M. Borland, C Saunders, R. Soliday

Definition in file tmpname.c.

Macro Definition Documentation

◆ __getpid

#define __getpid   getpid

Definition at line 62 of file tmpname.c.

◆ __lxstat64

#define __lxstat64 ( version,
path,
buf )
Value:
lstat(path, buf)

Definition at line 66 of file tmpname.c.

◆ __set_errno

#define __set_errno ( Val)
Value:
errno = (Val)

Definition at line 70 of file tmpname.c.

◆ struct_stat64

#define struct_stat64   struct stat

Definition at line 61 of file tmpname.c.

Function Documentation

◆ mktempOAG()

char * mktempOAG ( char * template)

Generates a unique temporary filename based on a template.

Replaces the last six characters ('XXXXXX') of the template with random characters from a predefined set to create a unique temporary filename. It ensures that the generated filename does not already exist by checking the filesystem.

Parameters
templateA string containing the template for the temporary filename. The last six characters should be 'XXXXXX'.
Returns
A pointer to the modified template with the unique temporary filename. If no unique name could be generated, the first character of the template is set to null.

Definition at line 85 of file tmpname.c.

85 {
86 int len, pid;
87 char *XXXXXX;
88 static uint64_t value;
89 uint64_t random_time_bits;
90 unsigned int count;
91 int save_errno = errno;
92 struct_stat64 st;
93 static const char letters[] =
94 "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
95
96#define ATTEMPTS_MIN (62 * 62 * 62)
97
98#if ATTEMPTS_MIN < TMP_MAX
99 unsigned int attempts = TMP_MAX;
100#else
101 unsigned int attempts = ATTEMPTS_MIN;
102#endif
103
104 len = strlen(template);
105 if (len < 6 || memcmp(&template[len - 6], "XXXXXX", 6)) {
106 template[0] = '\0';
107 return (template);
108 }
109
110 /* This is where the Xs start. */
111 XXXXXX = &template[len - 6];
112
113 /* Get some more or less random data. */
114 random_time_bits = time(NULL);
115
116 pid = __getpid();
117 value += random_time_bits ^ (pid * pid);
118 for (count = 0; count < attempts; value += 7777, ++count) {
119 uint64_t v = value;
120
121 /* Fill in the random bits. */
122 XXXXXX[0] = letters[v % 62];
123 v /= 31;
124 XXXXXX[1] = letters[v % 62];
125 v /= 31;
126 XXXXXX[2] = letters[v % 62];
127 v /= 31;
128 XXXXXX[3] = letters[v % 62];
129 v /= 31;
130 XXXXXX[4] = letters[v % 62];
131 v /= 31;
132 XXXXXX[5] = letters[v % 62];
133
134#if defined(vxWorks)
135 if (!fexists(template) < 0) {
136 return (template);
137 }
138#else
139 if (__lxstat64(_STAT_VER, template, &st) < 0) {
140 if (errno == ENOENT) {
141 __set_errno(save_errno);
142 return (template);
143 } else {
144 /* Give up now. */
145 template[0] = '\0';
146 return (template);
147 }
148 }
149#endif
150 }
151
152 /* We got out of the loop because we ran out of combinations to try. */
153 template[0] = '\0';
154 return (template);
155}
long fexists(const char *filename)
Checks if a file exists.
Definition fexists.c:27

◆ tmpname()

char * tmpname ( char * s)

Supplies a unique temporary filename.

Generates a unique temporary filename by appending the process ID and an incrementing counter to a base name. Ensures that the filename does not already exist.

Parameters
sPointer to a buffer where the temporary filename will be stored. If NULL, the function allocates memory for the filename.
Returns
A pointer to the unique temporary filename string.

Definition at line 34 of file tmpname.c.

35{
36 static long i = 1;
37 static long pid = -1;
38 if (s == NULL)
39 s = tmalloc((unsigned)(40 * sizeof(*s)));
40 if (pid < 0)
41#if !defined(vxWorks)
42 pid = getpid();
43#endif
44 do {
45#if defined(vxWorks)
46 sprintf(s, "tmp.%ld", i);
47#else
48 sprintf(s, "tmp%ld.%ld", pid, i);
49#endif
50 i += 1;
51 if (!fexists(s))
52 break;
53 } while (1);
54 return (s);
55}
void * tmalloc(uint64_t size_of_block)
Allocates a memory block of the specified size with zero initialization.
Definition array.c:59