summaryrefslogtreecommitdiff
path: root/sci-mathematics/nauty/files/nauty-2.9.3-zlib-dimacs2g.patch
blob: b96a19d5db3f86a760db70df0cb99e34d24983e3 (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
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
215
216
217
218
219
220
221
222
223
224
225
226
From 1f1346ae31c22f37db2f1511295f6f810b3aa7cc Mon Sep 17 00:00:00 2001
From: Michael Orlitzky <michael@orlitzky.com>
Date: Sat, 15 Aug 2026 16:36:57 -0400
Subject: [PATCH] dimacs2g.c,makefile.in: prefer zlib to gunzip exec

This is the debian upstream-zlib-dimacs2g.patch, but with the
makefile.in hunk edited to apply cleanly to the upstream src.
---
 dimacs2g.c  | 101 +++++++++++++++++++++-------------------------------
 makefile.in |   2 +-
 2 files changed, 42 insertions(+), 61 deletions(-)

diff --git a/dimacs2g.c b/dimacs2g.c
index 0b68d51..5bafc99 100644
--- 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 @@ typedef struct
    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 @@ readdimacsgraph(FILE *f, sparsegraph *g)
                 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 @@ readdimacsgraph(FILE *f, sparsegraph *g)
                 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 @@ readdimacsgraph(FILE *f, sparsegraph *g)
                 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 @@ readdimacsgraph(FILE *f, sparsegraph *g)
             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 @@ readdimacsgraph(FILE *f, sparsegraph *g)
 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 @@ main(int argc, char *argv[])
     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 @@ main(int argc, char *argv[])
 
         if (nofiles)
             nofiles = FALSE;
-        else if (iszip)
-        {
-#if HAVE_GUNZIP && HAVE_POPEN
-            pclose(infile);
-#endif
-        }
         else
-            fclose(infile);
+            gzclose(infile);
     }
 
     exit(0);
diff --git a/makefile.in b/makefile.in
index da077f0..3a9850b 100644
--- a/makefile.in
+++ b/makefile.in
@@ -597,7 +597,7 @@ ancestorg : ${GTOOLSH} ancestorg.c gtools${XO} ${XNAUTYO}
 	${LTP} -o ancestorg ancestorg.c gtools${XO} ${XNAUTYO}
 
 dimacs2g : ${GTOOLSH} dimacs2g.c naututil${XO} gtools${XO} ${XNAUTYO}
-	${LTP} -o dimacs2g dimacs2g.c naututil${XO} gtools${XO} ${XNAUTYO}
+	${LTP} -o dimacs2g dimacs2g.c naututil${XO} gtools${XO} ${XNAUTYO} -lz
 
 catg : ${GTOOLSH} catg.c gtools${XO}
 	${LTP} -o catg catg.c gtools${XO}
-- 
2.55.0