File: dcasehost.c

package info (click to toggle)
smail 3.2.0.115-7.1
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 5,192 kB
  • ctags: 4,249
  • sloc: ansic: 48,248; sh: 4,321; makefile: 2,074; perl: 1,147; awk: 884; yacc: 428; sed: 256; xml: 57
file content (171 lines) | stat: -rw-r--r-- 4,080 bytes parent folder | download
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
/*
#ident	"@(#)smail/util:RELEASE-3_2_0_115:dcasehost.c,v 1.15 2003/04/02 22:15:04 woods Exp"
 */

/*
 *    Copyright (C) 1987, 1988 Ronald S. Karr and Landon Curt Noll
 *    Copyright (C) 1992  Ronald S. Karr
 * 
 * See the file COPYING, distributed with smail, for restriction
 * and warranty information.
 */

/*
 * dcasehost - downcase the host field of standard pathalias output
 *
 * Downcase the first field (non-while chars up to whitespace).
 * In the common case of pathalias, this downcases the hostname
 * while leaving the path untouched.
 *
 * If the -c option is used, then input is assumed to be of
 * pathalias -c form:
 *	cost hostname path
 * and is converted to:
 *	hostname path cost
 * before downcase is done.
 */

#include "defs.h"

#include <sys/types.h>
#include <stdio.h>
#include <ctype.h>

#ifdef STDC_HEADERS
# include <stdlib.h>
# include <stddef.h>
#else
# ifdef HAVE_STDLIB_H
#  include <stdlib.h>
# endif
#endif

#if defined(__GLIBC__) && !defined(__OPTIMIZE_SIZE__)
/*
 * XXX __NO_STRING_INLINES may even be a better choice here, but this works for
 * now for at least GLIBC 2.3 on Red Hat 8.0....
 */
# define __OPTIMIZE_SIZE__      1       /* avoid buggy inline macros */
#endif

#ifdef HAVE_STRING_H
# if !defined(STDC_HEADERS) && defined(HAVE_MEMORY_H)
#  include <memory.h>
# endif
# include <string.h>
#endif
#ifdef HAVE_STRINGS_H
# include <strings.h>
#endif

#include "smail.h"
#include "main.h"
#include "smailport.h"

char *program;			/* argv[0] from main */

char buf[BUFSIZ+1+1];		/* i/o line buffer */
char buf2[BUFSIZ+1+1];		/* holder for field one switch (-c) */
char buf3[BUFSIZ+1+1];		/* holder for other fields (-c) */

int
main(argc, argv)
    int argc;		/* arg count */
    char *argv[];	/* args */
{
    char *f1_white;	/* first whitespace beyond field 1 */
    int cflag = 0;	/* cflag == 1 ==> field switch */

    /*
     * parse args
     */
    program = argv[0];
    if ( argc > 2 ) {
	fprintf(stderr, "%s: usage: %s [-c]\n", program, program);
	exit(-1);
    } else if (argc == 2) {
	if (strcmp(argv[1], "-c") != 0) {
	    fprintf(stderr, "%s: usage: %s [-c]\n", program, program);
	    exit(-1);
	}
	cflag = 1;
    }

    /*
     * process input lines until EOF
     */
    while (fgets(buf, BUFSIZ, stdin) != NULL) {

	/*
	 * if -c, move the fields
	 */
	if (cflag) {
	    char *f2_field = NULL;	/* first char of field 2 */
	    char *f_end;		/* last char */

	    /* find first whitespace char beyond field 1 */
	    f1_white = strpbrk(&buf[strspn(buf, " \t\n")], " \t\n");

	    /* find first char of field 2 */
	    if (f1_white != NULL) {
		f2_field = &f1_white[strspn(f1_white, " \t\n")];
	    }

	    /* find last char */
	    f_end = &buf[strlen(buf)-1];

	    /* switch fields if firewall checks  allow it */
	    if (f1_white != NULL && *f1_white != '\0' &&
		*f2_field != '\0' &&
		f_end > buf && *f_end == '\n') {

	        /* save field 1 with newline and NULL */
	        strncpy(buf2, buf, (size_t)(f1_white-buf));
		buf2[f1_white-buf] = '\n';
		buf2[f1_white-buf+1] = '\0';

		/* move field 2 and beyond to front of line */
		strcpy(buf3, f2_field);
		strcpy(buf, buf3);

		/*
		 * add field 1 on the end of new line
		 *
		 * use white space before field 1 to separate
		 * field 1 from the end of the line, if we can
		 */
		if (buf2[0] == ' ' || buf2[0] == '\t') {
		    /* overwrite the old trailing newline */
		    strncpy(&buf[f_end-f2_field], buf2, (size_t) (f1_white-buf+2));
		    
		} else {
		    /* separate old line end and field 1 with a tab */
		    buf[f_end-f2_field] = '\t';
		    strncpy(&buf[f_end-f2_field+1], buf2, (size_t) (f1_white-buf+2));
		}
	    }
	}

	/*
	 * downcase the first field
	 */
	/* find first whitespace char beyond field 1 */
	f1_white = strpbrk(&buf[strspn(buf, " \t\n")], " \t\n");
	/* downcase field 1 if it exists */
	if (f1_white != NULL) {
	    register char *p;	/* index */

	    for (p=buf; p < f1_white; ++p) {
		*p = tolower((int) *p);
	    }
	}

	/*
	 * print the resulting line
	 */
	fputs(buf, stdout);
    }
    exit(0);

    /* NOTREACHED */
}