FENIX_coreutils/echo.c

79 lines
1.5 KiB
C
Executable file

#define _POSIX_C_SOURCE 200809L
#include <stdio.h>
int ctoi(char c);
int main(int argc, char ** argv) {
int i, j, oct_temp;
char char_temp;
int trailing_newline = 1; //1 if true, 0 if \c present
for(i = 1; i < argc; i++) {
for(j = 0; argv[i][j] != '\0'; j++) {
if(argv[i][j] == '\\') {
j++;
switch(argv[i][j]) {
case 'a': printf("\a"); break;
case 'b': printf("\b"); break;
case 'c': trailing_newline = 0; break;
case 'f': printf("\f"); break;
case 'n': printf("\n"); break;
case 'r': printf("\r"); break;
case 't': printf("\t"); break;
case 'v': printf("\v"); break;
case '\\': printf("\\"); break;
case '0': {
j++;
oct_temp = ctoi(argv[i][j]);
j++;
if(ctoi(argv[i][j] != -1)) {
oct_temp *= 8;
oct_temp += ctoi(argv[i][j]);
j++;
if(ctoi(argv[i][j] != -1)) {
oct_temp *= 8;
oct_temp += ctoi(argv[i][j]);
}
else {
j--;
}
}
else {
j--;
}
printf("%c", (char)oct_temp);
}
break;
default: printf("\\%c", argv[i][j]);
break;
}
}
else {
printf("%c", argv[i][j]);
}
}
}
if(trailing_newline) {
printf("\n");
}
return 0;
}
int ctoi(char c) {
switch(c) {
case '0': return 0;
case '1': return 1;
case '2': return 2;
case '3': return 3;
case '4': return 4;
case '5': return 5;
case '6': return 6;
case '7': return 7;
case '8': return 8;
case '9': return 9;
default: return -1;
}
}