diff options
| author | klaar36 <klas.arvidsson@liu.se> | 2015-03-20 17:30:24 +0100 |
|---|---|---|
| committer | klaar36 <klas.arvidsson@liu.se> | 2015-03-20 17:30:24 +0100 |
| commit | e7bc50ca8ffcaa6ed68ebd2315f78b0f5a7d10ad (patch) | |
| tree | 4de97af7207676b69cb6a9aba8cb443cc134855d /src/examples/sumargv.c | |
| parent | b0418a24e709f0632d2ede5b0f327c422931939b (diff) | |
| download | pintos-rs-e7bc50ca8ffcaa6ed68ebd2315f78b0f5a7d10ad.tar.gz | |
Initial Pintos
Diffstat (limited to 'src/examples/sumargv.c')
| -rw-r--r-- | src/examples/sumargv.c | 54 |
1 files changed, 54 insertions, 0 deletions
diff --git a/src/examples/sumargv.c b/src/examples/sumargv.c new file mode 100644 index 0000000..1bd6d14 --- /dev/null +++ b/src/examples/sumargv.c @@ -0,0 +1,54 @@ +/* klaar@ida + + A test program for the stack setup that do not rely on any (they + are not yet implemented!) system calls. + + The intended pintos command line running from userprog is: + + pintos -v -k --fs-disk=2 -p ../examples/sumargv -a sumargv -- -f -q run 'sumargv' + + This invocation shall return 0. To return any other number, invoke + with numbers that sum to that number. 'sumargv 1000 200 4 30 will + result in 1234. + + Note -v (no vga) and -q (poweroff when done) may cause trouble. If + you have problems exiting pintos, pres C-a and then x. If the + program does not seem to work it may be due to -q do poweroff too + fast (process_wait is incorrect). + */ +#include <stdlib.h> + +int +main (int argc, char **argv) +{ + int i; + int sum = 0; + char* argv_me = "sumargv"; + char* p; + + /* If we do not have a valid `argv', exit with error code 111. */ + if (argv == 0) + return 111; + + /* Weighted sum of characters in "sumargv". */ + for (p = argv_me, i = 0; *p; ++p, ++i) + sum += (*p - 'a') * i; + + /* Weighted sum of characters in `argv[0]'. */ + for (p = argv[0], i = 0; *p; ++p, ++i) + sum -= (*p - 'a') * i; + + /* The `sum' should now be zero if the program name `argv[0]' is + * correctly set up. */ + + /* Interpret all remaining command line words as numbers and sum them. */ + for (i = 1; i < argc; i++) + sum += atoi(argv[i]); + + /* If `argv' ends correctly with a null pointer this has no effect. */ + sum += (int)argv[argc]; + + /* The final return value should now be the sum of the numbers + * specified after sumargv on the command line. */ + return sum; +} |
