2012-01-29 02:19:57 +04:00
|
|
|
/* vim: tabstop=4 shiftwidth=4 noexpandtab
|
|
|
|
*
|
|
|
|
* echo
|
|
|
|
*/
|
|
|
|
#include <stdio.h>
|
2012-01-29 08:27:37 +04:00
|
|
|
#include <string.h>
|
2011-04-16 01:31:58 +04:00
|
|
|
|
|
|
|
void usage() {
|
2012-01-29 02:19:57 +04:00
|
|
|
printf("echo [-n] [-e] [STRING]...\n"
|
|
|
|
" -n do not output a new line at the end\n"
|
|
|
|
" -e process escape sequences\n");
|
2011-04-16 01:31:58 +04:00
|
|
|
}
|
|
|
|
|
2011-04-09 01:02:30 +04:00
|
|
|
int main(int argc, char ** argv) {
|
2011-04-16 01:31:58 +04:00
|
|
|
int start = 1;
|
|
|
|
int use_newline = 1;
|
|
|
|
int process_escapes = 0;
|
|
|
|
|
|
|
|
for (int i = start; i < argc; ++i) {
|
|
|
|
if (argv[i][0] != '-') {
|
|
|
|
start = i;
|
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
if (argv[i][1] == 'h') {
|
|
|
|
usage();
|
|
|
|
return 1;
|
|
|
|
} else if (argv[i][1] == 'n') {
|
|
|
|
use_newline = 0;
|
|
|
|
} else if (argv[i][1] == 'e') {
|
|
|
|
process_escapes = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int i = start; i < argc; ++i) {
|
|
|
|
if (process_escapes) {
|
|
|
|
for (int j = 0; j < strlen(argv[i]) - 1; ++j) {
|
|
|
|
if (argv[i][j] == '\\') {
|
|
|
|
if (argv[i][j+1] == 'e') {
|
|
|
|
argv[i][j] = '\033';
|
|
|
|
for (int k = j + 1; k < strlen(argv[i]); ++k) {
|
|
|
|
argv[i][k] = argv[i][k+1];
|
|
|
|
}
|
|
|
|
}
|
2011-04-19 10:50:33 +04:00
|
|
|
if (argv[i][j+1] == 'n') {
|
|
|
|
argv[i][j] = '\n';
|
|
|
|
for (int k = j + 1; k < strlen(argv[i]); ++k) {
|
|
|
|
argv[i][k] = argv[i][k+1];
|
|
|
|
}
|
|
|
|
}
|
2011-04-16 01:31:58 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-01-29 02:19:57 +04:00
|
|
|
printf("%s",argv[i]);
|
2011-04-12 01:51:31 +04:00
|
|
|
if (i != argc - 1) {
|
2012-01-29 02:19:57 +04:00
|
|
|
printf(" ");
|
2011-04-12 01:51:31 +04:00
|
|
|
}
|
2011-04-09 01:02:30 +04:00
|
|
|
}
|
2011-04-16 01:31:58 +04:00
|
|
|
|
|
|
|
if (use_newline) {
|
2012-01-29 02:19:57 +04:00
|
|
|
printf("\n");
|
2011-04-16 01:31:58 +04:00
|
|
|
}
|
2011-04-12 01:51:31 +04:00
|
|
|
return 0;
|
2011-04-09 01:02:30 +04:00
|
|
|
}
|