1999-08-01 20:09:10 +02:00
|
|
|
char scat_c_cvs_version[] =
|
2000-02-10 01:17:35 +01:00
|
|
|
"$Id: scat.c,v 1.3 2000-02-10 00:17:35 hjp Exp $";
|
1999-08-01 20:09:10 +02:00
|
|
|
/* scat - safe cat
|
|
|
|
*
|
|
|
|
* catenate input files and print to standard output.
|
|
|
|
* replace all non-printable characters with C \xXX escapes.
|
|
|
|
*
|
|
|
|
* $Log: scat.c,v $
|
2000-02-10 01:17:35 +01:00
|
|
|
* Revision 1.3 2000-02-10 00:17:35 hjp
|
|
|
|
* Shut up gcc
|
|
|
|
*
|
|
|
|
* Revision 1.2 1999/08/01 18:09:09 hjp
|
1999-08-01 20:09:10 +02:00
|
|
|
* First release
|
|
|
|
*
|
|
|
|
*/
|
1999-08-01 19:27:29 +02:00
|
|
|
#include <ctype.h>
|
1999-08-01 20:09:10 +02:00
|
|
|
#include <errno.h>
|
1999-08-01 19:27:29 +02:00
|
|
|
#include <locale.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
1999-08-01 20:09:10 +02:00
|
|
|
#include <string.h>
|
1999-08-01 19:27:29 +02:00
|
|
|
|
|
|
|
#include <features.h>
|
|
|
|
#include <nl_types.h>
|
|
|
|
|
|
|
|
#include "scat.h"
|
|
|
|
|
|
|
|
char *cmnd;
|
|
|
|
nl_catd catalog;
|
|
|
|
|
2000-02-10 01:17:35 +01:00
|
|
|
static void usage(void) {
|
1999-08-01 19:27:29 +02:00
|
|
|
fprintf(stderr,
|
|
|
|
catgets(catalog, MSG_Set, MSG_USAGE,
|
|
|
|
"Usage: %s [file ...]\n"),
|
|
|
|
cmnd);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-02-10 01:17:35 +01:00
|
|
|
static void do_safecat(const char *filename) {
|
1999-08-01 19:27:29 +02:00
|
|
|
FILE *fp;
|
|
|
|
int c;
|
|
|
|
|
|
|
|
|
|
|
|
if (strcmp(filename, "-") == 0) {
|
|
|
|
fp = stdin;
|
|
|
|
} else {
|
|
|
|
if ((fp = fopen(filename, "r")) == NULL) {
|
1999-08-01 20:09:10 +02:00
|
|
|
fprintf(stderr, catgets(catalog, MSG_Set, MSG_OPEN,
|
1999-08-01 19:27:29 +02:00
|
|
|
"%s: cannot open `%s' for reading: %s\n"),
|
1999-08-01 20:09:10 +02:00
|
|
|
cmnd, filename, strerror(errno));
|
1999-08-01 19:27:29 +02:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
while ((c = getc(fp)) != EOF) {
|
|
|
|
if (isprint(c) || isspace(c)) {
|
|
|
|
if (putchar(c) == EOF) {
|
|
|
|
fprintf(stderr,
|
|
|
|
catgets(catalog, MSG_Set, MSG_WRITE,
|
|
|
|
"%s: cannot write stdout: %s\n"),
|
1999-08-01 20:09:10 +02:00
|
|
|
cmnd, strerror(errno));
|
1999-08-01 19:27:29 +02:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (printf("\\x%02X", c) == EOF) {
|
|
|
|
fprintf(stderr,
|
|
|
|
catgets(catalog, MSG_Set, MSG_WRITE,
|
|
|
|
"%s: cannot write stdout: %s\n"),
|
1999-08-01 20:09:10 +02:00
|
|
|
cmnd, strerror(errno));
|
1999-08-01 19:27:29 +02:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (ferror(fp)) {
|
|
|
|
fprintf(stderr,
|
|
|
|
catgets(catalog, MSG_Set, MSG_READ,
|
|
|
|
"%s: cannot read from `%s': %s\n"),
|
1999-08-01 20:09:10 +02:00
|
|
|
cmnd, filename, strerror(errno));
|
1999-08-01 19:27:29 +02:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
if (strcmp(filename, "-") == 0) {
|
|
|
|
} else {
|
|
|
|
fclose(fp);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int main(int argc, char **argv) {
|
|
|
|
setlocale(LC_ALL, "");
|
|
|
|
|
1999-08-01 20:09:10 +02:00
|
|
|
cmnd = argv[0];
|
1999-08-01 19:27:29 +02:00
|
|
|
catalog = catopen("scat", 0);
|
|
|
|
if (argc == 1) {
|
|
|
|
do_safecat("-");
|
|
|
|
} else {
|
|
|
|
int i;
|
|
|
|
for (i = 1; i < argc; i++) {
|
|
|
|
do_safecat(argv[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|