1 | /* |
---|
2 | * Copyright (c) 1992-1998 Michael A. Cooper. |
---|
3 | * This software may be freely used and distributed provided it is not |
---|
4 | * sold for profit or used in part or in whole for commercial gain |
---|
5 | * without prior written agreement, and the author is credited |
---|
6 | * appropriately. |
---|
7 | */ |
---|
8 | |
---|
9 | #ifndef lint |
---|
10 | static char *RCSid = "$Revision: 1.1.1.3 $"; |
---|
11 | #endif |
---|
12 | |
---|
13 | /* |
---|
14 | * Miscellaneous functions |
---|
15 | */ |
---|
16 | |
---|
17 | #include "defs.h" |
---|
18 | #include <sys/stat.h> |
---|
19 | |
---|
20 | /* |
---|
21 | * Front end to calloc() |
---|
22 | */ |
---|
23 | void *xcalloc(nele, esize) |
---|
24 | size_t nele; |
---|
25 | size_t esize; |
---|
26 | { |
---|
27 | void *p; |
---|
28 | |
---|
29 | #if defined(SYSINFO_ALLOC_DEBUG) |
---|
30 | SImsg(SIM_DBG, "calloc nele %d size %d", nele, esize); |
---|
31 | #endif |
---|
32 | if ((p = (void *) calloc(nele, esize)) == NULL) { |
---|
33 | SImsg(SIM_GERR, "calloc(%d, %d) failed.", nele, esize); |
---|
34 | exit(1); |
---|
35 | } |
---|
36 | |
---|
37 | return(p); |
---|
38 | } |
---|
39 | |
---|
40 | void *xmalloc(size) |
---|
41 | size_t size; |
---|
42 | { |
---|
43 | void *newptr; |
---|
44 | |
---|
45 | #if defined(SYSINFO_ALLOC_DEBUG) |
---|
46 | SImsg(SIM_DBG, "malloc size %d", size); |
---|
47 | #endif |
---|
48 | if (!(newptr = (void *) malloc(size))) { |
---|
49 | SImsg(SIM_GERR, "malloc size %d failed: %s", size, SYSERR); |
---|
50 | exit(1); |
---|
51 | } |
---|
52 | |
---|
53 | return(newptr); |
---|
54 | } |
---|
55 | |
---|
56 | void *xrealloc(ptr, size) |
---|
57 | void *ptr; |
---|
58 | size_t size; |
---|
59 | { |
---|
60 | void *newptr; |
---|
61 | |
---|
62 | #if defined(SYSINFO_ALLOC_DEBUG) |
---|
63 | SImsg(SIM_DBG, "realloc 0x%x size %d", ptr, size); |
---|
64 | #endif |
---|
65 | if (!(newptr = (void *) realloc(ptr, size))) { |
---|
66 | SImsg(SIM_GERR, "realloc 0x%x size %d failed: %s", ptr, size, SYSERR); |
---|
67 | exit(1); |
---|
68 | } |
---|
69 | |
---|
70 | return(newptr); |
---|
71 | } |
---|
72 | |
---|
73 | /* |
---|
74 | * Convert integer to ASCII |
---|
75 | */ |
---|
76 | char *itoa(Num) |
---|
77 | u_long Num; |
---|
78 | { |
---|
79 | static char Buf[64]; |
---|
80 | |
---|
81 | (void) snprintf(Buf, sizeof(Buf), "%d", Num); |
---|
82 | |
---|
83 | return(Buf); |
---|
84 | } |
---|
85 | |
---|
86 | /* |
---|
87 | * Convert integer to string version in hex |
---|
88 | */ |
---|
89 | char *itoax(Num) |
---|
90 | u_long Num; |
---|
91 | { |
---|
92 | static char Buff[64]; |
---|
93 | |
---|
94 | (void) snprintf(Buff, sizeof(Buff), "0x%x", Num); |
---|
95 | |
---|
96 | return(Buff); |
---|
97 | } |
---|
98 | |
---|
99 | /* |
---|
100 | * Test to see if FileName exists. |
---|
101 | */ |
---|
102 | extern int FileExists(FileName) |
---|
103 | char *FileName; |
---|
104 | { |
---|
105 | int Exists = FALSE; |
---|
106 | struct stat StatBuf; |
---|
107 | |
---|
108 | if (!FileName) |
---|
109 | return(0); |
---|
110 | |
---|
111 | if (stat(FileName, &StatBuf) == 0) |
---|
112 | Exists = TRUE; |
---|
113 | |
---|
114 | SImsg(SIM_DBG, "File <%s> %s", FileName, (Exists) ? "Exists" : SYSERR); |
---|
115 | |
---|
116 | return(Exists); |
---|
117 | } |
---|
118 | |
---|
119 | /* |
---|
120 | * Test to see if Path is a file |
---|
121 | */ |
---|
122 | extern int IsFile(PathName) |
---|
123 | char *PathName; |
---|
124 | { |
---|
125 | int RetVal = FALSE; |
---|
126 | struct stat StatBuf; |
---|
127 | |
---|
128 | if (!PathName) |
---|
129 | return(0); |
---|
130 | |
---|
131 | if (stat(PathName, &StatBuf) == 0 && S_ISREG(StatBuf.st_mode)) |
---|
132 | RetVal = TRUE; |
---|
133 | |
---|
134 | SImsg(SIM_DBG, "Path <%s> %s", PathName, (RetVal) ? "IsFile" : "NotFile"); |
---|
135 | |
---|
136 | return(RetVal); |
---|
137 | } |
---|
138 | |
---|
139 | /* |
---|
140 | * Test to see if Path is a Directory |
---|
141 | */ |
---|
142 | extern int IsDir(PathName) |
---|
143 | char *PathName; |
---|
144 | { |
---|
145 | int RetVal = FALSE; |
---|
146 | struct stat StatBuf; |
---|
147 | |
---|
148 | if (!PathName) |
---|
149 | return(0); |
---|
150 | |
---|
151 | if (stat(PathName, &StatBuf) == 0 && S_ISDIR(StatBuf.st_mode)) |
---|
152 | RetVal = TRUE; |
---|
153 | |
---|
154 | SImsg(SIM_DBG, "Path <%s> %s", PathName, (RetVal) ? "IsDir" : "NotDir"); |
---|
155 | |
---|
156 | return(RetVal); |
---|
157 | } |
---|
158 | |
---|
159 | |
---|
160 | /* |
---|
161 | * Clean "String" by removing leading and trailing whitespace. |
---|
162 | * Returns pointer to new (allocated) copy. |
---|
163 | */ |
---|
164 | extern char *CleanString(String, StrLen, Flags) |
---|
165 | char *String; |
---|
166 | int StrLen; |
---|
167 | int Flags; |
---|
168 | { |
---|
169 | register char *cp; |
---|
170 | register char *bp; |
---|
171 | register char *end; |
---|
172 | register int Len; |
---|
173 | register int i; |
---|
174 | char *Buffer; |
---|
175 | char LastChar = CNULL; |
---|
176 | |
---|
177 | if (!String || !StrLen) |
---|
178 | return((char *) NULL); |
---|
179 | |
---|
180 | /* Skip over any leading white space */ |
---|
181 | for (cp = String; cp && *cp && isspace(*cp); ++cp); |
---|
182 | Len = StrLen - (cp - String); |
---|
183 | |
---|
184 | /* Copy to working Buffer */ |
---|
185 | Buffer = (char *) xcalloc(1, Len + 1); |
---|
186 | for (bp = Buffer, i = 0; i < Len; ++i, ++cp) { |
---|
187 | /* |
---|
188 | * Skip if this is SPACE and last char was SPACE or this char |
---|
189 | * is not printable |
---|
190 | */ |
---|
191 | if ((LastChar && isspace(*cp) && isspace(LastChar)) || !isprint(*cp)) { |
---|
192 | LastChar = *cp; |
---|
193 | continue; |
---|
194 | } |
---|
195 | *bp++ = *cp; |
---|
196 | *bp = CNULL; |
---|
197 | LastChar = *cp; |
---|
198 | } |
---|
199 | |
---|
200 | /* |
---|
201 | * Remove all trailing spaces. |
---|
202 | * bp points to end of Buffer at this point. |
---|
203 | */ |
---|
204 | end = bp; |
---|
205 | for (cp = end - 1; cp > Buffer && isspace(*cp); --cp); |
---|
206 | if (cp != end - 1 && cp && isspace(*++cp)) |
---|
207 | *cp = CNULL; |
---|
208 | |
---|
209 | return(Buffer); |
---|
210 | } |
---|