Debian Codegraph @

bin/utils.c

raw symbols SCIP
1 /*
2 * utils.c
3 *
4 * Author: Kazu Yamamoto <[email protected]>
5 * Created: Sep 29, 2000
6 *
7 * Code:
8 */
9
10 #include "mew.h"
11
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <string.h>
15 #include <ctype.h>
16 #include <stdarg.h>
17
18 #if !HAVE_MEMMOVE
19 #define memmove(d, s, n) bcopy((s), (d), (n))
20 #endif
21
22 char *warn_prog;
23
24 public char *
25 getprognm(char *filename)
26 {
27 char *p, *q;
28
29 if ((p = strrchr(filename, FILESEP)) != NULL)
30 p++;
31 else
32 p = filename;
33
34 for (q = p; *q; q++) {
35 *q = tolower(*q & 0xff);
36 }
37 return p;
38 }
39
40 public void
41 warning(const char *fmt, ...)
42 {
43 va_list ap;
44
45 if (warn_prog != NULL)
46 fprintf(stderr, "%s: ", warn_prog);
47 va_start(ap, fmt);
48 if (fmt != NULL)
49 vfprintf(stderr, fmt, ap);
50 va_end(ap);
51 fprintf(stderr, "\n");
52 }
53
54 public void
55 warn_exit(const char *fmt, ...)
56 {
57 va_list ap;
58
59 if (warn_prog != NULL)
60 fprintf(stderr, "%s: ", warn_prog);
61 va_start(ap, fmt);
62 if (fmt != NULL)
63 vfprintf(stderr, fmt, ap);
64 va_end(ap);
65 fprintf(stderr, "\n");
66
67 exit(EXIT_FAILURE);
68 }
69
70 #ifndef HAVE_SNPRINTF
71 public int
72 snprintf(char *buf, int len, const char *fmt, ...)
73 {
74 int ret;
75 va_list ap;
76
77 va_start(ap, fmt);
78 ret = vsprintf(buf, fmt, ap);
79 va_end(ap);
80 return ret;
81 }
82 #endif
83
84 #ifndef HAVE_FGETLN
85 private char*
86 fgetln(FILE *fp, size_t *len)
87 {
88 static size_t bufsize = 0;
89 static char *buf = NULL;
90 char tmp[BUFSIZ];
91 size_t readlen;
92
93 if (buf == NULL) {
94 bufsize = BUFSIZ;
95 MALLOC(buf, bufsize);
96 }
97
98 *len = 0;
99 while (1) {
100 if (fgets(tmp, BUFSIZ, fp) == NULL)
101 return NULL;
102 readlen = strlen(tmp);
103
104 if (*len + readlen > bufsize) {
105 bufsize += BUFSIZ;
106 REALLOC(buf, bufsize);
107 }
108 memmove(&buf[*len], tmp, readlen);
109 *len += readlen;
110 if (readlen < BUFSIZ - 1 || tmp[BUFSIZ - 2] == '\n')
111 break;
112 }
113 return buf;
114 }
115 #endif
116
117 char*
118 Getline(FILE* fp)
119 {
120 char *s, *t;
121 size_t len;
122 if ((t = fgetln(fp, &len)) == NULL)
123 return NULL;
124 MALLOC(s, len+1);
125 memmove(s, t, len);
126 s[len] = '\0';
127 return s;
128 }
129
130 int Optind = 1;
131 char *Optarg = NULL;
132
133 public int
134 Getopt(int argc, char *argv[], const char *fmt)
135 {
136 char *p, *q, c;
137
138 Optarg = NULL;
139 if (Optind >= argc)
140 return EOF;
141 p = argv[Optind];
142
143 if (*p++ != '-')
144 return EOF;
145 c = *p;
146 if (c == NUL)
147 return EOF;
148 if (*(p + 1) != NUL)
149 warn_exit("unknown long option '-%s'.", p);
150 if ((q = strchr(fmt, c)) == NULL)
151 warn_exit("unknown option '-%c'.", c);
152 if (++q != NULL && *q == ':') {
153 if (++Optind >= argc)
154 warn_exit("no parameter for '-%c'.", c);
155 Optarg = argv[Optind];
156 }
157 Optind++;
158 return c;
159 }
160
161 /*
162 * Copyright (C) 2000-2005 Mew developing team.
163 * All rights reserved.
164 *
165 * Redistribution and use in source and binary forms, with or without
166 * modification, are permitted provided that the following conditions
167 * are met:
168 *
169 * 1. Redistributions of source code must retain the above copyright
170 * notice, this list of conditions and the following disclaimer.
171 * 2. Redistributions in binary form must reproduce the above copyright
172 * notice, this list of conditions and the following disclaimer in the
173 * documentation and/or other materials provided with the distribution.
174 * 3. Neither the name of the team nor the names of its contributors
175 * may be used to endorse or promote products derived from this software
176 * without specific prior written permission.
177 *
178 * THIS SOFTWARE IS PROVIDED BY THE TEAM AND CONTRIBUTORS ``AS IS'' AND
179 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
180 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
181 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE TEAM OR CONTRIBUTORS BE
182 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
183 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
184 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
185 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
186 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
187 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
188 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
189 */
190
191 /*
192 * utils.c ends here
193 */