summaryrefslogtreecommitdiff
path: root/biology/coalesce/patches/patch-coalesce.c
blob: 30beb7caf539145a4803fa69c6a2c2450d18b52d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
$NetBSD: patch-coalesce.c,v 1.1 2015/11/07 21:06:37 dholland Exp $

gets() considered harmful.

--- coalesce.c~	1996-02-29 16:00:09.000000000 +0000
+++ coalesce.c
@@ -70,6 +70,20 @@ void readparmfile()
 boolean		accept_slide;
 long		slid, slacc, indecks, chaintype;
 
+static void dogets(char *buf, size_t max)
+{
+   size_t len;
+
+   if (!fgets(buf, max, stdin)) {
+      *buf = '\0';
+      return;
+   }
+   len = strlen(buf);
+   if (len > 0 && buf[len-1] == '\n') {
+      buf[len-1] = '\0';
+   }
+}
+
 void openfile(FILE **fp, char *filename, char *mode, char *application,
    char *perm)
 {
@@ -88,7 +102,7 @@ void openfile(FILE **fp, char *filename,
         file[0] = '\0';
         while (file[0] =='\0'){
           fprintf(stdout,"Please enter a new filename>");
-          gets(file);
+          dogets(file, sizeof(file));
           }
         break;
       case 'w':
@@ -96,7 +110,7 @@ void openfile(FILE **fp, char *filename,
         file[0] = '\0';
         while (file[0] =='\0'){
           fprintf(stdout,"Please enter a new filename>");
-          gets(file);
+          dogets(file, sizeof(file));
           }
         break;
       }
@@ -775,7 +789,7 @@ void getoptions()
     }
     printf("\n");
     printf("Are these settings correct? (type Y or the letter for one to change)\n");
-    gets(input);
+    dogets(input, sizeof(input));
     ch = input[0];
     ch = toupper(ch);
     done = (ch == 'Y');
@@ -787,7 +801,7 @@ void getoptions()
 	case 'S':
 	  do {
 	    printf("How many Short Chains?\n");
-            gets(input);
+            dogets(input, sizeof(input));
             chains[0] = atoi(input);
 	    if (chains[0] < 0)
 	      printf("Must be non-negative\n");
@@ -797,7 +811,7 @@ void getoptions()
 	case 'L':
 	  do {
 	    printf("How many Long Chains?\n");
-            gets(input);
+            dogets(input, sizeof(input));
             chains[1] = atoi(input);
 	    if (chains[1] < 0)
 	      printf("Must be non-negative\n");
@@ -811,7 +825,7 @@ void getoptions()
 	  if (ctgry) {
 	    do {
 	      printf("Number of categories ?");
-              gets(input);
+              dogets(input, sizeof(input));
               categs = atoi(input);
 	    } while (categs < 1);
 	    free(rate);
@@ -869,7 +883,7 @@ void getoptions()
 	case 'T':
 	  do {
 	    printf("Transition/transversion ratio?\n");
-            gets(input);
+            dogets(input, sizeof(input));
             locus_ttratio = atof(input);
 	  } while (locus_ttratio < 0.0);
 	  break;
@@ -883,7 +897,7 @@ void getoptions()
 	  if (!watt) {
 	    do {
 	      printf("Initial theta estimate?\n");
-              gets(input);
+              dogets(input, sizeof(input));
               theta0 = atof(input);
 	    } while (theta0 <= 0.0);
 	  }
@@ -909,7 +923,7 @@ void getoptions()
 	  done1 = false;
 	  while (!done1) {
 	    printf("How often to sample trees?\n");
-            gets(input);
+            dogets(input, sizeof(input));
             increm[0] = atoi(input);
 	    if (increm[0] > 0)
 	      done1 = true;
@@ -922,7 +936,7 @@ void getoptions()
 	  done1 = false;
 	  while (!done1) {
 	    printf("How many short steps?\n");
-            gets(input);
+            dogets(input, sizeof(input));
             steps[0] = atoi(input);
 	    if (steps[0] > 0)
 	      done1 = true;
@@ -935,7 +949,7 @@ void getoptions()
 	  done1 = false;
 	  while (!done1) {
 	    printf("How often to sample trees?\n");
-            gets(input);
+            dogets(input, sizeof(input));
             increm[1] = atoi(input);
 	    if (increm[1] > 0)
 	      done1 = true;
@@ -948,7 +962,7 @@ void getoptions()
 	  done1 = false;
 	  while (!done1) {
 	    printf("How many long steps?\n");
-            gets(input);
+            dogets(input, sizeof(input));
             steps[1] = atoi(input);
 	    if (steps[1] > 0)
 	      done1 = true;