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
|
/* @(#) dcasehost.c,v 1.5 1997/12/16 22:07:28 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 <stdio.h>
#include <ctype.h>
#include "defs.h"
#ifdef HAVE_STDLIB_H
# include <stdlib.h>
#endif
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; /* 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, (int)(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, 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, 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 = lowercase(*p);
}
}
/*
* print the resulting line
*/
fputs(buf, stdout);
}
exit(0);
/* NOTREACHED */
}
|