summaryrefslogtreecommitdiffstats
path: root/src/examples/sumargv.c
diff options
context:
space:
mode:
authorFelipe Boeira <felipe.boeira@liu.se>2019-01-08 18:39:03 +0100
committerFelipe Boeira <felipe.boeira@liu.se>2019-01-08 18:39:03 +0100
commitd4522b8e9854178473adcea0fbb84f23f6e744bd (patch)
treefbcf620617c5023154eba3f965b3a982daa64a47 /src/examples/sumargv.c
downloadpintos-d4522b8e9854178473adcea0fbb84f23f6e744bd.tar.gz
Initial commit
Diffstat (limited to 'src/examples/sumargv.c')
-rw-r--r--src/examples/sumargv.c26
1 files changed, 26 insertions, 0 deletions
diff --git a/src/examples/sumargv.c b/src/examples/sumargv.c
new file mode 100644
index 0000000..06962f9
--- /dev/null
+++ b/src/examples/sumargv.c
@@ -0,0 +1,26 @@
+#include <stdlib.h>
+
+int
+main (int argc, char **argv)
+{
+ int i;
+ int sum = 0;
+ char* argv_me = "sumargv";
+ char* p;
+
+ for (p = argv_me, i = 0; *p; ++p, ++i)
+ sum += (*p - 'a') * i;
+
+ for (p = argv[0], i = 0; *p; ++p, ++i)
+ sum -= (*p - 'a') * i;
+
+ /* if program name is correctly set up sum should now be zero */
+
+ for (i = 1; i < argc; i++)
+ sum += atoi(argv[i]);
+
+ /* if argv ends correctly with a null pointer this has no effect */
+ sum += argv[argc];
+
+ return sum;
+}