Skip to content

Commit

Permalink
adding support for reading from stdin.
Browse files Browse the repository at this point in the history
  • Loading branch information
UserXGnu committed Apr 5, 2017
1 parent 566920e commit ec9c4ec
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 20 deletions.
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
all:
gcc -Wall -Wextra -pedantic -o hdump hdump.c

debug:
gcc -Wall -Wextra -pedantic -DDEBUG -o hdump hdump.c

install:
install -m 0755 hdump /usr/local/bin/hdump

Expand Down
100 changes: 80 additions & 20 deletions hdump.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,25 +21,60 @@
#define _FILE_OFFSET_BITS 64

#include <unistd.h>
#include <sys/select.h>

#endif /*__unix__ */

#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>

#include <ctype.h>
#include <getopt.h>

#define VERSION "2.4"
#define BANNER puts("hdump "VERSION" by Fernando Merces - mentebinaria.com.br")
#define USAGE fatal("Usage:\n\thdump [-c columns] [-s skip] [-n length] file")


void fatal(const char *msg)
{
fprintf(stderr, "%s\n", msg);
exit(EXIT_FAILURE);
}

#ifdef __unix__
/**
* **@Nota:
* Testa se há algo no buffer de entrada.
*/
bool
available_input (void)
{

FILE * in;
bool available;
struct timeval tv;
fd_set fds;
tv.tv_sec = 0;
tv.tv_usec = 0;

if (!(in = fopen ("/dev/stdin", "rb")))
fatal ("checking standart input");


FD_ZERO (&fds);
FD_SET (fileno (in), &fds);
select ((fileno (in) + 1), &fds, NULL, NULL, &tv);
available = FD_ISSET (fileno (in), &fds);

fclose (in);

return available;
}

#endif /* __unix__ */

/* This function handles all the output space characters number calculation */
int get_spaces(int bread, int cols)
Expand All @@ -59,17 +94,39 @@ int get_spaces(int bread, int cols)

int main(int argc, char *argv[])
{
FILE *file;
FILE *file = NULL;
unsigned char *buff, *ascii;
register unsigned int i;
size_t cols;
unsigned long int bread, skip, length, address;
int c;
bool in = false;

bread = skip = length = address = cols = 0;

/**
* **@Nota:
* Caso seja windows, apenas imprime USAGE
* caso contrário, testa se tem algo no buffer
* de entrada, se tiver, assume que deve ler de lah
* ja que nenhum arquivo ou opção foi provida
* pelo usuário.
*/
if (argc < 2)
#ifndef __unix__
USAGE;
#else
{
if (!(file = fopen ("/dev/stdin", "rb")))
fatal ("checking standart input");
if (!(in = available_input ()))
USAGE;

if (!(file = fopen ("/dev/stdin", "rb")))
fatal ("opening standart input");

}
#endif /* __unix__ */

while ((c = getopt (argc, argv, "c:s:n:vh")) != -1)
{
Expand All @@ -89,33 +146,35 @@ int main(int argc, char *argv[])
USAGE;
}
}

if (!cols)
cols = 16;

if (!(file = fopen(argv[argc-1], "rb")))
fatal("file not found or not readable");

#ifdef __unix__

/*
* Caso o arquivo de entrada seja na verdade uma
* pseudo-tty como /dev/stdin.
/**
* **@Nota:
* caso seja windows, apenas tenta abrir o arquivo.
* Caso contrário, testa se há algo disponivel no buffer de entrada
* se sim, assume que deve ler de lá, evitando confundir as argumentos
* passados.
*/
if (isatty (fileno (file))) {
for ( ;skip > 0; skip --) {
if (fgetc (file) == EOF)
fatal ("skipping too much");
}
if (!file) {
#ifndef __unix__
if (!(file = fopen (argv[argc-1], "rb")))
#else
if (!(file = fopen ((available_input () ? "/dev/stdin" : argv[argc-1]), "rb")))
#endif /* __unix__ */
fatal("file not found or not readable");
}


#else

/* anda #skip posicoes para frente (-s) */
if (fseek(file, skip, SEEK_SET))
fatal("unable to seek through file");

#endif /* __unix__ */
for ( ;skip > 0; skip --) {
if (fgetc (file) == EOF)
fatal ("skipping too much");
}

buff = (unsigned char *) calloc (1, sizeof(unsigned char) * cols);
ascii = (unsigned char *) calloc (1, (sizeof(unsigned char) * cols) + 1);
Expand All @@ -126,6 +185,7 @@ int main(int argc, char *argv[])
do
{
bread = (int) fread(buff, sizeof(char), cols, file);

for (i=0; i<bread; i++)
{
/* imprime o offset */
Expand Down

0 comments on commit ec9c4ec

Please sign in to comment.