Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <stdbool.h>
- #include <string.h>
- ////////////////////////////
- FILE *file;
- char *filename = "shellconfig";
- char *promptArg;
- char *pathArg[50];
- int pathArgCount = 0;
- //////////////////////////
- void main() {
- file = fopen(filename, "r");
- checkFile();
- checkFileContents();
- printf("promptArg %s \n", promptArg);
- fclose(file);
- }
- void checkFile() {
- if (file == NULL) {
- fprintf(stderr, "File %s could not be opened. Please check if it exists\n", filename);
- exit(1);
- }
- }
- void checkFileContents() {
- char string[80];
- while(!feof(file)) {
- fgets(string, 80, file);
- tokenizeString(string);
- }
- }
- void tokenizeString(char string[]) {
- char *tokenizedStr;
- int promptOrPath; //0 for Prompt, 1 for Path
- tokenizedStr = strtok(string, " ");
- promptOrPath = checkIfValid(tokenizedStr);
- while (1) {
- tokenizedStr = strtok(NULL, " ");
- if (promptOrPath == 0) {
- createPromptArg(tokenizedStr);
- } else if (promptOrPath == 1) {
- createPathArgs(tokenizedStr);
- }
- if (tokenizedStr == NULL) {
- break;
- }
- }
- }
- int checkIfValid(char *tokenizedStr) {
- if ((strcmp(tokenizedStr,"PROMPT:") != 0) && (strcmp(tokenizedStr, "PATH:") != 0)) {
- fprintf(stderr, "File %s does not comply with the correct format. Please fix it and try again\n", filename);
- exit(EXIT_FAILURE);
- } else if (strcmp(tokenizedStr,"PROMPT:") == 0) {
- return 0;
- } else if (strcmp(tokenizedStr,"PATH:") == 0) {
- return 1;
- }
- }
- void createPromptArg(char *tokenizedStr) {
- if(tokenizedStr != NULL) {
- strcpy(promptArg,tokenizedStr);
- }
- }
- void createPathArgs(char *tokenizedStr) {
- if (tokenizedStr != NULL){
- strcpy(pathArg[pathArgCount],tokenizedStr);
- pathArgCount++;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement