mirror of
https://github.com/RfidResearchGroup/proxmark3.git
synced 2025-03-20 20:18:17 +08:00
Simpler newfilenamemcopy
This commit is contained in:
parent
35ea8e745e
commit
73b6fa7cbc
1 changed files with 6 additions and 11 deletions
|
@ -73,24 +73,19 @@ static char *filenamemcopy(const char *preferredName, const char *suffix) {
|
|||
static char *newfilenamemcopy(const char *preferredName, const char *suffix) {
|
||||
if (preferredName == NULL) return NULL;
|
||||
if (suffix == NULL) return NULL;
|
||||
char *preferredNameTmp = (char *) calloc(strlen(preferredName) + 1, sizeof(uint8_t));
|
||||
if (preferredNameTmp == NULL)
|
||||
return NULL;
|
||||
strcpy(preferredNameTmp, preferredName);
|
||||
if (str_endswith(preferredNameTmp, suffix))
|
||||
preferredNameTmp[strlen(preferredNameTmp) - strlen(suffix)] = '\0';
|
||||
char *fileName = (char *) calloc(strlen(preferredNameTmp) + strlen(suffix) + 1 + 10, sizeof(uint8_t)); // 10: room for filenum to ensure new filename
|
||||
uint16_t preferredNameLen = strlen(preferredName);
|
||||
if (str_endswith(preferredName, suffix))
|
||||
preferredNameLen -= strlen(suffix);
|
||||
char *fileName = (char *) calloc(preferredNameLen + strlen(suffix) + 1 + 10, sizeof(uint8_t)); // 10: room for filenum to ensure new filename
|
||||
if (fileName == NULL) {
|
||||
free(preferredNameTmp);
|
||||
return NULL;
|
||||
}
|
||||
int num = 1;
|
||||
sprintf(fileName, "%s%s", preferredNameTmp, suffix);
|
||||
sprintf(fileName, "%.*s%s", preferredNameLen, preferredName, suffix);
|
||||
while (fileExists(fileName)) {
|
||||
sprintf(fileName, "%s-%d%s", preferredNameTmp, num, suffix);
|
||||
sprintf(fileName, "%.*s-%d%s", preferredNameLen, preferredName, num, suffix);
|
||||
num++;
|
||||
}
|
||||
free(preferredNameTmp);
|
||||
return fileName;
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue