Added stack address randomization example.
[C-Programming-Examples.git] / ex_1-23.c
blob9911d876b764f510dd034a33cb4a2792f801e452
1 /*
3 store state
5 store previous char
7 only check when we are not in a quoted area
8 check current char for * or / if prev char is a / than we are in a comment
10 echo out prev char
15 #include <stdio.h>
17 void advbuf(char buf[], char mrk[])
19 buf[0] = buf[1];
20 mrk[0] = mrk[1];
22 //buf[1] = buf[2];
23 //mrk[1] = mrk[2];
25 /* if buffer has comment string, continue comment by default */
26 /* similar to carrying in math :) */
29 int main()
31 char buf[2], mrk[2] = { '\0' };
33 /* mrk c = comment q = quote p = program */
35 char strtyp = '\0'; /* store string type */
36 int comment = 0;
37 /* pre-load buffer */
39 for(;;)
41 advbuf(buf, mrk);
42 buf[1] = getchar(); // load new char at start of buffer
43 if(buf[1] == EOF) { return 0; } /* exit when we get to end of input */
45 mrk[1] = 'p';
47 /* found new quote */
48 if(buf[1] == '\'' || buf[1] == '"') /* found one of the quotes */
50 mrk[1] = 'q';
51 strtyp = buf[1];
54 /* if quote is in effect, keep quoting until we find corresponding quote */
55 if(strtyp == '\'' || strtyp == '"')
57 if(mrk[0] == 'q')
59 if(buf[1] == strtyp)
61 strtyp = '\0';
63 mrk[1] = 'q';
67 if(mrk[1] == 'p' || mrk[1] == 'c')
70 /* look for start of comment */
71 if(buf[1] == '*' || buf[1] == '/')
73 if(buf[0] == '/')
75 mrk[0] = 'c';
76 mrk[1] = 'c';
77 comment = 1;
81 /* look for end of comment */
82 if(comment == 1)
84 if(buf[1] == '/')
85 if(buf[0] == '*' || buf[0] == '/')
87 mrk[0] = 'c';
88 mrk[1] = 'c';
89 comment = 0;
91 mrk[1] = 'c';
95 //printf("%c%c", buf[0], buf[1]);
96 //printf("->%c%c\n", mrk[0], mrk[1]);
98 /* based on current state print output */
99 if(mrk[0] == 'p' || mrk[0] == 'q') { putchar(buf[0]); }
102 printf("This is a comment in a quote /* comment here */ just ignore it.\n");
104 return 0;