C   21

stack.c

Guest on 15th January 2022 05:06:57 AM

  1. /* An indirect recusion stack-overflow example */
  2. #include <stdio.h>
  3. #include <string.h>
  4.  
  5. int read_after_num(void);
  6. int read_after_nonnum(void);
  7.  
  8. int
  9. read_after_num(void) {
  10.   char data[100];
  11.   int data_len;
  12.   int i, is_num;
  13.  
  14.   printf("Numeric mode\n");
  15.  
  16.   is_num = 1;
  17.   fgets(data, 100, stdin);
  18.   data_len = strlen(data);
  19.  
  20.   if(strcmp(data, "000\n") == 0) return 0;
  21.  
  22.   for(i = 0; i < data_len; i++) {
  23.       if((data[i] < 0x30 || data[i] > 0x39) && data[i] != '\n') {
  24.           is_num = 0;
  25.           break;
  26.       }
  27.   }
  28.  
  29.   return (is_num == 1)?read_after_num():read_after_nonnum();
  30. }
  31.  
  32. int
  33. read_after_nonnum(void) {
  34.   char data[100];
  35.   int data_len;
  36.   int i, is_num;
  37.  
  38.   printf("Alpha mode\n");
  39.  
  40.   is_num = 1;
  41.   fgets(data, 100, stdin);
  42.   data_len = strlen(data);
  43.  
  44.   if(strcmp(data, "quit\n") == 0) return 0;
  45.  
  46.   for(i = 0; i < data_len; i++) {
  47.       if((data[i] < 0x30 || data[i] > 0x39) && data[i] != '\n') {
  48.           is_num = 0;
  49.           break;
  50.       }
  51.   }
  52.  
  53.   printf("is num: %d\n", is_num);
  54.  
  55.   return (is_num == 1)?read_after_num():read_after_nonnum();
  56. }
  57.  
  58. int
  59. main(int argc, char **argv) {
  60.   read_after_nonnum();
  61. }

Raw Paste


Login or Register to edit or fork this paste. It's free.