]> cvs.zerfleddert.de Git - proxmark3-svn/blame - client/loclass/fileutils.c
Changed io threshold to 140, fixed boolean error in comparison
[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) {
14 struct stat st;
15 int result = stat(filename, &st);
16 return result == 0;
17}
18
19int saveFile(const char *preferredName, const char *suffix, const void* data, size_t datalen)
20{
6116c796 21 int size = sizeof(char) * (strlen(preferredName)+strlen(suffix)+10);
3ad48540
MHS
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) {
6116c796 37 PrintAndLog("Failed to write to file '%s'", fileName);
ca4714cd 38 free(fileName);
3ad48540
MHS
39 return 1;
40 }
41 fwrite(data, 1, datalen, fileHandle);
42 fclose(fileHandle);
6116c796
MHS
43 PrintAndLog(">Saved data to '%s'", fileName);
44
3ad48540
MHS
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 */
58void prnlog(char *fmt, ...)
59{
6f101995 60 char buffer[2048] = {0};
3ad48540
MHS
61 va_list args;
62 va_start(args,fmt);
6f101995 63 vsprintf (buffer,fmt, args);
3ad48540 64 va_end(args);
6f101995
MHS
65 PrintAndLog(buffer);
66
3ad48540 67}
Impressum, Datenschutz