#include /* for fprintf() etc */ #include /* for str...() */ #include /* for toupper() */ /* --- global data */ /* current list of allowed courses */ char* course_list[] = { "CENG303-50", "ISYS290-04", "ISYS290-06", "ISYS291-01", "ISYS291-50", "" /* end_of_list marked by empty entry */ }; /* --- function prototypes */ int process_file (char* result, char* source); int errhndlr (char err_type, int err_code, char* other_info); int is_in_list (char* arg, char* list_name[]); int newmain (char* result, char* source[]); int main(void) { char* result[35]; char* source[35] = {"REGISTER_EMAIL ISYS290-04 GRDJ0002"}; int num =0; //int count=0; clrscr(); //printf("\n%s", *s); //getch(); //num = process(result, source); num = newmain(result, source); /*printf("\n"); for(count=0; count<35; count++) { printf("%c", r[count]); }*/ printf("\nresult: '%s'", *result); printf("\nreturn num: %d", num); getch(); getch(); return 0; } //int newmain(int argc, char *argv[]) int newmain(char* arg, char *argv[]) { int argc=4; int k = 0; /* index/loop control */ int err = 0; /* error indicator, zero = NO ERRROR */ char temp[81] = "\0"; /* temporary_use string */ char *out = NULL; /* output file pointer */ /* check number of arguments */ if (argc < 4) errhndlr('E', 1, ""); /*--- if enough arguments, do some checking */ /* check course id */ if (strlen(argv[1]) != 10) errhndlr('E', 4, argv[1]); /* change to upper case */ strcpy(temp, argv[1]); for (k = 0; temp[k] != '\0'; k++) { temp[k] = toupper(temp[k]); } k = is_in_list(temp, course_list); if (k == 0) errhndlr('E', 4, argv[1]); /* report original */ /* check function name */ strcpy(temp, argv[2]); for (k = 0; temp[k] != '\0'; k++) { /* function name should **NOT** have a period. existence of a period possibly indicates missing function ... the first filename is being tested as if it were the function name */ if (temp[k] == '.') errhndlr('E', 5, argv[2]); /* report original */ } /* --- process each filename in list */ for (k = 3; k < argc; k++) { err = process_file(argv[k], out); if (err) errhndlr('E', err, argv[k]); } /*--------- wrap up -------*/ errhndlr ('I', 0, "**OK --- output file TESTIT.TMP is mailable **\n"); return 0; } /* =========================================================== */ #include /* for printf() etc */ #include /* for str...() etc */ #define MAXLEN 1024 int process_file(char* filename, char* out) { char temp[MAXLEN+1]; /* holds various input lines */ int k = 0; /* loop control */ FILE *in = NULL; /* input file pointer */ in = fopen(filename, "r"); /* open current input file */ if (in == NULL) return 3; /* file open error */ /* pick off only the actual file name - lose directory and/or drive letter */ strcpy(temp, filename); for (k = strlen(temp); k != 0; k--) { if ((temp[k-1] == '\\') || (temp[k-1] == '/') || (temp[k-1] == ':')) break; } fprintf(out, " ### FILE %s\n", temp+k); /* read/copy inpupt file */ fgets(temp, MAXLEN, in); /* read (next) line from input file */ while (!feof(in)) { fprintf(out, "%s", temp); /* write out current source line */ fgets(temp, MAXLEN, in); /* read (next) line from input file */ } /* wrap up */ if (temp[strlen(temp)-1] != '\n') fprintf(out,"%s\n", temp); fclose (in); /* close current input file */ return 0; } /* =========================================================== */ #include /* for printf() etc */ #include /* for exit() */ char *msgs[] = { /* 0 */ "%s\n", /* message 0 is a generic message */ /* 1 */ "USAGE IS: testit course_id function filename [filename...]%s\n", /* 2 */ "**** INVALID OUTPUT ---- DO NOT MAIL ****%s\n", /* 3 */ "Can't open file %s\n", /* 4 */ "Invalid course id, %s\n", /* 5 */ "Invalid, possibly missing, function name, %s\n", /* 6 */ "%s\n", /* 7 */ "%s\n" /* future messages must contain ONE %s */ }; int errhndlr(char type, int err, char *extra_data) { fprintf(stderr, "\n%c %d ", type, err); fprintf(stderr, msgs[err], extra_data); fprintf(stderr, "\n"); if ((type == 'E') || (type == 'F')) { exit (err); /* abort if fatal error */ } return err; /* otherwise return */ } /* =========================================================== */ #include /* for str...() etc */ int is_in_list( char* item, char* list[]) { /* searches for item in list returns 1 if item is present in list, returns 0 otherwise end of list is marked by and empty entry ( == "") */ int k = 0; for (k = 0; strcmp(list[k],""); k++) { if (!strcmp(item, list[k])) return 1; } return 0; }