Advertisement
Guest User

Untitled

a guest
Jun 23rd, 2017
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.75 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <stdbool.h>
  4. #include <string.h>
  5.  
  6.  
  7. ////////////////////////////
  8. FILE *file;
  9. char *filename = "shellconfig";
  10. char *promptArg;
  11. char *pathArg[50];
  12. int pathArgCount = 0;
  13. //////////////////////////
  14.  
  15. void main() {
  16.  
  17.     file = fopen(filename, "r");
  18.     checkFile();
  19.     checkFileContents();
  20.     printf("promptArg %s \n", promptArg);
  21.     fclose(file);
  22. }
  23.  
  24.  
  25. void checkFile() {
  26.     if (file == NULL) {
  27.         fprintf(stderr, "File %s could not be opened. Please check if it exists\n", filename);
  28.         exit(1);
  29.     }
  30. }
  31.  
  32. void checkFileContents() {
  33.     char string[80];
  34.  
  35.     while(!feof(file)) {
  36.         fgets(string, 80, file);
  37.         tokenizeString(string);
  38.     }
  39. }
  40.  
  41. void tokenizeString(char string[]) {
  42.     char *tokenizedStr;
  43.     int promptOrPath; //0 for Prompt, 1 for Path
  44.  
  45.     tokenizedStr = strtok(string, " ");
  46.     promptOrPath = checkIfValid(tokenizedStr);
  47.     while (1) {
  48.         tokenizedStr = strtok(NULL, " ");
  49.         if (promptOrPath == 0) {
  50.             createPromptArg(tokenizedStr);
  51.         } else if (promptOrPath == 1) {
  52.             createPathArgs(tokenizedStr);
  53.         }
  54.         if (tokenizedStr == NULL) {
  55.             break;
  56.         }
  57.     }
  58. }
  59.  
  60. int checkIfValid(char *tokenizedStr) {
  61.     if ((strcmp(tokenizedStr,"PROMPT:") != 0) && (strcmp(tokenizedStr, "PATH:") != 0)) {
  62.         fprintf(stderr, "File %s does not comply with the correct format. Please fix it and try again\n", filename);
  63.         exit(EXIT_FAILURE);
  64.     } else if (strcmp(tokenizedStr,"PROMPT:") == 0) {
  65.         return 0;
  66.     } else if (strcmp(tokenizedStr,"PATH:") == 0) {
  67.         return 1;
  68.     }
  69. }
  70.  
  71. void createPromptArg(char *tokenizedStr) {
  72.     if(tokenizedStr != NULL) {
  73.         strcpy(promptArg,tokenizedStr);
  74.     }
  75. }
  76.  
  77. void createPathArgs(char *tokenizedStr) {
  78.     if (tokenizedStr != NULL){
  79.         strcpy(pathArg[pathArgCount],tokenizedStr);
  80.         pathArgCount++;
  81.     }
  82. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement