wm: teppich

Download patch

ref: fbbd09eb0b56768760764bcef1e90a2087da2e53
parent: 79a7ec816f9e84257b6dc0a462e0cec8c29affe3
author: de88 <hamidreza.soleimani88@gmail.com>
date: Sat Dec 9 03:50:44 EST 2023

added octal dump and WC

--- a/Makefile
+++ b/Makefile
@@ -30,6 +30,8 @@
 	cmd/cat.o\
 	cmd/login.o\
 	cmd/id.o\
+	cmd/wc.o\
+	cmd/od.o\
 
 L = \
 	libc/strlen.o\
--- /dev/null
+++ b/cmd/od.c
@@ -1,0 +1,60 @@
+#include <u.h>
+#include <libc.h>
+	
+int
+od_main (int argc, char **argv)
+{
+    printf("Enter text, ESC to end input.\n");
+    char c;
+    char *txt;
+	txt = malloc(100);
+	memset(txt, 0, 100);
+    int i = 0;
+    // while for reading input
+    while(1)
+    {
+        c = ps2_getc();
+        // end input if ESC pressed
+        if ((int)c == 27)
+        {
+            printf("%c", '\n');
+            break;
+        }
+        // remove with backspace
+        if (c == '\b')
+        {
+            if(!i) continue;
+            i--;
+            txt[i] = 0;
+        }
+        txt[i] = c;
+        i++;
+	    printf("%c", c);
+    }
+    
+    // calculate answer
+    i = 0;
+    int answer, pow;
+    char input;
+    while (strcmp(txt+i,"\0"))
+    {
+        input = *(txt+i);
+        answer = 0;
+        pow = 1;
+        for (int mult = 0; input > 0; mult ++)
+        {
+            answer += (input % 8) * pow;
+            input = input / 8;
+            pow *= 10;
+        }
+        i++;
+        printf("%d  ", answer);
+
+        // newline after 10 outputs
+        if (i%10 == 0) printf("\n");
+    }
+    // for debug
+    // printf("%d\n", argc);
+    // printf("%d\n", *cwd);
+    
+}
--- a/cmd/rc.h
+++ b/cmd/rc.h
@@ -16,6 +16,8 @@
 extern int cat_main(int argc, char **argv);
 extern int login_main(int argc, char **argv);
 extern int id_main(int argc, char **argv);
+extern int od_main(int argc, char **argv);
+extern int wc_main(int argc, char **argv);
 
 static prog_t cmdtab[] =
 {
@@ -29,4 +31,6 @@
 	{"cat", cat_main},
 	{"login", login_main},
 	{"id", id_main},
+	{"od", od_main},
+	{"wc", wc_main},
 };
--- /dev/null
+++ b/cmd/wc.c
@@ -1,0 +1,94 @@
+#include <u.h>
+#include <libc.h>
+
+#define memsize 1000
+void
+wc_main(int argc, char **argv)
+{
+    printf("Enter text, ESC to end input.\n");
+    char c;
+    char *txt;
+    int i = 0;
+	txt = malloc(memsize);
+	memset(txt, 0, memsize);
+    
+    // while for reading input
+    while(1)
+    {
+        c = ps2_getc();
+        // end input if ESC pressed
+        if ((int)c == 27 && i < memsize)
+        {
+            printf("%c", '\n');
+            break;
+        }
+        // remove with backspace
+        if (c == '\b')
+        {
+            if(!i) continue;
+            i--;
+            txt[i] = 0;
+        }
+        txt[i] = c;
+        i++;
+	    printf("%c", c);
+    }
+
+    // start counting
+    int lines = 1;
+    int words = 0;
+    int bytes = 0;
+    int is_space = 1;
+    i = 0;
+    while (txt[i] != '\0')
+    {
+        // break if end of text
+        if (txt[i] == '\0')
+        {
+            break;
+        }
+        i ++;
+        bytes ++;
+        
+        // remember lines for counting words
+        if (txt[i] == '\r' || txt[i] == '\n') 
+        {
+            lines++;
+            is_space = 1;
+            continue;
+        }
+        
+        // remember spaces for counting words
+        if (txt[i] == ' ')
+        {
+            is_space = 1;
+            continue;
+        }
+        
+        // count words if previouscharacter was space
+        if ((is_space == 1) && (txt[i] != ' ') && (txt[i] != '\n')) 
+        {
+            words++;
+            is_space = 0;
+        }
+    }
+    if (bytes == 0) lines == 0;
+
+    // print output without flags
+    if (argc == 1)
+    {
+        printf("number of words: %d\n", words);
+        printf("number of lines: %d\n", lines);
+        printf("number of bytes: %d\n", bytes);
+        return;
+    }
+    // print output with flags
+    i = 1;
+    while (i < argc)
+    {
+        if (!strcmp(argv[i], "-w")) printf("number of words: %d\n", words);
+        else if (!strcmp(argv[i], "-l")) printf("number of lines: %d\n", lines);
+        else if (!strcmp(argv[i], "-b"))  printf("number of bytes: %d\n", bytes);    
+        i++;
+    }
+}