diff options
Diffstat (limited to 'reference/C/CONTRIB/OR_PRACTICAL_C')
83 files changed, 3551 insertions, 0 deletions
diff --git a/reference/C/CONTRIB/OR_PRACTICAL_C/01_1.c b/reference/C/CONTRIB/OR_PRACTICAL_C/01_1.c new file mode 100644 index 0000000..7c852bf --- /dev/null +++ b/reference/C/CONTRIB/OR_PRACTICAL_C/01_1.c @@ -0,0 +1,6 @@ +#include <stdio.h> +main() +{ + (void) printf("Hello World\n"); + return (0); +} diff --git a/reference/C/CONTRIB/OR_PRACTICAL_C/01_2.c b/reference/C/CONTRIB/OR_PRACTICAL_C/01_2.c new file mode 100644 index 0000000..7c852bf --- /dev/null +++ b/reference/C/CONTRIB/OR_PRACTICAL_C/01_2.c @@ -0,0 +1,6 @@ +#include <stdio.h> +main() +{ + (void) printf("Hello World\n"); + return (0); +} diff --git a/reference/C/CONTRIB/OR_PRACTICAL_C/02_1.c b/reference/C/CONTRIB/OR_PRACTICAL_C/02_1.c new file mode 100644 index 0000000..63ebe38 --- /dev/null +++ b/reference/C/CONTRIB/OR_PRACTICAL_C/02_1.c @@ -0,0 +1,18 @@ +/******************************************************** + * hello -- program to print out "Hello World". * + * Not an especially earth-shattering program. * + * * + * Author: Steve Oualline * + * * + * Purpose: Demonstration of a simple program * + * * + * Usage: * + * Run the program and the message appears * + ********************************************************/ +#include <stdio.h> +main() +{ + /* Tell the world hello */ + (void) printf("Hello World\n"); + return (0); +} diff --git a/reference/C/CONTRIB/OR_PRACTICAL_C/03_1.c b/reference/C/CONTRIB/OR_PRACTICAL_C/03_1.c new file mode 100644 index 0000000..db625d5 --- /dev/null +++ b/reference/C/CONTRIB/OR_PRACTICAL_C/03_1.c @@ -0,0 +1,5 @@ +main() +{ + (1 + 2) * 4; + return (0); +} diff --git a/reference/C/CONTRIB/OR_PRACTICAL_C/03_2.c b/reference/C/CONTRIB/OR_PRACTICAL_C/03_2.c new file mode 100644 index 0000000..915998b --- /dev/null +++ b/reference/C/CONTRIB/OR_PRACTICAL_C/03_2.c @@ -0,0 +1,13 @@ +#include <stdio.h> + +int term; /* term used in two expressions */ +int term_2; /* twice term */ +int term_3; /* three times term */ +main() +{ + + term = 3 * 5; + term_2 = 2 * term; + term_3 = 3 * term; + return (0); +} diff --git a/reference/C/CONTRIB/OR_PRACTICAL_C/03_3.c b/reference/C/CONTRIB/OR_PRACTICAL_C/03_3.c new file mode 100644 index 0000000..e97347e --- /dev/null +++ b/reference/C/CONTRIB/OR_PRACTICAL_C/03_3.c @@ -0,0 +1,11 @@ +#include <stdio.h> + +int term; /* term used in two expressions */ +main() +{ + + term = 3 * 5; + (void)printf("Twice %d is %d\n", term, 2*term); + (void)printf("Three time %d is %d\n", term, 3*term); + return (0); +} diff --git a/reference/C/CONTRIB/OR_PRACTICAL_C/03_4.c b/reference/C/CONTRIB/OR_PRACTICAL_C/03_4.c new file mode 100644 index 0000000..ac008b9 --- /dev/null +++ b/reference/C/CONTRIB/OR_PRACTICAL_C/03_4.c @@ -0,0 +1,16 @@ +#include <stdio.h> + +char char1; /* first character */ +char char2; /* second character */ +char char3; /* third character */ + +main() +{ + char1 = 'A'; + char2 = 'B'; + char3 = 'C'; + (void)printf("%c%c%c reversed is %c%c%c\n", + char1, char2, char3, + char3, char2, char1); + return (0); +} diff --git a/reference/C/CONTRIB/OR_PRACTICAL_C/04_1.c b/reference/C/CONTRIB/OR_PRACTICAL_C/04_1.c new file mode 100644 index 0000000..caa3401 --- /dev/null +++ b/reference/C/CONTRIB/OR_PRACTICAL_C/04_1.c @@ -0,0 +1,19 @@ +#include <stdio.h> + +float data[5]; /* data to average and total */ +float total; /* the total of the data items */ +float average; /* average of the items */ + +main() +{ + data[0] = 34.0; + data[1] = 27.0; + data[2] = 45.0; + data[3] = 82.0; + data[4] = 22.0; + + total = data[0] + data[1] + data[2] + data[3] + data[4]; + average = total / 5.0; + (void)printf("Total %f Average %f\n", total, average); + return (0); +} diff --git a/reference/C/CONTRIB/OR_PRACTICAL_C/04_2.c b/reference/C/CONTRIB/OR_PRACTICAL_C/04_2.c new file mode 100644 index 0000000..5fa81b3 --- /dev/null +++ b/reference/C/CONTRIB/OR_PRACTICAL_C/04_2.c @@ -0,0 +1,9 @@ +#include <string.h> +#include <stdio.h> +char name[30]; /* First name of someone */ +main() +{ + (void)strcpy(name, "Sam"); /* Initialize the name */ + (void)printf("The name is %s\n", name); + return (0); +} diff --git a/reference/C/CONTRIB/OR_PRACTICAL_C/04_3.c b/reference/C/CONTRIB/OR_PRACTICAL_C/04_3.c new file mode 100644 index 0000000..14ee846 --- /dev/null +++ b/reference/C/CONTRIB/OR_PRACTICAL_C/04_3.c @@ -0,0 +1,20 @@ +#include <string.h> +#include <stdio.h> + +char first[100]; /* first name */ +char last[100]; /* last name */ +char full_name[200]; /* full version of first and last name */ + +main() +{ + (void)strcpy(first, "Steve"); /* Initialize first name */ + (void)strcpy(last, "Oualline"); /* Initialize last name */ + + (void)strcpy(full_name, first); /* full = "Steve" */ + /* Note: strcat not strcpy */ + (void)strcat(full_name, " "); /* full = "Steve " */ + (void)strcat(full_name, last); /* full = "Steve Oualline" */ + + (void)printf("The full name is %s\n", full_name); + return (0); +} diff --git a/reference/C/CONTRIB/OR_PRACTICAL_C/04_4.c b/reference/C/CONTRIB/OR_PRACTICAL_C/04_4.c new file mode 100644 index 0000000..2fa8ced --- /dev/null +++ b/reference/C/CONTRIB/OR_PRACTICAL_C/04_4.c @@ -0,0 +1,13 @@ +#include <string.h> +#include <stdio.h> + +char line[100]; + +main() +{ + (void)printf("Enter a line: "); + (void)fgets(line, sizeof(line), stdin); + + (void)printf("The length of the line is: %d\n", strlen(line)); + return (0); +} diff --git a/reference/C/CONTRIB/OR_PRACTICAL_C/04_5.c b/reference/C/CONTRIB/OR_PRACTICAL_C/04_5.c new file mode 100644 index 0000000..a651145 --- /dev/null +++ b/reference/C/CONTRIB/OR_PRACTICAL_C/04_5.c @@ -0,0 +1,22 @@ +#include <stdio.h> +#include <string.h> + +char first[100]; /* first name of person we are working with */ +char last[100]; /* His last name */ + +/* First and last name of the person (computed) */ +char full[200]; +main() { + (void)printf("Enter first name: "); + (void)fgets(first, sizeof(first), stdin); + + (void)printf("Enter last name: "); + (void)fgets(last, sizeof(last), stdin); + + (void)strcpy(full, first); + (void)strcat(full, " "); + (void)strcat(full, last); + + (void)printf("The name is %s\n", full); + return (0); +} diff --git a/reference/C/CONTRIB/OR_PRACTICAL_C/04_6.c b/reference/C/CONTRIB/OR_PRACTICAL_C/04_6.c new file mode 100644 index 0000000..a6b6e66 --- /dev/null +++ b/reference/C/CONTRIB/OR_PRACTICAL_C/04_6.c @@ -0,0 +1,26 @@ +#include <stdio.h> +#include <string.h> + +char first[100]; /* first name of person we are working with */ +char last[100]; /* His last name */ + +/* First and last name of the person (computed) */ +char full[100]; +main() { + (void)printf("Enter first name: "); + (void)fgets(first, sizeof(first), stdin); + /* trim off last character */ + first[strlen(first)-1] = '\0'; + + (void)printf("Enter last name: "); + (void)fgets(last, sizeof(last), stdin); + /* trim off last character */ + last[strlen(last)-1] = '\0'; + + (void)strcpy(full, first); + (void)strcat(full, " "); + (void)strcat(full, last); + + (void)printf("The name is %s\n", full); + return (0); +} diff --git a/reference/C/CONTRIB/OR_PRACTICAL_C/04_7.c b/reference/C/CONTRIB/OR_PRACTICAL_C/04_7.c new file mode 100644 index 0000000..9116633 --- /dev/null +++ b/reference/C/CONTRIB/OR_PRACTICAL_C/04_7.c @@ -0,0 +1,13 @@ +#include <stdio.h> +char line[100]; /* input line from console */ +int value; /* a value to double */ + +main() +{ + (void) printf("Enter a value: "); + (void) fgets(line, sizeof(line), stdin); + (void) sscanf(line, "%d", &value); + (void) printf("Twice %d is %d\n", + value, value * 2); + return (0); +} diff --git a/reference/C/CONTRIB/OR_PRACTICAL_C/05_1.c b/reference/C/CONTRIB/OR_PRACTICAL_C/05_1.c new file mode 100644 index 0000000..6b5e8f2 --- /dev/null +++ b/reference/C/CONTRIB/OR_PRACTICAL_C/05_1.c @@ -0,0 +1,25 @@ +#include <stdio.h> +int old_number; /* previous Fibonacci number */ + +int current_number; /* current Fibonacci number */ + +int next_number; /* next number in the series */ + +main() +{ + /* start things out */ + old_number = 1; + current_number = 1; + + (void)printf("1\n"); /* Print first number */ + + while (current_number < 100) { + + (void) printf("%d\n", current_number); + next_number = current_number + old_number; + + old_number = current_number; + current_number = next_number; + } + return (0); +} diff --git a/reference/C/CONTRIB/OR_PRACTICAL_C/05_2.c b/reference/C/CONTRIB/OR_PRACTICAL_C/05_2.c new file mode 100644 index 0000000..d1d41ea --- /dev/null +++ b/reference/C/CONTRIB/OR_PRACTICAL_C/05_2.c @@ -0,0 +1,25 @@ +#include <stdio.h> +char line[100];/* line of data for input */ + +/* Running total of all numbers so far */ +int total; + +/* next item to add to the list */ +int item; + +main() +{ + total = 0; + while (1) { + (void) printf("Enter # to add \n"); + (void) printf(" or 0 to stop:"); + (void) fgets(line, sizeof(line), stdin); + (void) sscanf(line, "%d", &item); + if (item == 0) + break; + total += item; + (void) printf("Total: %d\n", total); + } + (void) printf("Final total %d\n", total); + return (0); +} diff --git a/reference/C/CONTRIB/OR_PRACTICAL_C/05_3.c b/reference/C/CONTRIB/OR_PRACTICAL_C/05_3.c new file mode 100644 index 0000000..c3c51e1 --- /dev/null +++ b/reference/C/CONTRIB/OR_PRACTICAL_C/05_3.c @@ -0,0 +1,38 @@ +#include <stdio.h> +char line[100]; /* line from input */ + +/* Running total of all numbers so far */ +int total; + +/* next item to add to the list */ +int item; + +/* number of negative items */ +int minus_items; + +main() +{ + total = 0; + minus_items = 0; + while (1) { + (void) printf("Enter # to add\n"); + (void) printf(" or 0 to stop:"); + (void) fgets(line, sizeof(line), stdin); + (void) sscanf(line, "%d", &item); + + if (item == 0) + break; + + if (item < 0) { + minus_items++; + continue; + } + total += item; + (void) printf("Total: %d\n", total); + } + + (void) printf("Final total %d\n", total); + (void) printf("with %d negative items omitted\n", + minus_items); + return (0); +} diff --git a/reference/C/CONTRIB/OR_PRACTICAL_C/06_1.c b/reference/C/CONTRIB/OR_PRACTICAL_C/06_1.c new file mode 100644 index 0000000..8f889a5 --- /dev/null +++ b/reference/C/CONTRIB/OR_PRACTICAL_C/06_1.c @@ -0,0 +1,25 @@ +#include <stdio.h> +char line[100];/* line of data from the input */ + +int result; /* the result of the calculations */ +char operator; /* operator the user specified */ +int value; /* value specified after the operator */ +main() +{ + result = 0; /* initialize the result */ + + /* Loop forever (or till we hit the break statement) */ + while (1) { + (void) printf("Result: %d\n", result); + + (void) printf("Enter operator and number: "); + (void) fgets(line, sizeof(line), stdin); + (void) sscanf(line, "%c %d", &operator, &value); + + if (operator = '+') { + result += value; + } else { + (void) printf("Unknown operator %c\n", operator); + } + } +} diff --git a/reference/C/CONTRIB/OR_PRACTICAL_C/06_2.c b/reference/C/CONTRIB/OR_PRACTICAL_C/06_2.c new file mode 100644 index 0000000..1bdfb0f --- /dev/null +++ b/reference/C/CONTRIB/OR_PRACTICAL_C/06_2.c @@ -0,0 +1,39 @@ +#include <stdio.h> +char line[100];/* line of text from input */ + +int result; /* the result of the calculations */ +char operator; /* operator the user specified */ +int value; /* value specified after the operator */ +main() +{ + result = 0; /* initialize the result */ + + /* loop forever (or until break reached) */ + while (1) { + (void) printf("Result: %d\n", result); + (void) printf("Enter operator and number: "); + + (void) fgets(line, sizeof(line), stdin); + (void) sscanf(line, "%c %d", &operator, &value); + + if ((operator == 'q') || (operator == 'Q')) + break; + + if (operator == '+') { + result += value; + } else if (operator == '-') { + result -= value; + } else if (operator == '*') { + result *= value; + } else if (operator == '/') { + if (value == 0) { + (void)printf("Error:Divide by zero\n"); + (void)printf(" operation ignored\n"); + } else + result /= value; + } else { + (void) printf("Unknown operator %c\n", operator); + } + } + return (0); +} diff --git a/reference/C/CONTRIB/OR_PRACTICAL_C/06_3.c b/reference/C/CONTRIB/OR_PRACTICAL_C/06_3.c new file mode 100644 index 0000000..bc0598a --- /dev/null +++ b/reference/C/CONTRIB/OR_PRACTICAL_C/06_3.c @@ -0,0 +1,66 @@ +/******************************************************** + * guess -- a simple guessing game * + * * + * Usage: * + * guess * + * * + * A random number is chosen between 1 and 100. * + * The player is given a set of bounds and * + * must choose a number between them. * + * If the player chooses the correct number he wins* + * Otherwise the bounds are adjusted to reflect * + * the player's guess and the game continues. * + * * + * * + * Restrictions: * + * The random number is generated by the statement * + * rand() % 100. Because rand() returns a number * + * 0 <= rand() <= maxint this slightly favors * + * the lower numbers. * + ********************************************************/ +#include <stdio.h> +#include <stdlib.h> /* ANSI Standard only */ +int number_to_guess; /* random number to be guessed */ +int low_limit; /* current lower limit of player's range */ +int high_limit; /* current upper limit of player's range */ +int guess_count; /* number of times player guessed */ +int player_number; /* number gotten from the player */ +char line[80]; /* input buffer for a single line */ +main() +{ + while (1) { + /* + * Not a pure random number, see restrictions + */ + number_to_guess = rand() % 100 + 1; + + /* Initialize variables for loop */ + low_limit = 0; + high_limit = 100; + guess_count = 0; + + while (1) { + /* tell user what the bounds are and get his guess */ + (void) printf("Bounds %d - %d\n", low_limit, high_limit); + (void) printf("Value[%d]? ", guess_count); + + guess_count++; + + (void) fgets(line, sizeof(line), stdin); + (void) sscanf(line, "%d", &player_number); + + /* did he guess right? */ + if (player_number == number_to_guess) + break; + + /* adjust bounds for next guess */ + if (player_number < number_to_guess) + low_limit = player_number; + else + high_limit = player_number; + + } + (void) printf("Bingo\n"); + } + return (0); +} diff --git a/reference/C/CONTRIB/OR_PRACTICAL_C/07_1.c b/reference/C/CONTRIB/OR_PRACTICAL_C/07_1.c new file mode 100644 index 0000000..ab87eb1 --- /dev/null +++ b/reference/C/CONTRIB/OR_PRACTICAL_C/07_1.c @@ -0,0 +1,24 @@ +#include <stdio.h> + +int total; /* total of all the numbers */ +int current; /* current value from the user */ +int counter; /* while loop counter */ + +char line[80]; /* Line from keyboard */ + +main() { + total = 0; + + counter = 0; + while (counter < 5) { + (void)printf("Number? "); + + (void)fgets(line, sizeof(line), stdin); + (void)sscanf(line, "%d", ¤t); + total += current; + + counter++; + } + (void)printf("The grand total is %d\n", total); + return (0); +} diff --git a/reference/C/CONTRIB/OR_PRACTICAL_C/07_2.c b/reference/C/CONTRIB/OR_PRACTICAL_C/07_2.c new file mode 100644 index 0000000..02eedd6 --- /dev/null +++ b/reference/C/CONTRIB/OR_PRACTICAL_C/07_2.c @@ -0,0 +1,20 @@ +#include <stdio.h> + +int total; /* total of all the numbers */ +int current; /* current value from the user */ +int counter; /* for loop counter */ + +char line[80]; /* Input from keyboard */ + +main() { + total = 0; + for (counter = 0; counter < 5; counter++) { + (void)printf("Number? "); + + (void)fgets(line, sizeof(line), stdin); + (void)sscanf(line, "%d", ¤t); + total += current; + } + (void)printf("The grand total is %d\n", total); + return (0); +} diff --git a/reference/C/CONTRIB/OR_PRACTICAL_C/07_3.c b/reference/C/CONTRIB/OR_PRACTICAL_C/07_3.c new file mode 100644 index 0000000..bf66eb7 --- /dev/null +++ b/reference/C/CONTRIB/OR_PRACTICAL_C/07_3.c @@ -0,0 +1,44 @@ +#include <stdio.h> +char line[100]; /* line of text from input */ + +int result; /* the result of the calculations */ +char operator; /* operator the user specified */ +int value; /* value specified after the operator */ +main() +{ + result = 0; /* initialize the result */ + + /* loop forever (or until break reached) */ + while (1) { + (void) printf("Result: %d\n", result); + (void) printf("Enter operator and number: "); + + (void) fgets(line, sizeof(line), stdin); + (void) sscanf(line, "%c %d", &operator, &value); + + if ((operator == 'q') || (operator == 'Q')) + break; + switch (operator) { + case '+': + result += value; + break; + case '-': + result -= value; + break; + case '*': + result *= value; + break; + case '/': + if (value == 0) { + (void)printf("Error:Divide by zero\n"); + (void)printf(" operation ignored\n"); + } else + result /= value; + break; + default: + (void) printf("Unknown operator %c\n", operator); + break; + } + } + return (0); +} diff --git a/reference/C/CONTRIB/OR_PRACTICAL_C/08_1.c b/reference/C/CONTRIB/OR_PRACTICAL_C/08_1.c new file mode 100644 index 0000000..5cd2c0d --- /dev/null +++ b/reference/C/CONTRIB/OR_PRACTICAL_C/08_1.c @@ -0,0 +1,15 @@ +#include <stdio.h> + +main() { + int counter; /* loop counter */ + for (counter = 0; counter < 3; counter++) { + int temporary = 1; + static int permanent = 1; + + (void)printf("Temporary %d Permanent %d\n", + temporary, permanent); + temporary++; + permanent++; + } + return (0); +} diff --git a/reference/C/CONTRIB/OR_PRACTICAL_C/08_2.c b/reference/C/CONTRIB/OR_PRACTICAL_C/08_2.c new file mode 100644 index 0000000..9adbad0 --- /dev/null +++ b/reference/C/CONTRIB/OR_PRACTICAL_C/08_2.c @@ -0,0 +1,30 @@ +#include <cstdio.h> + +main() +{ + /* Compute a triangle */ + float triangle(float width, float height); + + (void)printf("Triangle #1 %f\n", triangle(1.3, 8.3)); + (void)printf("Triangle #2 %f\n", triangle(4.8, 9.8)); + (void)printf("Triangle #3 %f\n", triangle(1.2, 2.O)); + return (0); +} + +/******************************************** + * triangle -- compute area of a triangle * + * * + * Parameters * + * width -- width of the triangle * + * height -- height of the triangle * + * * + * Returns * + * area of the triangle * + ********************************************/ +float triangle(float width, float height) +{ + float area; /* Area of the triangle */ + + area = width * height / 2.0; + return (area); +} diff --git a/reference/C/CONTRIB/OR_PRACTICAL_C/08_3.c b/reference/C/CONTRIB/OR_PRACTICAL_C/08_3.c new file mode 100644 index 0000000..839c008 --- /dev/null +++ b/reference/C/CONTRIB/OR_PRACTICAL_C/08_3.c @@ -0,0 +1,7 @@ +main() +{ + + init(); + solve_problems(); + finish_up(); +} diff --git a/reference/C/CONTRIB/OR_PRACTICAL_C/09_1.c b/reference/C/CONTRIB/OR_PRACTICAL_C/09_1.c new file mode 100644 index 0000000..805521b --- /dev/null +++ b/reference/C/CONTRIB/OR_PRACTICAL_C/09_1.c @@ -0,0 +1,13 @@ +int data[10]; /* some data */ +int twice[10]; /* twice some data */ + +main() +{ + int index; /* index into the data */ + + for (index = 0; index < 10; index++) { + data[index] = index; + twice[index] = index * 2; + } + return (0); +} diff --git a/reference/C/CONTRIB/OR_PRACTICAL_C/09_2.c b/reference/C/CONTRIB/OR_PRACTICAL_C/09_2.c new file mode 100644 index 0000000..22c4b2d --- /dev/null +++ b/reference/C/CONTRIB/OR_PRACTICAL_C/09_2.c @@ -0,0 +1,15 @@ +#define SIZE 20 /* work on 20 elements */ + +int data[SIZE]; /* some data */ +int twice[SIZE]; /* twice some data */ + +main() +{ + int index; /* index into the data */ + + for (index = 0; index < SIZE; index++) { + data[index] = index; + twice[index] = index * 2; + } + return (0); +} diff --git a/reference/C/CONTRIB/OR_PRACTICAL_C/09_3.c b/reference/C/CONTRIB/OR_PRACTICAL_C/09_3.c new file mode 100644 index 0000000..915ada7 --- /dev/null +++ b/reference/C/CONTRIB/OR_PRACTICAL_C/09_3.c @@ -0,0 +1,16 @@ +#define BIG_NUMBER 10 ** 10 + +main() +{ + /* index for our calculations */ + int index; + + index = 0; + + /* syntax error on next line */ + while (index < BIG_NUMBER) { + index = index * 8; + } + return (0); +} + diff --git a/reference/C/CONTRIB/OR_PRACTICAL_C/09_4.c b/reference/C/CONTRIB/OR_PRACTICAL_C/09_4.c new file mode 100644 index 0000000..cda0066 --- /dev/null +++ b/reference/C/CONTRIB/OR_PRACTICAL_C/09_4.c @@ -0,0 +1,13 @@ +#include <stdio.h> +#define SQR(x) (x * x) + +main() +{ + int counter; /* counter for loop */ + + for (counter = 0; counter < 5; counter++) { + (void)printf("x %d, x squared %d\n", + counter+1, SQR(counter+1)); + } + return (0); +} diff --git a/reference/C/CONTRIB/OR_PRACTICAL_C/09_5.c b/reference/C/CONTRIB/OR_PRACTICAL_C/09_5.c new file mode 100644 index 0000000..6428f43 --- /dev/null +++ b/reference/C/CONTRIB/OR_PRACTICAL_C/09_5.c @@ -0,0 +1,13 @@ +#include <stdio.h> +#define SQR(x) ((x) * (x)) + +main() +{ + int counter; /* counter for loop */ + + counter = 0; + while (counter < 5) + (void)printf("x %d square %d\n", + counter, SQR(counter++)); + return (0); +} diff --git a/reference/C/CONTRIB/OR_PRACTICAL_C/10_1.c b/reference/C/CONTRIB/OR_PRACTICAL_C/10_1.c new file mode 100644 index 0000000..3dac7ad --- /dev/null +++ b/reference/C/CONTRIB/OR_PRACTICAL_C/10_1.c @@ -0,0 +1,46 @@ +#include <stdio.h> + +#define X_SIZE 40 /* size of array in the X direction */ +#define Y_SIZE 60 /* size of the array in Y direction */ +/* + * We use X_SIZE/8 since we pack 8 bits per byte + */ +char graphics[X_SIZE / 8][Y_SIZE]; /* the graphics data */ + +#define SET_BIT(x,y) graphics[(x)/8][y] |= (0x80 >>((x)%8)) + +main() +{ + int loc; /* current location we are setting */ + void print_graphics(void); /* print the data */ + + for (loc = 0; loc < X_SIZE; loc++) + SET_BIT(loc, loc); + + print_graphics(); + return (0); +} +/******************************************************** + * print_graphics -- print the graphics bit array * + * as a set of X and .'s. * + ********************************************************/ +void print_graphics(void) +{ + int x; /* current x BYTE */ + int y; /* current y location */ + int bit; /* bit we are testing in the current byte */ + + for (y = 0; y < Y_SIZE; y++) { + /* Loop for each byte in the array */ + for (x = 0; x < X_SIZE / 8; x++) { + /* Handle each bit */ + for (bit = 0x80; bit > 0; bit = (bit >> 1)) { + if ((graphics[x][y] & bit) != 0) + (void) printf("X"); + else + (void) printf("."); + } + } + (void) printf("\n"); + } +} diff --git a/reference/C/CONTRIB/OR_PRACTICAL_C/11_1.c b/reference/C/CONTRIB/OR_PRACTICAL_C/11_1.c new file mode 100644 index 0000000..cc1ce51 --- /dev/null +++ b/reference/C/CONTRIB/OR_PRACTICAL_C/11_1.c @@ -0,0 +1,19 @@ +/* + * Define a variable to hold an integer or + * a real number (but not both) + */ +union value { + long int i_value; /* The real number */ + float f_value; /* The floating-point number */ +} data; +int i; /* Random integer */ +float f; /* Random floating-point number */ +main() +{ + data.f_value = 5.0; + data.i_value = 3; /* data.f_value overwritten */ + i = data.i_value; /* legal */ + f = data.f_value; /* not legal, will generate unexpected results */ + data.f_value = 5.5; /* put something in f_value/clobber i_value */ + i = data.i_value; /* not legal, will generate unexpected results */ +} diff --git a/reference/C/CONTRIB/OR_PRACTICAL_C/12_1.c b/reference/C/CONTRIB/OR_PRACTICAL_C/12_1.c new file mode 100644 index 0000000..fa15a49 --- /dev/null +++ b/reference/C/CONTRIB/OR_PRACTICAL_C/12_1.c @@ -0,0 +1,18 @@ +#include <stdio.h> +main() +{ + int thing_var; /* define a variable for thing */ + int *thing_ptr; /* define a pointer to thing */ + + thing_var = 2; /* assigning a value to thing */ + (void)printf("Thing %d\n", thing_var); + + thing_ptr = &thing_var; /* make the pointer point to thing */ + *thing_ptr = 3; /* thing_ptr points to thing_var so */ + /* thing_var changes to 3 */ + (void)printf("Thing %d\n", thing_var); + + /* another way of doing the printf */ + (void)printf("Thing %d\n", *thing_ptr); + return (0); +} diff --git a/reference/C/CONTRIB/OR_PRACTICAL_C/12_2.c b/reference/C/CONTRIB/OR_PRACTICAL_C/12_2.c new file mode 100644 index 0000000..1ca14c0 --- /dev/null +++ b/reference/C/CONTRIB/OR_PRACTICAL_C/12_2.c @@ -0,0 +1,17 @@ +#include <stdio.h> +main() +{ + int count = 0; /* number of times through */ + + void inc_count(int *);/* update the counter */ + + while (count < 10) + inc_count(&count); + + return (0); +} + +void inc_count(int *count_ptr) +{ + (*count_ptr)++; +} diff --git a/reference/C/CONTRIB/OR_PRACTICAL_C/12_3.c b/reference/C/CONTRIB/OR_PRACTICAL_C/12_3.c new file mode 100644 index 0000000..f608eb3 --- /dev/null +++ b/reference/C/CONTRIB/OR_PRACTICAL_C/12_3.c @@ -0,0 +1,15 @@ +#define ARRAY_SIZE 10 /* Number of characters in array */ +/* Array to print */ +char array[ARRAY_SIZE] = "012345678"; + +main() +{ + int index; /* Index into the array */ + + for (index = 0; index < ARRAY_SIZE; index++) { + (void)printf( + "&array[index]=0x%x (array+index)=0x%x array[index]=0x%x\n", + &array[index], (array+index), array[index]); + } + return (0); +} diff --git a/reference/C/CONTRIB/OR_PRACTICAL_C/12_4.c b/reference/C/CONTRIB/OR_PRACTICAL_C/12_4.c new file mode 100644 index 0000000..608ac9f --- /dev/null +++ b/reference/C/CONTRIB/OR_PRACTICAL_C/12_4.c @@ -0,0 +1,15 @@ +#include <stdio.h> + +int array[10] = {4, 5, 8, 9, 8, 1, 0, 1, 9, 3}; +int index; + +main() +{ + index = 0; + while (array[index] != 0) + index++; + + (void) printf("Number of elements before zero %d\n", + index); + return (0); +} diff --git a/reference/C/CONTRIB/OR_PRACTICAL_C/12_5.c b/reference/C/CONTRIB/OR_PRACTICAL_C/12_5.c new file mode 100644 index 0000000..f1db660 --- /dev/null +++ b/reference/C/CONTRIB/OR_PRACTICAL_C/12_5.c @@ -0,0 +1,16 @@ +#include <stdio.h> + +int array[10] = {4, 5, 8, 9, 8, 1, 0, 1, 9, 3}; +int *array_ptr; + +main() +{ + array_ptr = array; + + while ((*array_ptr) != 0) + array_ptr++; + + (void) printf("Number of elements before zero %d\n", + array_ptr - array); + return (0); +} diff --git a/reference/C/CONTRIB/OR_PRACTICAL_C/12_6.c b/reference/C/CONTRIB/OR_PRACTICAL_C/12_6.c new file mode 100644 index 0000000..7667200 --- /dev/null +++ b/reference/C/CONTRIB/OR_PRACTICAL_C/12_6.c @@ -0,0 +1,50 @@ +#define MAX 10 +main() +{ + int array[MAX]; + + void init_array_1(); + void init_array_2(); + + /* one way of initializing the array */ + init_array_1(array); + + /* another way of initializing the array */ + init_array_1(&array[0]); + + /* works, but the compiler generates a warning */ + init_array_1(&array); + + /* Similar to the first method but */ + /* function is different */ + init_array_2(array); + + return (0); +} +/******************************************************** + * init_array_1 -- Zero out an array * + * * + * Parameters * + * data -- the array to zero * + ********************************************************/ +void init_array_1(int data[]) +{ + int index; + + for (index = 0; index < MAX; index++) + data[index] = 0; +} + +/******************************************************** + * init_array_2 -- Zero out an array * + * * + * Parameters * + * data_ptr -- pointer to array to zero * + ********************************************************/ +void init_array_2(int *data_ptr) +{ + int index; + + for (index = 0; index < MAX; index++) + *(data_ptr + index) = 0; +} diff --git a/reference/C/CONTRIB/OR_PRACTICAL_C/12_7.c b/reference/C/CONTRIB/OR_PRACTICAL_C/12_7.c new file mode 100644 index 0000000..aac1173 --- /dev/null +++ b/reference/C/CONTRIB/OR_PRACTICAL_C/12_7.c @@ -0,0 +1,65 @@ +/******************************************************** + * split -- split an entry of the form Last/First * + * into two parts. * + ********************************************************/ +#include <stdio.h> +#include <string.h> + +/* Our version of the library function */ +char *strchr(char *, char); + +main() +{ + char line[80]; /* The input line */ + char *first_ptr; /* pointer to the first name */ + char *last_ptr; /* pointer to the last name */ + + (void)fgets(line, sizeof(line), stdin); + + /* Get rid of trailing newline */ + line[strlen(line)-1] = '\0'; + + last_ptr = line; /* last name is at beginning of line */ + + first_ptr = strchr(line, '/'); /* Find slash */ + + /* Check for an error */ + if (first_ptr == NULL) { + (void)fprintf(stderr, + "Error: Unable to find slash in %s\n", line); + exit (8); + } + + *first_ptr = '\0'; /* Zero out the slash */ + + first_ptr++; /* Move to first character of name */ + + (void)printf("First:%s Last:%s\n", first_ptr, last_ptr); + return (0); +} +/******************************************************** + * strchr -- find a character in a string * + * Duplicate of a standard library function, * + * put here for illustrative purposes. * + * * + * Parameters * + * string_ptr -- string to look through * + * find -- character to find * + * * + * Returns * + * pointer to 1st occurrence of character * + * in string or NULL for error * + ********************************************************/ +char *strchr(char * string_ptr, char find) +{ + while (*string_ptr != find) { + + /* Check for end */ + + if (*string_ptr == '\0') + return (NULL); /* not found */ + + string_ptr++; + } + return (string_ptr); /* Found */ +} diff --git a/reference/C/CONTRIB/OR_PRACTICAL_C/12_8.c b/reference/C/CONTRIB/OR_PRACTICAL_C/12_8.c new file mode 100644 index 0000000..500683b --- /dev/null +++ b/reference/C/CONTRIB/OR_PRACTICAL_C/12_8.c @@ -0,0 +1,103 @@ +/******************************************************** + * print -- format files for printing * + ********************************************************/ +#include <stdio.h> +#include <stdlib.h> /* ANSI Standard only */ + +int verbose = 0; /* verbose mode (default = false) */ +char *out_file = "print.out"; /* output filename */ +char *program_name; /* name of the program (for errors) */ +int line_max = 66; /* number of lines per page */ + +main(int argc, char *argv[]) +{ + void do_file(char *); /* print a file */ + void usage(void); /* tell user how to use the program */ + + /* save the program name for future use */ + program_name = argv[0]; + + /* + * loop for each option. + * Stop if we run out of arguments + * or we get an argument without a dash. + */ + while ((argc > 1) && (argv[1][0] == '-')) { + /* + * argv[1][1] is the actual option character. + */ + switch (argv[1][1]) { + /* + * -v verbose + */ + case 'v': + verbose = 1; + break; + /* + * -o<name> output file + * [0] is the dash + * [1] is the "o" + * [2] starts the name + */ + case 'o': + out_file = &argv[1][2]; + break; + /* + * -l<number> set max number of lines + */ + case 'l': + line_max = atoi(&argv[1][2]); + break; + default: + (void)fprintf(stderr,"Bad option %s\n", argv[1]); + usage(); + } + /* + * move the argument list up one + * move the count down one + */ + argv++; + argc--; + } + + /* + * At this point all the options have been processed. + * Check to see if we have no files in the list + * and if so, we need to process just standard in. + */ + if (argc == 1) { + do_file("print.in"); + } else { + while (argc > 1) { + do_file(argv[1]); + argv++; + argc--; + } + } + return (0); +} +/******************************************************** + * do_file -- dummy routine to handle a file * + * * + * Parameter * + * name -- name of the file to print * + ********************************************************/ +void do_file(char *name) +{ + (void)printf("Verbose %d Lines %d Input %s Output %s\n", + verbose, line_max, name, out_file); +} +/******************************************************** + * usage -- tell the user how to use this program and * + * exit * + ********************************************************/ +void usage(void) +{ + (void)fprintf(stderr,"Usage is %s [options] [file-list]\n", + program_name); + (void)fprintf(stderr,"Options\n"); + (void)fprintf(stderr," -v verbose\n"); + (void)fprintf(stderr," -l<number> Number of lines\n"); + (void)fprintf(stderr," -o<name> Set output filename\n"); + exit (8); +} diff --git a/reference/C/CONTRIB/OR_PRACTICAL_C/13_1.c b/reference/C/CONTRIB/OR_PRACTICAL_C/13_1.c new file mode 100644 index 0000000..4f44233 --- /dev/null +++ b/reference/C/CONTRIB/OR_PRACTICAL_C/13_1.c @@ -0,0 +1,30 @@ +#include <stdio.h> +#define FILE_NAME "input.txt" +#include <stdlib.h> /* ANSI Standard C file */ + +main() +{ + int count = 0; /* number of characters seen */ + FILE *in_file; /* input file */ + + /* character or EOF flag from input */ + int ch; + + in_file = fopen(FILE_NAME, "r"); + if (in_file == NULL) { + (void)printf("Can not open %s\n", FILE_NAME); + exit(8); + } + + while (1) { + ch = fgetc(in_file); + if (ch == EOF) + break; + count++; + } + (void) printf("Number of characters in %s is %d\n", + FILE_NAME, count); + + (void) fclose(in_file); + return (0); +} diff --git a/reference/C/CONTRIB/OR_PRACTICAL_C/13_2.c b/reference/C/CONTRIB/OR_PRACTICAL_C/13_2.c new file mode 100644 index 0000000..a1dd50c --- /dev/null +++ b/reference/C/CONTRIB/OR_PRACTICAL_C/13_2.c @@ -0,0 +1,70 @@ +/**************************************** + * copy -- copy one file to another. * + * * + * Usage * + * copy <from> <to> * + * * + * <from> -- the file to copy from * + * <to> -- the file to copy into * + ****************************************/ +#include <stdio.h> +#ifndef __MSDOS__ /* if we are not MS-DOS */ +#define __UNIX__ /* then we are UNIX */ +#endif __MSDOS__ + +#include <stdlib.h> /* ANSI Standard C file */ + +#ifdef __UNIX__ +#include <sys/types.h> /* file defines for UNIX filesystem */ +#include <sys/stat.h> +#include <fcntl.h> +#endif __UNIX__ + +#ifdef __MSDOS__ +#include <fcntl.h> /* file defines for DOS filesystem */ +#include <sys\stat.h> +#include <io.h> +#endif __MSDOS__ + +void exit(); /* lib routine */ + +#define BUFFER_SIZE (16 * 1024) /* use 16K buffers */ + +main(int argc, char *argv[]) +{ + char buffer[BUFFER_SIZE]; /* buffer for data */ + int in_file; /* input file descriptor */ + int out_file; /* output file descriptor */ + int read_size; /* number of bytes on last read */ + + if (argc != 3) { + (void) fprintf(stderr, "Error:Wrong number of arguments\n"); + (void) fprintf(stderr, "Usage is: copy <from> <to>\n"); + exit(8); + } + in_file = open(argv[1], O_RDONLY); + if (in_file < 0) { + (void) fprintf("Error:Unable to open %s\n", argv[1]); + exit(8); + } + out_file = open(argv[2], O_WRONLY | O_TRUNC | O_CREAT, 0666); + if (out_file < 0) { + (void) fprintf("Error:Unable to open %s\n", argv[2]); + exit(8); + } + while (1) { + read_size = read(in_file, buffer, sizeof(buffer)); + + if (read_size == 0) + break; /* end of file */ + + if (read_size < 0) { + (void) fprintf(stderr, "Error:Read error\n"); + exit(8); + } + (void) write(out_file, buffer, (unsigned int) read_size); + } + (void) close(in_file); + (void) close(out_file); + return (0); +} diff --git a/reference/C/CONTRIB/OR_PRACTICAL_C/14_01.c b/reference/C/CONTRIB/OR_PRACTICAL_C/14_01.c new file mode 100644 index 0000000..43c1937 --- /dev/null +++ b/reference/C/CONTRIB/OR_PRACTICAL_C/14_01.c @@ -0,0 +1,70 @@ +/******************************************************** + * Database -- A very simple database program to * + * look up names in a hardcoded list. * + * * + * Usage: * + * database * + * Program will ask you for a name. * + * Enter the name; it will tell you if * + * it is the list. * + * * + * A blank name terminates the program. * + ********************************************************/ +#define STRING_LENGTH 80 /* Length of typical string */ +#include <stdio.h> + +main() +{ + char name[STRING_LENGTH]; /* a name to lookup */ + + int lookup(char *); /* lookup a name */ + + while (1) { + (void)printf("Enter name: "); + (void)fgets(name, sizeof(name), stdin); + + /* Check for blank name */ + /* (remember 1 character for newline) */ + if (strlen(name) <= 1) + break; + + /* Get rid of newline */ + name[strlen(name)-1] = '\0'; + + if (lookup(name)) + (void)printf("%s is in the list\n", name); + else + (void)printf("%s is not in the list\n", name); + } + return (0); +} +/******************************************************** + * lookup -- lookup a name in a list * + * * + * Parameters * + * name -- name to lookup * + * * + * Returns * + * 1 -- name in the list * + * 0 -- name not in the list * + ********************************************************/ +int lookup(char *name) +{ + /* List of people in the database */ + /* Note: Last name is a NULL for end of list */ + static char *list[] = { + "John", + "Jim", + "Jane", + "Clyde", + NULL + }; + + int index; /* index into list */ + + for (index = 0; list[index] != NULL; index++) { + if (strcmp(list[index], name) == 0) + return (1); + } + return (0); +} diff --git a/reference/C/CONTRIB/OR_PRACTICAL_C/14_02.c b/reference/C/CONTRIB/OR_PRACTICAL_C/14_02.c new file mode 100644 index 0000000..a88a33d --- /dev/null +++ b/reference/C/CONTRIB/OR_PRACTICAL_C/14_02.c @@ -0,0 +1,34 @@ +#include <stdio.h> +/* + * The main program will open this file if -S is on + * the command line. + */ +FILE *save_file = NULL; +/******************************************************** + * extended_fgets -- get a line from the input file * + * and record it in a save file if needed. * + * * + * Parameters * + * line -- the line to read * + * size -- sizeof(line) -- maximum number of * + * characters to read * + * file -- file to read data from * + * (normally stdin) * + * * + * Returns * + * NULL -- error or end of file in read * + * otherwise line (just like fgets) * + ********************************************************/ +char *extended_fgets(char *line, int size, FILE *file) +{ + + char *result; /* result of fgets */ + + result = fgets(line, size, file); + + /* did someone ask for a save file */ + if (save_file != NULL) + (void)fputs(line, save_file); + + return (result); +} diff --git a/reference/C/CONTRIB/OR_PRACTICAL_C/14_03.c b/reference/C/CONTRIB/OR_PRACTICAL_C/14_03.c new file mode 100644 index 0000000..0c9151b --- /dev/null +++ b/reference/C/CONTRIB/OR_PRACTICAL_C/14_03.c @@ -0,0 +1,52 @@ +/******************************************************** + * Database -- A very simple database program to * + * lookup names in a hardcoded list. * + * * + * Usage: * + * database [-S<file>] * + * * + * -S<file> Specify save file for * + * debugging purposes * + * * + * Program will ask you for a name. * + * Enter the name; it will tell you if * + * it is the list. * + * * + * A blank name terminates the program. * + ********************************************************/ +#include <stdio.h> + +FILE *save_file = NULL; /* Save file if any */ +char *extended_fgets(char *, int, FILE *); + +main(int argc, char *argv[]) +{ + char name[80]; /* a name to lookup */ + char *save_file_name; /* Name of the save file */ + + int lookup(char *); /* lookup a name */ + + while ((argc > 1) && (argv[1][0] == '-')) { + switch (argv[1][1]) { + case 'S': + save_file_name = &argv[1][2]; + save_file = fopen(save_file_name, "w"); + if (save_file == NULL) + (void)fprintf(stderr, + "Warning:Unable to open save file %s\n", + save_file_name); + break; + default: + (void)fprintf(stderr,"Bad option: %s\n", argv[1]); + exit (8); + } + argc--; + argv++; + } + + + while (1) { + (void)printf("Enter name: "); + (void)extended_fgets(name, sizeof(name), stdin); + + /* ... Rest of program ... */ diff --git a/reference/C/CONTRIB/OR_PRACTICAL_C/14_04.c b/reference/C/CONTRIB/OR_PRACTICAL_C/14_04.c new file mode 100644 index 0000000..ca5c709 --- /dev/null +++ b/reference/C/CONTRIB/OR_PRACTICAL_C/14_04.c @@ -0,0 +1,38 @@ +#include <stdio.h> +FILE *save_file = NULL; /* Save input in this file */ +FILE *playback_file = NULL; /* Playback data from this file */ +/******************************************************** + * extended_fgets -- get a line from the input file * + * and record it in a save file if needed * + * * + * Parameters * + * line -- the line to read * + * size -- sizeof(line) -- maximum number of * + * characters to read * + * file -- file to read data from * + * (normally stdin) * + * * + * Returns * + * NULL -- error or end of file in read * + * otherwise line (just like fgets) * + ********************************************************/ +char *extended_fgets(char *line, int size, FILE *file) +{ + extern FILE *save_file; /* file to save strings in */ + extern FILE *playback_file; /* file for alternate input */ + + char *result; /* result of fgets */ + + if (playback_file != NULL) { + result = fgets(line, size, file); + /* echo the input to the standard out so the user sees it */ + (void)fputs(line, stdout); + } else + result = fgets(line, size, file); + + /* did someone ask for a save file */ + if (save_file != NULL) + (void)fputs(line, save_file); + + return (result); +} diff --git a/reference/C/CONTRIB/OR_PRACTICAL_C/14_05.c b/reference/C/CONTRIB/OR_PRACTICAL_C/14_05.c new file mode 100644 index 0000000..5e04459 --- /dev/null +++ b/reference/C/CONTRIB/OR_PRACTICAL_C/14_05.c @@ -0,0 +1,63 @@ +/******************************************************** + * Database -- A very simple database program to * + * lookup names in a hardcoded list. * + * * + * Usage: * + * database [-S<file>] [-P<file>] * + * * + * -S<file> Specify save file for * + * debugging purposes. * + * * + * -P<file> Specify playback file for * + * debugging or demonstration. * + * * + * * + * Program will ask you for a name. * + * Enter the name; it will tell you if * + * it is the list. * + * * + * A blank name terminates the program. * + ********************************************************/ +#include <stdio.h> + +FILE *save_file = NULL; /* Save file if any */ +FILE *playback_file = NULL; /* Playback file if any */ +char *extended_fgets(char *, int, FILE *); + +main(int argc, char *argv[]) +{ + char name[80]; /* a name to lookup */ + char *save_file_name; /* Name of the save file */ + char *playback_file_name; /* Name of the playback file */ + + int lookup(char *); /* lookup a name */ + + while ((argc > 1) && (argv[1][0] == '-')) { + switch (argv[1][1]) { + case 'S': + save_file_name = &argv[1][2]; + save_file = fopen(save_file_name, "w"); + if (save_file == NULL) + (void)fprintf(stderr, + "Warning:Unable to open save file %s\n", + save_file_name); + break; + case 'P': + playback_file_name = &argv[1][2]; + playback_file = fopen(playback_file_name, "r"); + if (playback_file == NULL) { + (void)fprintf(stderr, + "Error:Unable to open playback file %s\n", + playback_file_name); + exit (8); + } + break; + default: + (void)fprintf(stderr,"Bad option: %s\n", argv[1]); + exit (8); + } + argc--; + argv++; + } + + /* ... rest of program ... */ diff --git a/reference/C/CONTRIB/OR_PRACTICAL_C/14_06.c b/reference/C/CONTRIB/OR_PRACTICAL_C/14_06.c new file mode 100644 index 0000000..5dca028 --- /dev/null +++ b/reference/C/CONTRIB/OR_PRACTICAL_C/14_06.c @@ -0,0 +1,37 @@ +#include <stdio.h> +int seven_count; /* number of seven's in the data */ +int data[5]; /* the data to count 3 and 7 in */ +int three_count; /* the number of threes in the data */ + +main() { + int index; /* index into the data */ + void get_data(int data[]); + + seven_count = 0; + three_count = 0; + get_data(data); + + for (index = 1; index <= 5; index++) { + if (data[index] == 3) + three_count++; + if (data[index] == 7) + seven_count++; + } + (void)printf("Three's %d Seven's %d\n", + three_count, seven_count); + return (0); +} +/******************************************************** + * get_data -- get 5 numbers from the command line * + ********************************************************/ +void get_data(int data[]) +{ + char line[100]; /* line of input */ + + (void)printf("Enter 5 numbers\n"); + (void)fgets(line, sizeof(line), stdin); + (void)sscanf(line, "%d %d %d %d %d", + &data[1], &data[2], &data[3], + &data[4], &data[5]); +} + diff --git a/reference/C/CONTRIB/OR_PRACTICAL_C/14_07.c b/reference/C/CONTRIB/OR_PRACTICAL_C/14_07.c new file mode 100644 index 0000000..e2a5b52 --- /dev/null +++ b/reference/C/CONTRIB/OR_PRACTICAL_C/14_07.c @@ -0,0 +1,76 @@ +/******************************************************** + * search -- Search a set of numbers. * + * * + * Usage: * + * search * + * you will be asked numbers to lookup * + * * + * Files: * + * numbers.dat -- numbers 1 per line to search * + * (Numbers must be ordered) * + ********************************************************/ +#include <stdio.h> +#define MAX_NUMBERS 1000 /* Max numbers in file */ +#define DATA_FILE "numbers.dat" /* File with numbers */ + +int data[MAX_NUMBERS]; /* Array of numbers to search */ +int max_count; /* Number of valid elements in data */ +main() +{ + FILE *in_file; /* Input file */ + int middle; /* Middle of our search range */ + int low, high; /* Upper/lower bound */ + int search; /* number to search for */ + char line[80]; /* Input line */ + + in_file = fopen(DATA_FILE, "r"); + if (in_file == NULL) { + (void)fprintf(stderr,"Error:Unable to open %s\n", DATA_FILE); + exit (8); + } + + /* + * Read in data + */ + + max_count = 0; + while (1) { + if (fgets(line, sizeof(line), in_file) == NULL) + break; + + /* convert number */ + (void)sscanf(line, "%d", data[max_count]); + max_count++; + } + + while (1) { + (void)printf("Enter number to search for or -1 to quit:" ); + (void)fgets(line, sizeof(line), stdin); + (void)sscanf(line, "%d", &search); + + if (search == -1) + break; + + low = 0; + high = max_count; + + while (1) { + middle = (low + high) / 2; + + if (data[middle] == search) { + (void)printf("Found at index %d\n", middle); + } + + if (low == high) { + (void)printf("Not found\n"); + break; + } + + if (data[middle] < search) + low = middle; + else + high = middle; + } + } + return (0); +} diff --git a/reference/C/CONTRIB/OR_PRACTICAL_C/14_08.c b/reference/C/CONTRIB/OR_PRACTICAL_C/14_08.c new file mode 100644 index 0000000..917c702 --- /dev/null +++ b/reference/C/CONTRIB/OR_PRACTICAL_C/14_08.c @@ -0,0 +1,77 @@ +/******************************************************** + * search -- Search a set of numbers. * + * * + * Usage: * + * search * + * you will be asked numbers to lookup * + * * + * Files: * + * numbers.dat -- numbers 1 per line to search * + * (Numbers must be ordered) * + ********************************************************/ +#include <stdio.h> +#define MAX_NUMBERS 1000 /* Max numbers in file */ +#define DATA_FILE "numbers.dat" /* File with numbers */ + +int data[MAX_NUMBERS]; /* Array of numbers to search */ +int max_count; /* Number of valid elements in data */ +main() +{ + FILE *in_file; /* Input file */ + int middle; /* Middle of our search range */ + int low, high; /* Upper/lower bound */ + int search; /* number to search for */ + char line[80]; /* Input line */ + + in_file = fopen(DATA_FILE, "r"); + if (in_file == NULL) { + (void)fprintf(stderr,"Error:Unable to open %s\n", DATA_FILE); + exit (8); + } + + /* + * Read in data + */ + + max_count = 0; + while (1) { + if (fgets(line, sizeof(line), in_file) == NULL) + break; + + /* convert number */ + (void)sscanf(line, "%d", &data[max_count]); + max_count++; + } + + while (1) { + (void)printf("Enter number to search for or -1 to quit:" ); + (void)fgets(line, sizeof(line), stdin); + (void)sscanf(line, "%d", &search); + + if (search == -1) + break; + + low = 0; + high = max_count; + + while (1) { + if (low >= high) { + (void)printf("Not found\n"); + break; + } + + middle = (low + high) / 2; + + if (data[middle] == search) { + (void)printf("Found at index %d\n", middle); + break; + } + + if (data[middle] < search) + low = middle +1; + else + high = middle -1; + } + } + return (0); +} diff --git a/reference/C/CONTRIB/OR_PRACTICAL_C/14_09.c b/reference/C/CONTRIB/OR_PRACTICAL_C/14_09.c new file mode 100644 index 0000000..6ca905d --- /dev/null +++ b/reference/C/CONTRIB/OR_PRACTICAL_C/14_09.c @@ -0,0 +1,12 @@ + +float point_color(point_number) +int point_number; +{ + float correction; /* color correction factor */ + extern float red,green,blue;/* current colors */ + + correction = lookup(point_number); + return (red*correction * 100.0 + + blue*correction * 10.0 + + green*correction); +} diff --git a/reference/C/CONTRIB/OR_PRACTICAL_C/14_10.c b/reference/C/CONTRIB/OR_PRACTICAL_C/14_10.c new file mode 100644 index 0000000..987b213 --- /dev/null +++ b/reference/C/CONTRIB/OR_PRACTICAL_C/14_10.c @@ -0,0 +1,13 @@ +#include <stdio.h> +main() +{ + int i,j; /* two random integers */ + + i = 1; + j = 0; + (void)printf("Starting\n"); + (void)printf("Before divide..."); + i = i / j; /* divide by zero error */ + (void)printf("After\n"); + return(0); +} diff --git a/reference/C/CONTRIB/OR_PRACTICAL_C/14_11.c b/reference/C/CONTRIB/OR_PRACTICAL_C/14_11.c new file mode 100644 index 0000000..b3f2233 --- /dev/null +++ b/reference/C/CONTRIB/OR_PRACTICAL_C/14_11.c @@ -0,0 +1,16 @@ +#include <stdio.h> +main() +{ + int i,j; /* two random integers */ + + i = 1; + j = 0; + (void)printf("Starting\n"); + (void)fflush(stdout); + (void)printf("Before divide..."); + (void)fflush(stdout); + i = i / j; /* divide by zero error */ + (void)printf("After\n"); + (void)fflush(stdout); + return(0); +} diff --git a/reference/C/CONTRIB/OR_PRACTICAL_C/14_12.c b/reference/C/CONTRIB/OR_PRACTICAL_C/14_12.c new file mode 100644 index 0000000..c97d54e --- /dev/null +++ b/reference/C/CONTRIB/OR_PRACTICAL_C/14_12.c @@ -0,0 +1,15 @@ +#define X_SIZE 60 +#define Y_SIZE 30 + +int matrix[X_SIZE][Y_SIZE]; + +void init_matrix(void) +{ + int x,y; /* current element to zero */ + + for (x = 0; x < X_SIZE; x++) { + for (y = 0; y < Y_SIZE; y++) { + matrix[x][y] = -1; + } + } +} diff --git a/reference/C/CONTRIB/OR_PRACTICAL_C/14_13.c b/reference/C/CONTRIB/OR_PRACTICAL_C/14_13.c new file mode 100644 index 0000000..0f7c4dd --- /dev/null +++ b/reference/C/CONTRIB/OR_PRACTICAL_C/14_13.c @@ -0,0 +1,15 @@ +#define X_SIZE 60 +#define Y_SIZE 30 + +int matrix[X_SIZE][Y_SIZE]; + +void init_matrix(void) +{ + register int x,y; /* current element to zero */ + + for (x = 0; x < X_SIZE; x++) { + for (y = 0; y < Y_SIZE; y++) { + matrix[x][y] = -1; + } + } +} diff --git a/reference/C/CONTRIB/OR_PRACTICAL_C/14_14.c b/reference/C/CONTRIB/OR_PRACTICAL_C/14_14.c new file mode 100644 index 0000000..01a45bb --- /dev/null +++ b/reference/C/CONTRIB/OR_PRACTICAL_C/14_14.c @@ -0,0 +1,15 @@ +#define X_SIZE 60 +#define Y_SIZE 30 + +int matrix[X_SIZE][Y_SIZE]; + +void init_matrix(void) +{ + register int x,y; /* current element to zero */ + + for (y = 0; y < Y_SIZE; y++) { + for (x = 0; x < X_SIZE; x++) { + matrix[x][y] = -1; + } + } +} diff --git a/reference/C/CONTRIB/OR_PRACTICAL_C/14_15.c b/reference/C/CONTRIB/OR_PRACTICAL_C/14_15.c new file mode 100644 index 0000000..441b2f2 --- /dev/null +++ b/reference/C/CONTRIB/OR_PRACTICAL_C/14_15.c @@ -0,0 +1,15 @@ +#define X_SIZE 60 +#define Y_SIZE 32 + +int matrix[X_SIZE][Y_SIZE]; + +void init_matrix(void) +{ + register int x,y; /* current element to zero */ + + for (y = 0; y < Y_SIZE; y++) { + for (x = 0; x < X_SIZE; x++) { + matrix[x][y] = -1; + } + } +} diff --git a/reference/C/CONTRIB/OR_PRACTICAL_C/14_16.c b/reference/C/CONTRIB/OR_PRACTICAL_C/14_16.c new file mode 100644 index 0000000..5b040e4 --- /dev/null +++ b/reference/C/CONTRIB/OR_PRACTICAL_C/14_16.c @@ -0,0 +1,16 @@ +#define X_SIZE 60 +#define Y_SIZE 30 + +int matrix[X_SIZE][Y_SIZE]; + +void init_matrix(void) +{ + register int index; /* element counter */ + register int *matrix_ptr; + + matrix_ptr = &matrix[0][0]; + for (index = 0; index < X_SIZE * Y_SIZE; index++) { + *matrix_ptr = -1; + matrix_ptr++; + } +} diff --git a/reference/C/CONTRIB/OR_PRACTICAL_C/14_17.c b/reference/C/CONTRIB/OR_PRACTICAL_C/14_17.c new file mode 100644 index 0000000..d7e1fd7 --- /dev/null +++ b/reference/C/CONTRIB/OR_PRACTICAL_C/14_17.c @@ -0,0 +1,16 @@ +#define X_SIZE 60 +#define Y_SIZE 30 + +int matrix[X_SIZE][Y_SIZE]; + +void init_matrix(void) +{ + register int *matrix_ptr; + + for (matrix_ptr = &matrix[0][0]; + matrix_ptr <= &matrix[X_SIZE-1][Y_SIZE-1]; + matrix_ptr++) { + + *matrix_ptr = -1; + } +} diff --git a/reference/C/CONTRIB/OR_PRACTICAL_C/14_18.c b/reference/C/CONTRIB/OR_PRACTICAL_C/14_18.c new file mode 100644 index 0000000..1924966 --- /dev/null +++ b/reference/C/CONTRIB/OR_PRACTICAL_C/14_18.c @@ -0,0 +1,8 @@ +#define X_SIZE 60 +#define Y_SIZE 30 + +int matrix[X_SIZE][Y_SIZE]; +void init_matrix(void) +{ + (void)memset(matrix, -1, sizeof(matrix)); +} diff --git a/reference/C/CONTRIB/OR_PRACTICAL_C/14_19.c b/reference/C/CONTRIB/OR_PRACTICAL_C/14_19.c new file mode 100644 index 0000000..1fa8b50 --- /dev/null +++ b/reference/C/CONTRIB/OR_PRACTICAL_C/14_19.c @@ -0,0 +1,7 @@ +#define X_SIZE 60 +#define Y_SIZE 30 + +int matrix[X_SIZE][Y_SIZE]; + +#define init_matrix() \ + (void)memset(matrix, -1, sizeof(matrix)); diff --git a/reference/C/CONTRIB/OR_PRACTICAL_C/15_1.c b/reference/C/CONTRIB/OR_PRACTICAL_C/15_1.c new file mode 100644 index 0000000..69ecdef --- /dev/null +++ b/reference/C/CONTRIB/OR_PRACTICAL_C/15_1.c @@ -0,0 +1,31 @@ +#include <stdio.h> +main() +{ + /* two numbers to work with */ + float number1, number2; + float result; /* result of calculation */ + int counter; /* loop counter and accuracy check */ + + number1 = 1.0; + number2 = 1.0; + counter = 0; + + while (number1 + number2 != number1) { + counter++; + number2 = number2 / 10.0; + } + (void) printf("%2d digits accuracy in calculations\n", counter); + + number2 = 1.0; + counter = 0; + + while (1) { + result = number1 + number2; + if (result == number1) + break; + counter++; + number2 = number2 / 10.0; + } + (void) printf("%2d digits accuracy in storage\n", counter); + return (0); +} diff --git a/reference/C/CONTRIB/OR_PRACTICAL_C/16_1.c b/reference/C/CONTRIB/OR_PRACTICAL_C/16_1.c new file mode 100644 index 0000000..6f2c0f2 --- /dev/null +++ b/reference/C/CONTRIB/OR_PRACTICAL_C/16_1.c @@ -0,0 +1,6 @@ +#include <stdlib.h> /* using ANSI C standard libraries */ +main() +{ + char *string_ptr; + + string_ptr = malloc(80); diff --git a/reference/C/CONTRIB/OR_PRACTICAL_C/16_2.c b/reference/C/CONTRIB/OR_PRACTICAL_C/16_2.c new file mode 100644 index 0000000..37dbc5f --- /dev/null +++ b/reference/C/CONTRIB/OR_PRACTICAL_C/16_2.c @@ -0,0 +1,27 @@ +/******************************************************** + * find -- look for a data item in the list * + * * + * Parameters * + * name -- name to look for in the list * + * * + * Returns * + * 1 if name is found * + * 0 if name is not found * + ********************************************************/ +int file(char *name) +{ + /* current structure we are looking at */ + struct linked_list *current_ptr; + + current_ptr = first_ptr; + + while ((strcmp(current_ptr->data, name) != 0) && + (current_ptr != NULL)) + current_ptr = current_ptr->next_ptr; + + /* + * If current_ptr is null, we fell off the end of the list and + * didn't find the name + */ + return (current_ptr != NULL); +} diff --git a/reference/C/CONTRIB/OR_PRACTICAL_C/16_3.c b/reference/C/CONTRIB/OR_PRACTICAL_C/16_3.c new file mode 100644 index 0000000..958e544 --- /dev/null +++ b/reference/C/CONTRIB/OR_PRACTICAL_C/16_3.c @@ -0,0 +1,159 @@ +/******************************************************** + * words -- scan a file and print out a list of words * + * in ASCII order. * + * * + * Usage: * + * words <file> * + ********************************************************/ +#include <stdio.h> +#include <ctype.h> +#include <string.h> +#include <stdlib.h> /* ANSI Standard only */ + +struct node { + struct node *right; /* tree to the right */ + struct node *left; /* tree to the left */ + char *word; /* word for this tree */ +}; + +/* the top of the tree */ +static struct node *root = NULL; + +main(int argc, char *argv[]) +{ + void scan(char *); /* scan the files for words */ + void print_tree(struct node *);/* print the words in the file */ + + if (argc != 2) { + (void) fprintf(stderr, "Error:Wrong number of parameters\n"); + (void) fprintf(stderr, " on the command line\n"); + (void) fprintf(stderr, "Usage is:\n"); + (void) fprintf(stderr, " words 'file'\n"); + exit(8); + } + scan(argv[1]); + print_tree(root); + return (0); +} +/******************************************************** + * scan -- scan the file for words * + * * + * Parameters * + * name -- name of the file to scan * + ********************************************************/ +void scan(char *name) +{ + char word[100]; /* word we are working on */ + int index; /* index into the word */ + int ch; /* current character */ + FILE *in_file; /* input file */ + + /* enter a word into the tree */ + void enter(struct node **, char *); + + in_file = fopen(name, "r"); + if (in_file == NULL) { + (void) fprintf(stderr, + "Error:Unable to open %s\n", name); + exit(8); + } + while (1) { + /* scan past the whitespace */ + while (1) { + ch = fgetc(in_file); + + if (isalpha(ch) || (ch == EOF)) + break; + } + + if (ch == EOF) + break; + + word[0] = ch; + for (index = 1; index < sizeof(word); index++) { + ch = fgetc(in_file); + if (!isalpha(ch)) + break; + word[index] = ch; + } + /* put a null on the end */ + word[index] = '\0'; + + enter(&root, word); + } + (void) fclose(in_file); +} +/******************************************************** + * enter -- enter a word into the tree * + * * + * Parameters * + * node -- current node we are looking at * + * word -- word to enter * + ********************************************************/ +void enter(struct node **node, char *word) +{ + int result; /* result of strcmp */ + + char *save_string(char *); /* save a string on the heap */ + void memory_error(void); /* tell user no more room */ + + /* see if we have reached the end */ + if ((*node) == NULL) { + (*node) = (struct node *) malloc(sizeof(struct node)); + if ((*node) == NULL) + memory_error(); + (*node)->left = NULL; + (*node)->right = NULL; + (*node)->word = save_string(word); + } + result = strcmp((*node)->word, word); + if (result == 0) + return; + if (result < 0) + enter(&(*node)->right, word); + else + enter(&(*node)->left, word); +} +/******************************************************** + * save_string -- save a string on the heap * + * * + * Parameters * + * string -- string to save * + * * + * Returns * + * pointer to malloc-ed section of memory with * + * the string copied into it. * + ********************************************************/ +char *save_string(char *string) +{ + char *new_string; /* where we are going to put string */ + + new_string = malloc((unsigned) (strlen(string) + 1)); + if (new_string == NULL) + memory_error(); + (void) strcpy(new_string, string); + return (new_string); +} +/******************************************************** + * memory_error -- write error and die * + ********************************************************/ +void memory_error(void) +{ + (void) fprintf(stderr, "Error:Out of memory\n"); + exit(8); +} +/******************************************************** + * print_tree -- print out the words in a tree * + * * + * Parameters * + * top -- the root of the tree to print * + ********************************************************/ +void print_tree(struct node *top) +{ + if (top == NULL) + return; /* short tree */ + + print_tree(top->left); + (void) printf("%s\n", top->word); + print_tree(top->right); +} diff --git a/reference/C/CONTRIB/OR_PRACTICAL_C/17_1.c b/reference/C/CONTRIB/OR_PRACTICAL_C/17_1.c new file mode 100644 index 0000000..f0f68c9 --- /dev/null +++ b/reference/C/CONTRIB/OR_PRACTICAL_C/17_1.c @@ -0,0 +1,117 @@ +/******************************************************** + * infinite-array -- routines to handle infinite arrays * + * * + * An infinite array is an array that grows as needed. * + * There is no index too large for an infinite array * + * (unless we run out of memory). * + ********************************************************/ +#include "ia.h" /* get common definitions */ +#include <stdio.h> +#include <stdlib.h> /* ANSI Standard only */ + +/* + * the following three variables implement + * a very simple pointer cache + * + * They store information on the last infinite array used, + * the last bucket we had and the last index used + * + * They are initialized to values we know will never + * be used by any real array. + */ +static struct infinite_array *cache_array_ptr = NULL; +static struct infinite_array *cache_bucket_ptr = NULL; +static int cache_index = -1; +/******************************************************** + * ia_store -- store an element into an infinite array. * + * * + * Parameters * + * array_ptr -- pointer to the array to use * + * index -- index into the array * + * store_data -- data to store * + ********************************************************/ +void ia_store(struct infinite_array * array_ptr, + int index, float store_data) +{ + /* pointer to the current bucket */ + struct infinite_array *current_ptr; + int current_index; /* index into the current bucket */ + + /* get the location in an infinite array cell */ + struct infinite_array *ia_locate( + struct infinite_array *, int, int *); + + current_ptr = ia_locate(array_ptr, index, ¤t_index); + current_ptr->data[current_index] = store_data; +} +/******************************************************** + * ia_get -- get an element from an infinite array. * + * * + * Parameters * + * array_ptr -- pointer to the array to use * + * index -- index into the array * + * * + * Returns * + * the value of the element * + ********************************************************/ +float ia_get(struct infinite_array *array_ptr, int index) +{ + /* pointer to the current bucket */ + struct infinite_array *current_ptr; + int current_index; /* index into the current bucket */ + + /* get the location an infinite array cell */ + struct infinite_array *ia_locate(); + + current_ptr = ia_locate(array_ptr, index, ¤t_index); + return (current_ptr->data[current_index]); +} +/******************************************************** + * ia_locate -- get the location of an infinite array * + * element. * + * * + * Parameters * + * array_ptr -- pointer to the array to use * + * index -- index into the array * + * current_index -- pointer to the index into this * + * bucket (returned) * + * * + * Returns * + * pointer to the current bucket * + ********************************************************/ +static struct infinite_array ia_locate( + struct infinite_array *array_ptr, int index + int *current_index_ptr) +{ + /* pointer to the current bucket */ + struct infinite_array *current_ptr; + + /* see if the cache will do us any good */ + if ((cache_array_ptr == array_ptr) && (index >= cache_index)) { + current_ptr = cache_bucket_ptr; + *current_index_ptr = index - cache_index; + } else { + current_ptr = array_ptr; + *current_index_ptr = index; + } + + while (*current_index_ptr > BLOCK_SIZE) { + if (current_ptr->next == NULL) { + current_ptr->next = + (struct infinite_array *) + malloc(sizeof(struct infinite_array)); + if (current_ptr->next == NULL) { + (void) fprintf(stderr, "Error:Out of memory\n"); + exit(8); + } + } + current_ptr = current_ptr->next; + *current_index_ptr -= BLOCK_SIZE; + } + + cache_index = index - (index % BLOCK_SIZE); + cache_array_ptr = array_ptr; + cache_bucket_ptr = current_ptr; + + return (current_ptr); +} diff --git a/reference/C/CONTRIB/OR_PRACTICAL_C/17_2.c b/reference/C/CONTRIB/OR_PRACTICAL_C/17_2.c new file mode 100644 index 0000000..57fdeae --- /dev/null +++ b/reference/C/CONTRIB/OR_PRACTICAL_C/17_2.c @@ -0,0 +1,142 @@ +/******************************************************** + * hist -- generate a histogram of an array of numbers * + * * + * Usage * + * hist <file> * + * * + * Where * + * file is the name of the file to work on * + ********************************************************/ +#include "ia.h" +#include <stdio.h> +#include <stdlib.h> /* ANSI Standard only */ +#include <mem.h> +/* + * the following definitions define the histogram + */ +#define NUMBER_OF_LINES 50 +#define LOW_BOUND 0.0 +#define HIGH_BOUND 99.0 +/* + * if we have NUMBER_OF_LINES data to + * output then each item must use + * the following factor + */ +#define FACTOR \ + ((HIGH_BOUND - LOW_BOUND) / ((float) (NUMBER_OF_LINES-1))) + +/* number of characters wide to make the histogram */ +#define WIDTH 60 + +static struct infinite_array data_array; +static int data_items; + +main(int argc, char *argv[]) +{ + void read_data(char *); /* get the data into the array */ + void print_histogram(void);/* print the data */ + + if (argc != 2) { + (void) fprintf(stderr, + "Error:Wrong number of arguments\n"); + (void) fprintf(stderr, + "Usage is:\n"); + (void) fprintf(stderr, + " hist <data-file>\n"); + exit(8); + } + ia_init(&data_array); + data_items = 0; + + read_data(argv[1]); + print_histogram(); + return (0); +} +/******************************************************** + * read_data -- read data from the input file into * + * the data_array. * + * * + * Parameters * + * name -- the name of the file to read * + ********************************************************/ +void read_data(char *name) +{ + char line[100]; /* line from input file */ + FILE *in_file; /* input file */ + float data; /* data from input */ + + in_file = fopen(name, "r"); + if (in_file == NULL) { + (void) fprintf(stderr, + "Error:Unable to open %s\n", name); + exit(8); + } + while (1) { + if (fgets(line, sizeof(line), in_file) == NULL) + break; + + if (sscanf(line, "%f", &data) != 1) { + (void) fprintf(stderr, + "Error: Input data not floating point number\n"); + (void) fprintf(stderr, "Line:%s", line); + } + ia_store(&data_array, data_items, data); + data_items++; + } + fclose(in_file); +} +/******************************************************** + * print_histogram -- print the histogram output. * + ********************************************************/ +void print_histogram(void) +{ + /* upper bound for printout */ + int counters[NUMBER_OF_LINES]; + float low; /* lower bound for printout */ + int out_of_range = 0;/* number of items out of bounds */ + int max_count = 0;/* biggest counter */ + float scale; /* scale for outputting dots */ + int index; /* index into the data */ + + (void) memset((char *) counters, '\0', sizeof(counters)); + + for (index = 0; index < data_items; index++) { + float data;/* data for this point */ + + data = ia_get(&data_array, index); + + if ((data < LOW_BOUND) || (data > HIGH_BOUND)) + out_of_range++; + else { + /* index into counters array */ + int count_index; + + count_index = (data - LOW_BOUND) / FACTOR; + + counters[count_index]++; + if (counters[count_index] > max_count) + max_count = counters[count_index]; + } + } + + scale = ((float) max_count) / ((float) WIDTH); + + low = LOW_BOUND; + for (index = 0; index < NUMBER_OF_LINES; index++) { + /* index for outputting the dots */ + int char_index; + int number_of_dots; /* number of * to output */ + + (void) printf("%2d:%3.0f-%3.0f (%4d): ", + index, low, low + FACTOR -1, + counters[index]); + + number_of_dots = (int) (((float) counters[index]) / scale); + for (char_index = 0; char_index < number_of_dots; + char_index++) + (void) printf("*"); + (void) printf("\n"); + low += FACTOR; + } + (void) printf("%d items out of range\n", out_of_range); +} diff --git a/reference/C/CONTRIB/OR_PRACTICAL_C/HEADER.html b/reference/C/CONTRIB/OR_PRACTICAL_C/HEADER.html new file mode 100644 index 0000000..56e67da --- /dev/null +++ b/reference/C/CONTRIB/OR_PRACTICAL_C/HEADER.html @@ -0,0 +1,10 @@ +<h1>Practical C - O'Reilly</h1> +Reprinted with permission from +'Practical C Programming' copyright 1993 O'Reilly and Associates, Inc. For +orders and information, call 1-800-988-9938. +<p> +Source directory can be extracted from +ftp.uu.net:/published/oreilly/nutshell/practical_c. +<p> +I strongly recomended you get the book to put these examples into +context. Martin diff --git a/reference/C/CONTRIB/OR_PRACTICAL_C/Makefile.am b/reference/C/CONTRIB/OR_PRACTICAL_C/Makefile.am new file mode 100644 index 0000000..9bdc55f --- /dev/null +++ b/reference/C/CONTRIB/OR_PRACTICAL_C/Makefile.am @@ -0,0 +1,6 @@ +EXTRA_DIST = $(wildcard *.html) $(wildcard *.c) $(wildcard *.h) troff.test test.csh \ + makefile.tcc makefile.unix standard.mac +docs_DATA = $(EXTRA_DIST) + +docsdir = $(kde_htmldir)/en/kdevelop/$(subdir) + diff --git a/reference/C/CONTRIB/OR_PRACTICAL_C/Makefile.in b/reference/C/CONTRIB/OR_PRACTICAL_C/Makefile.in new file mode 100644 index 0000000..f547246 --- /dev/null +++ b/reference/C/CONTRIB/OR_PRACTICAL_C/Makefile.in @@ -0,0 +1,412 @@ +# KDE tags expanded automatically by am_edit - $Revision: 1.2 $ +# Makefile.in generated automatically by automake 1.5 from Makefile.am. + +# Copyright 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001 +# Free Software Foundation, Inc. +# This Makefile.in is free software; the Free Software Foundation +# gives unlimited permission to copy and/or distribute it, +# with or without modifications, as long as this notice is preserved. + +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY, to the extent permitted by law; without +# even the implied warranty of MERCHANTABILITY or FITNESS FOR A +# PARTICULAR PURPOSE. + +@SET_MAKE@ + +SHELL = @SHELL@ + +srcdir = @srcdir@ +top_srcdir = @top_srcdir@ +VPATH = @srcdir@ +prefix = @prefix@ +exec_prefix = @exec_prefix@ + +bindir = @bindir@ +sbindir = @sbindir@ +libexecdir = @libexecdir@ +datadir = @datadir@ +sysconfdir = @sysconfdir@ +sharedstatedir = @sharedstatedir@ +localstatedir = @localstatedir@ +libdir = @libdir@ +infodir = @infodir@ +mandir = @mandir@ +includedir = @includedir@ +oldincludedir = /usr/include +pkgdatadir = $(datadir)/@PACKAGE@ +pkglibdir = $(libdir)/@PACKAGE@ +pkgincludedir = $(includedir)/@PACKAGE@ +top_builddir = ../../../.. + +ACLOCAL = @ACLOCAL@ +AUTOCONF = @AUTOCONF@ +AUTOMAKE = @AUTOMAKE@ +AUTOHEADER = @AUTOHEADER@ + +INSTALL = @INSTALL@ +INSTALL_PROGRAM = @INSTALL_PROGRAM@ +INSTALL_DATA = @INSTALL_DATA@ +INSTALL_SCRIPT = @INSTALL_SCRIPT@ +INSTALL_HEADER = $(INSTALL_DATA) +transform = @program_transform_name@ +NORMAL_INSTALL = : +PRE_INSTALL = : +POST_INSTALL = : +NORMAL_UNINSTALL = : +PRE_UNINSTALL = : +POST_UNINSTALL = : +build_alias = @build_alias@ +build_triplet = @build@ +host_alias = @host_alias@ +host_triplet = @host@ +target_alias = @target_alias@ +target_triplet = @target@ +AMTAR = @AMTAR@ +ARTSCCONFIG = @ARTSCCONFIG@ +AS = @AS@ +AUTODIRS = @AUTODIRS@ +AWK = @AWK@ +CC = @CC@ +CONF_FILES = @CONF_FILES@ +CPP = @CPP@ +CXX = @CXX@ +CXXCPP = @CXXCPP@ +DCOPIDL = @DCOPIDL@ +DCOPIDL2CPP = @DCOPIDL2CPP@ +DCOP_DEPENDENCIES = @DCOP_DEPENDENCIES@ +DEPDIR = @DEPDIR@ +DLLTOOL = @DLLTOOL@ +DPMSINC = @DPMSINC@ +DPMSLIB = @DPMSLIB@ +EXEEXT = @EXEEXT@ +GCJ = @GCJ@ +GCJFLAGS = @GCJFLAGS@ +GLINC = @GLINC@ +GLLIB = @GLLIB@ +GMSGFMT = @GMSGFMT@ +IDL = @IDL@ +IDL_DEPENDENCIES = @IDL_DEPENDENCIES@ +INSTALL_STRIP_PROGRAM = @INSTALL_STRIP_PROGRAM@ +JAR = @JAR@ +JAVAC = @JAVAC@ +JAVAH = @JAVAH@ +JVMLIBS = @JVMLIBS@ +KDECONFIG = @KDECONFIG@ +KDE_CXXFLAGS = @KDE_CXXFLAGS@ +KDE_EXTRA_RPATH = @KDE_EXTRA_RPATH@ +KDE_INCLUDES = @KDE_INCLUDES@ +KDE_LDFLAGS = @KDE_LDFLAGS@ +KDE_PLUGIN = @KDE_PLUGIN@ +KDE_RPATH = @KDE_RPATH@ +KDE_USE_CLOSURE_FALSE = @KDE_USE_CLOSURE_FALSE@ +KDE_USE_CLOSURE_TRUE = @KDE_USE_CLOSURE_TRUE@ +KDE_USE_FINAL_FALSE = @KDE_USE_FINAL_FALSE@ +KDE_USE_FINAL_TRUE = @KDE_USE_FINAL_TRUE@ +KDE_XSL_STYLESHEET = @KDE_XSL_STYLESHEET@ +LIBCOMPAT = @LIBCOMPAT@ +LIBCRYPT = @LIBCRYPT@ +LIBDL = @LIBDL@ +LIBGEN = @LIBGEN@ +LIBJPEG = @LIBJPEG@ +LIBMICO = @LIBMICO@ +LIBOBJS = @LIBOBJS@ +LIBPNG = @LIBPNG@ +LIBPTHREAD = @LIBPTHREAD@ +LIBPYTHON = @LIBPYTHON@ +LIBQIMGIO = @LIBQIMGIO@ +LIBRESOLV = @LIBRESOLV@ +LIBSHADOW = @LIBSHADOW@ +LIBSM = @LIBSM@ +LIBSOCKET = @LIBSOCKET@ +LIBTIFF = @LIBTIFF@ +LIBTOOL = @LIBTOOL@ +LIBUCB = @LIBUCB@ +LIBUTIL = @LIBUTIL@ +LIBXINERAMA = @LIBXINERAMA@ +LIBZ = @LIBZ@ +LIB_KAB = @LIB_KAB@ +LIB_KDECORE = @LIB_KDECORE@ +LIB_KDEUI = @LIB_KDEUI@ +LIB_KFILE = @LIB_KFILE@ +LIB_KFM = @LIB_KFM@ +LIB_KFORMULA = @LIB_KFORMULA@ +LIB_KHTML = @LIB_KHTML@ +LIB_KIMGIO = @LIB_KIMGIO@ +LIB_KIO = @LIB_KIO@ +LIB_KPARTS = @LIB_KPARTS@ +LIB_KSPELL = @LIB_KSPELL@ +LIB_KSYCOCA = @LIB_KSYCOCA@ +LIB_KWRITE = @LIB_KWRITE@ +LIB_QT = @LIB_QT@ +LIB_SMB = @LIB_SMB@ +LIB_X11 = @LIB_X11@ +LN_S = @LN_S@ +MCOPIDL = @MCOPIDL@ +MEINPROC = @MEINPROC@ +MICO_INCLUDES = @MICO_INCLUDES@ +MICO_LDFLAGS = @MICO_LDFLAGS@ +MOC = @MOC@ +MSGFMT = @MSGFMT@ +NOOPT_CXXFLAGS = @NOOPT_CXXFLAGS@ +NOREPO = @NOREPO@ +OBJDUMP = @OBJDUMP@ +OBJEXT = @OBJEXT@ +PACKAGE = @PACKAGE@ +PAMLIBS = @PAMLIBS@ +PASSWDLIBS = @PASSWDLIBS@ +PYTHONINC = @PYTHONINC@ +PYTHONLIB = @PYTHONLIB@ +PYTHONMODDIR = @PYTHONMODDIR@ +QT_INCLUDES = @QT_INCLUDES@ +QT_LDFLAGS = @QT_LDFLAGS@ +RANLIB = @RANLIB@ +REPO = @REPO@ +SETUIDFLAGS = @SETUIDFLAGS@ +STRIP = @STRIP@ +TOPSUBDIRS = @TOPSUBDIRS@ +UIC = @UIC@ +UIC_TR = @UIC_TR@ +USER_INCLUDES = @USER_INCLUDES@ +USER_LDFLAGS = @USER_LDFLAGS@ +USE_EXCEPTIONS = @USE_EXCEPTIONS@ +USE_RTTI = @USE_RTTI@ +USE_THREADS = @USE_THREADS@ +VERSION = @VERSION@ +XGETTEXT = @XGETTEXT@ +XPMINC = @XPMINC@ +XPMLIB = @XPMLIB@ +X_EXTRA_LIBS = @X_EXTRA_LIBS@ +X_INCLUDES = @X_INCLUDES@ +X_LDFLAGS = @X_LDFLAGS@ +X_PRE_LIBS = @X_PRE_LIBS@ +all_includes = @all_includes@ +all_libraries = @all_libraries@ +am__include = @am__include@ +am__quote = @am__quote@ +idldir = @idldir@ +install_sh = @install_sh@ +jni_includes = @jni_includes@ +kde_appsdir = @kde_appsdir@ +kde_bindir = @kde_bindir@ +kde_confdir = @kde_confdir@ +kde_datadir = @kde_datadir@ +kde_htmldir = @kde_htmldir@ +kde_icondir = @kde_icondir@ +kde_includes = @kde_includes@ +kde_libraries = @kde_libraries@ +kde_libs_htmldir = @kde_libs_htmldir@ +kde_libs_prefix = @kde_libs_prefix@ +kde_locale = @kde_locale@ +kde_mimedir = @kde_mimedir@ +kde_moduledir = @kde_moduledir@ +kde_qtver = @kde_qtver@ +kde_servicesdir = @kde_servicesdir@ +kde_servicetypesdir = @kde_servicetypesdir@ +kde_sounddir = @kde_sounddir@ +kde_templatesdir = @kde_templatesdir@ +kde_wallpaperdir = @kde_wallpaperdir@ +micodir = @micodir@ +qt_includes = @qt_includes@ +qt_libraries = @qt_libraries@ +x_includes = @x_includes@ +x_libraries = @x_libraries@ + +EXTRA_DIST = $(wildcard *.html) $(wildcard *.c) $(wildcard *.h) troff.test test.csh \ + makefile.tcc makefile.unix standard.mac + +docs_DATA = $(EXTRA_DIST) + +docsdir = $(kde_htmldir)/en/kdevelop/$(subdir) +subdir = reference/C/CONTRIB/OR_PRACTICAL_C +mkinstalldirs = $(SHELL) $(top_srcdir)/admin/mkinstalldirs +CONFIG_HEADER = $(top_builddir)/config.h +CONFIG_CLEAN_FILES = +DIST_SOURCES = +DATA = $(docs_DATA) + +DIST_COMMON = Makefile.am Makefile.in +#>- all: all-am +#>+ 1 +all: docs-am all-am + +.SUFFIXES: + +mostlyclean-libtool: + -rm -f *.lo + +clean-libtool: + -rm -rf .libs _libs + +distclean-libtool: + -rm -f libtool +$(srcdir)/Makefile.in: Makefile.am $(top_srcdir)/configure.in $(ACLOCAL_M4) +#>- cd $(top_srcdir) && \ +#>- $(AUTOMAKE) --gnu reference/C/CONTRIB/OR_PRACTICAL_C/Makefile +#>+ 3 + cd $(top_srcdir) && \ + $(AUTOMAKE) --gnu reference/C/CONTRIB/OR_PRACTICAL_C/Makefile + cd $(top_srcdir) && perl admin/am_edit reference/C/CONTRIB/OR_PRACTICAL_C/Makefile.in +Makefile: $(srcdir)/Makefile.in $(top_builddir)/config.status + cd $(top_builddir) && \ + CONFIG_HEADERS= CONFIG_LINKS= \ + CONFIG_FILES=$(subdir)/$@ $(SHELL) ./config.status +uninstall-info-am: +install-docsDATA: $(docs_DATA) + @$(NORMAL_INSTALL) + $(mkinstalldirs) $(DESTDIR)$(docsdir) + @list='$(docs_DATA)'; for p in $$list; do \ + if test -f "$$p"; then d=; else d="$(srcdir)/"; fi; \ + f="`echo $$p | sed -e 's|^.*/||'`"; \ + echo " $(INSTALL_DATA) $$d$$p $(DESTDIR)$(docsdir)/$$f"; \ + $(INSTALL_DATA) $$d$$p $(DESTDIR)$(docsdir)/$$f; \ + done + +uninstall-docsDATA: + @$(NORMAL_UNINSTALL) + @list='$(docs_DATA)'; for p in $$list; do \ + f="`echo $$p | sed -e 's|^.*/||'`"; \ + echo " rm -f $(DESTDIR)$(docsdir)/$$f"; \ + rm -f $(DESTDIR)$(docsdir)/$$f; \ + done +tags: TAGS +TAGS: + + +#>- DISTFILES = $(DIST_COMMON) $(DIST_SOURCES) $(TEXINFOS) $(EXTRA_DIST) +#>+ 4 +KDE_DIST=12_3.c 14_18.c 05_2.c 04_4.c 14_01.c 15_1.c 12_7.c 01_1.c 07_2.c 14_13.c 17_1.c 16_3.c 14_05.c 03_1.c 09_2.c 12_2.c 14_17.c 14_09.c 05_1.c 04_3.c 12_6.c tlint.c 07_1.c 06_3.c 14_12.c 10_1.c 04_7.c 16_2.c 14_04.c gen.h 09_1.c symbol.c 08_3.c font.h 12_1.c 14_16.c 14_08.c macro.h 04_2.c 03_4.c 09_5.c 12_5.c fonts.c 06_2.c 14_11.c 04_6.c 16_1.c 14_03.c 02_1.c 08_2.c 14_15.c 14_07.c 04_1.c 03_3.c 09_4.c 13_2.c 12_4.c 14_19.c HEADER.html macros.c 06_1.c 05_3.c 14_10.c 04_5.c 14_02.c 12_8.c 01_2.c 08_1.c 07_3.c 11_1.c 14_14.c 17_2.c 14_06.c 03_2.c 09_3.c 13_1.c + +DISTFILES= $(DIST_COMMON) $(DIST_SOURCES) $(TEXINFOS) $(EXTRA_DIST) $(KDE_DIST) + + +top_distdir = ../../../.. +distdir = $(top_distdir)/$(PACKAGE)-$(VERSION) + +distdir: $(DISTFILES) + @for file in $(DISTFILES); do \ + if test -f $$file; then d=.; else d=$(srcdir); fi; \ + dir=`echo "$$file" | sed -e 's,/[^/]*$$,,'`; \ + if test "$$dir" != "$$file" && test "$$dir" != "."; then \ + $(mkinstalldirs) "$(distdir)/$$dir"; \ + fi; \ + if test -d $$d/$$file; then \ + cp -pR $$d/$$file $(distdir) \ + || exit 1; \ + else \ + test -f $(distdir)/$$file \ + || cp -p $$d/$$file $(distdir)/$$file \ + || exit 1; \ + fi; \ + done +check-am: all-am +check: check-am +all-am: Makefile $(DATA) + +installdirs: + $(mkinstalldirs) $(DESTDIR)$(docsdir) + +install: install-am +install-exec: install-exec-am +install-data: install-data-am +uninstall: uninstall-am + +install-am: all-am + @$(MAKE) $(AM_MAKEFLAGS) install-exec-am install-data-am + +installcheck: installcheck-am +install-strip: + $(MAKE) $(AM_MAKEFLAGS) INSTALL_PROGRAM="$(INSTALL_STRIP_PROGRAM)" \ + `test -z '$(STRIP)' || \ + echo "INSTALL_PROGRAM_ENV=STRIPPROG='$(STRIP)'"` install +mostlyclean-generic: + +clean-generic: + +distclean-generic: + -rm -f Makefile $(CONFIG_CLEAN_FILES) stamp-h stamp-h[0-9]* + +maintainer-clean-generic: + @echo "This command is intended for maintainers to use" + @echo "it deletes files that may require special tools to rebuild." +#>- clean: clean-am +#>+ 1 +clean: kde-rpo-clean clean-am + +clean-am: clean-generic clean-libtool mostlyclean-am + +distclean: distclean-am + +distclean-am: clean-am distclean-generic distclean-libtool + +dvi: dvi-am + +dvi-am: + +info: info-am + +info-am: + +install-data-am: install-docsDATA + +install-exec-am: + +install-info: install-info-am + +install-man: + +installcheck-am: + +maintainer-clean: maintainer-clean-am + +maintainer-clean-am: distclean-am maintainer-clean-generic + +mostlyclean: mostlyclean-am + +mostlyclean-am: mostlyclean-generic mostlyclean-libtool + +uninstall-am: uninstall-docsDATA uninstall-info-am + +.PHONY: all all-am check check-am clean clean-generic clean-libtool \ + distclean distclean-generic distclean-libtool distdir dvi \ + dvi-am info info-am install install-am install-data \ + install-data-am install-docsDATA install-exec install-exec-am \ + install-info install-info-am install-man install-strip \ + installcheck installcheck-am installdirs maintainer-clean \ + maintainer-clean-generic mostlyclean mostlyclean-generic \ + mostlyclean-libtool uninstall uninstall-am uninstall-docsDATA \ + uninstall-info-am + +# Tell versions [3.59,3.63) of GNU make to not export all variables. +# Otherwise a system limit (for SysV at least) may be exceeded. +.NOEXPORT: + +#>+ 2 +docs-am: + +#>+ 6 +force-reedit: + cd $(top_srcdir) && \ + $(AUTOMAKE) --gnu reference/C/CONTRIB/OR_PRACTICAL_C/Makefile + cd $(top_srcdir) && perl admin/am_edit reference/C/CONTRIB/OR_PRACTICAL_C/Makefile.in + + +#>+ 2 +final: + $(MAKE) all-am +#>+ 2 +final-install: + $(MAKE) install-am +#>+ 2 +no-final: + $(MAKE) all-am +#>+ 2 +no-final-install: + $(MAKE) install-am +#>+ 3 +cvs-clean: + $(MAKE) -f $(top_srcdir)/admin/Makefile.common cvs-clean + +#>+ 3 +kde-rpo-clean: + -rm -f *.rpo diff --git a/reference/C/CONTRIB/OR_PRACTICAL_C/font.h b/reference/C/CONTRIB/OR_PRACTICAL_C/font.h new file mode 100644 index 0000000..46962dd --- /dev/null +++ b/reference/C/CONTRIB/OR_PRACTICAL_C/font.h @@ -0,0 +1,20 @@ +/******************************************************** + * font.h -- Definitions for the font package * + * package. * + * * + * Procedures * + * load_fonts -- load a font file onto the symbol * + * table * + ********************************************************/ + +extern struct symbol *font_symbol_ptr; /* A list of + legal fonts */ +/******************************************************** + * load_fonts -- load fonts into symbol table * + * * + * Parameters * + * file_name -- filename of file to load * + * * + * Aborts on error * + ********************************************************/ +void load_fonts(char *name); diff --git a/reference/C/CONTRIB/OR_PRACTICAL_C/fonts.c b/reference/C/CONTRIB/OR_PRACTICAL_C/fonts.c new file mode 100644 index 0000000..61f1496 --- /dev/null +++ b/reference/C/CONTRIB/OR_PRACTICAL_C/fonts.c @@ -0,0 +1,65 @@ +/******************************************************** + * fonts -- handle font-related data structures * + * * + * Functions * + * load_fonts -- load a font file * + ********************************************************/ +#include <stdio.h> +#include "gen.h" +#include "symbol.h" +#include <string.h> +#include <stdlib.h> + +/* the top of the symbol table */ +struct symbol *font_symbol_ptr = NULL; +/******************************************************** + * load_fonts -- load fonts into symbol table * + * * + * Parameters * + * file_name -- filename of file to load * + * * + * Aborts on error. * + ********************************************************/ +void load_fonts(char *file_name) +{ + FILE *in_file; /* Input file */ + char name[3]; /* Name of the current font */ + /* We have to point to something for our data */ + static char *an_object = "an object"; + + in_file = fopen(file_name, "r"); + if (in_file == NULL) { + (void) fprintf(stderr, "Error:Can't open %s for reading\n", file_name); + exit(8); + } + + while (1) { + char line[80]; /* Input line from data file */ + char *cur_char; /* Pointer to current input character */ + + if (fgets(line, sizeof(line), in_file) == NULL) { + (void) fclose(in_file); + return; + } + cur_char = line; + + while (*cur_char != '\0') { + SKIP_WHITESPACE(cur_char); + + if (*cur_char == '\0') + break; + + /* Copy two-character macro name */ + name[0] = *cur_char; + cur_char++; + if (*cur_char > ' ') { + name[1] = *cur_char; + cur_char++; + name[2] = '\0'; + } else + name[1] = '\0'; + + enter(&font_symbol_ptr, name, (generic *) an_object); + } + } +} diff --git a/reference/C/CONTRIB/OR_PRACTICAL_C/gen.h b/reference/C/CONTRIB/OR_PRACTICAL_C/gen.h new file mode 100644 index 0000000..b8f342a --- /dev/null +++ b/reference/C/CONTRIB/OR_PRACTICAL_C/gen.h @@ -0,0 +1,25 @@ +/******************************************************** + * gen.h -- general purpose macros. * + ********************************************************/ + +/* + * Define a boolean type + */ +#ifndef TRUE +typedef int boolean; +#define TRUE 1 +#define FALSE 0 +#endif /* TRUE */ + +/******************************************************** + * SKIP_WHITESPACE -- move a character pointer * + * past whitespace * + * * + * Parameters * + * cur_char -- pointer to current character * + * (will be moved) * + ********************************************************/ +/* Move past whitespace */ +#define SKIP_WHITESPACE(cur_char) \ + while ((*(cur_char) <= ' ') && (*(cur_char) != '\0')) \ + (cur_char)++; diff --git a/reference/C/CONTRIB/OR_PRACTICAL_C/macro.h b/reference/C/CONTRIB/OR_PRACTICAL_C/macro.h new file mode 100644 index 0000000..791eedd --- /dev/null +++ b/reference/C/CONTRIB/OR_PRACTICAL_C/macro.h @@ -0,0 +1,26 @@ +/******************************************************** + * macros.h -- Definitions for the macro lookup * + * package. * + * * + * Procedures * + * load_macros -- load a macro onto the symbol tbl * + * * + * macro_check -- check a macro line for * + * correctness * + ********************************************************/ + +/******************************************************** + * load_macros -- load macros into symbol table * + * * + * Parameters * + * name -- filename of file to load * + ********************************************************/ +void load_macros(char *name); + +/******************************************************** + * macro_check -- check a macro line for correctness * + * * + * Parameters * + * line -- line to check * + ********************************************************/ +void macro_check(char *name); diff --git a/reference/C/CONTRIB/OR_PRACTICAL_C/macros.c b/reference/C/CONTRIB/OR_PRACTICAL_C/macros.c new file mode 100644 index 0000000..ae5f221 --- /dev/null +++ b/reference/C/CONTRIB/OR_PRACTICAL_C/macros.c @@ -0,0 +1,418 @@ +/******************************************************** + * macros -- Handle macro related data structure. * + * * + * Functions * + * load_macros -- load a macro file into the macro * + * symbol table * + * macro_check -- check macro line * + ********************************************************/ +#include <stdio.h> +#include "gen.h" +#include "symbol.h" +#include "macro.h" +#include "font.h" +#include <string.h> +#include <ctype.h> +#include <stdlib.h> +extern void error(char *); /* Write error message */ + +/* + * Each argument to a macro has the following structure + */ +struct arg { + char type; /* Type of argument */ + boolean many; /* True if we can repeat this argument */ +}; + +#define MAX_ARGS 10 /* Max arguments for each macro */ + +struct arg_list { + int num_args; /* Number of arguments */ + struct arg args[MAX_ARGS]; /* The arguments */ +}; + +/* the top of the symbol table */ +static struct symbol *macro_symbol_ptr = NULL; + +/* + * Skip past remaining argument + */ +#define SKIP_ARG(cur_char) \ + while (*(cur_char) > ' ') \ + (cur_char)++; + +/******************************************************** + * load_macros -- load macros into symbol table * + * * + * Parameters * + * name -- filename of file to load * + * * + * Aborts on error. * + *------------------------------------------------------* + * Input file format: * + * # line -- comment * + * mac arg arg # comment * + * * + * mac -- one or two-character macro name * + * arg -- argument type letter * + * | -- vertical bar * + * s -- string * + * n -- number * + * c -- single character * + * f -- font specification * + * t -- three-part title * + * * + * If followed by a star, set the many flag. * + ********************************************************/ +void load_macros(char *name) +{ + FILE *in_file; /* Input file */ + int line_number; /* Line number of the input file */ + char *type_list = "|sncft"; /* Characters for argument type */ + int num_args;/* Number of arguments we've seen */ + char macro_name[3]; /* Name of the current macro */ + + /* The macro we are working on */ + struct arg_list *arg_list_ptr; + + in_file = fopen(name, "r"); + if (in_file == NULL) { + (void) fprintf(stderr, "Error:Can't open %s for reading\n", name); + exit(8); + } + line_number = 0; + + while (1) { + char line[80]; /* Input line from data file */ + char *cur_char; /* Pointer to current input character */ + + if (fgets(line, sizeof(line), in_file) == NULL) { + (void) fclose(in_file); + return; + } + line_number++; + + cur_char = line; + /* Trim off leading whitespace */ + SKIP_WHITESPACE(cur_char); + + /* Continue on comment or blank line */ + if ((*cur_char == '#') || (cur_char == '\0')) + continue; + + /* Copy two-character macro name */ + macro_name[0] = *cur_char; + cur_char++; + if (*cur_char > ' ') { + macro_name[1] = *cur_char; + cur_char++; + macro_name[2] = '\0'; + } else + macro_name[1] = '\0'; + + /* + * create new argument list + */ + arg_list_ptr = (struct arg_list *) malloc(sizeof(struct arg_list)); + + for (num_args = 0; num_args < MAX_ARGS; num_args++) { + + /* Move past whitespace */ + SKIP_WHITESPACE(cur_char); + + /* End of list? */ + if ((*cur_char == '#') || (cur_char == '\0')) + break; + + /* Check for legal character */ + if (strchr(type_list, *cur_char) == NULL) { + (void) fprintf(stderr, + "Error on line %d:Bad argument character %c\n", + line_number, *cur_char); + } + arg_list_ptr->args[num_args].type = *cur_char; + cur_char++; + + if (*cur_char == '*') { + arg_list_ptr->args[num_args].many = TRUE; + cur_char++; + } else + arg_list_ptr->args[num_args].many = FALSE; + + } + arg_list_ptr->num_args = num_args; + enter(¯o_symbol_ptr, macro_name, (generic *) arg_list_ptr); + } +} +/******************************************************** + * macro_check -- check a macro line for correctness * + * * + * Parameters * + * line -- line to check * + ********************************************************/ +void macro_check(char *line) +{ + char *cur_char = &line[1]; /* Pointer to current character */ + char name[3]; /* Macro name */ + struct arg_list *arg_list_ptr; /* The argument list */ + int arg_index; /* Index into argument list */ + + extern char *check_string(char *); /* String? */ + extern char *check_number(char *); /* Number? */ + extern char *check_char(char *); /* Character? */ + extern char *check_font(char *); /* Font specification? */ + extern char *check_title(char *); /* Three-part title? */ + + SKIP_WHITESPACE(cur_char); + + /* Copy two-character macro name */ + name[0] = *cur_char; + cur_char++; + if (*cur_char > ' ') { + name[1] = *cur_char; + cur_char++; + name[2] = '\0'; + } else + name[1] = '\0'; + + arg_list_ptr = (struct arg_list *) lookup(macro_symbol_ptr, name); + + if (arg_list_ptr == NULL) { + char error_msg[30]; + (void) sprintf(error_msg, "No such macro %s", name); + error(error_msg); + return; + } + arg_index = 0; + while (1) { + if (arg_index >= arg_list_ptr->num_args) + break; + + /* Start at beginning of next macro */ + SKIP_WHITESPACE(cur_char); + + /* Check for end of string */ + if (*cur_char == '\0') + break; + + switch (arg_list_ptr->args[arg_index].type) { + /* Vertical Bar (optional) */ + case '|': + if (*cur_char == '|') + cur_char++; + break; + + /* s -- a string */ + case 's': + cur_char = check_string(cur_char); + break; + + /* n -- number */ + case 'n': + cur_char = check_number(cur_char); + break; + + /* c -- character */ + case 'c': + cur_char = check_char(cur_char); + break; + + /* f -- font specification */ + case 'f': + cur_char = check_font(cur_char); + break; + + /* t -- three-part title */ + case 't': + cur_char = check_title(cur_char); + break; + + default: + (void) printf("Internal error, bad type %c\n", + arg_list_ptr->args[arg_index].type); + break; + } + + if (arg_list_ptr->args[arg_index].many == FALSE) + arg_index++; + + } + SKIP_WHITESPACE(cur_char); + if (*cur_char != '\0') + error("Too many arguments"); +} +/******************************************************** + * check_string -- check argument to make sure it's * + * pointing to a string * + * * + * A string is a word or a set of words enclosed in * + * double quotes. * + * * + * I.E. sam "This is a test" * + * * + * Parameters * + * cur_char -- pointer to the string * + * * + * Returns * + * pointer to character after the string * + ********************************************************/ +char *check_string(char *cur_char) +{ + /* What type of string is it? */ + + /* Quoted string? */ + if (*cur_char == '"') { + cur_char++; + + /* Move to end of string */ + while ((*cur_char != '"') && (*cur_char != '\0')) + cur_char++; + + /* Check for proper termination */ + if (*cur_char == '\0') + error("Missing closing \" on string parameter"); + else + cur_char++; /* Move past closing quote */ + + } else { + /* Simple word string */ + while (!isspace(*cur_char)) + cur_char++; + } + return (cur_char); +} +/******************************************************** + * check_number -- check argument to make sure it's * + * pointing to a expression * + * * + * Parameters * + * cur_char -- pointer to the integer expression * + * * + * * + * Returns * + * pointer to character after the integer exp * + ********************************************************/ +char *check_number(char *cur_char) +{ + /* Characters allowed in expressions */ + static char *number_chars = "0123456789+-*/%."; + + if (strchr(number_chars, *cur_char) == NULL) { + error("Expression expected"); + SKIP_ARG(cur_char); + return (cur_char); + } + while (strchr(number_chars, *cur_char) != NULL) + cur_char++; + + if (!(isspace(*cur_char) || (*cur_char == '\0'))) { + SKIP_ARG(cur_char); + error("Illegal expression"); + } + + return (cur_char); +} +/******************************************************** + * check_char -- check argument to make sure it's * + * pointing to a char * + * * + * Parameters * + * cur_char -- pointer to the char * + * * + * Returns * + * pointer to character after the char * + * * + * Note: This is a simple character check and does not * + * try to figure out all of the crazy \ * + * characters that can be used in troff. * + ********************************************************/ +char *check_char(char *cur_char) +{ + cur_char++; + + if (!(isspace(*cur_char) || (*cur_char == '\0'))) + error("Expected single character"); + + return (cur_char); +} +/******************************************************** + * check_font -- check argument to make sure it's * + * pointing to a legal font * + * * + * Parameters * + * cur_char -- pointer to the font * + * * + * Returns * + * pointer to character after the font * + ********************************************************/ +char *check_font(char *cur_char) +{ + char name[3]; /* Font name */ + + name[0] = *cur_char; + cur_char++; + + if (isalnum(*cur_char)) { + name[1] = *cur_char; + cur_char++; + name[2] = '\0'; + } else + name[1] = '\0'; + + if (lookup(font_symbol_ptr, name) == NULL) + error("Expected font"); + + return (cur_char); +} + +/******************************************************** + * check_title -- check argument to make sure it's * + * pointing to a three-part title * + * of the form: 'xxxx'yyyy'zzz'. * + * * + * Parameters * + * cur_char -- pointer to the title * + * * + * Returns * + * pointer to character after the title * + ********************************************************/ +char *check_title(char *cur_char) +{ + if (*cur_char != '\'') { + error("Expected beginning of three-part title"); + SKIP_ARG(cur_char); + return (cur_char); + } + cur_char++; + + while ((*cur_char != '\'') && (*cur_char != '\0')) + cur_char++; + + if (*cur_char != '\'') { + error("Expected middle part of three-part title"); + SKIP_ARG(cur_char); + return (cur_char); + } + cur_char++; + + while ((*cur_char != '\'') && (*cur_char != '\0')) + cur_char++; + + if (*cur_char != '\'') { + error("Expected third part of three-part title"); + SKIP_ARG(cur_char); + return (cur_char); + } + cur_char++; + + while ((*cur_char != '\'') && (*cur_char != '\0')) + cur_char++; + + if (*cur_char != '\'') { + error("Expected end of three-part title"); + SKIP_ARG(cur_char); + return (cur_char); + } + cur_char++; + return (cur_char); +} diff --git a/reference/C/CONTRIB/OR_PRACTICAL_C/makefile.tcc b/reference/C/CONTRIB/OR_PRACTICAL_C/makefile.tcc new file mode 100644 index 0000000..e96bd90 --- /dev/null +++ b/reference/C/CONTRIB/OR_PRACTICAL_C/makefile.tcc @@ -0,0 +1,19 @@ +CC=tcc +CFLAGS=-ml -g -w -A +SRCS=tlint.c macros.c symbol.c fonts.c +OBJS=tlint.obj macros.obj symbol.obj fonts.obj + +tlint: $(OBJS) + $(CC) $(CFLAGS) -etlint $(OBJS) + +fonts.obj: fonts.c gen.h symbol.h + $(CC) $(CFLAGS) -c fonts.c + +macros.obj: font.h gen.h macro.h macros.c symbol.h + $(CC) $(CFLAGS) -c macros.c + +symbol.obj: symbol.c symbol.h + $(CC) $(CFLAGS) -c symbol.c + +tlint.obj: font.h gen.h macro.h tlint.c + $(CC) $(CFLAGS) -c tlint.c diff --git a/reference/C/CONTRIB/OR_PRACTICAL_C/makefile.unix b/reference/C/CONTRIB/OR_PRACTICAL_C/makefile.unix new file mode 100644 index 0000000..292313f --- /dev/null +++ b/reference/C/CONTRIB/OR_PRACTICAL_C/makefile.unix @@ -0,0 +1,16 @@ +SRCS=tlint.c macros.c symbol.c fonts.c +OBJS=tlint.o macros.o symbol.o fonts.o + +tlint:$(OBJS) + cc $(CFLAGS) -o tlint $(OBJS) + +lint: + lint -xh $(SRCS) + +fonts.o: fonts.c gen.h symbol.h + +macros.o: font.h gen.h macro.h macros.c symbol.h + +symbol.o: symbol.c symbol.h + +tlint.o: font.h gen.h macro.h tlint.c diff --git a/reference/C/CONTRIB/OR_PRACTICAL_C/standard.mac b/reference/C/CONTRIB/OR_PRACTICAL_C/standard.mac new file mode 100644 index 0000000..07aa74f --- /dev/null +++ b/reference/C/CONTRIB/OR_PRACTICAL_C/standard.mac @@ -0,0 +1,100 @@ +# +# This file contains the definitions for the built-in +# macros used by troff +# +# The format of the file is +# macro <argument-list> +# +# Arguments are: +# | Vertical bar +# s String +# n Numeric expression +# c Single character +# f Font specification +# t Three-part title +# +# If followed by a * they can be repeated 0 or more +# times +# +# Anything past a # is considered a comment +br # Break +ad s # Adjust margins +na # No adjust +nf # No fill +fi # Fill +nh # No hyphen +hy n # Set hyphenation mode +hw s* # Specify how to hyphenate words +hc c # Hyphenation character +ce n # Center lines +ul n # Underline +cu n # Continuous underline +uf f # Select font for underline +po | n # Page offset +ll n # Line length +in | n # Indent +ti | n # Temporary indent +pl | n # Page length +bp n # Begin page +pn n # Page number +ne n # Specify need space +mk s # Mark current location +sp | n # Vertical space +ps n # Point size +vs | n # Vertical space +ls n # Line spacing +sv n # Save space +os # Output saved space +ns # Set no-space mode +rs # Set restore-space mode +ss n # Set character space size +cs s n n # Constant space mode +ft f # Specify font +fp f n # Set font position +fz f n # Font size request +bd f n # Specify how to bold a font +lg n # Select ligatures +ta n* # Specify tabs +tc c # Tab character +lc c # Leader character +fc c s # Field character +lt | n # Title length +pc c # Page character +tl t # Title +so s # Source another file +nx s # Change to another file +pi s # Pipe output to a program +rd s # Read data from keyboard +ex s # Exit +tm s* # Send message to standard output +ds s s # Define string +as s s # Append string +de s s # Define macro +. # End of macro +rm s # Remove macro +rn s s # Rename macro +am s s # Append to macro +di s # Divert to macro +da s # Append to diversion +wh n s # Set place to execute macro +ch s n # Change place macro executes +dt n s # Set diversion trap +it n s # Set input trap +em s # Set macro for end of input +nr s n n # Define number register +af s c # Assign format +rr s # Remove register +mc c # Margin character +ec c # Set escape character +eo # Turn off escape +cc c # Set control character +c2 # Set second control character +tr s # Translate characters +nm n n n n # Line number +nn n # No numbering +ig s # Ignore input +pm s # Print macros +fl # Flush output +ab s # Abort +ev n # Set environment +\" s* # Comment diff --git a/reference/C/CONTRIB/OR_PRACTICAL_C/symbol.c b/reference/C/CONTRIB/OR_PRACTICAL_C/symbol.c new file mode 100644 index 0000000..600bc4c --- /dev/null +++ b/reference/C/CONTRIB/OR_PRACTICAL_C/symbol.c @@ -0,0 +1,77 @@ +/******************************************************** + * symbol -- handle the symbol table * + * * + * Functions * + * enter -- put a symbol in a symbol table * + * lookup -- get the data associated with a symbol * + ********************************************************/ +#include <stdio.h> +#include "symbol.h" +#include <string.h> +#include <stdlib.h> + +/******************************************************** + * enter -- enter a word into the symbol table * + * * + * Parameters * + * node -- top node of the symbol table for add * + * symbol -- symbol name to add (1 or 2 chars) * + * data -- data associated with the symbol * + ********************************************************/ +void enter(struct symbol **node_ptr, char *symbol, generic *data) +{ + int result; /* result of strcmp */ + /* New node that we are creating */ + struct symbol *new_node_ptr; + + /* see if we have reached the end */ + if ((*node_ptr) == NULL) { + new_node_ptr = (struct symbol *) malloc(sizeof(struct symbol)); + (void)strcpy(new_node_ptr->name, symbol); + new_node_ptr->data = data; + new_node_ptr->left_ptr = NULL; + new_node_ptr->right_ptr = NULL; + *node_ptr = new_node_ptr; + return; + } + /* + * Need to sub-divide the symbol table and try again + */ + result = strcmp((*node_ptr)->name, symbol); + + if (result == 0) + return; + + if (result > 0) + enter(&(*node_ptr)->left_ptr, symbol, data); + else + enter(&(*node_ptr)->right_ptr, symbol, data); +} +/******************************************************** + * lookup -- lookup a symbol in a table * + * * + * Parameters * + * root -- root of the symbol table to search * + * name -- name to lookup. * + * * + * Returns * + * Pointer to the data or NULL if not found. * + ********************************************************/ +generic *lookup(struct symbol *root_ptr, char *name) +{ + int result; /* Result of string compare */ + + if (root_ptr == NULL) + return (NULL); + + result = strcmp(root_ptr->name, name); + + if (result == 0) { + return (root_ptr->data); + } + + if (result > 0) + return (lookup(root_ptr->left_ptr, name)); + else + return (lookup(root_ptr->right_ptr, name)); +} diff --git a/reference/C/CONTRIB/OR_PRACTICAL_C/test.csh b/reference/C/CONTRIB/OR_PRACTICAL_C/test.csh new file mode 100644 index 0000000..b3beec8 --- /dev/null +++ b/reference/C/CONTRIB/OR_PRACTICAL_C/test.csh @@ -0,0 +1,32 @@ +#!/bin/csh +# +# Test file for the program tlint +# +#---------------------------------------- +# Print commands as they are executed +# +set echo +#---------------------------------------- +# Try something normal +# +tlint troff.test +#---------------------------------------- +# Try non-existent macro and font file +tlint -mno-name +tlint -fno-name +#---------------------------------------- +# Try a bad macro file +tlint -mbad.mac </dev/null +#---------------------------------------- +# Try font file with two-character name +tlint -ftwo.font </dev/null +#---------------------------------------- +# Try a single-character macro +tlint -msingle.mac bold.test +#---------------------------------------- +# Try an illegal option +tlint -qdummy +#---------------------------------------- +# Try non-existent file +tlint dummy + diff --git a/reference/C/CONTRIB/OR_PRACTICAL_C/tlint.c b/reference/C/CONTRIB/OR_PRACTICAL_C/tlint.c new file mode 100644 index 0000000..d38d18b --- /dev/null +++ b/reference/C/CONTRIB/OR_PRACTICAL_C/tlint.c @@ -0,0 +1,123 @@ +/******************************************************** + * tlint -- check troff files for problems * + * * + * Usage: * + * tlint [options] [files] * + * * + * Options * + * -m<file> Add the data from <file> * + * to the tables used for syntax * + * checking. * + * -f<file> Add font data from <file> * + * to list of legal fonts. * + * * + * [files] is a list of files to check. * + * (none=check standard in.) * + * * + ********************************************************/ +#include <stdio.h> +#include "gen.h" +#include "macro.h" +#include "font.h" +#include <stdlib.h> + +#define MAX_LINE 100 /* longest line we can expect */ + +#define MACRO_START_1 '.' /* Macros can begin with a dot */ +#define MACRO_START_2 '\'' /* or an apostrophe */ + +main(int argc, char *argv[]) +{ + void usage(void); /* Tell the user what to do */ + void do_file(char *, FILE *); /* Process a file */ + + load_macros("standard.mac"); + load_fonts("standard.fonts"); + + while ((argc > 1) && (argv[1][0] == '-')) { + switch (argv[1][1]) { + case 'm': + load_macros(&argv[1][2]); + break; + case 'f': + load_fonts(&argv[1][2]); + break; + default: + usage(); + } + argc--; + argv++; + } + if (argc == 1) { + do_file("standard-in", stdin); + } else { + while (argc > 1) { + FILE *in_file; /* File for reading data */ + + in_file = fopen(argv[1], "r"); + if (in_file == NULL) { + (void)fprintf(stderr,"Unable to open %s\n", argv[1]); + } else { + do_file(argv[1], in_file); + (void)fclose(in_file); + } + argc--; + argv++; + } + } + return (0); +} +/******************************************************** + * usage -- tell the user what to do * + ********************************************************/ +static void usage(void) +{ + (void)printf("Usage is:\n"); + (void)printf(" tlint [options] [file1] [file2] ...\n"); + (void)printf("Options:\n"); + (void)printf(" -m<file> -- add <file> to list\n"); + (void)printf(" of macro files\n"); + (void)printf(" -f<file> -- specify additional font file\n"); + exit (8); +} +static char *file_name; /* Name of the file we are processing */ +static int line_number; /* Current line number */ +static char line[MAX_LINE]; /* A line from the input file */ +static int line_out; /* True if a line has been output */ +/******************************************************** + * do_file -- process a single file * + * * + * Parameters * + * name -- name of the file to use * + * in_file -- file to check * + ********************************************************/ +static void do_file(char *name, FILE *in_file) +{ + + file_name = name; + line_number = 0; + while (1) { + if (fgets(line, sizeof(line), in_file) == NULL) + break; + line_out = 0; /* We have not written the line */ + line_number++; + + if ((line[0] == MACRO_START_1) || (line[0] == MACRO_START_2)) + macro_check(line); + } +} +/******************************************************** + * error -- tell the user that there is an error * + * * + * Parameters * + * message -- error message * + ********************************************************/ +void error(char *message) +{ + if (line_out == 0) { + (void)fprintf(stderr,"%s", line); + line_out = 1; + } + (void)fprintf(stderr,"Error %s in file %s Line %d\n", + message, file_name, line_number); +} diff --git a/reference/C/CONTRIB/OR_PRACTICAL_C/troff.test b/reference/C/CONTRIB/OR_PRACTICAL_C/troff.test new file mode 100644 index 0000000..98686f7 --- /dev/null +++ b/reference/C/CONTRIB/OR_PRACTICAL_C/troff.test @@ -0,0 +1,36 @@ +Test file for the troff line program +.tm this is a test + .tm test is more test +.tm test once again +.xx this is an error +Test of macro that has no arguments +.br +Put an argument where there should be none +.br +Expect string argument +.tm string +Two strings +.tm string string +Integer +.br +12+34 +String where number expected +.br +test +Vertical bar +.lt |0 +No vertical bar +.lt 0 +Font check +Bad font check +Character +'left'middle'right' +bad +'bad +'bad'bad +'bad'bad' +"quoted string" +"almost quoted string +.sp 12.0 +Cause an expression error +.sp 12.0_12.0 |