1
0
mirror of https://github.com/hashcat/hashcat.git synced 2024-12-27 00:48:13 +00:00
hashcat/src/filehandling.c

145 lines
1.8 KiB
C
Raw Normal View History

/**
* Author......: See docs/credits.txt
* License.....: MIT
*/
#include "common.h"
2016-09-16 15:01:18 +00:00
#include "types.h"
#include "memory.h"
2017-07-06 08:35:25 +00:00
#include "shared.h"
#include "filehandling.h"
u64 count_lines (FILE *fd)
{
u64 cnt = 0;
char *buf = (char *) hcmalloc (HCBUFSIZ_LARGE + 1);
char prev = '\n';
while (!feof (fd))
{
2017-07-06 08:35:25 +00:00
size_t nread = hc_fread (buf, sizeof (char), HCBUFSIZ_LARGE, fd);
if (nread < 1) continue;
size_t i;
for (i = 0; i < nread; i++)
{
if (prev == '\n') cnt++;
prev = buf[i];
}
}
hcfree (buf);
return cnt;
}
2018-02-08 18:13:29 +00:00
size_t fgetl (FILE *fp, char *line_buf)
{
2018-02-08 18:13:29 +00:00
size_t line_len = 0;
while (!feof (fp))
{
const int c = fgetc (fp);
if (c == EOF) break;
line_buf[line_len] = (char) c;
line_len++;
2016-09-08 10:17:56 +00:00
if (line_len == HCBUFSIZ_LARGE) line_len--;
if (c == '\n') break;
}
if (line_len == 0) return 0;
while (line_len)
{
if (line_buf[line_len - 1] == '\n')
{
line_len--;
continue;
}
if (line_buf[line_len - 1] == '\r')
{
line_len--;
continue;
}
break;
}
line_buf[line_len] = 0;
return (line_len);
}
size_t superchop_with_length (char *buf, const size_t len)
{
size_t new_len = len;
while (new_len)
{
if (buf[new_len - 1] == '\n')
{
new_len--;
buf[new_len] = 0;
continue;
}
if (buf[new_len - 1] == '\r')
{
new_len--;
buf[new_len] = 0;
continue;
}
break;
}
return new_len;
}
2018-02-08 18:13:29 +00:00
size_t in_superchop (char *buf)
{
2016-11-17 05:17:28 +00:00
size_t len = strlen (buf);
while (len)
{
if (buf[len - 1] == '\n')
{
len--;
buf[len] = 0;
continue;
}
if (buf[len - 1] == '\r')
{
len--;
buf[len] = 0;
continue;
}
break;
}
return len;
}