Skip to content
This repository
Browse code

make new code like old code

Variable declarations at top of function,
separate from initialization.

Use == 0 instead of ! for checking pointers.

Consistent spacing around {, *, casts.

Declare 0-parameter functions as (void) not ().

Integer valued functions return -1 on failure, 0 on success.
  • Loading branch information...
commit 1a81e38b17144624415d252a521fd5a06079d681 1 parent 2406796
Russ Cox authored
2  bootmain.c
@@ -33,7 +33,7 @@ bootmain(void)
33 33
   // Load each program segment (ignores ph flags).
34 34
   ph = (struct proghdr*)((uchar*)elf + elf->phoff);
35 35
   eph = ph + elf->phnum;
36  
-  for(; ph < eph; ph++) {
  36
+  for(; ph < eph; ph++){
37 37
     va = (uchar*)ph->va;
38 38
     readseg(va, ph->filesz, ph->offset);
39 39
     if(ph->memsz > ph->filesz)
5  console.c
@@ -27,15 +27,16 @@ printint(int xx, int base, int sgn)
27 27
 {
28 28
   static char digits[] = "0123456789abcdef";
29 29
   char buf[16];
30  
-  int i = 0, neg = 0;
  30
+  int i, neg;
31 31
   uint x;
32 32
 
33  
-  if(sgn && xx < 0){
  33
+  if(sgn && (neg = xx < 0)){
34 34
     neg = 1;
35 35
     x = -xx;
36 36
   } else
37 37
     x = xx;
38 38
 
  39
+  i = 0;
39 40
   do{
40 41
     buf[i++] = digits[x % base];
41 42
   }while((x /= base) != 0);
8  defs.h
@@ -62,7 +62,7 @@ void            ioapicinit(void);
62 62
 // kalloc.c
63 63
 char*           kalloc(void);
64 64
 void            kfree(char*);
65  
-void            kinit();
  65
+void            kinit(void);
66 66
 
67 67
 // kbd.c
68 68
 void            kbdintr(void);
@@ -117,8 +117,8 @@ void            getcallerpcs(void*, uint*);
117 117
 int             holding(struct spinlock*);
118 118
 void            initlock(struct spinlock*, char*);
119 119
 void            release(struct spinlock*);
120  
-void            pushcli();
121  
-void            popcli();
  120
+void            pushcli(void);
  121
+void            popcli(void);
122 122
 
123 123
 // string.c
124 124
 int             memcmp(const void*, const void*, uint);
@@ -164,7 +164,7 @@ void            inituvm(pde_t*, char*, uint);
164 164
 int             loaduvm(pde_t*, char*, struct inode *, uint, uint);
165 165
 pde_t*          copyuvm(pde_t*,uint);
166 166
 void            switchuvm(struct proc*);
167  
-void            switchkvm();
  167
+void            switchkvm(void);
168 168
 int             copyout(pde_t *pgdir, uint va, void *buf, uint len);
169 169
 
170 170
 // number of elements in fixed-size array
24  exec.c
@@ -10,16 +10,17 @@ int
10 10
 exec(char *path, char **argv)
11 11
 {
12 12
   char *s, *last;
13  
-  int i, off;
14  
-  uint sz = 0;
  13
+  int i, off, argc;
  14
+  uint sz, sp, strings[MAXARG];
15 15
   struct elfhdr elf;
16  
-  struct inode *ip = 0;
  16
+  struct inode *ip;
17 17
   struct proghdr ph;
18  
-  pde_t *pgdir = 0, *oldpgdir;
  18
+  pde_t *pgdir, *oldpgdir;
19 19
 
20 20
   if((ip = namei(path)) == 0)
21 21
     return -1;
22 22
   ilock(ip);
  23
+  pgdir = 0;
23 24
 
24 25
   // Check ELF header
25 26
   if(readi(ip, (char*)&elf, 0, sizeof(elf)) < sizeof(elf))
@@ -27,10 +28,11 @@ exec(char *path, char **argv)
27 28
   if(elf.magic != ELF_MAGIC)
28 29
     goto bad;
29 30
 
30  
-  if(!(pgdir = setupkvm()))
  31
+  if((pgdir = setupkvm()) == 0)
31 32
     goto bad;
32 33
 
33 34
   // Load program into memory.
  35
+  sz = 0;
34 36
   for(i=0, off=elf.phoff; i<elf.phnum; i++, off+=sizeof(ph)){
35 37
     if(readi(ip, (char*)&ph, off, sizeof(ph)) != sizeof(ph))
36 38
       goto bad;
@@ -38,9 +40,9 @@ exec(char *path, char **argv)
38 40
       continue;
39 41
     if(ph.memsz < ph.filesz)
40 42
       goto bad;
41  
-    if(!(sz = allocuvm(pgdir, sz, ph.va + ph.memsz)))
  43
+    if((sz = allocuvm(pgdir, sz, ph.va + ph.memsz)) == 0)
42 44
       goto bad;
43  
-    if(!loaduvm(pgdir, (char *)ph.va, ip, ph.offset, ph.filesz))
  45
+    if(loaduvm(pgdir, (char*)ph.va, ip, ph.offset, ph.filesz) < 0)
44 46
       goto bad;
45 47
   }
46 48
   iunlockput(ip);
@@ -48,7 +50,7 @@ exec(char *path, char **argv)
48 50
 
49 51
   // Allocate a one-page stack at the next page boundary
50 52
   sz = PGROUNDUP(sz);
51  
-  if(!(sz = allocuvm(pgdir, sz, sz + PGSIZE)))
  53
+  if((sz = allocuvm(pgdir, sz, sz + PGSIZE)) == 0)
52 54
     goto bad;
53 55
 
54 56
   // initialize stack content:
@@ -64,24 +66,22 @@ exec(char *path, char **argv)
64 66
   // argc                             -- argc argument to main()
65 67
   // ffffffff                         -- return PC for main() call
66 68
 
67  
-  uint sp = sz;
  69
+  sp = sz;
68 70
 
69 71
   // count arguments
70  
-  int argc;
71 72
   for(argc = 0; argv[argc]; argc++)
72 73
     ;
73 74
   if(argc >= MAXARG)
74 75
     goto bad;
75 76
 
76 77
   // push strings and remember where they are
77  
-  uint strings[MAXARG];
78 78
   for(i = argc - 1; i >= 0; --i){
79 79
     sp -= strlen(argv[i]) + 1;
80 80
     strings[i] = sp;
81 81
     copyout(pgdir, sp, argv[i], strlen(argv[i]) + 1);
82 82
   }
83 83
 
84  
-#define PUSH(x) { int xx = (int)(x); sp -= 4; copyout(pgdir, sp, &xx, 4); }
  84
+#define PUSH(x){ int xx = (int)(x); sp -= 4; copyout(pgdir, sp, &xx, 4); }
85 85
 
86 86
   PUSH(0); // argv[argc] is zero
87 87
 
4  ide.c
@@ -131,7 +131,7 @@ iderw(struct buf *b)
131 131
   if((b->flags & (B_VALID|B_DIRTY)) == B_VALID)
132 132
     panic("iderw: nothing to do");
133 133
   if(b->dev != 0 && !havedisk1)
134  
-    panic("idrw: ide disk 1 not present");
  134
+    panic("iderw: ide disk 1 not present");
135 135
 
136 136
   acquire(&idelock);
137 137
 
@@ -147,7 +147,7 @@ iderw(struct buf *b)
147 147
   
148 148
   // Wait for request to finish.
149 149
   // Assuming will not sleep too long: ignore proc->killed.
150  
-  while((b->flags & (B_VALID|B_DIRTY)) != B_VALID) {
  150
+  while((b->flags & (B_VALID|B_DIRTY)) != B_VALID){
151 151
     sleep(b, &idelock);
152 152
   }
153 153
 
14  kalloc.c
@@ -23,9 +23,11 @@ extern char end[]; // first address after kernel loaded from ELF file
23 23
 void
24 24
 kinit(void)
25 25
 {
  26
+  char *p;
  27
+
26 28
   initlock(&kmem.lock, "kmem");
27  
-  char *p = (char*)PGROUNDUP((uint)end);
28  
-  for( ; p + PGSIZE - 1 < (char*) PHYSTOP; p += PGSIZE)
  29
+  p = (char*)PGROUNDUP((uint)end);
  30
+  for(; p + PGSIZE - 1 < (char*)PHYSTOP; p += PGSIZE)
29 31
     kfree(p);
30 32
 }
31 33
 
@@ -39,14 +41,14 @@ kfree(char *v)
39 41
 {
40 42
   struct run *r;
41 43
 
42  
-  if(((uint) v) % PGSIZE || v < end || (uint)v >= PHYSTOP) 
  44
+  if((uint)v % PGSIZE || v < end || (uint)v >= PHYSTOP) 
43 45
     panic("kfree");
44 46
 
45 47
   // Fill with junk to catch dangling refs.
46 48
   memset(v, 1, PGSIZE);
47 49
 
48 50
   acquire(&kmem.lock);
49  
-  r = (struct run *) v;
  51
+  r = (struct run*)v;
50 52
   r->next = kmem.freelist;
51 53
   kmem.freelist = r;
52 54
   release(&kmem.lock);
@@ -56,7 +58,7 @@ kfree(char *v)
56 58
 // Returns a pointer that the kernel can use.
57 59
 // Returns 0 if the memory cannot be allocated.
58 60
 char*
59  
-kalloc()
  61
+kalloc(void)
60 62
 {
61 63
   struct run *r;
62 64
 
@@ -65,6 +67,6 @@ kalloc()
65 67
   if(r)
66 68
     kmem.freelist = r->next;
67 69
   release(&kmem.lock);
68  
-  return (char*) r;
  70
+  return (char*)r;
69 71
 }
70 72
 
30  main.c
@@ -7,7 +7,7 @@
7 7
 
8 8
 static void bootothers(void);
9 9
 static void mpmain(void);
10  
-void jkstack(void)  __attribute__((noreturn));
  10
+void jmpkstack(void)  __attribute__((noreturn));
11 11
 void mainc(void);
12 12
 
13 13
 // Bootstrap processor starts running C code here.
@@ -20,19 +20,20 @@ main(void)
20 20
   lapicinit(mpbcpu());
21 21
   seginit();       // set up segments
22 22
   kinit();         // initialize memory allocator
23  
-  jkstack();       // call mainc() on a properly-allocated stack 
  23
+  jmpkstack();       // call mainc() on a properly-allocated stack 
24 24
 }
25 25
 
26 26
 void
27  
-jkstack(void)
  27
+jmpkstack(void)
28 28
 {
29  
-  char *kstack = kalloc();
30  
-  if(!kstack)
31  
-    panic("jkstack\n");
32  
-  char *top = kstack + PGSIZE;
33  
-  asm volatile("movl %0,%%esp" : : "r" (top));
34  
-  asm volatile("call mainc");
35  
-  panic("jkstack");
  29
+  char *kstack, *top;
  30
+  
  31
+  kstack = kalloc();
  32
+  if(kstack == 0)
  33
+    panic("jmpkstack kalloc");
  34
+  top = kstack + PGSIZE;
  35
+  asm volatile("movl %0,%%esp; call mainc" : : "r" (top));
  36
+  panic("jmpkstack");
36 37
 }
37 38
 
38 39
 // Set up hardware and software.
@@ -67,7 +68,7 @@ mainc(void)
67 68
 static void
68 69
 mpmain(void)
69 70
 {
70  
-  if(cpunum() != mpbcpu()) {
  71
+  if(cpunum() != mpbcpu()){
71 72
     seginit();
72 73
     lapicinit(cpunum());
73 74
   }
@@ -87,9 +88,9 @@ bootothers(void)
87 88
   struct cpu *c;
88 89
   char *stack;
89 90
 
90  
-  // Write bootstrap code to unused memory at 0x7000.  The linker has
91  
-  // placed the start of bootother.S there.
92  
-  code = (uchar *) 0x7000;
  91
+  // Write bootstrap code to unused memory at 0x7000.
  92
+  // The linker has placed the image of bootother.S in _binary_bootother_start.
  93
+  code = (uchar*)0x7000;
93 94
   memmove(code, _binary_bootother_start, (uint)_binary_bootother_size);
94 95
 
95 96
   for(c = cpus; c < cpus+ncpu; c++){
@@ -110,4 +111,3 @@ bootothers(void)
110 111
       ;
111 112
   }
112 113
 }
113  
-
24  mkfs.c
@@ -35,7 +35,7 @@ ushort
35 35
 xshort(ushort x)
36 36
 {
37 37
   ushort y;
38  
-  uchar *a = (uchar*) &y;
  38
+  uchar *a = (uchar*)&y;
39 39
   a[0] = x;
40 40
   a[1] = x >> 8;
41 41
   return y;
@@ -45,7 +45,7 @@ uint
45 45
 xint(uint x)
46 46
 {
47 47
   uint y;
48  
-  uchar *a = (uchar*) &y;
  48
+  uchar *a = (uchar*)&y;
49 49
   a[0] = x;
50 50
   a[1] = x >> 8;
51 51
   a[2] = x >> 16;
@@ -177,7 +177,7 @@ winode(uint inum, struct dinode *ip)
177 177
 
178 178
   bn = i2b(inum);
179 179
   rsect(bn, buf);
180  
-  dip = ((struct dinode*) buf) + (inum % IPB);
  180
+  dip = ((struct dinode*)buf) + (inum % IPB);
181 181
   *dip = *ip;
182 182
   wsect(bn, buf);
183 183
 }
@@ -191,7 +191,7 @@ rinode(uint inum, struct dinode *ip)
191 191
 
192 192
   bn = i2b(inum);
193 193
   rsect(bn, buf);
194  
-  dip = ((struct dinode*) buf) + (inum % IPB);
  194
+  dip = ((struct dinode*)buf) + (inum % IPB);
195 195
   *ip = *dip;
196 196
 }
197 197
 
@@ -231,7 +231,7 @@ balloc(int used)
231 231
   printf("balloc: first %d blocks have been allocated\n", used);
232 232
   assert(used < 512);
233 233
   bzero(buf, 512);
234  
-  for(i = 0; i < used; i++) {
  234
+  for(i = 0; i < used; i++){
235 235
     buf[i/8] = buf[i/8] | (0x1 << (i%8));
236 236
   }
237 237
   printf("balloc: write bitmap block at sector %zu\n", ninodes/IPB + 3);
@@ -243,7 +243,7 @@ balloc(int used)
243 243
 void
244 244
 iappend(uint inum, void *xp, int n)
245 245
 {
246  
-  char *p = (char*) xp;
  246
+  char *p = (char*)xp;
247 247
   uint fbn, off, n1;
248 248
   struct dinode din;
249 249
   char buf[512];
@@ -256,24 +256,24 @@ iappend(uint inum, void *xp, int n)
256 256
   while(n > 0){
257 257
     fbn = off / 512;
258 258
     assert(fbn < MAXFILE);
259  
-    if(fbn < NDIRECT) {
260  
-      if(xint(din.addrs[fbn]) == 0) {
  259
+    if(fbn < NDIRECT){
  260
+      if(xint(din.addrs[fbn]) == 0){
261 261
         din.addrs[fbn] = xint(freeblock++);
262 262
         usedblocks++;
263 263
       }
264 264
       x = xint(din.addrs[fbn]);
265 265
     } else {
266  
-      if(xint(din.addrs[NDIRECT]) == 0) {
  266
+      if(xint(din.addrs[NDIRECT]) == 0){
267 267
         // printf("allocate indirect block\n");
268 268
         din.addrs[NDIRECT] = xint(freeblock++);
269 269
         usedblocks++;
270 270
       }
271 271
       // printf("read indirect block\n");
272  
-      rsect(xint(din.addrs[NDIRECT]), (char*) indirect);
273  
-      if(indirect[fbn - NDIRECT] == 0) {
  272
+      rsect(xint(din.addrs[NDIRECT]), (char*)indirect);
  273
+      if(indirect[fbn - NDIRECT] == 0){
274 274
         indirect[fbn - NDIRECT] = xint(freeblock++);
275 275
         usedblocks++;
276  
-        wsect(xint(din.addrs[NDIRECT]), (char*) indirect);
  276
+        wsect(xint(din.addrs[NDIRECT]), (char*)indirect);
277 277
       }
278 278
       x = xint(indirect[fbn-NDIRECT]);
279 279
     }
14  mmu.h
@@ -98,18 +98,18 @@ struct segdesc {
98 98
 //  \--- PDX(la) --/ \--- PTX(la) --/ 
99 99
 
100 100
 // page directory index
101  
-#define PDX(la)		((((uint) (la)) >> PDXSHIFT) & 0x3FF)
  101
+#define PDX(la)		(((uint)(la) >> PDXSHIFT) & 0x3FF)
102 102
 
103 103
 // page table index
104  
-#define PTX(la)		((((uint) (la)) >> PTXSHIFT) & 0x3FF)
  104
+#define PTX(la)		(((uint)(la) >> PTXSHIFT) & 0x3FF)
105 105
 
106 106
 // construct linear address from indexes and offset
107  
-#define PGADDR(d, t, o)	((uint) ((d) << PDXSHIFT | (t) << PTXSHIFT | (o)))
  107
+#define PGADDR(d, t, o)	((uint)((d) << PDXSHIFT | (t) << PTXSHIFT | (o)))
108 108
 
109 109
 // turn a kernel linear address into a physical address.
110 110
 // all of the kernel data structures have linear and
111 111
 // physical addresses that are equal.
112  
-#define PADDR(a)       ((uint) a)
  112
+#define PADDR(a)       ((uint)(a))
113 113
 
114 114
 // Page directory and page table constants.
115 115
 #define NPDENTRIES	1024		// page directory entries per page directory
@@ -136,7 +136,7 @@ struct segdesc {
136 136
 #define PTE_MBZ		0x180	// Bits must be zero
137 137
 
138 138
 // Address in page table or page directory entry
139  
-#define PTE_ADDR(pte)	((uint) (pte) & ~0xFFF)
  139
+#define PTE_ADDR(pte)	((uint)(pte) & ~0xFFF)
140 140
 
141 141
 typedef uint pte_t;
142 142
 
@@ -205,7 +205,7 @@ struct gatedesc {
205 205
 //        this interrupt/trap gate explicitly using an int instruction.
206 206
 #define SETGATE(gate, istrap, sel, off, d)                \
207 207
 {                                                         \
208  
-  (gate).off_15_0 = (uint) (off) & 0xffff;                \
  208
+  (gate).off_15_0 = (uint)(off) & 0xffff;                \
209 209
   (gate).cs = (sel);                                      \
210 210
   (gate).args = 0;                                        \
211 211
   (gate).rsv1 = 0;                                        \
@@ -213,6 +213,6 @@ struct gatedesc {
213 213
   (gate).s = 0;                                           \
214 214
   (gate).dpl = (d);                                       \
215 215
   (gate).p = 1;                                           \
216  
-  (gate).off_31_16 = (uint) (off) >> 16;                  \
  216
+  (gate).off_31_16 = (uint)(off) >> 16;                  \
217 217
 }
218 218
 
4  mp.c
@@ -113,8 +113,8 @@ mpinit(void)
113 113
     switch(*p){
114 114
     case MPPROC:
115 115
       proc = (struct mpproc*)p;
116  
-      if(ncpu != proc->apicid) {
117  
-        cprintf("mpinit: ncpu=%d apicpid=%d", ncpu, proc->apicid);
  116
+      if(ncpu != proc->apicid){
  117
+        cprintf("mpinit: ncpu=%d apicpid=%d\n", ncpu, proc->apicid);
118 118
         panic("mpinit");
119 119
       }
120 120
       if(proc->flags & MPBOOT)
4  pipe.c
@@ -66,7 +66,7 @@ pipeclose(struct pipe *p, int writable)
66 66
     p->readopen = 0;
67 67
     wakeup(&p->nwrite);
68 68
   }
69  
-  if(p->readopen == 0 && p->writeopen == 0) {
  69
+  if(p->readopen == 0 && p->writeopen == 0){
70 70
     release(&p->lock);
71 71
     kfree((char*)p);
72 72
   } else
@@ -81,7 +81,7 @@ pipewrite(struct pipe *p, char *addr, int n)
81 81
 
82 82
   acquire(&p->lock);
83 83
   for(i = 0; i < n; i++){
84  
-    while(p->nwrite == p->nread + PIPESIZE) {  //DOC: pipewrite-full
  84
+    while(p->nwrite == p->nread + PIPESIZE){  //DOC: pipewrite-full
85 85
       if(p->readopen == 0 || proc->killed){
86 86
         release(&p->lock);
87 87
         return -1;
12  proc.c
@@ -120,7 +120,7 @@ userinit(void)
120 120
   
121 121
   p = allocproc();
122 122
   initproc = p;
123  
-  if(!(p->pgdir = setupkvm()))
  123
+  if((p->pgdir = setupkvm()) == 0)
124 124
     panic("userinit: out of memory?");
125 125
   inituvm(p->pgdir, _binary_initcode_start, (int)_binary_initcode_size);
126 126
   p->sz = PGSIZE;
@@ -144,12 +144,14 @@ userinit(void)
144 144
 int
145 145
 growproc(int n)
146 146
 {
147  
-  uint sz = proc->sz;
  147
+  uint sz;
  148
+  
  149
+  sz = proc->sz;
148 150
   if(n > 0){
149  
-    if(!(sz = allocuvm(proc->pgdir, sz, sz + n)))
  151
+    if((sz = allocuvm(proc->pgdir, sz, sz + n)) == 0)
150 152
       return -1;
151 153
   } else if(n < 0){
152  
-    if(!(sz = deallocuvm(proc->pgdir, sz, sz + n)))
  154
+    if((sz = deallocuvm(proc->pgdir, sz, sz + n)) == 0)
153 155
       return -1;
154 156
   }
155 157
   proc->sz = sz;
@@ -171,7 +173,7 @@ fork(void)
171 173
     return -1;
172 174
 
173 175
   // Copy process state from p.
174  
-  if(!(np->pgdir = copyuvm(proc->pgdir, proc->sz))){
  176
+  if((np->pgdir = copyuvm(proc->pgdir, proc->sz)) == 0){
175 177
     kfree(np->kstack);
176 178
     np->kstack = 0;
177 179
     np->state = UNUSED;
2  spinlock.c
@@ -71,7 +71,7 @@ getcallerpcs(void *v, uint pcs[])
71 71
   
72 72
   ebp = (uint*)v - 2;
73 73
   for(i = 0; i < 10; i++){
74  
-    if(ebp == 0 || ebp < (uint *) 0x100000 || ebp == (uint*)0xffffffff)
  74
+    if(ebp == 0 || ebp < (uint*)0x100000 || ebp == (uint*)0xffffffff)
75 75
       break;
76 76
     pcs[i] = ebp[1];     // saved %eip
77 77
     ebp = (uint*)ebp[0]; // saved %ebp
13  stressfs.c
@@ -14,20 +14,19 @@
14 14
 int
15 15
 main(int argc, char *argv[])
16 16
 {
17  
-  int i;
  17
+  int fd, i;
  18
+  char path[] = "stressfs0";
  19
+
18 20
   printf(1, "stressfs starting\n");
19 21
 
20  
-  for(i = 0; i < 4; i++){
21  
-    if(fork() > 0){
  22
+  for(i = 0; i < 4; i++)
  23
+    if(fork() > 0)
22 24
       break;
23  
-    }
24  
-  }
25 25
 
26 26
   printf(1, "%d\n", i);
27 27
 
28  
-  char path[] = "stressfs0";
29 28
   path[8] += i;
30  
-  int fd = open(path, O_CREATE | O_RDWR);
  29
+  fd = open(path, O_CREATE | O_RDWR);
31 30
   for(i = 0; i < 100; i++)
32 31
     printf(fd, "%d\n", i);
33 32
   close(fd);
9  syscall.c
@@ -32,8 +32,8 @@ fetchstr(struct proc *p, uint addr, char **pp)
32 32
 
33 33
   if(addr >= p->sz)
34 34
     return -1;
35  
-  *pp = (char *) addr;
36  
-  ep = (char *) p->sz;
  35
+  *pp = (char*)addr;
  36
+  ep = (char*)p->sz;
37 37
   for(s = *pp; s < ep; s++)
38 38
     if(*s == 0)
39 39
       return s - *pp;
@@ -44,8 +44,7 @@ fetchstr(struct proc *p, uint addr, char **pp)
44 44
 int
45 45
 argint(int n, int *ip)
46 46
 {
47  
-  int x = fetchint(proc, proc->tf->esp + 4 + 4*n, ip);
48  
-  return x;
  47
+  return fetchint(proc, proc->tf->esp + 4 + 4*n, ip);
49 48
 }
50 49
 
51 50
 // Fetch the nth word-sized system call argument as a pointer
@@ -60,7 +59,7 @@ argptr(int n, char **pp, int size)
60 59
     return -1;
61 60
   if((uint)i >= proc->sz || (uint)i+size >= proc->sz)
62 61
     return -1;
63  
-  *pp = (char *) i;
  62
+  *pp = (char*)i;
64 63
   return 0;
65 64
 }
66 65
 
2  sysfile.c
@@ -348,7 +348,7 @@ sys_exec(void)
348 348
   int i;
349 349
   uint uargv, uarg;
350 350
 
351  
-  if(argstr(0, &path) < 0 || argint(1, (int*)&uargv) < 0) {
  351
+  if(argstr(0, &path) < 0 || argint(1, (int*)&uargv) < 0){
352 352
     return -1;
353 353
   }
354 354
   memset(argv, 0, sizeof(argv));
2  ulib.c
@@ -45,7 +45,7 @@ strchr(const char *s, char c)
45 45
 {
46 46
   for(; *s; s++)
47 47
     if(*s == c)
48  
-      return (char*) s;
  48
+      return (char*)s;
49 49
   return 0;
50 50
 }
51 51
 
6  umalloc.c
@@ -26,7 +26,7 @@ free(void *ap)
26 26
 {
27 27
   Header *bp, *p;
28 28
 
29  
-  bp = (Header*) ap - 1;
  29
+  bp = (Header*)ap - 1;
30 30
   for(p = freep; !(bp > p && bp < p->s.ptr); p = p->s.ptr)
31 31
     if(p >= p->s.ptr && (bp > p || bp < p->s.ptr))
32 32
       break;
@@ -52,7 +52,7 @@ morecore(uint nu)
52 52
   if(nu < 4096)
53 53
     nu = 4096;
54 54
   p = sbrk(nu * sizeof(Header));
55  
-  if(p == (char*) -1)
  55
+  if(p == (char*)-1)
56 56
     return 0;
57 57
   hp = (Header*)p;
58 58
   hp->s.size = nu;
@@ -81,7 +81,7 @@ malloc(uint nbytes)
81 81
         p->s.size = nunits;
82 82
       }
83 83
       freep = prevp;
84  
-      return (void*) (p + 1);
  84
+      return (void*)(p + 1);
85 85
     }
86 86
     if(p == freep)
87 87
       if((p = morecore(nunits)) == 0)
4  user.h
@@ -18,10 +18,10 @@ int link(char*, char*);
18 18
 int mkdir(char*);
19 19
 int chdir(char*);
20 20
 int dup(int);
21  
-int getpid();
  21
+int getpid(void);
22 22
 char* sbrk(int);
23 23
 int sleep(int);
24  
-int uptime();
  24
+int uptime(void);
25 25
 
26 26
 // ulib.c
27 27
 int stat(char*, struct stat*);
100  usertests.c
@@ -47,12 +47,12 @@ writetest(void)
47 47
     printf(stdout, "error: creat small failed!\n");
48 48
     exit();
49 49
   }
50  
-  for(i = 0; i < 100; i++) {
51  
-    if(write(fd, "aaaaaaaaaa", 10) != 10) {
  50
+  for(i = 0; i < 100; i++){
  51
+    if(write(fd, "aaaaaaaaaa", 10) != 10){
52 52
       printf(stdout, "error: write aa %d new file failed\n", i);
53 53
       exit();
54 54
     }
55  
-    if(write(fd, "bbbbbbbbbb", 10) != 10) {
  55
+    if(write(fd, "bbbbbbbbbb", 10) != 10){
56 56
       printf(stdout, "error: write bb %d new file failed\n", i);
57 57
       exit();
58 58
     }
@@ -67,7 +67,7 @@ writetest(void)
67 67
     exit();
68 68
   }
69 69
   i = read(fd, buf, 2000);
70  
-  if(i == 2000) {
  70
+  if(i == 2000){
71 71
     printf(stdout, "read succeeded ok\n");
72 72
   } else {
73 73
     printf(stdout, "read failed\n");
@@ -75,7 +75,7 @@ writetest(void)
75 75
   }
76 76
   close(fd);
77 77
 
78  
-  if(unlink("small") < 0) {
  78
+  if(unlink("small") < 0){
79 79
     printf(stdout, "unlink small failed\n");
80 80
     exit();
81 81
   }
@@ -95,9 +95,9 @@ writetest1(void)
95 95
     exit();
96 96
   }
97 97
 
98  
-  for(i = 0; i < MAXFILE; i++) {
99  
-    ((int*) buf)[0] = i;
100  
-    if(write(fd, buf, 512) != 512) {
  98
+  for(i = 0; i < MAXFILE; i++){
  99
+    ((int*)buf)[0] = i;
  100
+    if(write(fd, buf, 512) != 512){
101 101
       printf(stdout, "error: write big file failed\n", i);
102 102
       exit();
103 103
     }
@@ -112,19 +112,19 @@ writetest1(void)
112 112
   }
113 113
 
114 114
   n = 0;
115  
-  for(;;) {
  115
+  for(;;){
116 116
     i = read(fd, buf, 512);
117  
-    if(i == 0) {
118  
-      if(n == MAXFILE - 1) {
  117
+    if(i == 0){
  118
+      if(n == MAXFILE - 1){
119 119
         printf(stdout, "read only %d blocks from big", n);
120 120
         exit();
121 121
       }
122 122
       break;
123  
-    } else if(i != 512) {
  123
+    } else if(i != 512){
124 124
       printf(stdout, "read failed %d\n", i);
125 125
       exit();
126 126
     }
127  
-    if(((int*)buf)[0] != n) {
  127
+    if(((int*)buf)[0] != n){
128 128
       printf(stdout, "read content of block %d is %d\n",
129 129
              n, ((int*)buf)[0]);
130 130
       exit();
@@ -132,7 +132,7 @@ writetest1(void)
132 132
     n++;
133 133
   }
134 134
   close(fd);
135  
-  if(unlink("big") < 0) {
  135
+  if(unlink("big") < 0){
136 136
     printf(stdout, "unlink big failed\n");
137 137
     exit();
138 138
   }
@@ -148,14 +148,14 @@ createtest(void)
148 148
 
149 149
   name[0] = 'a';
150 150
   name[2] = '\0';
151  
-  for(i = 0; i < 52; i++) {
  151
+  for(i = 0; i < 52; i++){
152 152
     name[1] = '0' + i;
153 153
     fd = open(name, O_CREATE|O_RDWR);
154 154
     close(fd);
155 155
   }
156 156
   name[0] = 'a';
157 157
   name[2] = '\0';
158  
-  for(i = 0; i < 52; i++) {
  158
+  for(i = 0; i < 52; i++){
159 159
     name[1] = '0' + i;
160 160
     unlink(name);
161 161
   }
@@ -166,22 +166,22 @@ void dirtest(void)
166 166
 {
167 167
   printf(stdout, "mkdir test\n");
168 168
 
169  
-  if(mkdir("dir0") < 0) {
  169
+  if(mkdir("dir0") < 0){
170 170
     printf(stdout, "mkdir failed\n");
171 171
     exit();
172 172
   }
173 173
 
174  
-  if(chdir("dir0") < 0) {
  174
+  if(chdir("dir0") < 0){
175 175
     printf(stdout, "chdir dir0 failed\n");
176 176
     exit();
177 177
   }
178 178
 
179  
-  if(chdir("..") < 0) {
  179
+  if(chdir("..") < 0){
180 180
     printf(stdout, "chdir .. failed\n");
181 181
     exit();
182 182
   }
183 183
 
184  
-  if(unlink("dir0") < 0) {
  184
+  if(unlink("dir0") < 0){
185 185
     printf(stdout, "unlink dir0 failed\n");
186 186
     exit();
187 187
   }
@@ -192,7 +192,7 @@ void
192 192
 exectest(void)
193 193
 {
194 194
   printf(stdout, "exec test\n");
195  
-  if(exec("echo", echoargv) < 0) {
  195
+  if(exec("echo", echoargv) < 0){
196 196
     printf(stdout, "exec echo failed\n");
197 197
     exit();
198 198
   }
@@ -330,17 +330,17 @@ mem(void)
330 330
   ppid = getpid();
331 331
   if((pid = fork()) == 0){
332 332
     m1 = 0;
333  
-    while((m2 = malloc(10001)) != 0) {
334  
-      *(char**) m2 = m1;
  333
+    while((m2 = malloc(10001)) != 0){
  334
+      *(char**)m2 = m1;
335 335
       m1 = m2;
336 336
     }
337  
-    while(m1) {
  337
+    while(m1){
338 338
       m2 = *(char**)m1;
339 339
       free(m1);
340 340
       m1 = m2;
341 341
     }
342 342
     m1 = malloc(1024*20);
343  
-    if(m1 == 0) {
  343
+    if(m1 == 0){
344 344
       printf(1, "couldn't allocate mem?!!\n");
345 345
       kill(ppid);
346 346
       exit();
@@ -1237,16 +1237,18 @@ forktest(void)
1237 1237
 void
1238 1238
 sbrktest(void)
1239 1239
 {
1240  
-  int pid;
1241  
-  char *oldbrk = sbrk(0);
  1240
+  int fds[2], pid, pids[32], ppid;
  1241
+  char *a, *b, *c, *lastaddr, *oldbrk, *p, scratch;
  1242
+  uint amt;
1242 1243
 
1243 1244
   printf(stdout, "sbrk test\n");
  1245
+  oldbrk = sbrk(0);
1244 1246
 
1245 1247
   // can one sbrk() less than a page?
1246  
-  char *a = sbrk(0);
  1248
+  a = sbrk(0);
1247 1249
   int i;
1248 1250
   for(i = 0; i < 5000; i++){
1249  
-    char *b = sbrk(1);
  1251
+    b = sbrk(1);
1250 1252
     if(b != a){
1251 1253
       printf(stdout, "sbrk test failed %d %x %x\n", i, a, b);
1252 1254
       exit();
@@ -1259,7 +1261,7 @@ sbrktest(void)
1259 1261
     printf(stdout, "sbrk test fork failed\n");
1260 1262
     exit();
1261 1263
   }
1262  
-  char *c = sbrk(1);
  1264
+  c = sbrk(1);
1263 1265
   c = sbrk(1);
1264 1266
   if(c != a + 1){
1265 1267
     printf(stdout, "sbrk test failed post-fork\n");
@@ -1271,18 +1273,18 @@ sbrktest(void)
1271 1273
 
1272 1274
   // can one allocate the full 640K?
1273 1275
   a = sbrk(0);
1274  
-  uint amt = (640 * 1024) - (uint) a;
1275  
-  char *p = sbrk(amt);
  1276
+  amt = (640 * 1024) - (uint)a;
  1277
+  p = sbrk(amt);
1276 1278
   if(p != a){
1277 1279
     printf(stdout, "sbrk test failed 640K test, p %x a %x\n", p, a);
1278 1280
     exit();
1279 1281
   }
1280  
-  char *lastaddr = (char *)(640 * 1024 - 1);
  1282
+  lastaddr = (char*)(640 * 1024 - 1);
1281 1283
   *lastaddr = 99;
1282 1284
 
1283 1285
   // is one forbidden from allocating more than 640K?
1284 1286
   c = sbrk(4096);
1285  
-  if(c != (char *) 0xffffffff){
  1287
+  if(c != (char*)0xffffffff){
1286 1288
     printf(stdout, "sbrk allocated more than 640K, c %x\n", c);
1287 1289
     exit();
1288 1290
   }
@@ -1290,7 +1292,7 @@ sbrktest(void)
1290 1292
   // can one de-allocate?
1291 1293
   a = sbrk(0);
1292 1294
   c = sbrk(-4096);
1293  
-  if(c == (char *) 0xffffffff){
  1295
+  if(c == (char*)0xffffffff){
1294 1296
     printf(stdout, "sbrk could not deallocate\n");
1295 1297
     exit();
1296 1298
   }
@@ -1314,15 +1316,15 @@ sbrktest(void)
1314 1316
   }
1315 1317
 
1316 1318
   c = sbrk(4096);
1317  
-  if(c != (char *) 0xffffffff){
  1319
+  if(c != (char*)0xffffffff){
1318 1320
     printf(stdout, "sbrk was able to re-allocate beyond 640K, c %x\n", c);
1319 1321
     exit();
1320 1322
   }
1321 1323
 
1322 1324
   // can we read the kernel's memory?
1323  
-  for(a = (char*)(640*1024); a < (char *)2000000; a += 50000){
1324  
-    int ppid = getpid();
1325  
-    int pid = fork();
  1325
+  for(a = (char*)(640*1024); a < (char*)2000000; a += 50000){
  1326
+    ppid = getpid();
  1327
+    pid = fork();
1326 1328
     if(pid < 0){
1327 1329
       printf(stdout, "fork failed\n");
1328 1330
       exit();
@@ -1338,21 +1340,18 @@ sbrktest(void)
1338 1340
   // if we run the system out of memory, does it clean up the last
1339 1341
   // failed allocation?
1340 1342
   sbrk(-(sbrk(0) - oldbrk));
1341  
-  int pids[32];
1342  
-  int fds[2];
1343 1343
   if(pipe(fds) != 0){
1344 1344
     printf(1, "pipe() failed\n");
1345 1345
     exit();
1346 1346
   }
1347 1347
   for(i = 0; i < sizeof(pids)/sizeof(pids[0]); i++){
1348  
-    if((pids[i] = fork()) == 0) {
  1348
+    if((pids[i] = fork()) == 0){
1349 1349
       // allocate the full 640K
1350 1350
       sbrk((640 * 1024) - (uint)sbrk(0));
1351 1351
       write(fds[1], "x", 1);
1352 1352
       // sit around until killed
1353 1353
       for(;;) sleep(1000);
1354 1354
     }
1355  
-    char scratch;
1356 1355
     if(pids[i] != -1)
1357 1356
       read(fds[0], &scratch, 1);
1358 1357
   }
@@ -1365,7 +1364,7 @@ sbrktest(void)
1365 1364
     kill(pids[i]);
1366 1365
     wait();
1367 1366
   }
1368  
-  if(c == (char*)0xffffffff) {
  1367
+  if(c == (char*)0xffffffff){
1369 1368
     printf(stdout, "failed sbrk leaked memory\n");
1370 1369
     exit();
1371 1370
   }
@@ -1392,14 +1391,14 @@ validateint(int *p)
1392 1391
 void
1393 1392
 validatetest(void)
1394 1393
 {
1395  
-  int hi = 1100*1024;
  1394
+  int hi, pid;
  1395
+  uint p;
1396 1396
 
1397 1397
   printf(stdout, "validate test\n");
  1398
+  hi = 1100*1024;
1398 1399
 
1399  
-  uint p;
1400  
-  for (p = 0; p <= (uint)hi; p += 4096) {
1401  
-    int pid;
1402  
-    if ((pid = fork()) == 0) {
  1400
+  for(p = 0; p <= (uint)hi; p += 4096){
  1401
+    if((pid = fork()) == 0){
1403 1402
       // try to crash the kernel by passing in a badly placed integer
1404 1403
       validateint((int*)p);
1405 1404
       exit();
@@ -1410,7 +1409,7 @@ validatetest(void)
1410 1409
     wait();
1411 1410
 
1412 1411
     // try to crash the kernel by passing in a bad string pointer
1413  
-    if (link("nosuchfile", (char*)p) != -1) {
  1412
+    if(link("nosuchfile", (char*)p) != -1){
1414 1413
       printf(stdout, "link should not succeed\n");
1415 1414
       exit();
1416 1415
     }
@@ -1425,6 +1424,7 @@ void
1425 1424
 bsstest(void)
1426 1425
 {
1427 1426
   int i;
  1427
+
1428 1428
   printf(stdout, "bss test\n");
1429 1429
   for(i = 0; i < sizeof(uninit); i++){
1430 1430
     if(uninit[i] != '\0'){
143  vm.c
@@ -42,23 +42,21 @@ seginit(void)
42 42
 static pte_t *
43 43
 walkpgdir(pde_t *pgdir, const void *va, int create)
44 44
 {
45  
-  uint r;
46 45
   pde_t *pde;
47 46
   pte_t *pgtab;
48 47
 
49 48
   pde = &pgdir[PDX(va)];
50 49
   if(*pde & PTE_P){
51  
-    pgtab = (pte_t*) PTE_ADDR(*pde);
52  
-  } else if(!create || !(r = (uint) kalloc()))
53  
-    return 0;
54  
-  else {
55  
-    pgtab = (pte_t*) r;
  50
+    pgtab = (pte_t*)PTE_ADDR(*pde);
  51
+  } else {
  52
+    if(!create || (pgtab = (pte_t*)kalloc()) == 0)
  53
+      return 0;
56 54
     // Make sure all those PTE_P bits are zero.
57 55
     memset(pgtab, 0, PGSIZE);
58 56
     // The permissions here are overly generous, but they can
59 57
     // be further restricted by the permissions in the page table 
60 58
     // entries, if necessary.
61  
-    *pde = PADDR(r) | PTE_P | PTE_W | PTE_U;
  59
+    *pde = PADDR(pgtab) | PTE_P | PTE_W | PTE_U;
62 60
   }
63 61
   return &pgtab[PTX(va)];
64 62
 }
@@ -69,13 +67,16 @@ walkpgdir(pde_t *pgdir, const void *va, int create)
69 67
 static int
70 68
 mappages(pde_t *pgdir, void *la, uint size, uint pa, int perm)
71 69
 {
72  
-  char *a = PGROUNDDOWN(la);
73  
-  char *last = PGROUNDDOWN(la + size - 1);
  70
+  char *a, *last;
  71
+  pte_t *pte;
  72
+  
  73
+  a = PGROUNDDOWN(la);
  74
+  last = PGROUNDDOWN(la + size - 1);
74 75
 
75  
-  while(1){
76  
-    pte_t *pte = walkpgdir(pgdir, a, 1);
  76
+  for(;;){
  77
+    pte = walkpgdir(pgdir, a, 1);
77 78
     if(pte == 0)
78  
-      return 0;
  79
+      return -1;
79 80
     if(*pte & PTE_P)
80 81
       panic("remap");
81 82
     *pte = pa | perm | PTE_P;
@@ -84,7 +85,7 @@ mappages(pde_t *pgdir, void *la, uint size, uint pa, int perm)
84 85
     a += PGSIZE;
85 86
     pa += PGSIZE;
86 87
   }
87  
-  return 1;
  88
+  return 0;
88 89
 }
89 90
 
90 91
 // The mappings from logical to linear are one to one (i.e.,
@@ -122,23 +123,26 @@ kvmalloc(void)
122 123
 pde_t*
123 124
 setupkvm(void)
124 125
 {
125  
-  pde_t *pgdir;
126 126
   extern char etext[];
127  
-  char *rwstart = PGROUNDDOWN(etext);
128  
-  uint rwlen = (uint)rwstart - 0x100000;
  127
+  char *rwstart;
  128
+  pde_t *pgdir;
  129
+  uint rwlen;
  130
+
  131
+  rwstart = PGROUNDDOWN(etext);
  132
+  rwlen = (uint)rwstart - 0x100000;
129 133
 
130 134
   // Allocate page directory
131  
-  if(!(pgdir = (pde_t *) kalloc()))
  135
+  if((pgdir = (pde_t*)kalloc()) == 0)
132 136
     return 0;
133 137
   memset(pgdir, 0, PGSIZE);
134 138
   if(// Map IO space from 640K to 1Mbyte
135  
-     !mappages(pgdir, (void *)USERTOP, 0x60000, USERTOP, PTE_W) ||
  139
+     mappages(pgdir, (void*)USERTOP, 0x60000, USERTOP, PTE_W) < 0 ||
136 140
      // Map kernel instructions
137  
-     !mappages(pgdir, (void *)0x100000, rwlen, 0x100000, 0) ||
  141
+     mappages(pgdir, (void*)0x100000, rwlen, 0x100000, 0) < 0 ||
138 142
      // Map kernel data and free memory pool
139  
-     !mappages(pgdir, rwstart, PHYSTOP-(uint)rwstart, (uint)rwstart, PTE_W) ||
  143
+     mappages(pgdir, rwstart, PHYSTOP-(uint)rwstart, (uint)rwstart, PTE_W) < 0 ||
140 144
      // Map devices such as ioapic, lapic, ...
141  
-     !mappages(pgdir, (void *)0xFE000000, 0x2000000, 0xFE000000, PTE_W))
  145
+     mappages(pgdir, (void*)0xFE000000, 0x2000000, 0xFE000000, PTE_W) < 0)
142 146
     return 0;
143 147
   return pgdir;
144 148
 }
@@ -189,14 +193,15 @@ switchuvm(struct proc *p)
189 193
 // processes directly.
190 194
 char*
191 195
 uva2ka(pde_t *pgdir, char *uva)
192  
-{    
193  
-  pte_t *pte = walkpgdir(pgdir, uva, 0);
  196
+{
  197
+  pte_t *pte;
  198
+
  199
+  pte = walkpgdir(pgdir, uva, 0);
194 200
   if((*pte & PTE_P) == 0)
195 201
     return 0;
196 202
   if((*pte & PTE_U) == 0)
197 203
     return 0;
198  
-  uint pa = PTE_ADDR(*pte);
199  
-  return (char *)pa;
  204
+  return (char*)PTE_ADDR(*pte);
200 205
 }
201 206
 
202 207
 // Load the initcode into address 0 of pgdir.
@@ -204,9 +209,11 @@ uva2ka(pde_t *pgdir, char *uva)
204 209
 void
205 210
 inituvm(pde_t *pgdir, char *init, uint sz)
206 211
 {
207  
-  char *mem = kalloc();
208  
-  if (sz >= PGSIZE)
  212
+  char *mem;
  213
+  
  214
+  if(sz >= PGSIZE)
209 215
     panic("inituvm: more than a page");
  216
+  mem = kalloc();
210 217
   memset(mem, 0, PGSIZE);
211 218
   mappages(pgdir, 0, PGSIZE, PADDR(mem), PTE_W|PTE_U);
212 219
   memmove(mem, init, sz);
@@ -223,15 +230,17 @@ loaduvm(pde_t *pgdir, char *addr, struct inode *ip, uint offset, uint sz)
223 230
   if((uint)addr % PGSIZE != 0)
224 231
     panic("loaduvm: addr must be page aligned\n");
225 232
   for(i = 0; i < sz; i += PGSIZE){
226  
-    if(!(pte = walkpgdir(pgdir, addr+i, 0)))
  233
+    if((pte = walkpgdir(pgdir, addr+i, 0)) == 0)
227 234
       panic("loaduvm: address should exist\n");
228 235
     pa = PTE_ADDR(*pte);
229  
-    if(sz - i < PGSIZE) n = sz - i;
230  
-    else n = PGSIZE;
231  
-    if(readi(ip, (char *)pa, offset+i, n) != n)
232  
-      return 0;
  236
+    if(sz - i < PGSIZE)
  237
+      n = sz - i;
  238
+    else
  239
+      n = PGSIZE;
  240
+    if(readi(ip, (char*)pa, offset+i, n) != n)
  241
+      return -1;
233 242
   }
234  
-  return 1;
  243
+  return 0;
235 244
 }
236 245
 
237 246
 // Allocate memory to the process to bring its size from oldsz to
@@ -241,12 +250,17 @@ loaduvm(pde_t *pgdir, char *addr, struct inode *ip, uint offset, uint sz)
241 250
 int
242 251
 allocuvm(pde_t *pgdir, uint oldsz, uint newsz)
243 252
 {
  253
+  char *a, *last, *mem;
  254
+
244 255
   if(newsz > USERTOP)
245 256
     return 0;
246  
-  char *a = (char *)PGROUNDUP(oldsz);
247  
-  char *last = PGROUNDDOWN(newsz - 1);
248  
-  for (; a <= last; a += PGSIZE){
249  
-    char *mem = kalloc();
  257
+  if(newsz < oldsz)
  258
+    return oldsz;
  259
+
  260
+  a = (char*)PGROUNDUP(oldsz);
  261
+  last = PGROUNDDOWN(newsz - 1);
  262
+  for(; a <= last; a += PGSIZE){
  263
+    mem = kalloc();
250 264
     if(mem == 0){
251 265
       cprintf("allocuvm out of memory\n");
252 266
       deallocuvm(pgdir, newsz, oldsz);
@@ -255,7 +269,7 @@ allocuvm(pde_t *pgdir, uint oldsz, uint newsz)
255 269
     memset(mem, 0, PGSIZE);
256 270
     mappages(pgdir, a, PGSIZE, PADDR(mem), PTE_W|PTE_U);
257 271
   }
258  
-  return newsz > oldsz ? newsz : oldsz;
  272
+  return newsz;
259 273
 }
260 274
 
261 275
 // Deallocate user pages to bring the process size from oldsz to
@@ -265,19 +279,26 @@ allocuvm(pde_t *pgdir, uint oldsz, uint newsz)
265 279
 int
266 280
 deallocuvm(pde_t *pgdir, uint oldsz, uint newsz)
267 281
 {
268  
-  char *a = (char *)PGROUNDUP(newsz);
269  
-  char *last = PGROUNDDOWN(oldsz - 1);
  282
+  char *a, *last;
  283
+  pte_t *pte;
  284
+  uint pa;
  285
+
  286
+  if(newsz >= oldsz)
  287
+    return oldsz;
  288
+
  289
+  a = (char*)PGROUNDUP(newsz);
  290
+  last = PGROUNDDOWN(oldsz - 1);
270 291
   for(; a <= last; a += PGSIZE){
271  
-    pte_t *pte = walkpgdir(pgdir, a, 0);
  292
+    pte = walkpgdir(pgdir, a, 0);
272 293
     if(pte && (*pte & PTE_P) != 0){
273  
-      uint pa = PTE_ADDR(*pte);
  294
+      pa = PTE_ADDR(*pte);
274 295
       if(pa == 0)
275 296
         panic("kfree");
276  
-      kfree((void *) pa);
  297
+      kfree((char*)pa);
277 298
       *pte = 0;
278 299
     }
279 300
   }
280  
-  return newsz < oldsz ? newsz : oldsz;
  301
+  return newsz;
281 302
 }
282 303
 
283 304
 // Free a page table and all the physical memory pages
@@ -287,14 +308,14 @@ freevm(pde_t *pgdir)
287 308
 {
288 309
   uint i;
289 310
 
290  
-  if(!pgdir)
  311
+  if(pgdir == 0)
291 312
     panic("freevm: no pgdir");
292 313
   deallocuvm(pgdir, USERTOP, 0);
293 314
   for(i = 0; i < NPDENTRIES; i++){
294 315
     if(pgdir[i] & PTE_P)
295  
-      kfree((void *) PTE_ADDR(pgdir[i]));
  316
+      kfree((char*)PTE_ADDR(pgdir[i]));
296 317
   }
297  
-  kfree((void *) pgdir);
  318
+  kfree((char*)pgdir);
298 319
 }
299 320
 
300 321
 // Given a parent process's page table, create a copy
@@ -302,22 +323,23 @@ freevm(pde_t *pgdir)
302 323
 pde_t*
303 324
 copyuvm(pde_t *pgdir, uint sz)
304 325
 {
305  
-  pde_t *d = setupkvm();
  326
+  pde_t *d;
306 327
   pte_t *pte;
307 328
   uint pa, i;
308 329
   char *mem;
309 330
 
310  
-  if(!d) return 0;
  331
+  if((d = setupkvm()) == 0)
  332
+    return 0;
311 333
   for(i = 0; i < sz; i += PGSIZE){
312  
-    if(!(pte = walkpgdir(pgdir, (void *)i, 0)))
  334
+    if((pte = walkpgdir(pgdir, (void*)i, 0)) == 0)
313 335
       panic("copyuvm: pte should exist\n");
314 336
     if(!(*pte & PTE_P))
315 337
       panic("copyuvm: page not present\n");
316 338
     pa = PTE_ADDR(*pte);
317  
-    if(!(mem = kalloc()))
  339
+    if((mem = kalloc()) == 0)
318 340
       goto bad;
319  
-    memmove(mem, (char *)pa, PGSIZE);
320  
-    if(!mappages(d, (void *)i, PGSIZE, PADDR(mem), PTE_W|PTE_U))
  341
+    memmove(mem, (char*)pa, PGSIZE);
  342
+    if(mappages(d, (void*)i, PGSIZE, PADDR(mem), PTE_W|PTE_U) < 0)
321 343
       goto bad;
322 344
   }
323 345
   return d;
@@ -334,13 +356,16 @@ copyuvm(pde_t *pgdir, uint sz)
334 356
 int
335 357
 copyout(pde_t *pgdir, uint va, void *xbuf, uint len)
336 358
 {