Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdlib.h>
- #include <stdio.h>
- #include <string.h>
- #include <time.h>
- #include <ctype.h>
- #define SIZE 201
- void parseToArray();
- int main() {
- char *user;
- char *param[5];
- char lineBuf[SIZE];
- char timeBuf[SIZE];
- time_t curtime;
- struct tm *loctime;
- /* Clear that array! */
- memset(¶m[0], 0, sizeof(param));
- /* Get current time in a nice format */
- curtime = time (NULL);
- loctime = localtime (&curtime);
- strftime (timeBuf, SIZE, "%T", loctime);
- /* Get name of currently logged in user */
- user = getenv("LOGNAME");
- /* Main loop */
- while (1) {
- /* Safely reads a line from input. Exits with Ctrl+D */
- if(fgets(lineBuf, SIZE, stdin) == NULL) return 0;
- #ifdef DEBUG
- fprintf(stderr, "%s\n", lineBuf);
- #endif
- /* Parse user input */
- parseToArray(lineBuf, param);
- #ifdef DEBUG
- int j;
- for (j = 0; j < 5; j++) {
- if (param[j] == '\0') {
- break;
- } else {
- printf("%d: %s\n", j, param[j]);
- }
- }
- #endif
- /* Clean memory DOES NOT WORK */
- //int i;
- //for (i = 0; i < 5; i++) {
- // if (param[i] != NULL) {
- // free(param[i]);
- // }
- //}
- }
- }
- void parseToArray(char *line, char *array[]) {
- char *string = (char *)malloc(40*sizeof(char));
- char *stringPos;
- stringPos = string;
- int inMsg = 0;
- int charNum = 0;
- int argNum = 0;
- /* Parse line char by char */
- while (*line != '\0') {
- if (!isspace(*line) && *line != '"') { //Regular char.
- *stringPos = *line;
- stringPos++;
- } else if (*line == '"') { //Quote
- if (inMsg == 0) { //begins
- inMsg = 1;
- } else {
- inMsg = 0; //ends.
- }
- } else if (inMsg == 1) { //Char within quote.
- *stringPos = *line;
- stringPos++;
- } else if (isspace(*line)) { //Delimeter
- *stringPos = '\0'; //Add nul byte.
- printf("%s\n", stringPos);
- array[argNum] = string; //Add string to array of strings
- argNum++;
- printf("%x\n", string);
- string = (char *)malloc(40*sizeof(char));
- stringPos = string;
- }
- line++;
- }
- array[argNum] = '\0'; //Add nul byte to array of strings.
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement