int cmd(char *s, int cs) { pid_t pid; int status; char **spt; if (!s || !*s) return (0); spt = ft_getarg(s); if (!ft_strcmp(spt[0], "get")) { ft_get(spt, cs); return (0); } if (!ft_strcmp(spt[0], "cd")) { ft_cd(spt[1], cs); return (0); } if (!(pid = fork())) ft_launch(s, cs); else if (pid > 0) wait4(pid, &status, 0, NULL); else if (pid == -1) ft_putendl_fd("fork", 2); return (1); }
int main(int ac, char **av) { int sock; char buf[1023]; int r; if (ac != 3) usage(av[0]); sock = ft_create_client(av[1], ft_atoi(av[2])); while (1) { write(1, "$> ", 3); r = read(0, buf, 1023); buf[r - 1] = 0; write(sock, buf, r); if (ft_strcmp(buf, "quit") == 0) break ; if (ft_strncmp(buf, "put", 3) == 0) { r = test(sock); if (r > -1) { ft_put(buf, sock); ft_putstr("SUCCESS\n"); } } else if (ft_strncmp(buf, "get", 3) == 0) ft_get(buf, sock); else test(sock); } close(sock); return (0); }
int ft_set(struct filetable* ft, struct filedescriptor* fd, int fti) { if (fti >= ft_array_size(ft)) { return 1; } array_setguy(ft->filedescriptor, fti, fd); if (ft_get(ft, fti) == fd) { return 1; } return 0; }
/* * ft_test() * This tests the implementation of the file table, insertion, deletion, fd recovery. * This test is not passable, it will crash the kernel. */ void ft_test(struct filetable* ft) { kprintf("filetable test begin\n"); kprintf("inserting file descriptors\n"); ft_attachstds(ft); kprintf("printing file descriptors\n"); int i = 0; for (i = 0; i < ft_array_size(ft); i++) { struct filedescriptor *fd; fd = ft_get(ft, i); kprintf("filetable index: %d\n", i); kprintf("fdn: %d ", fd->fdn); kprintf("\n"); } kprintf("inserting more file descriptors\n"); ft_attachstds(ft); kprintf("printing file descriptors\n"); for (i = 0; i < ft_array_size(ft); i++) { struct filedescriptor *fd; fd = ft_get(ft, i); kprintf("filetable index: %d ", i); kprintf("fdn: %d ", fd->fdn); kprintf("\n"); } kprintf("removing file descriptor 4\n"); ft_remove(ft, 4); kprintf("inserting more file descriptors\n"); ft_attachstds(ft); kprintf("printing file descriptors\n"); for (i = 0; i < ft_array_size(ft); i++) { struct filedescriptor *fd; fd = ft_get(ft, i); kprintf("filetable index: %d ", i); kprintf("fdn: %d ", fd->fdn); kprintf("\n"); } kprintf("inserting infinite number of file descriptors\n"); for (i = 0; i < 2147483647; i++) { kprintf("ft_array_size: %d\n", ft_array_size(ft)); ft_attachstds(ft); } panic("test end"); }
/* * ft_size() * This returns how many file descriptors are opened to the thread. */ int ft_size(struct filetable *ft) { assert(ft != NULL); int total = array_getnum(ft->filedescriptor); int i = 0; for (i = 0; i < ft_array_size(ft); i++) { if (ft_get(ft, i) == NULL) { total--; } } return total; }
/* * ft_remove() * This removes the file descriptor from the file table, it will add the file * descriptor id to the queue of availiable ids to use. */ int ft_remove(struct filetable* ft, int fti) { struct filedescriptor * fd = ft_get(ft, fti); if (fd != NULL) { // q_addtail(ft->nextfiledescriptor, (void *) fd->fdn); int spl = splhigh(); fd->numOwners--; if (fd->numOwners == 0) { vfs_close(fd->fdvnode); kfree(fd); } splx(spl); array_setguy(ft->filedescriptor, fti, NULL); } return 1; }
char *ft_build(va_list args, t_format *opt) { va_list ptr; char *content; int i; if (opt->type & 0xff) { if (opt->arg > 0) { va_copy(ptr, args); i = -1; while (++i < opt->arg) va_arg(ptr, void *); content = ft_get(ptr, opt); va_end(ptr); } else content = ft_get(args, opt); } else content = ft_alter(ft_ctos(opt->spec), opt, 0, 1); return (content); }
/* * ft_test() * This tests the implementation of the file table, insertion, deletion, fd recovery. * This test is not passable, it will crash the kernel. */ void ft_test_list(struct filetable* ft) { kprintf("printing file descriptors\n"); int i = 0; for (i = 0; i < ft_array_size(ft); i++) { struct filedescriptor *fd; fd = ft_get(ft, i); kprintf("filetable index: %d ", i); if (fd != NULL) { kprintf("fdn: %d ", fd->fdn); } else { kprintf("fdn: NULL "); } kprintf("\n"); } }
void condition(char *buff, int sock) { int r; if (!ft_strncmp("quit", buff, 4)) ft_quit(sock); if (!ft_strncmp("get", buff, 3)) ft_get(buff, sock); if (ft_strcmp("ls", buff) == 0 || ft_strcmp("pwd", buff) == 0 || ft_strncmp("cd", buff, 2) == 0) { while ((r = read(sock, buff, 1)) > 0 && buff[0] != '\0') { buff[r] = '\0'; write(1, buff, r); } } }
/* * ft_add() * This adds the file descriptor to the file table, it does by checking if there * is a free file descriptor in the queue, if not, it will add to the end of the * array. This will recover and reuse closed file descriptor ids. */ int ft_add(struct filetable* ft, struct filedescriptor* fd) { int fdn = 0; for (fdn = 0; fdn < ft_array_size(ft) && fdn < OPEN_MAX; fdn++) { if (ft_get(ft, fdn) == NULL) { array_setguy(ft->filedescriptor, fdn, fd); return fdn; } } if (fdn == OPEN_MAX) { return -1; } if (array_add(ft->filedescriptor, fd) != 0) { //if error (ENOMEM) return -1; } fd->numOwners++; assert(fdn != 0); return fdn; }
static void execcmd(char *buf, int sock) { if (ft_strcmp("quit", buf) == 0) { close(sock); exit(0); } else if (ft_strncmp(buf, "get ", 4) == 0) ft_get(sock, buf); else if (ft_strncmp(buf, "put ", 4) == 0) ft_put(sock, buf); else if (ft_strncmp(buf, "help", 4) == 0) { ft_bzero(buf, 1024); ft_help(); } else ft_putendl("ERROR"); }
void ft_build(va_list args, va_list start, t_format *opt) { va_list ptr; char *content; unsigned int i; if (opt->arg != 0) { va_copy(ptr, start); i = 0; while (++i < opt->arg) va_arg(ptr, void *); } else va_copy(ptr, args); content = ft_get(ptr, opt); va_end(ptr); printf("getting to it\n"); }
int sys_read(int *retval, int fdn, void *buf, size_t nbytes) { //Check for Bad memory reference. if (!buf || (u_int32_t) buf >= MIPS_KSEG0 || !as_valid_write_addr(curthread->t_vmspace,buf)) { return EFAULT; } //Get the file descriptor from the opened list of file descriptors that the current thread has, based on the fdn given. struct filedescriptor* fd = ft_get(curthread->ft, fdn); if (fd == NULL) { return EBADF; } //Make sure that the file is opened for writing. switch (O_ACCMODE & fd->mode) { case O_RDONLY: case O_RDWR: break; default: return EBADF; } //Make the uio struct uio u; mk_kuio(&u, (void *) buf, nbytes, fd->offset, UIO_READ); //Disable interrupt //int spl; //spl = splhigh(); //Read int sizeread = VOP_READ(fd->fdvnode, &u); //splx(spl); if (sizeread) { return sizeread; } //Find the number of bytes read sizeread = nbytes - u.uio_resid; *retval = sizeread; //Update the offset fd->offset += sizeread; return 0; }