Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*==================================================================
- *
- * University College Dublin
- * COMP20020 - Unix Programming
- *
- * File Name : assign1-18442024.c
- *
- * Description : program to carry out user controlled head and tail operations on a file read from stdin
- * checklist:
- * getopt working coorectly
- * default 10 lines working correctly
- * -n K lines function working correctly
- * -h program description working correctly
- * -V print version version info working correctly
- * -e/-o options working correctly
- * read from stdin if no recognisable file is supplied
- * all functions work form both supplied files and std input files
- *
- *
- * Author : Matthew Murnaghan
- *
- * Date : 16-02-20
- *
- *==================================================================*/
- #include <stdlib.h>
- #include <stdio.h>
- #include <getopt.h> // include relevant libraries
- #include <string.h>
- #include <stdbool.h>
- #define READONLY "r" // defined for use in file opening
- #define HEADERSIZE 10
- #define TAILSIZE 10
- #define MAXLINE_LENGTH 180 // defines max line length, can be changed to suit file, use for printing a seperating line between stdin input and stdin output file
- #define DEBUG 0 // defined to enable optional debug mode for analysis // see line 130
- #define MAX_ARGUEMENTS 5
- int main(int argc, char **argv)
- {
- int K, lines = 0, ctr = 1, evenOdd = 2, option, i; // declares int values for later use
- char *line; // declares pointer to line foe use in getline fucntion
- char head[] = "head", tail[] = "tail", V[] = "V", h[] = "h"; // declares char arrays for later use
- char usage[] = "Error - Input format must be: { head | tail } [OPTION ] . . . [ FILE ]\n\n"; // declares generic usage error message
- char optlist[] = "List of options for this program -->-n (int) K--returns int value K lines of read file\n-V-- returns version info\n-h-- returns help message\n-o-- returns odd lines of file\n-e-- returns even lines of file";
- bool headMode = false, tailMode = false, fsupplied = true, optsupplied = false; // declares booleans for condition checking
- size_t len = 0; // delcares size_t for use in get line for correct memory allocation
- FILE *fpin, *tmpFile; //declares needed file pointers
- if (DEBUG)
- printf("argc = %d\n\n", argc);
- if (DEBUG)
- for (i = 0; i < argc; i++)
- {
- printf("argv %d = %s \n", i, argv[i]); // prints all arguments to the function
- }
- if (argc > MAX_ARGUEMENTS)
- {
- printf("Error - arguments for this program must not exceed 5\n"); // Prints specific error message if user exceeds max number of arguements
- exit(2);
- }
- for (i = 1; i < argc; i++) // checks all arguments to see of -h or -V is present
- {
- if (strcmp(argv[i], "-h") ==0 || strcmp(argv[i], "-H") == 0)
- {
- if (DEBUG)
- {
- printf("%d\n", strcmp(argv[i], "-h"));
- printf("-h present - optsupplied = true\n");
- }
- optsupplied = true; // sets optsupplied to true for comparison check. if optsupplied == true
- }
- if (strcmp(argv[i], "-V") == 0 || strcmp(argv[i], "-v") == 0)
- {
- if (DEBUG)
- printf("-V present - optsupplied = true\n");
- optsupplied = true;
- }
- }
- if (argc<2)
- {
- printf("%sMust provide at least 2 arguements\n", usage);
- exit(2);
- }
- if (DEBUG)
- {
- printf("optsupplied false\n");
- }
- if (optsupplied == false)
- {
- if ((strncmp(head, argv[1], 4)) == 0)
- {
- if (DEBUG)
- printf("headmode\n");
- headMode = true;
- }
- else if ((strncmp(tail, argv[1], 4)) == 0)
- {
- if (DEBUG)
- printf("tailemode\n");
- tailMode = true; // printf("head/tail check\n");
- }
- }
- if (optsupplied == false) // checks to make sure h option hasnt been triggered before attempting to open a file.
- {
- if ((fpin = fopen(argv[argc - 1], READONLY)) == NULL)
- {
- fsupplied = false;
- printf("No File provided or file not recognised\n\nOpening temp file from stdin:\nTo end file input: user input --> \"Ctrl + D\"\n");
- fpin = stdin;
- tmpFile = tmpfile();
- if (tmpFile == NULL)
- {
- printf("Temp file failed to open \n");
- exit(EXIT_FAILURE);
- }
- while ((getline(&line, &len, fpin)) != EOF)
- {
- fputs(line, tmpFile);
- lines++;
- }
- if (DEBUG)
- printf("lines %d\n", lines);
- fpin = tmpFile;
- }
- if (DEBUG)
- {
- printf("%d\n", fsupplied);
- printf("file opened\n");
- }
- if (DEBUG)
- printf("lines %d\n", lines);
- if (fsupplied == false)
- {
- for (i = 0; i < MAXLINE_LENGTH; i++)
- fputs("-", stdout);
- fputs("\n", stdout);
- fseek(fpin, 0, SEEK_SET);
- }
- }
- if (DEBUG)
- printf("start of while loop\n");
- while ((option = getopt(argc, argv, "hvn:eo")) != -1 || argc < 4)
- {
- switch (option)
- {
- case 'o':
- if (argc > 4)
- {
- printf("Error - even odd option requires no integer input\n");
- exit(2);
- }
- while ((getline(&line, &len, fpin)) != EOF)
- {
- if (evenOdd % 2 == 0) /* code */
- {
- fputs(line, stdout);
- }
- evenOdd++;
- }
- exit(EXIT_SUCCESS);
- {
- fputs(usage, stdout);
- exit(2);
- {
- fputs(usage, stdout);
- exit(2);
- }
- }
- case 'e':
- if (argc > 4)
- {
- printf("Error - even odd option requires no integer input\n");
- exit(2);
- }
- evenOdd = 1;
- while ((getline(&line, &len, fpin)) != EOF)
- {
- if (evenOdd % 2 == 0)
- {
- fputs(line, stdout);
- }
- evenOdd++;
- }
- exit(EXIT_SUCCESS);
- case 'V':
- if (optsupplied == true && argc>2)
- {
- printf("Error -V option requires only 2 inputs\n");
- exit(2);
- }
- printf("Version info:\n\tProgram Author: Matthew Murnaghan\n\tStudent email: \tmatthew.murnaghan@ucdconnect.ie\n\tstudent number: 18442024\n");
- exit(EXIT_SUCCESS);
- case 'n':
- lines = 0;
- K = atoi(optarg);
- if (DEBUG)
- printf("%d\n", K);
- if (headMode == true)
- {
- for (ctr = 0; ctr < K; ctr++)
- {
- (getline(&line, &len, fpin));
- fputs(line, stdout);
- }
- }
- else if (tailMode == true)
- {
- if (DEBUG)
- printf("tailmode - before while\n");
- while ((getline(&line, &len, fpin)) != EOF)
- {
- lines++;
- }
- if (DEBUG)
- printf("after while\nlines = %d\n", lines);
- fseek(fpin, 0, SEEK_SET);
- while ((getline(&line, &len, fpin)) != EOF)
- {
- if (ctr > lines - K)
- fputs(line, stdout);
- ctr++;
- }
- fputs("\n", stdout);
- }
- exit(EXIT_SUCCESS);
- case 'h':
- if (optsupplied == true && argc>2)
- {
- printf("Error -h option requires only 2 inputs\n");
- exit(2);
- }
- fputs("\nThis is a program to print a user specified amount of lines from the head or tail of a file\n", stdout);
- fputs("Input format must be: { head | tail } [OPTION ] . . . [ FILE ]\n\n", stdout);
- printf("%s\n", optlist);
- exit(EXIT_SUCCESS);
- default:
- if (DEBUG)
- printf("default\n");
- if (headMode == true)
- {
- if (DEBUG)
- printf("forloop\n");
- if (lines < HEADERSIZE && fsupplied == false) //this part is used for when no file is supplied and the default case should trigger
- {
- if (DEBUG)
- printf("ctr = %d\n", ctr);
- for (ctr = 0; ctr < lines; ctr++)
- {
- (getline(&line, &len, fpin));
- fputs(line, stdout);
- }
- }
- else
- {
- lines = 0;
- for (ctr = 0; ctr < HEADERSIZE; ctr++) // this part is for when a file is supplied
- {
- (getline(&line, &len, fpin));
- fputs(line, stdout);
- }
- if (DEBUG)
- printf("past forloop\n");
- }
- }
- else if (tailMode == true)
- {
- if (DEBUG)
- printf("tail mode - before while\n");
- while ((getline(&line, &len, fpin)) != EOF)
- {
- lines++;
- }
- if (DEBUG)
- printf("after while\nlines =%d\n", lines);
- fseek(fpin, 0, SEEK_SET);
- while ((getline(&line, &len, fpin)) != EOF)
- {
- if (ctr > lines - TAILSIZE)
- fputs(line, stdout);
- ctr++;
- }
- fputs("\n", stdout);
- }
- else
- {
- printf("%s", usage);
- exit(2);
- }
- exit(EXIT_SUCCESS);
- }
- }
- if (headMode)
- {
- fclose(fpin);
- free(line);
- free(tmpFile);
- }
- exit(EXIT_SUCCESS);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement