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.
This commit is contained in:
parent
240679608c
commit
1a81e38b17
21 changed files with 227 additions and 199 deletions
24
exec.c
24
exec.c
|
|
@ -10,16 +10,17 @@ int
|
|||
exec(char *path, char **argv)
|
||||
{
|
||||
char *s, *last;
|
||||
int i, off;
|
||||
uint sz = 0;
|
||||
int i, off, argc;
|
||||
uint sz, sp, strings[MAXARG];
|
||||
struct elfhdr elf;
|
||||
struct inode *ip = 0;
|
||||
struct inode *ip;
|
||||
struct proghdr ph;
|
||||
pde_t *pgdir = 0, *oldpgdir;
|
||||
pde_t *pgdir, *oldpgdir;
|
||||
|
||||
if((ip = namei(path)) == 0)
|
||||
return -1;
|
||||
ilock(ip);
|
||||
pgdir = 0;
|
||||
|
||||
// Check ELF header
|
||||
if(readi(ip, (char*)&elf, 0, sizeof(elf)) < sizeof(elf))
|
||||
|
|
@ -27,10 +28,11 @@ exec(char *path, char **argv)
|
|||
if(elf.magic != ELF_MAGIC)
|
||||
goto bad;
|
||||
|
||||
if(!(pgdir = setupkvm()))
|
||||
if((pgdir = setupkvm()) == 0)
|
||||
goto bad;
|
||||
|
||||
// Load program into memory.
|
||||
sz = 0;
|
||||
for(i=0, off=elf.phoff; i<elf.phnum; i++, off+=sizeof(ph)){
|
||||
if(readi(ip, (char*)&ph, off, sizeof(ph)) != sizeof(ph))
|
||||
goto bad;
|
||||
|
|
@ -38,9 +40,9 @@ exec(char *path, char **argv)
|
|||
continue;
|
||||
if(ph.memsz < ph.filesz)
|
||||
goto bad;
|
||||
if(!(sz = allocuvm(pgdir, sz, ph.va + ph.memsz)))
|
||||
if((sz = allocuvm(pgdir, sz, ph.va + ph.memsz)) == 0)
|
||||
goto bad;
|
||||
if(!loaduvm(pgdir, (char *)ph.va, ip, ph.offset, ph.filesz))
|
||||
if(loaduvm(pgdir, (char*)ph.va, ip, ph.offset, ph.filesz) < 0)
|
||||
goto bad;
|
||||
}
|
||||
iunlockput(ip);
|
||||
|
|
@ -48,7 +50,7 @@ exec(char *path, char **argv)
|
|||
|
||||
// Allocate a one-page stack at the next page boundary
|
||||
sz = PGROUNDUP(sz);
|
||||
if(!(sz = allocuvm(pgdir, sz, sz + PGSIZE)))
|
||||
if((sz = allocuvm(pgdir, sz, sz + PGSIZE)) == 0)
|
||||
goto bad;
|
||||
|
||||
// initialize stack content:
|
||||
|
|
@ -64,24 +66,22 @@ exec(char *path, char **argv)
|
|||
// argc -- argc argument to main()
|
||||
// ffffffff -- return PC for main() call
|
||||
|
||||
uint sp = sz;
|
||||
sp = sz;
|
||||
|
||||
// count arguments
|
||||
int argc;
|
||||
for(argc = 0; argv[argc]; argc++)
|
||||
;
|
||||
if(argc >= MAXARG)
|
||||
goto bad;
|
||||
|
||||
// push strings and remember where they are
|
||||
uint strings[MAXARG];
|
||||
for(i = argc - 1; i >= 0; --i){
|
||||
sp -= strlen(argv[i]) + 1;
|
||||
strings[i] = sp;
|
||||
copyout(pgdir, sp, argv[i], strlen(argv[i]) + 1);
|
||||
}
|
||||
|
||||
#define PUSH(x) { int xx = (int)(x); sp -= 4; copyout(pgdir, sp, &xx, 4); }
|
||||
#define PUSH(x){ int xx = (int)(x); sp -= 4; copyout(pgdir, sp, &xx, 4); }
|
||||
|
||||
PUSH(0); // argv[argc] is zero
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue