1 /* -------------------------------------------------------------------- */
2 /* getword.c */
3 /* */
4 /* */
5 /* Copyright (C) 1997,1998 Angelo Masci */
6 /* */
7 /* This program is free software; you can redistribute it and/or modify */
8 /* it under the terms of the GNU General Public License as published by */
9 /* the Free Software Foundation; either version 2 of the License, or */
10 /* (at your option) any later version. */
11 /* */
12 /* This program is distributed in the hope that it will be useful, */
13 /* but WITHOUT ANY WARRANTY; without even the implied warranty of */
14 /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
15 /* GNU General Public License for more details. */
16 /* */
17 /* You should have received a copy of the GNU General Public License */
18 /* along with this program; if not, write to the Free Software */
19 /* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
20 /* */
21 /* You can contact the author at this e-mail address: */
22 /* */
23 /* angelo@styx.demon.co.uk */
24 /* */
25 /* --------------------------------------------------------------------
26 $Id$
27 -------------------------------------------------------------------- */
28
29 #include <string.h>
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <ctype.h>
33
34 #include "common.h"
35
36 /* -------------------------------------------------------------------- */
37 /* -------------------------------------------------------------------- */
libcommon_getword(char * ptr,char ** endptr)38 char *libcommon_getword(char *ptr, char **endptr)
39 {
40 char *buf,
41 *buf_ptr,
42 *lptr;
43
44 int len;
45
46
47 /* ---------------------------------------- */
48
49 while ((isspace(*ptr)) &&
50 (*ptr != '\0'))
51 {
52 ptr++;
53 }
54
55 /* ---------------------------------------- */
56
57 if (*ptr == '\0')
58 { return NULL;
59 }
60
61 /* ---------------------------------------- */
62
63 lptr = ptr;
64
65 len = 0;
66 while ((! isspace(*ptr)) &&
67 (*ptr != '\0'))
68 {
69 len++;
70 ptr++;
71 }
72
73 ptr = lptr;
74
75 /* ---------------------------------------- */
76
77 buf = (char *)malloc(sizeof(char) * (len + 1));
78 if (buf == NULL)
79 { return NULL;
80 }
81
82 /* ---------------------------------------- */
83
84 buf_ptr = buf;
85 while ((!isspace(*ptr)) &&
86 (*ptr != '\0'))
87 {
88 *buf_ptr++ = *ptr++;
89 }
90
91 *buf_ptr = '\0';
92 *endptr = ptr;
93
94 /* ---------------------------------------- */
95
96 return buf;
97 }
98