📄 mutils.c~
字号:
/* general utility routines */
#include <mutils.h>
/* file descriptor dot lock file */
static int
lock_fd=(-1);
/*
** mutilsHowmanyCommas()
** calculates how many commas will be added to the buffer
**
** RCS
** $Revision: 1 $
** $Date: 2/24/04 8:38p $
** Return Values:
** no of commas
**
** Parameters:
** buf buf to scan
**
** Side Effects:
** none
**
** Limitations and Comments:
** buf must be a initialized string (at least)
**
** Development History:
** who when why
** ma_muquit@fccc.edu Oct-18-1997 first cut
*/
int mutilsHowmanyCommas(char *buf)
{
int
n=0,
length;
if (*buf == '\0')
return(0);
length=strlen(buf);
if ((length % 3) == 0)
n=(length/3)-1;
else
n=length/3;
if ( n < 0)
n=0;
return(n);
}
/*
** mutilsCommaize()
** add a comma after every 3rd digit from right
**
** RCS
** $Revision: 1 $
** $Date: 2/24/04 8:38p $
** Return Values:
** none
**
** Parameters:
** buf buf to modify
**
** Side Effects:
** buf is modified
**
** Limitations and Comments:
** buf must have enough space to hold the extra , characters
**
** Development History:
** who when why
** ma_muquit@fccc.edu no idea first cut
** Oct-18-97 added dynamic buffer
** May-26-1999 rewrote as the old version has
** memory overstepping bug. Code adapted from
** C-Snippets, file commaflt.c by Bruce Wedding and
** and Kurt Kuzba
*/
void mutilsCommaize(char *buf)
{
char
*pbuf=(char *) NULL;
int
bf=0,
cm=0,
tm=0;
if (*buf != '\0')
{
pbuf=mutilsStrdup(buf);
if (pbuf == (char *) NULL)
return; /* malloc failed, return quitely */
mutilsReverseString(pbuf);
while ((buf[bf++]=pbuf[tm++]) != 0)
{
if(++cm % 3 == 0 && pbuf[tm])
buf[bf++]=',';
}
if (pbuf)
(void) free((char *) pbuf);
mutilsReverseString(buf);
}
}
/*
** NULL terminate the buffer at the first sight of a non-digit character
*/
/*
** mutilsCleanBuf()
* NULL terminate the buffer at the first sight of a non-digit character
**
** Parameters:
** char *buf the buffer to clean
** char *bytes_in_buf the buffer size
** int *length returns, bytes in buffer after cleaning
**
** Return Values:
**
**
** Limitations and Comments:
** buf is modified
**
** Development History:
** who when why
** ma_muquit@fccc.edu Jul-31-1999 first cut
*/
void mutilsCleanBuf(buf,bytes_in_buf,length)
char
*buf;
int
bytes_in_buf;
int
*length;
{
int
i;
int
c=0;
for (i=0; i < bytes_in_buf; i++)
{
if (!isdigit(buf[i]))
{
buf[i]='\0';
break;
}
c++;
}
*length=c;
}
/*
** mutilsParseURL()
** parse a URL of the form
** http://host:port/thepage.html
** http://host/thepage.html
**
** Parameters:
** char *url the url to parse
** char *hostname the hostname (returns)
** int hostname_len allocated space length in hostname
** int *port the port (returns)
** char *page the page (returns)
** int page_len the alloacated space length in page
**
** Return Values:
** 0 on success
** -1 on failure
**
** Limitations and Comments:
** - not much error checking.
** - hostname and page must have enough space preallocated by the caller.
**
** Development History:
** who when why
** ma_muquit@fccc.edu Jul-10-1999 first cut
** Jul-20-1999 handle buffer overflow
*/
int mutilsParseURL(url,hostname,hostname_len,port,page,page_len)
char
*url;
char
*hostname;
int
hostname_len;
int
*port;
char
*page;
int
page_len;
{
char
tmpbuf[32],
*q,
*r,
*p,
*ptmp;
int
tmpbuf_len,
hlen=0,
tlen=0;
if (url == NULL || *url == '\0')
return(-1);
/* initialize */
*port=80;
*hostname='\0';
*page='\0';
*tmpbuf='\0';
tmpbuf_len=sizeof(tmpbuf);
ptmp=mutilsStrdup(url);
if (!ptmp)
return (-1);
if (strlen(ptmp) < 7)
{
(void) free(ptmp);
return (-1);
}
/* skip http:// */
if ( (ptmp[0] == 'h' || ptmp[0] == 'H') &&
(ptmp[1] == 't' || ptmp[1] == 'T') &&
(ptmp[2] == 't' || ptmp[2] == 'T') &&
(ptmp[3] == 'p' || ptmp[3] == 'P') &&
(ptmp[4] == ':') &&
(ptmp[5] == '/') &&
(ptmp[6] == '/'))
{
p = ptmp+7;
}
else
{
(void) free(ptmp);
return (-1);
}
hlen=0;
tlen=0;
q=hostname;
r=tmpbuf;
for (; (*p != '/') && (*p != '\0'); p++)
{
/* get host part out */
if (*p != ':' && !isdigit(*p))
{
if (++hlen > hostname_len-1)
{
continue;
}
*q++ = *p;
}
/* get port out if any */
if (isdigit(*p))
{
if (++tlen > tmpbuf_len-1)
continue;
*r++ = *p;
}
}
/* NULL terminate */
*q='\0';
*r='\0';
if (*tmpbuf != '\0')
*port=atoi(tmpbuf);
/* the rest is the page */
if (*p == '\0')
return (-1);
(void) mutilsStrncpy(page,p,page_len);
(void) free(ptmp);
return (0);
}
/*
** mutilsSpacesToChar()
** converts all spaces to a single character
**
** Parameters:
** char *str - spaces to collapse from
** int c - to this character
**
** Return Values:
** pointer to the string
**
** Limitations and Comments:
** str is modified.
**
** adapted from C snipptes lib: lv1ws
**
** Development History:
** who when why
** muquit@lucent.com Mar-27-2001 first cut
*/
char *mutilsSpacesToChar(char *str,int c)
{
char
*ibuf,
*obuf,
*nbuf;
register int
i,
n;
if (str)
{
ibuf=obuf=str;
i=n=0;
while (*ibuf)
{
if (isspace(*ibuf) && n)
ibuf++;
else
{
if (!isspace(*ibuf))
n=0;
else
{
*ibuf=c;
n=1;
}
obuf[i++] = *ibuf++;
}
}
obuf[i]='\0';
}
return (str);
}
/*
** mutilsRmallws()
** remove all whitespace (leading or trailing) from a string.
**
** Parameters:
** char *str
**
** Return Values:
** pointer to the string
**
** Limitations and Comments:
** str is modified. borrowed from public domain c snippets libraray.
**
** Development History:
** who when why
** ma_muquit@fccc.edu Jul-31-1999 first cut
*/
char *mutilsRmallws(char *str)
{
char
*obuf,
*nbuf;
if (str)
{
for (obuf=str, nbuf=str; *obuf; ++obuf)
{
if (!isspace(*obuf))
*nbuf++ = *obuf;
}
*nbuf='\0';
}
return (str);
}
/*
** mutilsStristr()
** case insensitive version of ANSI strstr()
**
** Parameters:
** char *s hay stack
** char *t needle
**
** Return Values:
** pointer to hay stack where the needle is found
** NULL if not found
**
** Limitations and Comments:
** from swish package by kevin h , kevin called it lstrstr()
**
** Development History:
** who when why
** ma_muquit@fccc.edu Jul-31-1999 first cut
*/
char *mutilsStristr(s,t)
char
*s;
char
*t;
{
int
i,
j,
k,
l;
for (i = 0; s[i]; i++)
{
for (j = 0, l = k = i; s[k] && t[j] &&
tolower(s[k]) == tolower(t[j]); j++, k++)
;
if (t[j] == '\0')
return s + l;
}
return NULL;
}
/*
** mutilsIsinname()
** checks if the mask fits in the string
**
** RCS
** $Revision: 1 $
** $Date: 2/24/04 8:38p $
** Return Values:
** 1 if fits
** 0 if not
** -1 if there's a memory allocation problem
**
** Parameters:
** string source string
** mask mask string
**
** Example:
** www.fccc.edu *.fccc.edu fits
** www.fccc.edu www* fits
** www.fccc.edu *fccc* fits
** 132.138.4.6 132* fits
** etc.....
**
** Side Effects:
** none
**
** Limitations and Comments:
** borrowed from swish by Kevin H
**
** Development History:
** who when why
** ma_muquit@fccc.edu Oct-18-1997 first cut
*/
int mutilsIsinname(string,mask)
char
*string;
char
*mask;
{
int
i,
j;
char
firstchar,
lastchar,
*tempmask;
if ((*string == '\0') || (*mask == '\0'))
return (0); /* mm */
if (!strcmp(mask, "*"))
return 1;
firstchar=mask[0];
lastchar=mask[(strlen(mask) - 1)];
tempmask=(char *) malloc(strlen(mask)*sizeof(char)+1);
if (tempmask == NULL)
{
(void) fprintf(stderr,"libmutils.a-mutilsIsinname(), malloc failed\n");
return(-1);
}
for (i = j = 0; mask[i]; i++)
if (mask[i] != '*')
tempmask[j++] = mask[i];
tempmask[j]='\0';
if (firstchar == '*')
{
if (lastchar == '*')
{
if ((char *) mutilsStristr(string, tempmask))
{
free(tempmask);
return 1;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -