~ubuntu-branches/ubuntu/trusty/numdiff/trusty

« back to all changes in this revision

Viewing changes to read_line.c

  • Committer: Package Import Robot
  • Author(s): Paolo Greppi
  • Date: 2013-09-17 21:19:34 UTC
  • mfrom: (1.1.2)
  • Revision ID: package-import@ubuntu.com-20130917211934-b3oul22gubp4ehhp
Tags: 5.8.1-1
* New upstream release:
  - do not include the optional invariant sections clause of the GFDL and fix
    the lintian license-problem-gfdl-invariants error; closes: #709492
  - ndselect now recognizes the options -O and -D 
  - changed the long option --separator to --separators 
  - the options -s and -S support a larger set of escape sequences
  - detection of binary files
  - the short form of Numdiff option --dummy is not -D anymore, but -U
  - support for Unicode characters or whole strings as field delimiters
  - support for escape sequences in the currencies option
* Removed patches:
  - up-mkdir-manpages.patch that was integrated upstream
* fix lintian ancient-standards-version warning by updating the 
  Standards-Version field in debian/control
* fix the lintian hardening-no-relro warning by adding an
  override_dh_auto_configure in debian/rules
* update the machine-readable debian/copyright file to version 1.0 
  of the standard
* compile with -O0 as per upstream known issue with optimization and GMP;
  this causes a lintian hardening-no-fortify-functions but is required as
  long as this issue is not solved upstream

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/*
2
2
    Numdiff - compare putatively similar files, 
3
3
    ignoring small numeric differences
4
 
    Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012  Ivano Primi  <ivprimi@libero.it>
 
4
    Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013  Ivano Primi  <ivprimi@libero.it>
5
5
 
6
6
    This program is free software: you can redistribute it and/or modify
7
7
    it under the terms of the GNU General Public License as published by
17
17
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
18
18
*/
19
19
 
 
20
#ifdef _TEST_READ_LINE_
 
21
 
 
22
#include<stdio.h>
 
23
#include<stdlib.h>
 
24
#include<string.h>
 
25
 
 
26
#define BUFF_SIZE 32
 
27
 
 
28
/* Error codes */
 
29
#define OK             0
 
30
#define LINE_INTERR    1
 
31
#define EOF_REACHED    2
 
32
#define READING_ERROR  3
 
33
#define OUT_OF_MEMORY  4
 
34
/* *** */
 
35
#define OPEN_FAILED    5
 
36
#define WRONG_USAGE    6
 
37
#define FILE_IS_BINARY 7
 
38
 
 
39
#endif /* _TEST_READ_LINE_ */
 
40
 
20
41
char* read_line (FILE* pf, int* errcode)
21
42
{
22
43
  char buffer[BUFF_SIZE];
23
44
  char *ptr, *line = NULL;
24
45
  size_t lline = 0;
25
46
 
26
 
  while ((ptr = fgets (buffer, BUFF_SIZE,pf)))
 
47
  register size_t n;
 
48
  register int ch;
 
49
  int exception_occurred = 0;
 
50
 
 
51
  do
27
52
    {
28
 
      lline += strlen (buffer);
29
 
      if (!line)
30
 
        ptr = (char*) calloc (lline + 1, sizeof(char));
31
 
      else
32
 
        ptr = (char*) realloc ((void*)line, (lline + 1) * sizeof(char));
33
 
      if (!ptr)
34
 
        {
35
 
          if ((line))
36
 
            free ((void*)line);
37
 
          *errcode = OUT_OF_MEMORY;
38
 
          return NULL;
39
 
        }
40
 
      else
41
 
        {
42
 
          line = ptr;
43
 
          strcat (line, buffer);
44
 
        }
45
 
      if (lline > 0 && line[lline-1] == '\n')
46
 
        break;
47
 
    }
48
 
  if (!ptr)
 
53
      for (n = 0; n < BUFF_SIZE-1 && (ch = getc(pf)) != EOF && ch != '\0' && ch != '\n'; n++)
 
54
        buffer[n] = ch;
 
55
      if ( !(exception_occurred = ((ch == EOF && n == 0) || ch == '\0')) )
 
56
        {
 
57
          /* 
 
58
             If we enter this block, then CH is not the nul character. 
 
59
             Thus either is n == BUFF_SIZE-1, or is CH == EOF or is CH == NEWLINE.
 
60
           */
 
61
          if (n == BUFF_SIZE-1 || ch == EOF)
 
62
            {
 
63
              buffer[n] = '\0';
 
64
              lline += n;
 
65
            }
 
66
          else
 
67
            {
 
68
              buffer[n] = '\n';
 
69
              buffer[n+1] = '\0';
 
70
              lline += (n+1);         
 
71
            }
 
72
          if (!line)
 
73
            ptr = (char*) calloc (lline + 1, sizeof(char));
 
74
          else
 
75
            ptr = (char*) realloc ((void*)line, (lline + 1) * sizeof(char));
 
76
          if (!ptr)
 
77
            {
 
78
              if ((line))
 
79
                free ((void*)line);
 
80
              *errcode = OUT_OF_MEMORY;
 
81
              return NULL;
 
82
            }
 
83
          else
 
84
            {
 
85
              line = ptr;
 
86
              strcat (line, buffer);
 
87
            }
 
88
          if (lline > 0 && line[lline-1] == '\n')
 
89
            break;
 
90
        }
 
91
    } while (!exception_occurred);
 
