19
19
20
20
#ifdef __unix__
21
21
#define _FILE_OFFSET_BITS 64
22
+
23
+ #include <unistd.h>
24
+
22
25
#endif /*__unix__ */
23
26
24
27
#include <stdio.h>
@@ -90,20 +93,36 @@ int main(int argc, char *argv[])
90
93
if (!cols )
91
94
cols = 16 ;
92
95
93
- buff = ( unsigned char * ) calloc ( 1 , sizeof ( unsigned char ) * cols );
94
- ascii = ( unsigned char * ) calloc ( 1 , ( sizeof ( unsigned char ) * cols ) + 1 );
96
+ if (!( file = fopen ( argv [ argc - 1 ], "rb" )))
97
+ fatal ( "file not found or not readable" );
95
98
99
+ #ifdef __unix__
96
100
97
- if (!buff || !ascii )
98
- fatal ("not enough memory" );
101
+ /*
102
+ * Caso o arquivo de entrada seja na verdade uma
103
+ * pseudo-tty como /dev/stdin.
104
+ */
105
+ if (isatty (fileno (file ))) {
106
+ for ( ;skip > 0 ; skip -- ) {
107
+ if (fgetc (file ) == EOF )
108
+ fatal ("skipping too much" );
109
+ }
110
+ }
99
111
100
- if (!(file = fopen (argv [argc - 1 ], "rb" )))
101
- fatal ("file not found or not readable" );
112
+ #else
102
113
103
114
/* anda #skip posicoes para frente (-s) */
104
115
if (fseek (file , skip , SEEK_SET ))
105
116
fatal ("unable to seek through file" );
106
117
118
+ #endif /* __unix__ */
119
+
120
+ buff = (unsigned char * ) calloc (1 , sizeof (unsigned char ) * cols );
121
+ ascii = (unsigned char * ) calloc (1 , (sizeof (unsigned char ) * cols ) + 1 );
122
+
123
+ if (!buff || !ascii )
124
+ fatal ("not enough memory" );
125
+
107
126
do
108
127
{
109
128
bread = (int ) fread (buff , sizeof (char ), cols , file );
@@ -119,15 +138,12 @@ int main(int argc, char *argv[])
119
138
/* imprime os bytes separados por espaço */
120
139
printf ("%02x%*c" , (unsigned int ) * (buff + i ), (i + 1 == cols /2 ) ? 2 : 1 , ' ' );
121
140
122
- /*
123
- * define o final do array asciii (será usado como string)
124
- * imprime os caracteres ascii
125
- * */
126
- if (i == bread - 1 )
127
- {
128
- * (ascii + bread ) = '\0' ;
141
+ /* define o fim do array ascii (sera usado como string) */
142
+ * (ascii + bread ) = '\0' ;
143
+
144
+ /* imprime os caracteres ascii */
145
+ if (i == bread - 1 )
129
146
printf ("%*c|%s|\n" , get_spaces (bread , cols ), ' ' , ascii );
130
- }
131
147
}
132
148
/* atualiza o numero de endereços lidos */
133
149
address += bread ;
0 commit comments