File: upstream-zlib-dimacs2g.patch

package info (click to toggle)
nauty 2.9.1-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 100,416 kB
  • sloc: ansic: 125,567; sh: 4,157; makefile: 4,129
file content (214 lines) | stat: -rw-r--r-- 6,203 bytes parent folder | download | duplicates (2)
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
Description: Invoke zlib functions rather than calling gunzip in a subprocess.
 Inspired by the debian nauty-zlib-blisstog patch.
Author: Jerome Benoit <calculus@rezozer.net>
Author: Jerry James <loganjerry@gmail.com>
Origin: https://src.fedoraproject.org/rpms/nauty/blob/rawhide/f/nauty-zlib-dimacs2g.patch
Last-Update: 2022-11-18

--- a/dimacs2g.c
+++ b/dimacs2g.c
@@ -18,10 +18,10 @@
 
 /*************************************************************************/
 
+#include <zlib.h>
 #include "gtools.h" 
 
-#define GUNZIP "gunzip -c"
-
+#define BUFSIZE 256
 #define MAXCOMMENT 200
 static char comment[MAXCOMMENT+3];
 static int commentlen;
@@ -31,39 +31,30 @@
    int v,w;
 } vpair;
 
-static int
-nextchar(FILE *f)
-{
-    char s[2];
-
-    if (fscanf(f,"%1s",s) != 1) return EOF;
-    else                        return s[0];
-}
-
 static boolean
