annotate lab4/pas0.c @ 0:bfdcc3820b32

Basis
author Mike Spivey <mike@cs.ox.ac.uk>
date Thu, 05 Oct 2017 08:04:15 +0100
parents
children
rev   line source
mike@0 1 /* pas0.c */
mike@0 2
mike@0 3 #include <stdio.h>
mike@0 4 #include <stdlib.h>
mike@0 5 #include <string.h>
mike@0 6
mike@0 7 extern void pmain();
mike@0 8
mike@0 9 static int save_argc;
mike@0 10 static char **save_argv;
mike@0 11 static FILE *infile;
mike@0 12
mike@0 13 int main(int argc, char **argv) {
mike@0 14 save_argc = argc;
mike@0 15 save_argv = argv;
mike@0 16 infile = stdin;
mike@0 17 pmain();
mike@0 18 return 0;
mike@0 19 }
mike@0 20
mike@0 21 int argc(void) {
mike@0 22 return save_argc;
mike@0 23 }
mike@0 24
mike@0 25 void argv(int n, char *s) {
mike@0 26 strcpy(s, save_argv[n]);
mike@0 27 }
mike@0 28
mike@0 29 void print_string(char *s, int n) {
mike@0 30 printf("%.*s", n, s);
mike@0 31 }
mike@0 32
mike@0 33 void print_num(int n) {
mike@0 34 printf("%d", n);
mike@0 35 }
mike@0 36
mike@0 37 void print_char(int c) {
mike@0 38 printf("%c", c);
mike@0 39 }
mike@0 40
mike@0 41 void newline(void) {
mike@0 42 printf("\n");
mike@0 43 }
mike@0 44
mike@0 45 void read_char(char *c) {
mike@0 46 int c0 = fgetc(infile);
mike@0 47 *c = (c0 == EOF ? 127 : c0);
mike@0 48 }
mike@0 49
mike@0 50 int open_in(char *s) {
mike@0 51 FILE *f = fopen(s, "r");
mike@0 52 if (f == NULL) return 0;
mike@0 53 if (infile != stdin) fclose(infile);
mike@0 54 infile = f;
mike@0 55 return 1;
mike@0 56 }
mike@0 57
mike@0 58 void close_in(void) {
mike@0 59 if (infile == stdin) return;
mike@0 60 fclose(infile);
mike@0 61 infile = stdin;
mike@0 62 }
mike@0 63
mike@0 64 void check(int n) {
mike@0 65 fprintf(stderr, "Array bound error on line %d\n", n);
mike@0 66 exit(2);
mike@0 67 }
mike@0 68
mike@0 69 void nullcheck(int n) {
mike@0 70 fprintf(stderr, "Null pointer check on line %d\n", n);
mike@0 71 exit(2);
mike@0 72 }
mike@0 73
mike@0 74 void *new(int n) {
mike@0 75 char *q = malloc(n);
mike@0 76 if (q == NULL) {
mike@0 77 fprintf(stderr, "Out of memory space\n");
mike@0 78 exit(2);
mike@0 79 }
mike@0 80 return q;
mike@0 81 }
mike@0 82
mike@0 83 int int_div(int a, int b) {
mike@0 84 int quo = a / b, rem = a % b;
mike@0 85 if (rem != 0 && (rem ^ b) < 0) quo--;
mike@0 86 return quo;
mike@0 87 }
mike@0 88
mike@0 89 int int_mod(int a, int b) {
mike@0 90 int rem = a % b;
mike@0 91 if (rem != 0 && (rem ^ b) < 0) rem += b;
mike@0 92 return rem;
mike@0 93 }
mike@0 94