~ubuntu-branches/debian/sid/deborphan/sid

« back to all changes in this revision

Viewing changes to src/file.c

  • Committer: Bazaar Package Importer
  • Author(s): Peter Palfrader
  • Date: 2001-10-28 01:28:24 UTC
  • Revision ID: james.westby@ubuntu.com-20011028012824-0jvhpixo5ldm3h85
Tags: upstream-1.0
ImportĀ upstreamĀ versionĀ 1.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* file.c - Functions to read the status file for deborphan.
 
2
   Copyright (C) 2000 Cris van Pelt
 
3
 
 
4
   Distributed under the terms of the Artistic License.
 
5
*/
 
6
 
 
7
#include <stdio.h>
 
8
#include <stdlib.h>
 
9
#include <unistd.h>
 
10
#include <string.h>
 
11
#include <fcntl.h>
 
12
 
 
13
#include <sys/types.h>
 
14
#include <sys/stat.h>
 
15
 
 
16
#include <config.h>
 
17
#include <deborphan.h>
 
18
#include <xalloc.h>
 
19
 
 
20
#ifdef LOW_MEM
 
21
/* Be nice. This uses a little more memory than the longest line in your
 
22
 * status file (which probably means some 100 bytes).
 
23
 *
 
24
 * This is more or less how everything was handled before version 0.1.20.
 
25
 */
 
26
FILE *
 
27
statusopen(const char *file)
 
28
{
 
29
    FILE *f;
 
30
 
 
31
    f = fopen(file, "r");
 
32
 
 
33
    return f ? f : NULL;
 
34
}
 
35
 
 
36
char *
 
37
nextline(FILE **fp)
 
38
{
 
39
    char t[32];
 
40
    static int i =  32;
 
41
    static char *s;
 
42
    if (!s)
 
43
        s = xmalloc (i * sizeof(char));
 
44
 
 
45
    fgets(s, i-1, *fp);
 
46
    if (!s)
 
47
        return NULL;
 
48
 
 
49
    while (!strchr(s, '\n')) {
 
50
        if (!fgets(t, 31, *fp))
 
51
            return NULL;
 
52
        s = xrealloc(s, i+=32);
 
53
        strcat(s, t);
 
54
    }
 
55
    *(strchr(s, '\n')) = '\0';
 
56
 
 
57
    return s;
 
58
}
 
59
 
 
60
#else
 
61
/* Don't be nice. This uses up as much memory as your status file (near a
 
62
 * megabyte). It is considerably faster, though.
 
63
 */
 
64
char *
 
65
statusopen(const char *file)
 
66
{
 
67
    int fd;
 
68
    char *buf;
 
69
    struct stat statbuf;
 
70
 
 
71
    fd = open(file, O_RDONLY);
 
72
    if (fd < 0)
 
73
        return NULL;
 
74
 
 
75
    fstat(fd, &statbuf);
 
76
    buf = (char *) xmalloc(statbuf.st_size + 1 * sizeof(char));
 
77
 
 
78
    if (read(fd, buf, statbuf.st_size) < statbuf.st_size) {
 
79
        close(fd);
 
80
        return NULL;
 
81
    }
 
82
 
 
83
    *(buf + statbuf.st_size + 1) = '\0';
 
84
    close(fd);
 
85
    return buf;
 
86
}
 
87
 
 
88
/* This function is similar to strsep(s, "\n"), but I have the feeling this
 
89
 * is faster, because it only checks for '\n'. Well, that, and I just like 
 
90
 * writing this *(s+1) stuff.
 
91
 */
 
92
char *
 
93
nextline(char **s)
 
94
{
 
95
    char *a = *s;
 
96
    char *n;
 
97
    
 
98
    if (!a)
 
99
        return NULL;
 
100
    for (n = *s; *n && *n != '\n'; n++);
 
101
 
 
102
    if (*n == '\0')
 
103
        return NULL;
 
104
 
 
105
    *n = '\0';
 
106
    *s = n + 1;
 
107
 
 
108
    return a;
 
109
}
 
110
#endif /* ! LOW_MEM */