Skip to content

Commit

Permalink
adding extra fix to the issues merces#7 and merces#6, also cleaning s…
Browse files Browse the repository at this point in the history
…ome mistaken code made by me
  • Loading branch information
UserXGnu committed Apr 7, 2017
1 parent ec9c4ec commit 8cc2ffd
Showing 1 changed file with 34 additions and 22 deletions.
56 changes: 34 additions & 22 deletions hdump.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,21 @@
#include <unistd.h>
#include <sys/select.h>

/**
* **@Nota:
* IN é definido aqui dessa forma,
* para evitar o uso de #ifdefs pelo codigo
*/
#define IN "/dev/stdin"

#else /* WINDOWS */

#define IN ""

#endif /*__unix__ */



#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
Expand All @@ -44,15 +57,14 @@ void fatal(const char *msg)
exit(EXIT_FAILURE);
}

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

#ifdef __unix__
FILE * in;
bool available;
struct timeval tv;
Expand All @@ -72,9 +84,11 @@ available_input (void)
fclose (in);

return available;
#else
return false;
#endif /* __unix__ */
}

#endif /* __unix__ */

/* This function handles all the output space characters number calculation */
int get_spaces(int bread, int cols)
Expand Down Expand Up @@ -112,21 +126,15 @@ int main(int argc, char *argv[])
* ja que nenhum arquivo ou opção foi provida
* pelo usuário.
*/
in = available_input ();
if (argc < 2)
#ifndef __unix__
USAGE;
#else
{
if (!(file = fopen ("/dev/stdin", "rb")))
fatal ("checking standart input");
if (!(in = available_input ()))
{
if (!in)
USAGE;
}
if (!in && (strstr (argv [argc-2], "-")))
USAGE;

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

}
#endif /* __unix__ */

while ((c = getopt (argc, argv, "c:s:n:vh")) != -1)
{
Expand Down Expand Up @@ -159,14 +167,18 @@ int main(int argc, char *argv[])
* se sim, assume que deve ler de lá, evitando confundir as argumentos
* passados.
*/
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");

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

if (!in) {
fseek (file, 0, SEEK_END);
if (!ftell (file)) {
fatal ("Could not read the file: file is empty");
}
rewind (file);
}




Expand Down

0 comments on commit 8cc2ffd

Please sign in to comment.