]> git.zerfleddert.de Git - proxmark3-svn/blob - client/loclass/fileutils.c
Merge branch 'master' of https://github.com/pwpiwi/proxmark3
[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)+10);
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 PrintAndLog("Failed to write to file '%s'", fileName);
38 free(fileName);
39 return 1;
40 }
41 fwrite(data, 1, datalen, fileHandle);
42 fclose(fileHandle);
43 PrintAndLog(">Saved data to '%s'", fileName);
44
45 free(fileName);
46
47 return 0;
48 }
49
50 /**
51 * Utility function to print to console. This is used consistently within the library instead
52 * of printf, but it actually only calls printf (and adds a linebreak).
53 * The reason to have this method is to
54 * make it simple to plug this library into proxmark, which has this function already to
55 * write also to a logfile. When doing so, just delete this function.
56 * @param fmt
57 */
58 void prnlog(char *fmt, ...)
59 {
60
61 va_list args;
62 va_start(args,fmt);
63 PrintAndLog(fmt, args);
64 //vprintf(fmt,args);
65 va_end(args);
66 //printf("\n");
67 }
Impressum, Datenschutz