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