1
0
mirror of https://github.com/hashcat/hashcat.git synced 2024-12-26 16:38:35 +00:00
hashcat/src/filehandling.c

112 lines
1.4 KiB
C

/**
* Author......: See docs/credits.txt
* License.....: MIT
*/
#include "common.h"
#include "types.h"
#include "memory.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))
{
size_t nread = 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;
}
int fgetl (FILE *fp, char *line_buf)
{
int line_len = 0;
while (!feof (fp))
{
const int c = fgetc (fp);
if (c == EOF) break;
line_buf[line_len] = (char) c;
line_len++;
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);
}
int in_superchop (char *buf)
{
size_t len = strlen (buf);
while (len)
{
if (buf[len - 1] == '\n')
{
len--;
continue;
}
if (buf[len - 1] == '\r')
{
len--;
continue;
}
break;
}
buf[len] = 0;
return len;
}