Ticket #49235: patch-getline.diff-2

File patch-getline.diff-2, 1.9 KB (added by ballapete (Peter "Pete" Dyballa), 9 years ago)

Improved patch file

Line 
1--- src/polkitagent/polkitagenthelperprivate.c-orig     2015-06-19 22:31:02.000000000 +0200
2+++ src/polkitagent/polkitagenthelperprivate.c  2015-10-12 14:59:13.000000000 +0200
3@@ -45,6 +45,78 @@
4 }
5 #endif
6 
7+// getline() is only available on OS X Lion and newer
8+
9+#ifdef __APPLE__
10+#if __MAC_OS_X_VERSION_MIN_REQUIRED >= 1050 || __ENVIRONMENT_MAC_OS_X_VERSION_MIN_REQUIRED__ >= 1050
11+#include <Availability.h>
12+#endif
13+#if __MAC_OS_X_VERSION_MIN_REQUIRED <= 1060
14+
15+static const int line_size = 128;
16+
17+static ssize_t
18+getdelim (char **lineptr, size_t *n, int delim, FILE *stream);
19+
20+static ssize_t
21+getline (char **lineptr, size_t *n, FILE *stream);
22+
23+static ssize_t
24+getdelim (char **lineptr, size_t *n, int delim, FILE *stream)
25+{
26+  int indx = 0;
27+  int c;
28+
29+  /* Sanity checks.  */
30+  if (lineptr == NULL || n == NULL || stream == NULL)
31+    return -1;
32+
33+  /* Allocate the line the first time.  */
34+  if (*lineptr == NULL)
35+    {
36+      *lineptr = malloc (line_size);
37+      if (*lineptr == NULL)
38+        return -1;
39+      *n = line_size;
40+    }
41+
42+  /* Clear the line.  */
43+  memset (*lineptr, '\0', *n);
44+
45+  while ((c = getc (stream)) != EOF)
46+    {
47+      /* Check if more memory is needed.  */
48+      if (indx >= *n)
49+        {
50+          *lineptr = realloc (*lineptr, *n + line_size);
51+          if (*lineptr == NULL)
52+            {
53+              return -1;
54+            }
55+          /* Clear the rest of the line.  */
56+          memset(*lineptr + *n, '\0', line_size);
57+          *n += line_size;
58+        }
59+
60+      /* Push the result in the line.  */
61+      (*lineptr)[indx++] = c;
62+
63+      /* Bail out.  */
64+      if (c == delim)
65+        {
66+          break;
67+        }
68+    }
69+  return (c == EOF) ? -1 : indx;
70+}
71+
72+static ssize_t
73+getline (char **lineptr, size_t *n, FILE *stream)
74+{
75+  return getdelim (lineptr, n, '\n', stream);
76+}
77+#endif
78+#endif
79 
80 char *
81 read_cookie (int argc, char **argv)