92
  if ((exception_occurred))
49
93
    {
50
94
      if ( (ferror(pf)) )
51
95
        *errcode = READING_ERROR;
 
96
      else if ( ch == '\0' )
 
97
        *errcode = FILE_IS_BINARY;
52
98
      else if (lline > 0)
53
99
        *errcode = LINE_INTERR;
54
100
      else
58
104
    *errcode = OK;
59
105
  return line;
60
106
 
107
 
 
108
#ifdef _TEST_READ_LINE_
 
109
 
 
110
int main (int argc, char* argv[])
 
111
{
 
112
  FILE* fp;
 
113
 
 
114
  if (argc != 2)
 
115
    {
 
116
      fprintf (stderr, "\n***  Usage: %s FILEPATH\n", argv[0]);
 
117
      return 1;
 
118
    }
 
119
  else if ( !(fp = fopen (argv[1], "r")) )
 
120
    {
 
121
      fprintf (stderr, "\n*** Cannot open \"%s\" for reading:\n", argv[1]);
 
122
      perror(NULL);
 
123
      return 1;
 
124
    }
 
125
  else
 
126
    {
 
127
      char* line = NULL;
 
128
      int errcode = OK;
 
129
 
 
130
      do
 
131
        {
 
132
          line = read_line (fp, &errcode);
 
133
          switch (errcode)
 
134
            {
 
135
            case FILE_IS_BINARY:
 
136
              fputs ("\n***  Cannot read from a binary file\n", stderr);
 
137
              break;          
 
138
            case LINE_INTERR:
 
139
              fputs (line, stdout);
 
140
              puts ("<LNINTERR>");
 
141
              break;
 
142
            case READING_ERROR:
 
143
              fputs ("\n***  Error while reading from file\n", stderr);
 
144
              break;
 
145
            case OUT_OF_MEMORY:
 
146
              fputs ("\n***  Memory exhausted while reading from file, abort now...\n", stderr);
 
147
              break;
 
148
            case EOF_REACHED:
 
149
              puts ("<EOF>");
 
150
              break;
 
151
            default: /* OK */
 
152
              fputs (line, stdout);
 
153
              break;
 
154
            }
 
155
          if ((line))
 
156
            free ((void*)line);
 
157
        } while (errcode == OK);
 
158
      if ( (fclose(fp)) )
 
159
        {
 
160
          fprintf (stderr, "\n*** Could not close \"%s\":\n", argv[1]);
 
161
          perror(NULL);
 
162
          return 1;
 
163
        }
 
164
      else
 
165
        {
 
166
          return 0;
 
167
        }
 
168
    }
 
169
}
 
170
 
 
171
#endif /* _TEST_READ_LINE_ */