Beispiel #1
0
def wc(fd, name):
    l = 0
    w = 0
    c = 0
    inword = False
    
    # while((n = read(fd, buf, sizeof(buf))) > 0)
    while True:
        n, buf = read(fd, 512)
        if n <= 0:
            break
        
        for i in range(n):
            c += 1
            if buf[i] == "\n":
                l += 1
            if strchr(" \r\t\n\v", buf[i]):
                inword = False
            elif not inword:
                w += 1
                inword = True
    
    if n < 0:
        printf(1, "wc: read error\n")
        exit_(1)
    
    printf(1, "%d %d %d %s\n", l, w, c, name)
Beispiel #2
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 #3
0
def gets(max_length):

    buf = ""
    for i in range(max_length):
        cc, c = read(0, 1)
        if cc < 1:
            break
        buf += c
        if c == "\n" or c == "\r":
            break
    buf += "\0"
    return len(buf), buf
Beispiel #4
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 #5
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 #6
0
def publish(id, publishData):
    """
    Set draft status of a shift to false. Sync publishData field.
    If the shift is private only publish to the streams that
    the user has access. If the shift is publich publish it to
    any of the public non-user streams. Creates the comment stream
    if it doesn't already exist.
    Parameters:
        id - a shift id.
        publishData - a dictionary holding the publish options.
    """
    db = core.connect()
    theShift = db[id]
    theUser = db[theShift["createdBy"]]
    userId = theUser["_id"]
    allowed = []
    publishStreams = publishData.get("streams") or []
    if (publishData.get("private") == True) or (publishData.get("private") == None and isPrivate(id)):
        allowedStreams = permission.writeableStreams(userId)
        allowed = list(set(allowedStreams).intersection(set(publishStreams)))
        # add any private user streams this shift is directed to
        if publishData.get("users"):
            allowed.extend([user.privateStream(user.idForName(userName)) 
                            for userName in publishData["users"]
                            if user.read(userName)])
            del publishData["users"]
        # add streams this user can post to
        allowed.extend([astream for astream in publishStreams
                        if stream.canPost(astream, userId)])
    else:
        allowed.append(user.publicStream(userId))
    # TODO: commentStreams should use the permission of the streams the shift has been published to. -David 7/14/09
    if not commentStream(id):
        streamId = createCommentStream(id)
        user.addNotification(userId, streamId)
        
    # remove duplicates
    publishData["streams"] = list(set(allowed))
    newData = theShift["publishData"]
    newData.update(publishData)
    theShift["publishData"] = newData
    theShift["publishData"]["draft"] = False
    db[id] = theShift
Beispiel #7
0
def ls(path):
    
    fd = open_(path, 0)
    if fd < 0:
        printf(2, "ls: cannot open %s\n", path)
        return
    
    n, st = fstat(fd)
    if n < 0:
        printf(2, "ls: cannot stat %s\n", path)
        close(fd)
        return
    
    if st.type == T_FILE:
        printf(1, "%s %d %d %d\n", fmtname(path), st.type, st.ino, st.size)
    
    elif st.type == T_DIR:
        if strlen(path) + 1 + DIRSIZ + 1 > 512:
            printf(1, "ls: path too long\n")
        else:
            prefix = path + "/"
            
            #while (read(fd, &de, sizeof(de)) == sizeof(de))
            while True:
                n, de = read(fd, 20)  # @@@ 20 = sizeof(de)
                if n != 20:
                    break
                de_inum = int(de[:6])
                de_name = de[6:].strip()  # @@@
                
                if de_inum == 0:
                    continue
                path = prefix + de_name
                
                n, st = stat(path)
                if n < 0:
                    printf(1, "ls: cannot stat %s\n", path)
                    continue
                
                printf(1, "%s %d %d %d\n", fmtname(path), st.type, st.ino, st.size)
    close(fd)
Beispiel #8
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")