-readdimacsgraph(FILE *f, sparsegraph *g)
+readdimacsgraph(gzFile f, sparsegraph *g)
 /* Reads a graph from Dimacs format into a sparse graph */
 {
-    int n,c;
+    int n;
     unsigned long ne,j;
     int haven;
     int i,v,w;
     DYNALLSTAT(vpair,elist,elist_sz);
+    char buffer[BUFSIZE];
 
+    memset(buffer, '\0', BUFSIZE);
     commentlen = 0;
     haven = 0;
     j = 0;
-    while ((c = nextchar(f)) >= 0)
+    while (gzgets(f, buffer, BUFSIZE) != NULL && strlen(buffer) < BUFSIZE - 1)
     {
-        switch (c)
+        switch (*buffer)
         {
         case 'c':
-            commentlen = 0;
-            while ((c = getc(f)) != '\n' && c != EOF)
-            {
-                if (commentlen < MAXCOMMENT)
-                    comment[commentlen++] = c;
-            }
+            commentlen = strlen(buffer);
+            if (commentlen > MAXCOMMENT)
+                commentlen = MAXCOMMENT;
+            memcpy(comment, &buffer[1], commentlen);
             comment[commentlen] = '\0';
             break;
 
@@ -73,7 +64,7 @@
                 fprintf(stderr,"Duplicate p line\n");
                 exit(1);
             }
-            if (fscanf(f," edge %d %lu",&n,&ne) != 2)
+            if (sscanf(&buffer[1]," edge %d %lu",&n,&ne) != 2)
             {
                 fprintf(stderr,"Bad p line\n");
                 return FALSE;
@@ -88,7 +79,7 @@
                 fprintf(stderr,"Missing p line\n");
                 return FALSE;
             }  
-            if (fscanf(f,"%d%d",&w,&v) != 2 || w < 1 || w > n)
+            if (sscanf(&buffer[1],"%d%d",&w,&v) != 2 || w < 1 || w > n)
             {
                 fprintf(stderr,"Bad n line\n");
                 return FALSE;
@@ -101,7 +92,7 @@
                 fprintf(stderr,"Missing p line or too many e lines\n");
                 return FALSE;
             }
-            if (fscanf(f,"%d%d",&v,&w) != 2 || v < 1 || w < 1 || v > n || w > n)
+            if (sscanf(&buffer[1],"%d%d",&v,&w) != 2 || v < 1 || w < 1 || v > n || w > n)
             {
                 fprintf(stderr,"Bad e line\n");
                 return FALSE;
@@ -111,11 +102,22 @@
             break;
 
         default:
-            fprintf(stderr,"Unknown line %c\n",c);
+            fprintf(stderr,"Unknown line %c\n",*buffer);
             return FALSE;
         }
     }
 
+    if (errno)
+    {
+        fputs("Corrupted data file\n", stderr);
+        return FALSE;
+    }
+    else if (strlen(buffer) == BUFSIZE - 1)
+    {
+        fputs("Corruped data line\n", stderr);
+        return FALSE;
+    }
+
     if (j != ne)
     {
         fprintf(stderr,"Wrong number of e lines\n");
@@ -152,13 +154,11 @@
 int
 main(int argc, char *argv[])
 {
-    FILE *infile;
+    gzFile infile;
     int j,firstarg;
     SG_DECL(g);
-    size_t flen;
-    boolean nocomment,nofiles,nswitch,iszip,dreadnaut,badargs;
+    boolean nocomment,nofiles,nswitch,dreadnaut,badargs;
     long nmin,nmax;
-    char zcmd[550];
     char *arg,sw;
     char *prestring,*poststring;
 
@@ -205,36 +205,23 @@
     for (j = firstarg; j < argc || nofiles; ++j)
     {
         if (nofiles)
-            infile = stdin;
-        else
         {
-            flen = strlen(argv[j]);
-            if (flen >= 3 && strcmp(argv[j]+flen-3,".gz") == 0)
+            infile = gzdopen(STDIN_FILENO,"r");
+            if (infile == NULL)
             {
-#if HAVE_GUNZIP && HAVE_POPEN
-                if (strlen(argv[j]) > 500) gt_abort(">E file name too long\n");
-                sprintf(zcmd,"%s \"%s\"",GUNZIP,argv[j]);
-                if ((infile = popen(zcmd,"r")) == NULL)
-                {
-                    gt_abort_1(
-                       ">E dimacs2g: cannot open gunzip pipe for \"%s\"\n",
-                       argv[j]);
-                }
-                iszip = TRUE;
-#else
-                gt_abort(">E dimacs2g is not compiled with gunzip support\n");
-#endif
+                fputs(">E Can't open stdin\n",stderr);
+                gt_abort(NULL);
             }
-            else
+        }
+        else
+        {
+            infile = gzopen(argv[j],"r");
+            if (infile == NULL)
             {
-                if ((infile = fopen(argv[j],"r")) == NULL)
-                {
-                    gt_abort_1(">E Can't open file %s\n",argv[j]);
-                }
-                iszip = FALSE;
+                fprintf(stderr,">E Can't open file %s\n",argv[j]);
+                gt_abort(NULL);
             }
         }
-
         if (!readdimacsgraph(infile,&g))
         {
             gt_abort_1(">E Dimacs error in file %s\n",argv[j]);
@@ -260,14 +247,8 @@
 
         if (nofiles)
             nofiles = FALSE;
-        else if (iszip)
-        {
-#if HAVE_GUNZIP && HAVE_POPEN
-            pclose(infile);
-#endif
-        }
         else
-            fclose(infile);
+            gzclose(infile);
     }
 
     exit(0);
--- a/makefile.in
+++ b/makefile.in
@@ -504,7 +504,7 @@
 	${CC} -o ancestorg ${CFLAGS} ancestorg.c gtools.o ${NAUTYO} ${LDFLAGS}
 
 dimacs2g : ${GTOOLSH} dimacs2g.c naututil.o gtools.o ${NAUTYO}
-	${CC} -o dimacs2g ${CFLAGS} dimacs2g.c naututil.o gtools.o ${NAUTYO} ${LDFLAGS}
+	${CC} -o dimacs2g ${CFLAGS} dimacs2g.c naututil.o gtools.o ${NAUTYO} ${LDFLAGS} -lz
 
 catg : ${GTOOLSH} catg.c gtools.o
 	${CC} -o catg ${CFLAGS} catg.c gtools.o ${LDFLAGS}