Advertisement
Guest User

Untitled

a guest
Jun 19th, 2017
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.61 KB | None | 0 0
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <time.h>
  5. #include <ctype.h>
  6. #define SIZE 201
  7.  
  8. void parseToArray();
  9.  
  10. int main() {
  11.     char *user;
  12.     char *param[5];
  13.     char lineBuf[SIZE];
  14.     char timeBuf[SIZE];
  15.     time_t curtime;
  16.     struct tm *loctime;
  17.  
  18.     /* Clear that array! */
  19.     memset(&param[0], 0, sizeof(param));
  20.  
  21.     /* Get current time in a nice format */
  22.     curtime = time (NULL);
  23.     loctime = localtime (&curtime);
  24.     strftime (timeBuf, SIZE, "%T", loctime);
  25.  
  26.     /* Get name of currently logged in user */
  27.     user = getenv("LOGNAME");
  28.  
  29.     /* Main loop */
  30.     while (1) {
  31.  
  32.         /* Safely reads a line from input. Exits with Ctrl+D */
  33.         if(fgets(lineBuf, SIZE, stdin) == NULL) return 0;
  34.  
  35.         #ifdef DEBUG
  36.         fprintf(stderr, "%s\n", lineBuf);
  37.         #endif
  38.  
  39.         /* Parse user input */
  40.         parseToArray(lineBuf, param);
  41.  
  42.         #ifdef DEBUG
  43.         int j;
  44.         for (j = 0; j < 5; j++) {
  45.             if (param[j] == '\0') {
  46.                 break;
  47.             } else {
  48.                 printf("%d: %s\n", j, param[j]);
  49.             }
  50.         }
  51.         #endif
  52.  
  53.         /* Clean memory DOES NOT WORK */
  54.         //int i;
  55.         //for (i = 0; i < 5; i++) {
  56.         //  if (param[i] != NULL) {
  57.         //      free(param[i]);
  58.         //  }
  59.         //}
  60.  
  61.     }
  62. }
  63.  
  64. void parseToArray(char *line, char *array[]) {
  65.     char *string = (char *)malloc(40*sizeof(char));
  66.     char *stringPos;
  67.     stringPos = string;
  68.  
  69.     int inMsg = 0;
  70.     int charNum = 0;
  71.     int argNum = 0;
  72.  
  73.     /* Parse line char by char */
  74.     while (*line != '\0') {
  75.         if (!isspace(*line) && *line != '"') {  //Regular char.
  76.             *stringPos = *line;
  77.             stringPos++;
  78.         } else if (*line == '"') {              //Quote
  79.             if (inMsg == 0) {                   //begins
  80.                 inMsg = 1;
  81.             } else {
  82.                 inMsg = 0;                      //ends.
  83.             }
  84.  
  85.         } else if (inMsg == 1) {                //Char within quote.
  86.             *stringPos = *line;
  87.             stringPos++;
  88.  
  89.         } else if (isspace(*line)) {            //Delimeter
  90.             *stringPos = '\0';                  //Add nul byte.
  91.             printf("%s\n", stringPos);
  92.  
  93.             array[argNum] = string;         //Add string to array of strings
  94.             argNum++;
  95.  
  96.             printf("%x\n", string);
  97.             string = (char *)malloc(40*sizeof(char));
  98.             stringPos = string;
  99.         }
  100.  
  101.         line++;
  102.     }
  103.        
  104.     array[argNum] = '\0'; //Add nul byte to array of strings.
  105. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement