File: hex.c

package info (click to toggle)
libdumbnet 1.16.3-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 5,652 kB
  • sloc: ansic: 11,557; sh: 4,000; python: 255; makefile: 92
file content (62 lines) | stat: -rw-r--r-- 991 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
/*
 * hex.c
 *
 * Copyright (c) 2001 Dug Song <[email protected]>
 *
 * $Id$
 */

#include "config.h"

#include <sys/types.h>

#include <err.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

#include "dumbnet.h"
#include "aton.h"
#include "mod.h"

void
hex_usage(void)
{
	fprintf(stderr, "Usage: dnet hex <string> ...\n");
	exit(1);
}

int
hex_main(int argc, char *argv[])
{
	char buf[IP_LEN_MAX], *p = buf;
	int c, len;
	
	if (argc == 1 || *(argv[1]) == '-')
		hex_usage();
	
	for (c = 1; c < argc; c++) {
		if ((len = fmt_aton(argv[c], (u_char *)argv[c])) < 0)
			hex_usage();
		
		if (write(STDOUT_FILENO, argv[c], len) != len)
			err(1, "write");
	}
	if (!isatty(STDIN_FILENO)) {
		len = sizeof(buf);
		while ((c = read(STDIN_FILENO, p, len)) > 0) {
			p += c;
			len -= c;
		}
		len = p - buf;
		if (write(STDOUT_FILENO, buf, len) != len)
			err(1, "write");
	}                                  
	return (0);
}

struct mod mod_hex = {
	"hex",
	MOD_TYPE_DATA,
	hex_main
};