Beispiel #1
0
def grep(pattern, fd):
    m = 0
    buf = " " * 1024

    # while (n = read(fd, buf+m, sizeof(buf)-m)) > 0
    while True:
        n, s = read(fd, 1024 - m)
        buf = buf[:m] + s
        if n <= 0:
            break

        m += n
        p = buf

        # while (q = strchr(p, '\n')) != 0
        while True:
            q = strchr(p, "\n")
            if q == 0:
                break

            q = "\0" + q[1:]
            if match(pattern, p):
                q = "\n" + q[1:]
                write(1, p, len(p) + 1 - len(q))

            p = q[1:]

        if p == buf:
            m = 0
        if m > 0:
            m -= len(p) - len(buf)
            # memmove(buf, p, m)
            buf = p[:m] + buf[m:]
Beispiel #2
0
def cat(fd):
    
    # while((n = read(fd, buf, sizeof(buf))) > 0)
    while True:
        n, buf = read(fd, 512)
        if n <= 0:
            break
        write(1, buf, n)
    
    if n < 0:
        printf(1, "cat: read error\n")  # @@@ should that be 2?
    
    exit_(1)
Beispiel #3
0
def writetest():
    
    printf(stdout, "small file test\n")
    fd = open_("small", O_CREATE | O_RDWR)
    if fd >= 0:
        printf(stdout, "creat small succeeded; ok\n")
    else:
        printf(stdout, "error: creat small failed!\n")
        exit_()
    
    for i in range(100):
        if write(fd, "aaaaaaaaaa", 10) != 10:
            printf(stdout, "error: write aa %d new file failed\n", i)
            exit_()
        if write(fd, "bbbbbbbbbb", 10) != 10:
            printf(stdout, "error: write bb %d new file failed\n", i)
            exit_()
    
    printf(stdout, "writes ok\n")
    close(fd)
    fd = open_("small", O_RDONLY)
    
    if fd >= 0:
        printf(stdout, "open small succeeded ok\n")
    else:
        printf(stdout, "error: open small failed!\n")
        exit_()
    
    i, buf = read(fd, 2000)
    if i == 2000:
        printf(stdout, "read succeeded ok\n")
    else:
        printf(stdout, "read failed\n")
        exit_()
    close(fd)
    
    if unlink("small") < 0:
        printf(stdout, "unlink small failed\n")
        exit_()
    
    printf(stdout, "small file test ok\n")
Beispiel #4
0
def writetest1():
    buf = "\0" * 2048
    
    printf(stdout, "big files test\n")
    
    fd = open_("big", O_CREATE | O_RDWR)
    
    if fd < 0:
        printf(stdout, "error: creat big failed!\n")
        exit_()
    
    for i in range(MAXFILE):
        buf = chr(i % 256) + buf[1:]
        
        if write(fd, buf, 512) != 512:
            printf(stdout, "error: write big file failed\n", i)
            exit_()
    
    close(fd)
    
    fd = open_("big", O_RDONLY)
    
    if fd < 0:
        printf(stdout, "error: open big failed!\n")
        exit_()
    
    n = 0
    while True:
        i, buf = read(fd, 512)
        if i == 0:
            if n == MAXFILE - 1:
                printf(stdout, "read only %d blocks from big", n)
                exit_()
            break
        elif i != 512:
            printf(stdout, "read failed %d\n", i)
            exit_()
        
        if buf[0] != chr(n % 256):
            printf(stdout, "read content of block %d is %d\n", n, buf[0])
            exit_()
        n += 1
    
    close(fd)
    
    if unlink("big") < 0:
        printf(stdout, "unlink big failed\n")
        exit_()
    printf(stdout, "big files ok\n")
Beispiel #5
0
def putc(fd, c):
    write(fd, c, 1)