]> git.zerfleddert.de Git - proxmark3-svn/blob - client/loclass/fileutils.c
Merge branch 'iclass-research' of https://github.com/PenturaLabs/proxmark3 into Pentu...
[proxmark3-svn] / client / loclass / fileutils.c
1 #include <stdio.h>
2 #include <string.h>
3 #include <stdlib.h>
4 #include <sys/stat.h>
5 #include <stdarg.h>
6 #include "fileutils.h"
7 #include "ui.h"
8 /**
9 * @brief checks if a file exists
10 * @param filename
11 * @return
12 */
13 int fileExists(const char *filename) {
14 struct stat st;
15 int result = stat(filename, &st);
16 return result == 0;
17 }
18
19 int saveFile(const char *preferredName, const char *suffix, const void* data, size_t datalen)
20 {
21 int size = sizeof(char) * (strlen(preferredName)+strlen(suffix)+5);
22 char * fileName = malloc(size);
23
24 memset(fileName,0,size);
25 int num = 1;
26 sprintf(fileName,"%s.%s", preferredName, suffix);
27 while(fileExists(fileName))
28 {
29 sprintf(fileName,"%s-%d.%s", preferredName, num, suffix);
30 num++;
31 }
32 /* We should have a valid filename now, e.g. dumpdata-3.bin */
33
34 /*Opening file for writing in binary mode*/
35 FILE *fileHandle=fopen(fileName,"wb");
36 if(!fileHandle) {
37 prnlog("Failed to write to file '%s'", fileName);
38 return 1;
39 }
40 fwrite(data, 1, datalen, fileHandle);
41 fclose(fileHandle);
42 prnlog("Saved data to '%s'", fileName);
43 free(fileName);
44
45 return 0;
46 }
47
48 /**
49 * Utility function to print to console. This is used consistently within the library instead
50 * of printf, but it actually only calls printf (and adds a linebreak).
51 * The reason to have this method is to
52 * make it simple to plug this library into proxmark, which has this function already to
53 * write also to a logfile. When doing so, just delete this function.
54 * @param fmt
55 */
56 void prnlog(char *fmt, ...)
57 {
58
59 va_list args;
60 va_start(args,fmt);
61 PrintAndLog(fmt, args);
62 //vprintf(fmt,args);
63 va_end(args);
64 //printf("\n");
65 }
Impressum, Datenschutz