/* * felis.c * * 13-May-2000 * * Similar to 'cat', felis will display the contents of a file to stdout. * Unlike 'cat,' felis assumes the file contains only text. Felis will * combine any and all blank space, including newlines, into single spaces. * I wrote it specifically so I can use the contents of a simple text * file as arguments to another program. For example: * * sendmail `felis customers friends` < message * * The above lets me send the same message to my customers and friends * whose email addresses are listed in the files named 'customers' and * 'friends'. * * If one or more arguments are listed on the command line, felis expects * them to be the files to display. If there are no arguments, felis * reads its input from stdin. * * Felis ignores any leading and trailing blanks, but prints a space between * files. Other than combining blanks, felis does not interpret the contents * of its input in any way. For example, it does not treat quoted strings * in any special way. * * What is a blank? As far as felis is concerned, any byte between 0 and ' ' * is a blank, everything else is not. This is a deliberate choice to * make felis compatible with ISO-8859 and UTF-8, without having to know * anything about the file encoding. * * By the way, 'felis' is the Latin word for 'cat.' * * Copyright 2000 G. Adam Stanislav. * All rights reserved. * * I can be reached at adam@whizkidtech.net. * The latest version of felis will be at ftp.whizkidtech.net and * http://www.whizkidtech.net/felis/ . * * This is version 1.0. */ #include FILE *input = NULL; int lastchar = ' '; static void blanks(void) { register unsigned int c; for(;;) { c = (unsigned)fgetc(input); if (c == EOF) return; if (c > (unsigned)' ') { ungetc((int)c, input); if (lastchar != (int)' ') fputc(lastchar = (int)' ', stdout); return; } } } static void felis(void) { register unsigned int c; for(;;) { c = (unsigned)fgetc(input); if (c == EOF) return; if (c <= ' ') blanks(); else fputc(lastchar = (int)c, stdout); } } int main(int argc, char *argv[]) { register int arg; register int retval; retval = 0; if (argc < 2) { input = stdin; felis(); } else for (arg = 1; arg < argc; arg++) { input = fopen(argv[arg], "r"); if (input == NULL) { retval++; fprintf(stderr, "felis (%s): Can't open file\n", argv[arg]); } else { if (lastchar != ' ') fputc(lastchar = (int)' ', stdout); felis(); fclose(input); } } return retval; }