]> cvs.zerfleddert.de Git - proxmark3-svn/blob - client/loclass/fileutils.c
ADD: default_toys.lua a script which identify tnp3xx tags
[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
15 #ifdef _WIN32
16 struct _stat st;
17 int result = _stat(filename, &st);
18 #else
19 struct stat st;
20 int result = stat(filename, &st);
21 #endif
22 return result == 0;
23 }
24
25 int saveFile(const char *preferredName, const char *suffix, const void* data, size_t datalen)
26 {
27 int size = sizeof(char) * (strlen(preferredName)+strlen(suffix)+10);
28 char * fileName = malloc(size);
29
30 memset(fileName,0,size);
31 int num = 1;
32 sprintf(fileName,"%s.%s", preferredName, suffix);
33 while(fileExists(fileName))
34 {
35 sprintf(fileName,"%s-%d.%s", preferredName, num, suffix);
36 num++;
37 }
38 /* We should have a valid filename now, e.g. dumpdata-3.bin */
39
40 /*Opening file for writing in binary mode*/
41 FILE *fileHandle=fopen(fileName,"wb");
42 if(!fileHandle) {
43 PrintAndLog("Failed to write to file '%s'", fileName);
44 free(fileName);
45 return 1;
46 }
47 fwrite(data, 1, datalen, fileHandle);
48 fclose(fileHandle);
49 PrintAndLog("Saved data to '%s'", fileName);
50
51 free(fileName);
52
53 return 0;
54 }
55
56 /**
57 * Utility function to print to console. This is used consistently within the library instead
58 * of printf, but it actually only calls printf (and adds a linebreak).
59 * The reason to have this method is to
60 * make it simple to plug this library into proxmark, which has this function already to
61 * write also to a logfile. When doing so, just delete this function.
62 * @param fmt
63 */
64 void prnlog(char *fmt, ...)
65 {
66 char buffer[2048] = {0};
67 va_list args;
68 va_start(args,fmt);
69 vsprintf (buffer,fmt, args);
70 va_end(args);
71 PrintAndLog(buffer);
72
73 }
Impressum, Datenschutz