Fixed FAT_Read to return number of bytes read. Created a simple `cat` command
authorJohn Hodge <[email protected]>
Sun, 27 Sep 2009 08:43:12 +0000 (16:43 +0800)
committerJohn Hodge <[email protected]>
Sun, 27 Sep 2009 08:43:12 +0000 (16:43 +0800)
Kernel/vfs/fs/fat.c
Usermode/Applications/cat_src/Makefile [new file with mode: 0644]
Usermode/Applications/cat_src/main.c [new file with mode: 0644]

index 8a55827..e03df09 100644 (file)
@@ -342,6 +342,15 @@ Uint64 FAT_Read(tVFS_Node *node, Uint64 offset, Uint64 length, void *buffer)
                return 0;\r
        }\r
        \r
+       // Sanity Check offset\r
+       if(offset > node->Size){\r
+               return 0;\r
+       }\r
+       // Clamp Size\r
+       if(length + offset > node->Size) {\r
+               length = offset - node->Size;\r
+       }\r
+       \r
        // Single Cluster including offset\r
        if(length + offset < bpc)\r
        {\r
@@ -349,7 +358,7 @@ Uint64 FAT_Read(tVFS_Node *node, Uint64 offset, Uint64 length, void *buffer)
                memcpy( buffer, (void*)( tmpBuf + offset%bpc ), length );\r
                free(tmpBuf);\r
                LEAVE('i', 1);\r
-               return 1;\r
+               return length;\r
        }\r
        \r
        preSkip = offset / bpc;\r
@@ -379,7 +388,7 @@ Uint64 FAT_Read(tVFS_Node *node, Uint64 offset, Uint64 length, void *buffer)
        if (count == 1) {\r
                free(tmpBuf);\r
                LEAVE('i', 1);\r
-               return 1;\r
+               return length;\r
        }\r
        \r
        cluster = FAT_int_GetFatValue(handle, cluster);\r
diff --git a/Usermode/Applications/cat_src/Makefile b/Usermode/Applications/cat_src/Makefile
new file mode 100644 (file)
index 0000000..dcd326d
--- /dev/null
@@ -0,0 +1,33 @@
+# Project: cat\r
+\r
+CC = gcc\r
+AS = nasm\r
+LD = ld\r
+RM = @rm -f\r
+\r
+COBJ = main.o
+BIN = ../cat\r
+ACESSDIR = /home/hodgeja/Projects/Acess2/Usermode\r
+\r
+INCS = -I$(ACESSDIR)/include -I./include\r
+CFLAGS = -Wall -fno-builtin -fno-stack-protector $(INCS)\r
+ASFLAGS = -felf\r
+LDFLAGS = -T $(ACESSDIR)/Libraries/acess.ld -I /Acess/Libs/ld-acess.so -lc\r
+\r
+.PHONY : all clean\r
+\r
+all: $(BIN)\r
+\r
+$(BIN): $(COBJ)\r
+       @echo --- $(LD) -o $@\r
+       @$(LD) $(LDFLAGS) -o $@ $(COBJ) -Map Map.txt\r
+       objdump -d $(BIN) > $(BIN).dsm\r
+       cp $(BIN) /mnt/AcessHDD/Acess2/\r
+\r
+clean:\r
+       $(RM) $(COBJ) $(BIN)\r
+\r
+$(COBJ): %.o: %.c\r
+       @echo --- GCC -o $@
+       @$(CC) $(CFLAGS) -c $? -o $@
+\r
diff --git a/Usermode/Applications/cat_src/main.c b/Usermode/Applications/cat_src/main.c
new file mode 100644 (file)
index 0000000..c60ab27
--- /dev/null
@@ -0,0 +1,38 @@
+/*
+ * Acess2 CAT command
+ */
+#include <acess/sys.h>
+#include <stdlib.h>
+#include <stdio.h>
+
+#define        BUF_SIZE        1024
+
+int main(int argc, char *argv[])
+{
+        int    fd;
+        int    num;
+       char    buf[BUF_SIZE+1];
+
+       if(argc < 2) {
+               printf("Usage: cat <file>\n");
+               return -1;
+       }
+
+       fd = open(argv[1], OPENFLAG_READ);
+       if(fd == -1) {
+               printf("Unable to open '%s' for reading\n", argv[1]);
+               return -1;
+       }
+
+       do {
+               num = read(fd, BUF_SIZE, buf);
+               printf("num = %i\n", num);
+               buf[num] = '\0';
+               printf("%s", buf);
+       } while(num == BUF_SIZE);
+
+       close(fd);
+       printf("\n");
+
+       return 0;
+}

UCC git Repository :: git.ucc.asn.au