]> cvs.zerfleddert.de Git - proxmark3-svn/blame - client/loclass/fileutils.c
ADD: default_toys.lua a script which identify tnp3xx tags
[proxmark3-svn] / client / loclass / fileutils.c
CommitLineData
3ad48540
MHS
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 */
13int fileExists(const char *filename) {
3fe4ff4f 14
15#ifdef _WIN32
16 struct _stat st;
17 int result = _stat(filename, &st);
18#else
3ad48540
MHS
19 struct stat st;
20 int result = stat(filename, &st);
3fe4ff4f 21#endif
3ad48540
MHS
22 return result == 0;
23}
24
25int saveFile(const char *preferredName, const char *suffix, const void* data, size_t datalen)
26{
6116c796 27 int size = sizeof(char) * (strlen(preferredName)+strlen(suffix)+10);
3ad48540
MHS
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) {
6116c796 43 PrintAndLog("Failed to write to file '%s'", fileName);
ca4714cd 44 free(fileName);
3ad48540
MHS
45 return 1;
46 }
47 fwrite(data, 1, datalen, fileHandle);
48 fclose(fileHandle);
cb29e00a 49 PrintAndLog("Saved data to '%s'", fileName);
6116c796 50
3ad48540
MHS
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 */
64void prnlog(char *fmt, ...)
65{
6f101995 66 char buffer[2048] = {0};
3ad48540
MHS
67 va_list args;
68 va_start(args,fmt);
6f101995 69 vsprintf (buffer,fmt, args);
3ad48540 70 va_end(args);
6f101995
MHS
71 PrintAndLog(buffer);
72
3ad48540 73}
Impressum, Datenschutz