File: fileops.c

package info (click to toggle)
hcxtools 6.3.5-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,080 kB
  • sloc: ansic: 19,601; python: 144; sh: 99; makefile: 98
file content (180 lines) | stat: -rw-r--r-- 4,399 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
#define _GNU_SOURCE
#include <fcntl.h>
#include <stdbool.h>
#include <stdint.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <stdio.h>
#include <time.h>

#include "fileops.h"
/*===========================================================================*/
int getmagicnumber(int fd)
{
int res;
magicnr_t mnr;

res = read(fd, &mnr, 4);
if(res != 4) return 0;
return mnr.magic_number;
}
/*===========================================================================*/
void fwritetimestamphigh(uint32_t tshigh, FILE *fhd)
{
time_t pkttime;
struct tm *pkttm;

char tmbuf[64];

if(tshigh != 0)
	{
	pkttime = tshigh;
	pkttm = localtime(&pkttime);
	strftime(tmbuf, sizeof tmbuf, "%d%m%Y", pkttm);
	fprintf(fhd, "%s:", tmbuf);
	}
else fprintf(fhd, "00000000:");
return;
}
/*===========================================================================*/
void fwriteaddr1(uint8_t *macw, FILE *fhd)
{
int p;

for(p = 0; p< 6; p++) fprintf(fhd, "%02x", macw[p]);
fprintf(fhd, ":");
return;
}
/*===========================================================================*/
void fwriteaddr1addr2(uint8_t *mac1, uint8_t *mac2, FILE *fhd)
{
fwriteaddr1(mac1, fhd);
fwriteaddr1(mac2, fhd);
return;
}
/*===========================================================================*/
void fwriteessidstrnoret(uint8_t len, unsigned char *essidstr, FILE *fhd)
{
int p;

if(isasciistring(len, essidstr) != false) fprintf(fhd, "%.*s\n", len, essidstr);
else
	{
	fprintf(fhd, "$HEX[");
	for(p = 0; p < len; p++) fprintf(fhd, "%02x", essidstr[p]);
	fprintf(fhd, "]:");
	}
return;
}
/*===========================================================================*/
void fwriteessidstr(uint8_t len, unsigned char *essidstr, FILE *fhd)
{
int p;

if((len == 0) || (len > ESSID_LEN_MAX)) return;
if(essidstr[0] == 0) return;
if(isasciistring(len, essidstr) != false) fprintf(fhd, "%.*s\n", len, essidstr);
else
	{
	fprintf(fhd, "$HEX[");
	for(p = 0; p < len; p++) fprintf(fhd, "%02x", essidstr[p]);
	fprintf(fhd, "]\n");
	}
return;
}
/*===========================================================================*/
void fwritedeviceinfostr(uint8_t len, unsigned char *deviceinfostr, FILE *fhd)
{
int p;

fprintf(fhd, "\t");
if(deviceinfostr[0] == 0) return;
if(isasciistring(len, deviceinfostr) != false) fprintf(fhd, "%.*s", len, deviceinfostr);
else
	{
	fprintf(fhd, "$HEX[");
	for(p = 0; p < len; p++) fprintf(fhd, "%02x", deviceinfostr[p]);
	fprintf(fhd, "]");
	}
return;
}
/*===========================================================================*/
void fwritestring(uint8_t len, unsigned char *essidstr, FILE *fhd)
{
int p;

if(isasciistring(len, essidstr) != false) fprintf(fhd, "%.*s\n", len, essidstr);
else
	{
	fprintf(fhd, "$HEX[");
	for(p = 0; p < len; p++) fprintf(fhd, "%02x", essidstr[p]);
	fprintf(fhd, "]\n");
	}
return;
}
/*===========================================================================*/
void fwritehexbuffraw(uint8_t bufflen, uint8_t *buff, FILE *fhd)
{
int p;

for(p = 0; p < bufflen; p++) fprintf(fhd, "%02x", buff[p]);
return;
}
/*===========================================================================*/
void fwritehexbuff(uint8_t bufflen, uint8_t *buff, FILE *fhd)
{
int p;

for(p = 0; p < bufflen; p++) fprintf(fhd, "%02x", buff[p]);
fprintf(fhd, "\n");
return;
}
/*===========================================================================*/
void removeemptyfile(char *filenametoremove)
{
struct stat statinfo;

if(filenametoremove == NULL) return;
if(stat(filenametoremove, &statinfo) != 0) return;
if(statinfo.st_size == 0)
	{
	remove(filenametoremove);
	return;
	}
return;
}
/*===========================================================================*/
static size_t chop(char *buffer,  size_t len)
{
static char *ptr;

ptr = buffer +len -1;
while (len) {
	if (*ptr != '\n') break;
	*ptr-- = 0;
	len--;
	}

while (len) {
	if (*ptr != '\r') break;
	*ptr-- = 0;
	len--;
	}
return len;
}
/*---------------------------------------------------------------------------*/
static inline int fgetline(FILE *inputstream, size_t size, char *buffer)
{
static size_t len;
static char *buffptr;

if(feof(inputstream)) return -1;
buffptr = fgets (buffer, size, inputstream);
if(buffptr == NULL) return -1;
len = strlen(buffptr);
len = chop(buffptr, len);
return len;
}
/*===========================================================================*/