File: misc.c

package info (click to toggle)
newsgate 1.6-10
  • links: PTS
  • area: non-free
  • in suites: slink
  • size: 332 kB
  • ctags: 310
  • sloc: ansic: 2,682; yacc: 499; sh: 278; lex: 183; perl: 151; makefile: 113
file content (164 lines) | stat: -rw-r--r-- 3,068 bytes parent folder | download | duplicates (6)
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
/*
**  Miscellaneous routines.
*/
#include "gate.h"
#if	defined(RCSID)
static char RCS[] =
	"$Header: /nfs/papaya/u2/rsalz/src/newsgate/RCS/misc.c,v 1.9 91/07/18 11:04:27 rsalz Exp $";
#endif	/* defined(RCSID) */


/*
**  Allocate memory.
*/
align_t
MyAlloc(i)
    int			i;
{
    align_t		p;

    if ((p = (align_t)malloc((unsigned int)i)) == NULL) {
	Fprintf(stderr, "%s:  Could not allocate %d bytes: %s\n",
		Pname, i, strerror(errno));
	exit(EX_OSERR);
    }
    return p;
}


#if	!defined(HAVE_STRERROR)
/*
**  Return a printable representation of errno.
*/
char *
strerror(x)
    int			x;
{
    static char		buff[20];

    if (x >= 0 && x < sys_nerr)
	return sys_errlist[x];
    Sprintf(buff, "Error code %d", x);
    return buff;
}
#endif	/* !defined(HAVE_STRERROR) */


/*
**  Free up something that Split() made.
*/
void
SplitFree(p)
    char	***p;
{
    if (p && *p) {
	free((*p)[0]);
	free((char *)*p);
	*p = NULL;
    }
}


/*
**  This is an AWK-style split routine.  If the split character is NULL,
**  we split on space or tab and eat long stretches of same (that is, no
**  null fields will result).  Returns number of fields made.
*/
int
Split(s, p, c)
    register char	*s;
    char		***p;
    register char	c;
{
    register char	*cp;
    register int	blank;
    register int	n;
    register int	i;

    if (s == NULL || *s == '\0') {
	Fprintf(stderr, "%s:  Someone was a pinhead!\n", Pname);
	abort();
    }

    i = strlen(s) + 1;
    cp = NEW(char, i);
    /* This is too much -- we'll realloc what we really need later. */
    *p = NEW(char*, i);

    if (c == '\0') {
	/* Eat multiple whitespace characters. */
	for (blank = TRUE, n = 0; *s; s++)
	    if (WHITE(*s)) {
		if (!blank)
		    *cp++ = '\0';
		blank = TRUE;
	    }
	    else {
		if (blank)
		    (*p)[n++] = cp;
		*cp++ = *s;
		blank = FALSE;
	    }

	if (n == 0) {
	    (*p)[0] = cp;
	    SplitFree(p);
	    return 0;
	}
    }
    else
	/* Normal case: find all occurrences of "c" and null them as field
	 * terminators; add new string starts to the array of pointers to
	 * strings. */
	for ((*p)[0] = cp, n = 1; *s; s++)
	    if (*s == c) {
		*cp++ = '\0';
		(*p)[n++] = cp;
	    }
	    else
		*cp++ = *s;

    /* Make sure the last string is terminated. */
    *cp = '\0';

    /* Free up the excess space. */
    *p = (char **)REALLOC(*p, (n + 1) * sizeof (char *));
    (*p)[n] = NULL;
    return n;
}



/*
**  Read in a file of lines, dump it into an array of strings.
*/
char **
ReadFile(Name)
    char		*Name;
{
    register FILE	*F;
    register char	**v;
    register char	*p;
    register int	i;
    char		**value;
    char		buff[SM_SIZE];

    /* Open file, count number of lines therein. */
    if ((F = fopen(Name, "r")) == NULL) {
	perror(Name);
	exit(EX_OSFILE);
    }
    for (i = 1; fgets(buff, sizeof buff, F); i++)
	continue;

    rewind(F);
    for (v = value = NEW(char*, i); fgets(buff, sizeof buff, F); ) {
	if ((p = IDX(buff, '\n')) != NULL)
	    *p = '\0';
	if (buff[0] && buff[0] != '#')
	    *v++ = COPY(buff);
    }

    *v = NULL;
    return value;
}