// Create a directory and attach it to the provided inode number uint create_dir(uint inoparent, const char* path) { struct dirent de; int inocurrent; // Create directory entry inocurrent = ialloc(T_DIR); bzero(&de, sizeof(de)); de.d_ino = inocurrent; strcpy(de.d_name, path); iappend(inoparent, &de, sizeof(de)); // Create parent directory link bzero(&de, sizeof(de)); de.d_ino = inocurrent; strcpy(de.d_name, "."); iappend(inocurrent, &de, sizeof(de)); // Create current directory link bzero(&de, sizeof(de)); de.d_ino = inoparent; strcpy(de.d_name, ".."); iappend(inocurrent, &de, sizeof(de)); return inocurrent; }
integer CreateIntegerFromFile(char *fname) { FILE *fp; char c; integer r; iinit(&r); fp = fopen(fname , "r"); printf("1"); if(fp == NULL) { perror("open failed"); return ; } fread(&c, sizeof(char), 1, fp); if( c == '+') r.sign = '+'; else if( c == '-') r.sign = '-'; else { r.sign = '+'; iappend(&r, (c - '0')); } fread(&c, sizeof(char), 1, fp); while(isalnum(c)) { iappend(&r, (c - '0')); fread(&c, sizeof(char), 1, fp); } return r; }
integer AddIntegers(integer a, integer b) { integer c; iinit(&c); int sign; if((a.sign == '+') && (b.sign == '+') ) c.sign = '+'; else if((a.sign == '-') && (b.sign == '-')) c.sign = '-'; else { integer result ; iinit(&result); if (a.sign == '-') { a.sign = '+'; sign = '-'; } else { b.sign = '+'; sign = '+'; } result = SubstractIntegers(a, b); result.sign = sign; return result; } node *p , *q; p = a.tail; q = b.tail; int carry = 0, result ; while((p != a.head) && (q != b.head)) { result = p->digit + q->digit + carry ; carry = (result / 10000); iappend(&c, (result % 10000)); p = p->prev; q = q->prev; } iappend(&c, (p->digit + q->digit + carry )); while (p != a.head) { p = p->prev; iappend(&c, p->digit + carry); carry = 0; } while (q != b.head) { q = q->prev; iappend(&c, q->digit + carry); carry = 0; } ireverse(&c); return c; }
// Copy file from local filesystem to distribution image. Basename is used. void copy_file(uint parentino, const char* file) { char buf[512]; int cc; struct dirent de; uint ino = ialloc(T_FILE); bzero(&de, sizeof(de)); de.d_ino = ino; strcpy(de.d_name, strrchr(file, '/')+1); iappend(parentino, &de, sizeof(de)); int fd = open(file, 0); while ((cc = read(fd, buf, sizeof(buf))) > 0) iappend(ino, buf, cc); close(fd); }
/* Create and return an Integer from a string. The string can be assumed to be a string of digits */ integer CreateIntegerFromString(char *string) { integer i; char *temp; int j, len; iinit(&i); if(*string == '+') { i.sign = '+'; string++; } else if (*string == '-') { i.sign = '-'; string++; } else { i.sign = '+'; } temp = string; while(*temp != '\0') { iappend(&i, (*temp - '0')); temp++; } return i; }
int main(int argc, char *argv[]) { int i, cc, fd; uint rootino, inum, off; struct dirent de; char buf[512]; struct dinode din; if(argc < 2){ fprintf(stderr, "Usage: mkfs fs.img files...\n"); exit(1); } assert((512 % sizeof(struct dinode)) == 0); assert((512 % sizeof(struct dirent)) == 0); fsfd = open(argv[1], O_RDWR|O_CREAT|O_TRUNC, 0666); if(fsfd < 0){ perror(argv[1]); exit(1); } sb.size = xint(size); sb.nblocks = xint(nblocks); // so whole disk is size sectors sb.ninodes = xint(ninodes); bitblocks = size/(512*8) + 1; usedblocks = ninodes / IPB + 3 + bitblocks; freeblock = usedblocks; printf("used %d (bit %d ninode %zu) free %u total %d\n", usedblocks, bitblocks, ninodes/IPB + 1, freeblock, nblocks+usedblocks); assert(nblocks + usedblocks == size); for(i = 0; i < nblocks + usedblocks; i++) wsect(i, zeroes); wsect(1, &sb); rootino = ialloc(T_DIR); assert(rootino == ROOTINO); bzero(&de, sizeof(de)); de.inum = xshort(rootino); strcpy(de.name, "."); iappend(rootino, &de, sizeof(de)); bzero(&de, sizeof(de)); de.inum = xshort(rootino); strcpy(de.name, ".."); iappend(rootino, &de, sizeof(de)); for(i = 2; i < argc; i++){ assert(index(argv[i], '/') == 0); if((fd = open(argv[i], 0)) < 0){ perror(argv[i]); exit(1); } // Skip leading _ in name when writing to file system. // The binaries are named _rm, _cat, etc. to keep the // build operating system from trying to execute them // in place of system binaries like rm and cat. if(argv[i][0] == '_') ++argv[i]; inum = ialloc(T_FILE); bzero(&de, sizeof(de)); de.inum = xshort(inum); strncpy(de.name, argv[i], DIRSIZ); iappend(rootino, &de, sizeof(de)); while((cc = read(fd, buf, sizeof(buf))) > 0) iappend(inum, buf, cc); close(fd); } // fix size of root inode dir rinode(rootino, &din); off = xint(din.size); off = ((off/BSIZE) + 1) * BSIZE; din.size = xint(off); winode(rootino, &din); balloc(usedblocks); exit(0); }
int add_dir(DIR *cur_dir, int cur_inode, int parent_inode) { int r; int child_inode; int cur_fd, child_fd; struct xv6_dirent de; struct dinode din; struct dirent dir_buf; struct dirent *entry; struct stat st; int bytes_read; char buf[BLOCK_SIZE]; int off; bzero(&de, sizeof(de)); de.inum = xshort(cur_inode); strcpy(de.name, "."); iappend(cur_inode, &de, sizeof(de)); bzero(&de, sizeof(de)); de.inum = xshort(parent_inode); strcpy(de.name, ".."); iappend(cur_inode, &de, sizeof(de)); if (cur_dir == NULL) { return 0; } cur_fd = dirfd(cur_dir); if (cur_fd == -1){ perror("add_dir"); exit(EXIT_FAILURE); } if (fchdir(cur_fd) != 0){ perror("add_dir"); return -1; } while (true) { r = readdir_r(cur_dir, &dir_buf, &entry); if (r != 0) { perror("add_dir"); return -1; } if (entry == NULL) break; if (strcmp(entry->d_name, ".") == 0 || strcmp(entry->d_name, "..") == 0) continue; printf("%s\n", entry->d_name); child_fd = open(entry->d_name, O_RDONLY); if (child_fd == -1) { perror("open"); return -1; } r = fstat(child_fd, &st); if (r != 0) { perror("stat"); return -1; } if (S_ISDIR(st.st_mode)) { child_inode = ialloc(T_DIR); r = add_dir(fdopendir(child_fd), child_inode, cur_inode); if (r != 0) return r; if (fchdir(cur_fd) != 0) { perror("chdir"); return -1; } } else { bytes_read = 0; child_inode = ialloc(T_FILE); bzero(&de, sizeof(de)); while((bytes_read = read(child_fd, buf, sizeof(buf))) > 0) { iappend(child_inode, buf, bytes_read); } } close(child_fd); de.inum = xshort(child_inode); strncpy(de.name, entry->d_name, DIRSIZ); iappend(cur_inode, &de, sizeof(de)); } // fix size of inode cur_dir rinode(cur_inode, &din); off = xint(din.logical_size); off = ((off/BSIZE) + 1) * BSIZE; din.logical_size = xint(off); winode(cur_inode, &din); return 0; }
int main(int argc, char *argv[]) { int i, cc, fd; uint rootino, inum, off; struct dirent de; char buf[512]; struct dinode din; static_assert(sizeof(int) == 4, "Integers must be 4 bytes!"); if(argc < 2){ fprintf(stderr, "Usage: mkfs fs.img files...\n"); exit(1); } assert((512 % sizeof(struct dinode)) == 0); assert((512 % sizeof(struct dirent)) == 0); fsfd = open(argv[1], O_RDWR|O_CREAT|O_TRUNC, 0666); if(fsfd < 0){ perror(argv[1]); exit(1); } sb.size = xint(size); sb.nblocks = xint(nblocks); // so whole disk is size sectors sb.ninodes = xint(ninodes); sb.nlog = xint(nlog); bitblocks = size/(512*8) + 1; usedblocks = ninodes / IPB + 3 + bitblocks; freeblock = usedblocks; printf("used %d (bit %d ninode %zu) free %u log %u total %d\n", usedblocks, bitblocks, ninodes/IPB + 1, freeblock, nlog, nblocks+usedblocks+nlog); assert(nblocks + usedblocks + nlog == size); for(i = 0; i < nblocks + usedblocks + nlog; i++) wsect(i, zeroes); memset(buf, 0, sizeof(buf)); memmove(buf, &sb, sizeof(sb)); wsect(1, buf); rootino = ialloc(T_DIR); assert(rootino == ROOTINO); bzero(&de, sizeof(de)); de.inum = xshort(rootino); strcpy(de.name, "."); iappend(rootino, &de, sizeof(de)); bzero(&de, sizeof(de)); de.inum = xshort(rootino); strcpy(de.name, ".."); iappend(rootino, &de, sizeof(de)); for(i = 2; i < argc; i++){ char *name = argv[i]; if (!strncmp(name, "fs/", 3)) name += 3; assert(index(name, '/') == 0); if((fd = open(argv[i], 0)) < 0){ perror(argv[i]); exit(1); } inum = ialloc(T_FILE); bzero(&de, sizeof(de)); de.inum = xshort(inum); strncpy(de.name, name, DIRSIZ); iappend(rootino, &de, sizeof(de)); while((cc = read(fd, buf, sizeof(buf))) > 0) iappend(inum, buf, cc); close(fd); } // fix size of root inode dir rinode(rootino, &din); off = xint(din.size); off = ((off/BSIZE) + 1) * BSIZE; din.size = xint(off); winode(rootino, &din); balloc(usedblocks); exit(0); }
int main(int argc, char *argv[]) { int i, cc, fd; uint rootino, inum, off; struct dirent de; char buf[BSIZE]; struct dinode din; static_assert(sizeof(int) == 4, "Integers must be 4 bytes!"); if(argc < 2){ fprintf(stderr, "Usage: mkfs fs.img files...\n"); exit(1); } assert((BSIZE % sizeof(struct dinode)) == 0); assert((BSIZE % sizeof(struct dirent)) == 0); fsfd = open(argv[1], O_RDWR|O_CREAT|O_TRUNC, 0666); if(fsfd < 0){ perror(argv[1]); exit(1); } // 1 fs block = 1 disk sector nmeta = 2 + nlog + ninodeblocks + nbitmap; nblocks = FSSIZE - nmeta; sb.size = xint(FSSIZE); sb.nblocks = xint(nblocks); sb.ninodes = xint(NINODES); sb.nlog = xint(nlog); sb.logstart = xint(2); sb.inodestart = xint(2+nlog); sb.bmapstart = xint(2+nlog+ninodeblocks); printf("nmeta %d (boot, super, log blocks %u inode blocks %u, bitmap blocks %u) blocks %d total %d\n", nmeta, nlog, ninodeblocks, nbitmap, nblocks, FSSIZE); freeblock = nmeta; // the first free block that we can allocate for(i = 0; i < FSSIZE; i++) wsect(i, zeroes); memset(buf, 0, sizeof(buf)); memmove(buf, &sb, sizeof(sb)); wsect(1, buf); rootino = ialloc(T_DIR); assert(rootino == ROOTINO); bzero(&de, sizeof(de)); de.inum = xshort(rootino); strcpy(de.name, "."); iappend(rootino, &de, sizeof(de)); bzero(&de, sizeof(de)); de.inum = xshort(rootino); strcpy(de.name, ".."); iappend(rootino, &de, sizeof(de)); for(i = 2; i < argc; i++){ assert(index(argv[i], '/') == 0); if((fd = open(argv[i], 0)) < 0){ perror(argv[i]); exit(1); } // Skip leading _ in name when writing to file system. // The binaries are named _rm, _cat, etc. to keep the // build operating system from trying to execute them // in place of system binaries like rm and cat. if(argv[i][0] == '_') ++argv[i]; inum = ialloc(T_FILE); bzero(&de, sizeof(de)); de.inum = xshort(inum); strncpy(de.name, argv[i], DIRSIZ); iappend(rootino, &de, sizeof(de)); while((cc = read(fd, buf, sizeof(buf))) > 0) iappend(inum, buf, cc); close(fd); } // fix size of root inode dir rinode(rootino, &din); off = xint(din.size); off = ((off/BSIZE) + 1) * BSIZE; din.size = xint(off); winode(rootino, &din); balloc(freeblock); exit(0); }
/* When called with c==-1, it just creates the prompt */ static int itype(W *w, int c, void *obj, int *notify) { IREC *i; int omid; BW *bw; struct isrch *isrch = (struct isrch *)obj; WIND_BW(bw,w); if (isrch->quote) { goto in; } if (c == 8 || c == 127) { /* Backup */ if ((i = isrch->irecs.link.prev) != &isrch->irecs) { pgoto(bw->cursor, i->disp); if (globalsrch) globalsrch->wrap_flag = i->wrap_flag; omid = opt_mid; opt_mid = 1; dofollows(); opt_mid = omid; isrch->pattern = vstrunc(isrch->pattern, sLEN(isrch->pattern) - i->what); frirec(deque_f(IREC, link, i)); } else { if(joe_beep) ttputc(7); } } else if (c == 'Q' - '@' /* || c == '`' */) { isrch->quote = 1; } else if (c == 'S' - '@' || c == '\\' - '@' || c == 'L' - '@' || c == 'R' - '@') { /* Repeat */ if (c == 'R' - '@') { isrch->dir = 1; } else { isrch->dir = 0; } if (qempty(IREC, link, &isrch->irecs)) { if (lastpat && lastpat[0]) { iappend(bw, isrch, sv(lastpat)); } } else { SRCH *srch; i = alirec(); i->disp = i->start = bw->cursor->byte; i->what = 0; if (!globalsrch) srch = mksrch(NULL,NULL,opt_icase,isrch->dir,-1,0,0,0,0); else { srch = globalsrch; globalsrch = 0; } srch->addr = bw->cursor->byte; if (!srch->wrap_p || srch->wrap_p->b!=bw->b) { prm(srch->wrap_p); srch->wrap_p = pdup(bw->cursor, "itype"); srch->wrap_p->owner = &srch->wrap_p; srch->wrap_flag = 0; } i->wrap_flag = srch->wrap_flag; setpat(srch, vsncpy(NULL, 0, isrch->pattern, sLen(isrch->pattern))); srch->backwards = isrch->dir; if (dopfnext(bw, srch, NULL)) { if(joe_beep) ttputc(7); frirec(i); } else { enqueb(IREC, link, &isrch->irecs, i); } } } else if (c >= 0 && c < 32) { /* Done when a control character is received */ nungetc(c); if (notify) { *notify = 1; } smode = 2; if (lastisrch) { lastpat = vstrunc(lastpat, 0); lastpat = vsncpy(lastpat, 0, lastisrch->pattern, sLen(lastisrch->pattern)); rmisrch(lastisrch); } lastisrch = isrch; return 0; } else if (c != -1) { char buf[16]; ptrdiff_t buf_len; /* Search */ in: if (bw->b->o.charmap->type) { buf_len = utf8_encode(buf, c); } else { buf[0] = TO_CHAR_OK(from_uni(bw->b->o.charmap, c)); buf_len = 1; } isrch->quote = 0; iappend(bw, isrch, buf, buf_len); } omid = opt_mid; opt_mid = 1; bw->cursor->xcol = piscol(bw->cursor); dofollows(); opt_mid = omid; isrch->prompt = vstrunc(isrch->prompt, isrch->ofst); if (locale_map->type && !bw->b->o.charmap->type) { /* Translate bytes to utf-8 */ char buf[16]; int x; for (x=0; x!=sLEN(isrch->pattern); ++x) { int tc = to_uni(bw->b->o.charmap, isrch->pattern[x]); utf8_encode(buf, tc); isrch->prompt = vsncpy(sv(isrch->prompt),sz(buf)); } } else if (!locale_map->type && bw->b->o.charmap->type) { /* Translate utf-8 to bytes */ const char *p = isrch->pattern; ptrdiff_t len = sLEN(isrch->pattern); while (len) { int tc = utf8_decode_fwrd(&p, &len); if (tc >= 0) { tc = from_uni(locale_map, tc); isrch->prompt = vsadd(isrch->prompt, TO_CHAR_OK(tc)); } } } else { /* FIXME: translate when charmaps do not match */ isrch->prompt = vsncpy(sv(isrch->prompt),sv(isrch->pattern)); } if (mkqwnsr(bw->parent, sv(isrch->prompt), itype, iabrt, isrch, notify)) { return 0; } else { rmisrch(isrch); return -1; } }
int main(int argc, char *argv[]) { int i, cc, fd; uint rootino, inum, off, bin_inum, tests_inum, sbin_inum; //dir_inum struct dirent de; char buf[BSIZE]; struct dinode din; static_assert(sizeof(int) == 4, "Integers must be 4 bytes!"); if(argc < 2) { fprintf(stderr, "Usage: mkfs fs.img files...\n"); exit(1); } assert((BSIZE % sizeof(struct dinode)) == 0); assert((BSIZE % sizeof(struct dirent)) == 0); fsfd = open(argv[1], O_RDWR | O_CREAT | O_TRUNC, 0666); if(fsfd < 0) { perror(argv[1]); exit(1); } // 1 fs block = 1 disk sector nmeta = 2 + nlog + ninodeblocks + nbitmap; nblocks = FSSIZE - nmeta; sb.size = xint(FSSIZE); sb.nblocks = xint(nblocks); sb.ninodes = xint(NINODES); sb.nlog = xint(nlog); sb.logstart = xint(2); sb.inodestart = xint(2 + nlog); sb.bmapstart = xint(2 + nlog + ninodeblocks); printf("nmeta %d (boot, super, log blocks %u inode blocks %u, bitmap blocks %u) blocks %d total %d\n", nmeta, nlog, ninodeblocks, nbitmap, nblocks, FSSIZE); freeblock = nmeta; // the first free block that we can allocate for(i = 0; i < FSSIZE; i++) { wsect(i, zeroes); } memset(buf, 0, sizeof(buf)); memmove(buf, &sb, sizeof(sb)); wsect(1, buf); rootino = ialloc(T_DIR); assert(rootino == ROOTINO); bzero(&de, sizeof(de)); de.inum = xshort(rootino); strcpy(de.name, "."); iappend(rootino, &de, sizeof(de)); bzero(&de, sizeof(de)); de.inum = xshort(rootino); strcpy(de.name, ".."); iappend(rootino, &de, sizeof(de)); // create /bin folder bin_inum = ialloc(T_DIR); bzero(&de, sizeof(de)); de.inum = xshort(bin_inum); strcpy(de.name, "bin"); iappend(rootino, &de, sizeof(de)); bzero(&de, sizeof(de)); de.inum = xshort(bin_inum); strcpy(de.name, "."); iappend(bin_inum, &de, sizeof(de)); bzero(&de, sizeof(de)); de.inum = xshort(rootino); strcpy(de.name, ".."); iappend(bin_inum, &de, sizeof(de)); // create /dev folder inum = ialloc(T_DIR); bzero(&de, sizeof(de)); de.inum = xshort(inum); strcpy(de.name, "dev"); iappend(rootino, &de, sizeof(de)); bzero(&de, sizeof(de)); de.inum = xshort(inum); strcpy(de.name, "."); iappend(inum, &de, sizeof(de)); bzero(&de, sizeof(de)); de.inum = xshort(rootino); strcpy(de.name, ".."); iappend(inum, &de, sizeof(de)); // create /sbin folder sbin_inum = ialloc(T_DIR); bzero(&de, sizeof(de)); de.inum = xshort(sbin_inum); strcpy(de.name, "sbin"); iappend(rootino, &de, sizeof(de)); bzero(&de, sizeof(de)); de.inum = xshort(sbin_inum); strcpy(de.name, "."); iappend(sbin_inum, &de, sizeof(de)); bzero(&de, sizeof(de)); de.inum = xshort(rootino); strcpy(de.name, ".."); iappend(sbin_inum, &de, sizeof(de)); // create /tests folder tests_inum = ialloc(T_DIR); bzero(&de, sizeof(de)); de.inum = xshort(tests_inum); strcpy(de.name, "tests"); iappend(rootino, &de, sizeof(de)); bzero(&de, sizeof(de)); de.inum = xshort(tests_inum); strcpy(de.name, "."); iappend(tests_inum, &de, sizeof(de)); bzero(&de, sizeof(de)); de.inum = xshort(rootino); strcpy(de.name, ".."); iappend(tests_inum, &de, sizeof(de)); for(i = 2; i < argc; i++) { printf("argv[i]: %s and index: %s\n", argv[i], index(argv[i], '/')); //assert(index(argv[i], '/') == 0); if((fd = open(argv[i], 0)) < 0) { perror(argv[i]); exit(1); } if(index(argv[i], '/') != 0) { char foldername[256]; char *progname = malloc(sizeof(char) * 256); strcpy(progname, skipelem(argv[i], foldername)); if(progname[0] == '_') { progname++; } if(strcmp(foldername, "bin") == 0) { inum = ialloc(T_FILE); bzero(&de, sizeof(de)); de.inum = xshort(inum); strncpy(de.name, progname, DIRSIZ); iappend(bin_inum, &de, sizeof(de)); while((cc = read(fd, buf, sizeof(buf))) > 0) { iappend(inum, buf, cc); } close(fd); } else if(strcmp(foldername, "tests") == 0) { inum = ialloc(T_FILE); bzero(&de, sizeof(de)); de.inum = xshort(inum); strncpy(de.name, progname, DIRSIZ); iappend(tests_inum, &de, sizeof(de)); while((cc = read(fd, buf, sizeof(buf))) > 0) { iappend(inum, buf, cc); } close(fd); } else if(strcmp(foldername, "sbin") == 0) { inum = ialloc(T_FILE); bzero(&de, sizeof(de)); de.inum = xshort(inum); strncpy(de.name, progname, DIRSIZ); iappend(sbin_inum, &de, sizeof(de)); while((cc = read(fd, buf, sizeof(buf))) > 0) { iappend(inum, buf, cc); } close(fd); } else if(strcmp(foldername, "docs") == 0) { inum = ialloc(T_FILE); bzero(&de, sizeof(de)); de.inum = xshort(inum); strncpy(de.name, progname, DIRSIZ); iappend(rootino, &de, sizeof(de)); while((cc = read(fd, buf, sizeof(buf))) > 0) { iappend(inum, buf, cc); } close(fd); } else if(strcmp(foldername, "shell") == 0) { inum = ialloc(T_FILE); bzero(&de, sizeof(de)); de.inum = xshort(inum); strncpy(de.name, progname, DIRSIZ); iappend(bin_inum, &de, sizeof(de)); while((cc = read(fd, buf, sizeof(buf))) > 0) { iappend(inum, buf, cc); } close(fd); } else { printf("\nERROR: Found folder other than bin and tests: %s\n", foldername); exit(1); } } else { // Skip leading _ in name when writing to file system. // The binaries are named _rm, _cat, etc. to keep the // build operating system from trying to execute them // in place of system binaries like rm and cat. if(argv[i][0] == '_') { ++argv[i]; } inum = ialloc(T_FILE); bzero(&de, sizeof(de)); de.inum = xshort(inum); strncpy(de.name, argv[i], DIRSIZ); iappend(rootino, &de, sizeof(de)); while((cc = read(fd, buf, sizeof(buf))) > 0) { iappend(inum, buf, cc); } close(fd); } } // fix size of root inode dir rinode(rootino, &din); off = xint(din.size); off = ((off / BSIZE) + 1) * BSIZE; din.size = xint(off); winode(rootino, &din); balloc(freeblock); exit(0); }
integer AddDigitToInteger(integer a, char digit) { iappend(&a, (digit - '0')); return a ; }
integer MultiplyIntegers(integer a, integer b) { node *p, *q; p = a.tail; q = b.tail; integer no1, no2, r; iinit(&no1); iinit(&no2); iinit(&r); if (a.length > b.length) { no1 = a; no2 = b; } else if (a.length < b.length) { no1 = b; no2 = a; } else { while (p != a.tail) { if (p->digit > q->digit ) { no1 = a; no2 = b; } else if (p->digit < q->digit) { no1 = b; no2 = a; } else { p = p->next; q = q->next; } } } if (no1.head == NULL && no2.head == NULL) { if (p->digit > q->digit ) { no1 = a; no2 = b; } else if (p->digit < q->digit) { no1 = b; no2 = a; } else { no1 = a; no2 = b; } } node *m, *n; n = no2.tail; int size = no2.length, carry = 0, i, j; integer temp[size]; for(i = 0; i < size ; i++) { m = no1.tail; iinit(&temp[i]); temp[i].sign = '+'; printf("n->digit is %d\n", n->digit); for(j = 0; j < i; j++) { iappend(&temp[i], 0); } while (m != no1.head) { int r; r = m->digit * n->digit + carry; carry = (r/10); iappend(&temp[i], r%10); m = m->prev; } iappend( &temp[i], m->digit * n->digit +carry); iappend(&temp[i] , carry); ireverse(&temp[i]); n = n->prev; } for(i = 0; i < (size - 1) ; i++) { temp[i + 1] = AddIntegers(temp[i], temp[i + 1]); } if(no1.sign == '-' && no2.sign == '-' ) temp[(size - 1)].sign = '+'; else if (no1.sign == '+' && no2.sign == '+' ) temp[(size - 1)].sign = '+'; else temp[(size - 1)].sign = '-'; return temp[(size - 1)]; }
integer SubstractIntegers(integer a, integer b) { printf("a.sign = %c , b.sign = %c", a.sign , b.sign); if(a.sign != b.sign ) { integer result ; char sign; if (a.sign == '-') { a.sign = '+'; sign = '-'; } else { b.sign = '+'; sign = '+'; } result = AddIntegers(a, b); result.sign = sign; return result ; } node *p, *q; p = a.tail; q = b.tail; integer no1, no2, r; iinit(&no1); iinit(&no2); iinit(&r); if (a.length > b.length) { no1 = a; no2 = b; } else if (a.length < b.length) { no1 = b; no2 = a; } else { while (p != a.tail) { if (p->digit > q->digit ) { no1 = a; no2 = b; } else if (p->digit < q->digit) { no1 = b; no2 = a; } else { p = p->next; q = q->next; } } } if (no1.head == NULL && no2.head == NULL) { if (p->digit > q->digit ) { no1 = a; no2 = b; } else if (p->digit < q->digit) { no1 = b; no2 = a; } else return no1; } int borrow = 0; node *m, *n; m = no1.tail; n = no2.tail; while(n != no2.head) { if(m->digit < n->digit) { iappend(&r, (10 + m->digit - n->digit - borrow)); borrow = 1; } else if (m->digit > n->digit) { iappend(&r, (m->digit - n->digit - borrow)); borrow = 0; } else { if(borrow == 1) { iappend(&r, (10 + m->digit - n->digit - borrow)); borrow = 1; } else { iappend(&r, 0); borrow = 0; } } m = m->prev; n = n->prev; } if(m->digit < n->digit) { iappend(&r, (10 + m->digit - n->digit - borrow)); borrow = 1; } else if (m->digit > n->digit) { iappend(&r, (m->digit - n->digit - borrow)); borrow = 0; } else { if(borrow == 1) { iappend(&r, (10 + m->digit - n->digit - borrow)); borrow = 1; } else { iappend(&r, 0); borrow = 0; } } m = m->prev; while(m != no1.tail) { iappend(&r, (m->digit - borrow)); borrow = 0; m = m->prev; } ireverse(&r); r.sign = no1.sign; return r; }
int main(int argc, char *argv[]) { int i; uint rootino, off; struct dirent de; char buf[512]; struct dinode din; static_assert(sizeof(int) == 4, "Integers must be 4 bytes!"); if(argc != 3) { fprintf(stderr, "Usage: mkfs.xv6 <image name> <test|release>\n"); exit(1); } assert((512 % sizeof(struct dinode)) == 0); assert((512 % sizeof(struct dirent)) == 0); fsfd = open(argv[1], O_RDWR|O_CREAT|O_TRUNC, 0666); if(fsfd < 0){ perror(argv[1]); exit(1); } sb.size = xint(size); sb.nblocks = xint(nblocks); // so whole disk is size sectors sb.ninodes = xint(ninodes); sb.nlog = xint(nlog); bitblocks = size/(512*8) + 1; usedblocks = ninodes / IPB + 3 + bitblocks; freeblock = usedblocks; if (0) { printf("used %d (bit %d ninode %zu) free %u log %u total %d\n", usedblocks, bitblocks, ninodes/IPB + 1, freeblock, nlog, nblocks+usedblocks+nlog); } assert(nblocks + usedblocks + nlog == size); for(i = 0; i < nblocks + usedblocks + nlog; i++) wsect(i, zeroes); memset(buf, 0, sizeof(buf)); memmove(buf, &sb, sizeof(sb)); wsect(1, buf); rootino = ialloc(T_DIR); assert(rootino == ROOTINO); bzero(&de, sizeof(de)); de.d_ino = xshort(rootino); strcpy(de.d_name, "."); iappend(rootino, &de, sizeof(de)); bzero(&de, sizeof(de)); de.d_ino = xshort(rootino); strcpy(de.d_name, ".."); iappend(rootino, &de, sizeof(de)); if (!strcmp(argv[2], "release")) { create_fhs(rootino, "coreutils/init"); } else if (!strcmp(argv[2], "test")) { create_fhs(rootino, "tests/init"); create_tests(rootino); } // fix size of root inode dir rinode(rootino, &din); off = xint(din.size); off = ((off/BSIZE) + 1) * BSIZE; din.size = xint(off); winode(rootino, &din); balloc(usedblocks); exit(0); }