first commit
This commit is contained in:
@@ -0,0 +1,43 @@
|
||||
##
|
||||
# Makefile Generated by genmake 1.0, Nov-16-96
|
||||
# genmake 1.0 by ma_muquit@fccc.edu, RCS
|
||||
##
|
||||
CC= gcc
|
||||
DEFS= -DPACKAGE_NAME=\"\" -DPACKAGE_TARNAME=\"\" -DPACKAGE_VERSION=\"\" -DPACKAGE_STRING=\"\" -DPACKAGE_BUGREPORT=\"\" -DSTDC_HEADERS=1 -DHAVE_SYS_TYPES_H=1 -DHAVE_SYS_STAT_H=1 -DHAVE_STDLIB_H=1 -DHAVE_STRING_H=1 -DHAVE_MEMORY_H=1 -DHAVE_STRINGS_H=1 -DHAVE_INTTYPES_H=1 -DHAVE_STDINT_H=1 -DHAVE_UNISTD_H=1 -DHAVE_STRING_H=1 -DHAVE_STRINGS_H=1 -DHAVE_MEMORY_H=1 -DHAVE_MALLOC_H=1 -DHAVE_UNISTD_H=1 -DHAVE_CTYPE_H=1 -DHAVE_STDINT_H=1 -DHAVE_SYS_TYPES_H=1 -DHAVE_STDLIB_H=1 -DHAVE_FCNTL_H=1 -DHAVE_SYS_FILE_H=1 -DHAVE_FLOCK=1 -DHAVE_SOCKET=1 -DHAVE_HTONL=1 -DHAVE_GETHOSTNAME=1 -DHAVE_GETHOSTBYADDR=1 -DHAVE_YP_GET_DEFAULT_DOMAIN=1 -DHAVE_LIBNSL=1 -DHAVE_RES_SEARCH=1 -DHAVE_LIBRESOLV=1 -DHAVE_INET_ATON=1 -DHAVE_DN_SKIPNAME=1 -DHAVE_MKSTEMP=1 -DHAVE_OPENSSL=1
|
||||
AR= ar cq
|
||||
RANLIB= ranlib
|
||||
LIBNAME= libmsock.a
|
||||
|
||||
OPENSSL_DIR=/usr/
|
||||
OPENSSL_INC=-I/usr//include
|
||||
OPENSSL_LIBS=-L/usr//lib -lssl -lcrypto
|
||||
|
||||
INCLUDES= -I. -I/usr/include/malloc $(OPENSSL_INC)
|
||||
|
||||
DEFINES= $(INCLUDES) $(DEFS) -DSYS_UNIX=1
|
||||
CFLAGS= -O $(DEFINES)
|
||||
|
||||
SRCS = msock.c
|
||||
|
||||
OBJS = msock.o
|
||||
|
||||
.c.o:
|
||||
rm -f $@
|
||||
$(CC) $(CFLAGS) -c $*.c
|
||||
|
||||
all: $(LIBNAME)
|
||||
|
||||
$(LIBNAME) : $(OBJS)
|
||||
rm -f $@
|
||||
$(AR) $@ $(OBJS)
|
||||
$(RANLIB) $@
|
||||
|
||||
|
||||
clean:
|
||||
rm -f $(OBJS) $(LIBNAME) core
|
||||
|
||||
clean_all:
|
||||
make clean
|
||||
(cd examples/htget; make clean)
|
||||
(cd examples/echod; make clean)
|
||||
(cd examples/server_type; make clean)
|
||||
@@ -0,0 +1,337 @@
|
||||
/* a quick port of my libmsock routine to MS NT */
|
||||
/* muquit@Aug-20-2005 Mar 01 Eastern Standard Time 2001 */
|
||||
|
||||
#include "msock.h"
|
||||
|
||||
#ifdef HAVE_OPENSSL
|
||||
static SSL *s_ssl=NULL;
|
||||
#endif /* HAVE_OPENSSL */
|
||||
|
||||
static SOCKET s_sock;
|
||||
static int ssl_status=0;
|
||||
|
||||
void msock_set_socket(SOCKET sfd)
|
||||
{
|
||||
s_sock=sfd;
|
||||
}
|
||||
|
||||
SOCKET msock_get_socket(void)
|
||||
{
|
||||
return(s_sock);
|
||||
}
|
||||
|
||||
void msock_turn_ssl_on(void)
|
||||
{
|
||||
ssl_status=1;
|
||||
}
|
||||
void msock_turn_ssl_off(void)
|
||||
{
|
||||
ssl_status=0;
|
||||
}
|
||||
int msock_is_ssl_on(void)
|
||||
{
|
||||
return(ssl_status);
|
||||
}
|
||||
|
||||
#ifdef HAVE_OPENSSL
|
||||
void msock_set_ssl(SSL *ssl)
|
||||
{
|
||||
s_ssl=ssl;
|
||||
}
|
||||
SSL *msock_get_ssl(void)
|
||||
{
|
||||
return(s_ssl);
|
||||
}
|
||||
#endif /* HAVE_OPENSSL */
|
||||
|
||||
struct in_addr *atoAddr(char *address)
|
||||
{
|
||||
struct hostent
|
||||
*host;
|
||||
|
||||
static struct in_addr
|
||||
saddr;
|
||||
|
||||
saddr.s_addr=inet_addr(address);
|
||||
if (saddr.s_addr != -1)
|
||||
return (&saddr);
|
||||
|
||||
host=gethostbyname(address);
|
||||
if (host != (struct hostent *) NULL)
|
||||
return ((struct in_addr *) *host->h_addr_list);
|
||||
|
||||
return ((struct in_addr *) NULL);
|
||||
}
|
||||
|
||||
#ifdef WINNT
|
||||
/* returns 0 on success -1 on failure */
|
||||
int initWinSock(void)
|
||||
{
|
||||
WORD
|
||||
version_requested;
|
||||
|
||||
WSADATA
|
||||
wsa_data;
|
||||
|
||||
int
|
||||
err;
|
||||
|
||||
version_requested=MAKEWORD(2,0);
|
||||
err=WSAStartup(version_requested,&wsa_data);
|
||||
if (err != 0)
|
||||
{
|
||||
(void) fprintf(stderr," Unable to initialize winsock (%d)\n",err);
|
||||
return(-1);
|
||||
}
|
||||
|
||||
return(0);
|
||||
}
|
||||
#endif /* WINNT */
|
||||
|
||||
/* returns SOCKET on success INVALID_SOCKET on failure */
|
||||
SOCKET clientSocket(char *address,int port)
|
||||
{
|
||||
SOCKET
|
||||
s;
|
||||
|
||||
struct sockaddr_in
|
||||
sa;
|
||||
|
||||
struct in_addr
|
||||
*addr;
|
||||
|
||||
int
|
||||
rc;
|
||||
|
||||
#ifdef WINNT
|
||||
rc=initWinSock();
|
||||
if (rc != 0)
|
||||
return(INVALID_SOCKET);
|
||||
#endif /* WINNT */
|
||||
|
||||
addr=atoAddr(address);
|
||||
if (addr == NULL)
|
||||
{
|
||||
(void) fprintf(stderr," Invalid address: %s\n",address);
|
||||
return(INVALID_SOCKET);
|
||||
}
|
||||
|
||||
memset((char *) &sa,0,sizeof(sa));
|
||||
sa.sin_family=AF_INET;
|
||||
sa.sin_port=htons(port);
|
||||
sa.sin_addr.s_addr=addr->s_addr;
|
||||
|
||||
/* open the socket */
|
||||
s=socket(AF_INET,SOCK_STREAM,PF_UNSPEC);
|
||||
if (s == INVALID_SOCKET)
|
||||
{
|
||||
(void) fprintf(stderr," Could not create socket\n");
|
||||
return(INVALID_SOCKET);
|
||||
}
|
||||
|
||||
/* connect */
|
||||
rc=connect(s,(struct sockaddr *) &sa,sizeof(sa));
|
||||
if (rc < 0)
|
||||
return(INVALID_SOCKET);
|
||||
|
||||
|
||||
return(s);
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*
|
||||
** this function writes a character string out to a socket.
|
||||
** it returns -1 if the connection is closed while it is trying to
|
||||
** write
|
||||
*/
|
||||
static int sockWrite(SOCKET sock,char *str,size_t count)
|
||||
{
|
||||
size_t
|
||||
bytesSent=0;
|
||||
|
||||
int
|
||||
thisWrite;
|
||||
|
||||
while (bytesSent < count)
|
||||
{
|
||||
thisWrite=send(sock,str,count-bytesSent,0);
|
||||
/*
|
||||
(void) fprintf(stderr,"str=%s\n",str);
|
||||
(void) fprintf(stderr,"count=%d\n",count);
|
||||
*/
|
||||
|
||||
if (thisWrite <= 0)
|
||||
return (thisWrite);
|
||||
|
||||
bytesSent += thisWrite;
|
||||
str += thisWrite;
|
||||
}
|
||||
return (count);
|
||||
}
|
||||
|
||||
#ifdef HAVE_OPENSSL
|
||||
int sockWriteSSL(SSL *ssl,char *str,size_t count)
|
||||
{
|
||||
size_t
|
||||
bytesSent=0;
|
||||
|
||||
int
|
||||
thisWrite;
|
||||
|
||||
while (bytesSent < count)
|
||||
{
|
||||
thisWrite=SSL_write(ssl,str,count-bytesSent);
|
||||
if (thisWrite <= 0)
|
||||
return (thisWrite);
|
||||
|
||||
bytesSent += thisWrite;
|
||||
str += thisWrite;
|
||||
}
|
||||
return (count);
|
||||
}
|
||||
int sockPutsSSL(SSL *ssl,char *str)
|
||||
{
|
||||
return (sockWriteSSL(ssl,str,strlen(str)));
|
||||
}
|
||||
#endif /* HAVE_OPENSSL */
|
||||
|
||||
int sockPuts(SOCKET sock,char *str)
|
||||
{
|
||||
return (sockWrite(sock,str,strlen(str)));
|
||||
}
|
||||
|
||||
int sockGets(SOCKET sockfd,char *str,size_t count)
|
||||
{
|
||||
int
|
||||
bytesRead;
|
||||
|
||||
int
|
||||
totalCount=0;
|
||||
|
||||
char
|
||||
buf[1],
|
||||
*currentPosition;
|
||||
|
||||
char
|
||||
lastRead=0;
|
||||
|
||||
currentPosition=str;
|
||||
|
||||
while (lastRead != 10)
|
||||
{
|
||||
bytesRead=recv(sockfd,buf,1,0);
|
||||
if (bytesRead <= 0)
|
||||
{
|
||||
/*
|
||||
** the other side may have closed unexpectedly
|
||||
*/
|
||||
return (-1);
|
||||
}
|
||||
lastRead=buf[0];
|
||||
|
||||
if ((totalCount < count) && (lastRead != 10)
|
||||
&& (lastRead != 13))
|
||||
{
|
||||
*currentPosition=lastRead;
|
||||
currentPosition++;
|
||||
totalCount++;
|
||||
}
|
||||
}
|
||||
if (count > 0)
|
||||
*currentPosition=0;
|
||||
|
||||
return (totalCount);
|
||||
}
|
||||
#ifdef HAVE_OPENSSL
|
||||
int sockGetsSSL(SSL *ssl,char *str,size_t count)
|
||||
{
|
||||
int
|
||||
bytesRead;
|
||||
|
||||
int
|
||||
totalCount=0;
|
||||
|
||||
char
|
||||
buf[1],
|
||||
*currentPosition;
|
||||
|
||||
char
|
||||
lastRead=0;
|
||||
|
||||
currentPosition=str;
|
||||
while (lastRead != 10)
|
||||
{
|
||||
bytesRead=SSL_read(ssl,buf,1);
|
||||
if (bytesRead <= 0)
|
||||
{
|
||||
/*
|
||||
** the other side may have closed unexpectedly
|
||||
*/
|
||||
return (-1);
|
||||
}
|
||||
lastRead=buf[0];
|
||||
|
||||
if ((totalCount < count) && (lastRead != 10)
|
||||
&& (lastRead != 13))
|
||||
{
|
||||
*currentPosition=lastRead;
|
||||
currentPosition++;
|
||||
totalCount++;
|
||||
}
|
||||
}
|
||||
if (count > 0)
|
||||
*currentPosition=0;
|
||||
|
||||
return (totalCount);
|
||||
}
|
||||
#endif /* HAVE_OPENSSL */
|
||||
|
||||
/* must be called after msock_set_socket() */
|
||||
/* must be called after msock_set_ssl() if SSL is on */
|
||||
int msock_gets(char *str,size_t count)
|
||||
{
|
||||
if (! msock_is_ssl_on())
|
||||
{
|
||||
return(sockGets(msock_get_socket(),str,count));
|
||||
}
|
||||
else
|
||||
{
|
||||
#ifdef HAVE_OPENSSL
|
||||
return(sockGetsSSL(msock_get_ssl(),str,count));
|
||||
#endif /* HAVE_OPENSSL */
|
||||
}
|
||||
return(-1);
|
||||
}
|
||||
|
||||
/* must be called after msock_set_socket() */
|
||||
/* must be called after msock_set_ssl() if SSL is on */
|
||||
int msock_puts(char *str)
|
||||
{
|
||||
if (! msock_is_ssl_on())
|
||||
{
|
||||
return(sockPuts(msock_get_socket(),str));
|
||||
}
|
||||
else
|
||||
{
|
||||
#ifdef HAVE_OPENSSL
|
||||
return(sockPutsSSL(msock_get_ssl(),str));
|
||||
#endif /* HAVE_OPENSSL */
|
||||
}
|
||||
return(-1);
|
||||
}
|
||||
void msock_close_socket(SOCKET fd)
|
||||
{
|
||||
#ifdef WINNT
|
||||
closesocket(fd);
|
||||
#else
|
||||
close(fd);
|
||||
#endif /* MSOCK_WIN32 */
|
||||
|
||||
}
|
||||
|
||||
void msock_close(void)
|
||||
{
|
||||
msock_close_socket(msock_get_socket());
|
||||
}
|
||||
|
||||
@@ -0,0 +1,79 @@
|
||||
#ifndef MSOCK_H
|
||||
#define MSOCK_H
|
||||
|
||||
#ifdef WINNT
|
||||
#include <stdio.h>
|
||||
#include <io.h>
|
||||
#include <fcntl.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/stat.h>
|
||||
#include <string.h>
|
||||
#include <windows.h>
|
||||
|
||||
#else
|
||||
|
||||
#include <stdio.h>
|
||||
#include <unistd.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <sys/types.h> /* ULTRIX didn't like stat with types*/
|
||||
#include <sys/stat.h>
|
||||
#include <sys/file.h>
|
||||
#include <sys/socket.h>
|
||||
#include <ctype.h>
|
||||
#include <netinet/in.h>
|
||||
#include <netdb.h>
|
||||
#include <sys/ioctl.h>
|
||||
#include <arpa/inet.h> /* for inet_ntoa */
|
||||
#include <time.h> /* for ctime */
|
||||
#include <signal.h>
|
||||
#include <errno.h>
|
||||
#include <sys/wait.h>
|
||||
#include <pwd.h>
|
||||
#include <grp.h>
|
||||
#include <fcntl.h>
|
||||
#include <limits.h>
|
||||
|
||||
|
||||
#undef SOCKET
|
||||
#define SOCKET int
|
||||
|
||||
#undef INVALID_SOCKET
|
||||
#define INVALID_SOCKET -1
|
||||
#define _fileno fileno
|
||||
#define _isatty isatty
|
||||
|
||||
#endif /* ! WINNT */
|
||||
|
||||
#ifdef HAVE_OPENSSL
|
||||
|
||||
#include <openssl/ssl.h>
|
||||
#include <openssl/rand.h>
|
||||
#include <openssl/err.h>
|
||||
|
||||
#endif /* HAVE_OPENSSL */
|
||||
|
||||
SOCKET clientSocket(char *,int);
|
||||
int sockGets(SOCKET,char *,size_t);
|
||||
int sockPuts(SOCKET sock,char *str);
|
||||
|
||||
void msock_set_socket(SOCKET sock);
|
||||
SOCKET msock_get_socket(void);
|
||||
void msock_turn_ssl_on(void);
|
||||
void msock_turn_ssl_off(void);
|
||||
int msock_is_ssl_on(void);
|
||||
int msock_gets(char *buf,size_t bufsiz);
|
||||
int msock_puts(char *str);
|
||||
void msock_close_socket(SOCKET fd);
|
||||
void msock_close(void);
|
||||
|
||||
#ifdef HAVE_OPENSSL
|
||||
|
||||
int sockGetsSSL(SSL *ssl,char *buf,size_t count);
|
||||
int sockPutsSSL(SSL *ssl,char *str);
|
||||
SSL *msock_get_ssl(void);
|
||||
void msock_set_ssl(SSL *ssl);
|
||||
|
||||
#endif /* HAVE_OPENSSL */
|
||||
|
||||
#endif /* ! MSOCK_H */
|
||||
@@ -0,0 +1,34 @@
|
||||
#
|
||||
# ma_muquit@fccc.edu
|
||||
# jul-29-199
|
||||
#
|
||||
|
||||
CC= gcc
|
||||
DEFS= -DPACKAGE_NAME=\"\" -DPACKAGE_TARNAME=\"\" -DPACKAGE_VERSION=\"\" -DPACKAGE_STRING=\"\" -DPACKAGE_BUGREPORT=\"\" -DSTDC_HEADERS=1 -DHAVE_SYS_TYPES_H=1 -DHAVE_SYS_STAT_H=1 -DHAVE_STDLIB_H=1 -DHAVE_STRING_H=1 -DHAVE_MEMORY_H=1 -DHAVE_STRINGS_H=1 -DHAVE_INTTYPES_H=1 -DHAVE_STDINT_H=1 -DHAVE_UNISTD_H=1 -DHAVE_STRING_H=1 -DHAVE_STRINGS_H=1 -DHAVE_MEMORY_H=1 -DHAVE_MALLOC_H=1 -DHAVE_UNISTD_H=1 -DHAVE_CTYPE_H=1 -DHAVE_STDINT_H=1 -DHAVE_SYS_TYPES_H=1 -DHAVE_STDLIB_H=1 -DHAVE_FCNTL_H=1 -DHAVE_SYS_FILE_H=1 -DHAVE_FLOCK=1 -DHAVE_SOCKET=1 -DHAVE_HTONL=1 -DHAVE_GETHOSTNAME=1 -DHAVE_GETHOSTBYADDR=1 -DHAVE_YP_GET_DEFAULT_DOMAIN=1 -DHAVE_LIBNSL=1 -DHAVE_RES_SEARCH=1 -DHAVE_LIBRESOLV=1 -DHAVE_INET_ATON=1 -DHAVE_DN_SKIPNAME=1 -DHAVE_MKSTEMP=1 -DHAVE_OPENSSL=1
|
||||
AR= ar cq
|
||||
RANLIB= ranlib
|
||||
LIBNAME= libmutils.a
|
||||
|
||||
INCLUDES= -I. -I/usr/include/malloc
|
||||
|
||||
# replace -O with -g in order to debug
|
||||
|
||||
DEFINES= $(INCLUDES) $(DEFS) -DSYS_UNIX=1
|
||||
CFLAGS= -O $(DEFINES)
|
||||
|
||||
SRCS = string.c mutils.c mime.c
|
||||
OBJS = string.o mutils.o mime.o
|
||||
|
||||
.c.o:
|
||||
rm -f $@
|
||||
$(CC) $(CFLAGS) -c $*.c
|
||||
|
||||
all: $(LIBNAME)
|
||||
|
||||
$(LIBNAME): $(OBJS)
|
||||
rm -f $@
|
||||
$(AR) $@ $(OBJS)
|
||||
$(RANLIB) $@
|
||||
|
||||
clean:
|
||||
rm -f $(OBJS) $(LIBNAME) core
|
||||
@@ -0,0 +1,9 @@
|
||||
A utility library. All the functions starts with mtutils, for example:
|
||||
mutilsSafeStrcpy(), mutilsMalloc() etc. The library can be compiled with
|
||||
an ANSI or non-ANSI C compiler.
|
||||
|
||||
/* ANSIfiled, muquit@muquit.com, Mar-27-2001 */
|
||||
|
||||
--
|
||||
Muhammad Muquit
|
||||
Jul-29-199
|
||||
@@ -0,0 +1,20 @@
|
||||
#include "mutils.h"
|
||||
#include <string.h>
|
||||
|
||||
int main (int argc,char **argv)
|
||||
{
|
||||
char
|
||||
*s=NULL;
|
||||
int
|
||||
i;
|
||||
|
||||
if (argc != 2)
|
||||
{
|
||||
(void) fprintf(stderr,"usage: %s <string>\n",argv[0]);
|
||||
exit(1);
|
||||
}
|
||||
s=strdup(argv[1]);
|
||||
(void) fprintf(stderr,"MD5(%s) %s\n",s,getMD5Digest(s));
|
||||
|
||||
return(0);
|
||||
}
|
||||
@@ -0,0 +1,120 @@
|
||||
#include "mutils.h"
|
||||
|
||||
static char base64_chars[64] =
|
||||
{
|
||||
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O',
|
||||
'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd',
|
||||
'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's',
|
||||
't', 'u', 'v', 'w', 'x', 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7',
|
||||
'8', '9', '+', '/'
|
||||
};
|
||||
|
||||
|
||||
|
||||
/*
|
||||
** generateBoundary()
|
||||
** generate unuque string for boundary for MIME tag
|
||||
**
|
||||
** Parameters:
|
||||
** char *boundary - NULL terminated boundary string - returns
|
||||
** int len - size of boundary (malloc'd or static)
|
||||
**
|
||||
** Return Values:
|
||||
** none
|
||||
**
|
||||
** Limitations and Comments:
|
||||
** boundary must have len bytes in it to store the boundary. the
|
||||
functio
|
||||
n
|
||||
** calls rand() for random number, so the caller should call srand()
|
||||
** before calling this function.
|
||||
**
|
||||
** This function is adapted from mutt code.
|
||||
**
|
||||
** Development History:
|
||||
** who when why
|
||||
** muquit@muquit.com Mar-06-2002 needs MIME support
|
||||
*/
|
||||
#define BOUNDARY_LEN 16
|
||||
void mutilsGenerateMIMEBoundary(char *boundary,int len)
|
||||
{
|
||||
char
|
||||
*p;
|
||||
int
|
||||
i;
|
||||
memset(boundary,0,len);
|
||||
p=boundary;
|
||||
for (i=0; i < BOUNDARY_LEN; i++)
|
||||
{
|
||||
if (i >= (len-1))
|
||||
break;
|
||||
*p++ = base64_chars[rand() % sizeof(base64_chars)];
|
||||
}
|
||||
*p='\0';
|
||||
}
|
||||
|
||||
static void output64Chunk(int c1,int c2,int c3,int pads, FILE *outfile)
|
||||
{
|
||||
putc(base64_chars[c1>>2], outfile);
|
||||
putc(base64_chars[((c1 & 0x3)<< 4) | ((c2 & 0xF0) >> 4)], outfile);
|
||||
if (pads == 2)
|
||||
{
|
||||
putc('=', outfile);
|
||||
putc('=', outfile);
|
||||
}
|
||||
else if (pads)
|
||||
{
|
||||
putc(base64_chars[((c2 & 0xF) << 2) | ((c3 & 0xC0) >>6)], outfile);
|
||||
putc('=', outfile);
|
||||
}
|
||||
else
|
||||
{
|
||||
putc(base64_chars[((c2 & 0xF) << 2) | ((c3 & 0xC0) >>6)], outfile);
|
||||
putc(base64_chars[c3 & 0x3F], outfile);
|
||||
}
|
||||
}
|
||||
|
||||
void mutilsBase64Encode(FILE *ifp,FILE *ofp)
|
||||
{
|
||||
int
|
||||
c1,
|
||||
c2,
|
||||
c3,
|
||||
c4,
|
||||
ct=0;
|
||||
|
||||
while ((c1=getc(ifp)) != EOF)
|
||||
{
|
||||
c2=getc(ifp);
|
||||
if (c2 == EOF)
|
||||
{
|
||||
output64Chunk(c1,0,0,2,ofp);
|
||||
}
|
||||
else
|
||||
{
|
||||
c3=getc(ifp);
|
||||
if (c3 == EOF)
|
||||
{
|
||||
output64Chunk(c1,c2,0,1,ofp);
|
||||
}
|
||||
else
|
||||
{
|
||||
output64Chunk(c1,c2,c3,0,ofp);
|
||||
}
|
||||
}
|
||||
ct += 4;
|
||||
if (ct > 71)
|
||||
{
|
||||
putc('\r',ofp); /* qmail fix 15.07.05 (movi) */
|
||||
putc('\n',ofp);
|
||||
ct=0;
|
||||
}
|
||||
}
|
||||
if (ct)
|
||||
{
|
||||
putc('\r',ofp); /* qmail fix 15.07.05 (movi) */
|
||||
putc('\n',ofp);
|
||||
}
|
||||
|
||||
(void) fflush(ofp);
|
||||
}
|
||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,148 @@
|
||||
#ifndef MUTILS_H
|
||||
#define MUTILS_H
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
#if STDC_HEADERS || HAVE_STRING_H
|
||||
#include <string.h> /* ANSI string.h and pre-ANSI memory.h might conflict*/
|
||||
#if !STDC_HEADERS && HAVE_MEMORY_H
|
||||
#include <memory.h>
|
||||
#endif
|
||||
#else
|
||||
#if HAVE_STRINGS_H
|
||||
#include <strings.h>
|
||||
#endif
|
||||
#endif
|
||||
|
||||
|
||||
#if HAVE_CTYPE_H
|
||||
#include <ctype.h>
|
||||
#endif
|
||||
|
||||
#if HAVE_SYS_WAIT_H
|
||||
#include <sys/wait.h>
|
||||
#endif
|
||||
|
||||
#if SYS_UNIX
|
||||
#include <sys/types.h>
|
||||
#include <sys/stat.h>
|
||||
#endif
|
||||
|
||||
#ifdef WINNT
|
||||
#include <sys/types.h>
|
||||
#include <sys/stat.h>
|
||||
#include <io.h>
|
||||
#include <share.h>
|
||||
#define ftruncate chsize
|
||||
#endif
|
||||
|
||||
#if HAVE_SYS_PARAM_H
|
||||
#include <sys/param.h>
|
||||
#endif
|
||||
|
||||
#if HAVE_STDLIB_H
|
||||
#include <stdlib.h>
|
||||
#endif
|
||||
|
||||
#if HAVE_UNISTD_H
|
||||
#include <unistd.h>
|
||||
#endif
|
||||
|
||||
#if HAVE_FCNTL_H
|
||||
#ifndef O_RDONLY /* prevent multiple inclusion on lame systems (from
|
||||
vile)*/
|
||||
#include <fcntl.h>
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#if HAVE_MALLOC_H
|
||||
#include <malloc.h>
|
||||
#endif
|
||||
|
||||
|
||||
#ifdef HAVE_SYS_FILE_H
|
||||
#include <sys/file.h>
|
||||
#endif
|
||||
|
||||
#if TIME_WITH_SYS_TIME
|
||||
#include <sys/time.h>
|
||||
#include <time.h>
|
||||
#else
|
||||
#if HAVE_SYS_TIME_H
|
||||
#include <sys/time.h>
|
||||
#else
|
||||
#include <time.h>
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#define MUTILS_CFL __FILE__,__LINE__
|
||||
|
||||
|
||||
#if __STDC__ || defined(sgi) || defined(_AIX)
|
||||
#undef _Declare
|
||||
#define _Declare(formal_parameters) formal_parameters
|
||||
#else
|
||||
#define _Declare(formal_parameters) ()
|
||||
#define const
|
||||
#endif
|
||||
|
||||
#define MUTILS_MAX_TOKEN_LEN 1024
|
||||
|
||||
|
||||
#define MUTILS_CHECK_MALLOC(p) \
|
||||
do \
|
||||
{ \
|
||||
if (p == NULL) \
|
||||
{\
|
||||
(void) fprintf(stderr,"%s (%d) - memory allocation problem\n",__FILE__,__LINE__); \
|
||||
goto ExitProcessing; \
|
||||
}\
|
||||
}while(0)
|
||||
|
||||
|
||||
|
||||
/* function prototypes */
|
||||
void mutilsBase64Encode (FILE *ifp,FILE *ofp);
|
||||
void mutilsGenerateMIMEBoundary(char *boundary,int len);
|
||||
int mutilsParseURL (char *url,char *hostname,
|
||||
int hostname_len, int *port,
|
||||
char *page,int page_len);
|
||||
|
||||
void mutilsStripLeadingSpace (char *s);
|
||||
void mutilsStripTrailingSpace (char *s);
|
||||
char *mutilsReverseString (char *str);
|
||||
char *mutilsStrncat (char *dst,char *src,int n);
|
||||
char *mutilsStrncpy (char *dsr,char *src,int n);
|
||||
int mutilsStrncasecmp (char *s1,char *s2,int n);
|
||||
char *mutilsStrdup (char *str);
|
||||
int mutilsStrcasecmp (char *a,char *b);
|
||||
void mutilsSafeStrcpy (char *dst,char *src,int n);
|
||||
void mutilsSafeStrcat (char *dsr,char *src,int n,int ss,int sl);
|
||||
char *mutilsStrtok (char *s,char *delim);
|
||||
int mutilsHowmanyCommas (char *buf);
|
||||
void mutilsCommaize (char *buf);
|
||||
void mutilsCleanBuf (char *buf,int bufsize,int *length);
|
||||
char *mutilsRmallws (char *str);
|
||||
char *mutilsStristr (char *s,char *t);
|
||||
int mutilsIsinname (char *string,char *mask);
|
||||
char *mutilsGetTime (void);
|
||||
char mutilsChopNL (char *str);
|
||||
int mutilsTmpFilename (char *filename);
|
||||
char *mutilsBasename (char *path);
|
||||
int mutilsWhich (char *name);
|
||||
void mutilsSetLock (int fd);
|
||||
void mutilsDotLock (char *filepath,char *errbuf);
|
||||
void mutilsDotUnlock (int delete);
|
||||
char *mutilsStrUpper (char *str);
|
||||
char *mutilsStrLower (char *str);
|
||||
int mutilsEatComment (FILE *fp);
|
||||
int mutilsEatWhitespace (FILE *fp);
|
||||
char *mutilsGetDirname (char *file);
|
||||
char *mutilsSpacesToChar (char *str,int c);
|
||||
char **mutilsTokenize(char *str,int delim,int *ntokens);
|
||||
void mutilsFreeTokens(char **tokens,int ntokens);
|
||||
unsigned char *mutils_encode_base64(void *src,unsigned long srcl,unsigned long *len);
|
||||
void *mutils_decode_base64(unsigned char *src,unsigned long srcl,unsigned long *len);
|
||||
|
||||
|
||||
#endif /* MUTILS_H */
|
||||
@@ -0,0 +1,510 @@
|
||||
/* all interesting strngs related routines */
|
||||
|
||||
#include <mutils.h>
|
||||
|
||||
|
||||
/*
|
||||
** reverseString()
|
||||
** reverse a string
|
||||
**
|
||||
** Parameters:
|
||||
** char *str string to modify
|
||||
**
|
||||
** Return Values:
|
||||
** pointer to the modified string
|
||||
**
|
||||
** Limitations and Comments:
|
||||
** str is modified
|
||||
** borrowed from c-snippets STRREV.C, public domain by Bob Stout
|
||||
** The name of the function was stttev() -- muquit, May-26-1999
|
||||
**
|
||||
** Development History:
|
||||
** who when why
|
||||
** ma_muquit@fccc.edu may-26-1999 first cut
|
||||
*/
|
||||
|
||||
char *mutilsReverseString(char *str)
|
||||
{
|
||||
char
|
||||
*p1,
|
||||
*p2;
|
||||
|
||||
if (! str || ! *str)
|
||||
return str;
|
||||
|
||||
for (p1=str,p2 =str+strlen(str)-1; p2 > p1; ++p1, --p2)
|
||||
{
|
||||
*p1 ^= *p2;
|
||||
*p2 ^= *p1;
|
||||
*p1 ^= *p2;
|
||||
}
|
||||
return (str);
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** mutilsStrncat()
|
||||
** appends at most n characters of src string to dst string
|
||||
**
|
||||
** Parameters:
|
||||
** dst destination string
|
||||
** src source string
|
||||
** n number of character to take from src
|
||||
**
|
||||
**
|
||||
** Return Values:
|
||||
**
|
||||
**
|
||||
** Limitations and Comments:
|
||||
** taken from Henry Spencer's public domain string library
|
||||
**
|
||||
** Development History:
|
||||
** who when why
|
||||
** ma_muquit@fccc.edu Jul-31-1999 first cut
|
||||
*/
|
||||
char *mutilsStrncat(char *dst,char *src,int n)
|
||||
{
|
||||
register char
|
||||
*dscan,
|
||||
*sscan;
|
||||
|
||||
register int
|
||||
count;
|
||||
|
||||
for (dscan=dst; *dscan != '\0'; dscan++)
|
||||
continue;
|
||||
|
||||
sscan=src;
|
||||
count=n;
|
||||
|
||||
while (*sscan != '\0' && --count >= 0)
|
||||
*dscan++ = *sscan++;
|
||||
|
||||
*dscan++ = '\0';
|
||||
|
||||
return (dst);
|
||||
}
|
||||
/*
|
||||
** mutilsStrncpy()
|
||||
** copy at most n characters of string src to dst
|
||||
**
|
||||
** Parameters:
|
||||
** char *dst
|
||||
** char *str
|
||||
** int n
|
||||
**
|
||||
** Return Values:
|
||||
** pinter to the destination string
|
||||
**
|
||||
** Limitations and Comments:
|
||||
** adapted from henry's stringlib
|
||||
**
|
||||
** Development History:
|
||||
** who when why
|
||||
** ma_muquit@fccc.edu Jul-31-1999 first cut
|
||||
*/
|
||||
char *mutilsStrncpy(char *dst,char *src,int n)
|
||||
{
|
||||
register char
|
||||
*dscan;
|
||||
|
||||
register char
|
||||
*sscan;
|
||||
|
||||
register int
|
||||
count;
|
||||
|
||||
dscan=dst;
|
||||
sscan=src;
|
||||
count=n;
|
||||
|
||||
while (--count >= 0 && (*dscan++ = *sscan++) != '\0')
|
||||
continue;
|
||||
|
||||
while (--count >= 0)
|
||||
*dscan++ = '\0';
|
||||
return(dst);
|
||||
}
|
||||
|
||||
/*
|
||||
** mutilsStrdup()
|
||||
** duplicate a sting
|
||||
**
|
||||
** Parameters:
|
||||
** char *string string to duplicate
|
||||
**
|
||||
** Return Values:
|
||||
** pointer to the duplicated stirng. NULL if fails.
|
||||
**
|
||||
** Limitations and Comments:
|
||||
** allocate memory, caller is responsible to free it.
|
||||
**
|
||||
** Development History:
|
||||
** who when why
|
||||
** ma_muquit@fccc.edu Jul-31-1999 first cut
|
||||
*/
|
||||
char *mutilsStrdup(char *string)
|
||||
{
|
||||
char
|
||||
*tmp;
|
||||
|
||||
if (string == NULL || *string == '\0')
|
||||
return ((char *) NULL);
|
||||
|
||||
tmp = (char *) malloc ((int) strlen(string)*sizeof(char)+1 );
|
||||
|
||||
if (tmp == (char *) NULL)
|
||||
{
|
||||
return ((char *) NULL);
|
||||
}
|
||||
/* it's safe to copy this way */
|
||||
(void) strcpy(tmp, string);
|
||||
return (tmp);
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** mutilsStrcasecmp()
|
||||
** case insensitive string comparison
|
||||
**
|
||||
** Parameters:
|
||||
** char *a
|
||||
** char *b
|
||||
**
|
||||
** Return Values:
|
||||
** < 0 if a < b
|
||||
** > 0 if a > b
|
||||
** 0 if a = b
|
||||
**
|
||||
** Limitations and Comments:
|
||||
** found somewhere on the net. I didn't write it
|
||||
**
|
||||
** Development History:
|
||||
** who when why
|
||||
** ma_muquit@fccc.edu Jul-31-1999 first cut
|
||||
*/
|
||||
int mutilsStrcasecmp(char *a,char *b)
|
||||
{
|
||||
register char
|
||||
ac,
|
||||
bc;
|
||||
|
||||
for(;;)
|
||||
{
|
||||
ac = *a++;
|
||||
bc = *b++;
|
||||
|
||||
if(ac == 0)
|
||||
{
|
||||
if(bc == 0)
|
||||
return 0;
|
||||
else
|
||||
return -1;
|
||||
}
|
||||
else
|
||||
{
|
||||
if(bc == 0)
|
||||
return 1;
|
||||
else
|
||||
{
|
||||
if(ac != bc)
|
||||
{
|
||||
if(islower(ac)) ac = toupper(ac);
|
||||
if(islower(bc)) bc = toupper(bc);
|
||||
if( ac != bc )
|
||||
return ac - bc;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/*
|
||||
** mutilsStrncasecmp()
|
||||
** case in-sensitive comparison wth first n bytes of fist string
|
||||
**
|
||||
** Parameters:
|
||||
** char *s1
|
||||
* char *s2
|
||||
* int n
|
||||
**
|
||||
** Return Values:
|
||||
**
|
||||
**
|
||||
** Limitations and Comments:
|
||||
** < 0 if first n bytes of a < b
|
||||
** > 0 if first n bytes of a a > b
|
||||
** 0 if first n bytes of a a = b
|
||||
**
|
||||
** Development History:
|
||||
** who when why
|
||||
** ma_muquit@fccc.edu Jul-31-1999 first cut
|
||||
*/
|
||||
int mutilsStrncasecmp(char *s1,char *s2,int n)
|
||||
{
|
||||
register char
|
||||
*scan1,
|
||||
*scan2;
|
||||
|
||||
int
|
||||
count;
|
||||
|
||||
scan1=s1;
|
||||
scan2=s2;
|
||||
count=n;
|
||||
|
||||
while (--count >= 0 && *scan1 != '\0' && tolower(*scan1) == tolower(*scan2))
|
||||
{
|
||||
scan1++;
|
||||
scan2++;
|
||||
}
|
||||
if (count < 0)
|
||||
return (0);
|
||||
|
||||
|
||||
return(tolower(*scan1) - tolower(*scan2));
|
||||
}
|
||||
|
||||
/*
|
||||
** mutilsSafeStrcpy()
|
||||
** copy a string to another safely without overflowing buffer
|
||||
**
|
||||
** RCS
|
||||
** $Revision: 1 $
|
||||
** $Date: 2/24/04 8:38p $
|
||||
** Return Values:
|
||||
** none
|
||||
**
|
||||
** Parameters:
|
||||
** dst destination buffer
|
||||
** src source buffer
|
||||
** n max allowable n of the src
|
||||
**
|
||||
** Side Effects:
|
||||
** if copying is safe, dst buffer is modified.
|
||||
**
|
||||
** Limitations and Comments:
|
||||
** if the source string is longer than n, it's truncated to
|
||||
** n. dst must be static or dynamically allocated buffer, that
|
||||
** is it must be able to hold atleast lenth bytes buffer. The src
|
||||
** is checked if it is NULL or not. if NULL the routine will exit
|
||||
** after writing an error message image.
|
||||
**
|
||||
**
|
||||
** Development History:
|
||||
** who when why
|
||||
** ma_muquit@fccc.edu Oct-17-1997 first cut
|
||||
*/
|
||||
void mutilsSafeStrcpy(char *dst,char *src,int n)
|
||||
{
|
||||
/* now copy */
|
||||
/*
|
||||
(void) mutilsStrncpy(dst,src,n);
|
||||
*/
|
||||
(void) strncpy(dst,src,n);
|
||||
dst[n]='\0';
|
||||
}
|
||||
|
||||
/*
|
||||
* mutilsSafeStrcat()
|
||||
* safely appends one string to another without overflowing the source
|
||||
* buffer.
|
||||
*
|
||||
* Parameters:
|
||||
* dst appends with this string
|
||||
* src string to append
|
||||
* length first this many character of src to append
|
||||
* ssc_size maximum size of destination string
|
||||
* ssc_length length of string in destination buffer
|
||||
*
|
||||
* Return Values:
|
||||
* none
|
||||
*
|
||||
* Limitations and Comments:
|
||||
* dst must have enough space staically or dynamically allocated
|
||||
* before calling. Need to provide a portable strncat() as matt
|
||||
* suggested that some versions of strncat (e.g. solaris 2.5.1) write
|
||||
* at most n+1 characters past the end of s1 and then replace the
|
||||
* n+1st character with a '\0';
|
||||
*
|
||||
*
|
||||
* Development History:
|
||||
* who when why
|
||||
* ma_muquit@fccc.edu Oct-1997 first cut
|
||||
* mhpower@mit.edu Nov-03-1997 fixed
|
||||
*/
|
||||
void mutilsSafeStrcat(char *dst,char *src,int length,int ssc_size,
|
||||
int ssc_length)
|
||||
{
|
||||
int copy_length;
|
||||
|
||||
if ( NULL == src )
|
||||
{
|
||||
/*
|
||||
StringImage("Source buffer is NULL in safeStrcat()!");
|
||||
*/
|
||||
(void) fprintf(stderr,"Source buffer is NULL in safeStrcat()!\n");
|
||||
exit(0);
|
||||
}
|
||||
|
||||
if (strlen(src) >= ssc_size - ssc_length)
|
||||
{
|
||||
/*
|
||||
StringImage("buffer overflow detected! aborting");
|
||||
*/
|
||||
(void) fprintf(stderr,"buffer overflow detected! aborting\n");
|
||||
exit(0);
|
||||
}
|
||||
|
||||
if (length < ssc_size - ssc_length)
|
||||
{
|
||||
copy_length = length;
|
||||
}
|
||||
else
|
||||
{
|
||||
copy_length = ssc_size - ssc_length;
|
||||
}
|
||||
|
||||
/* now copy */
|
||||
(void) mutilsStrncat(dst,src,copy_length);
|
||||
|
||||
}
|
||||
|
||||
/*
|
||||
* Copyright (c) 1988, 1993
|
||||
* The Regents of the University of California. All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* 3. All advertising materials mentioning features or use of this software
|
||||
* must display the following acknowledgement:
|
||||
* This product includes software developed by the University of
|
||||
* California, Berkeley and its contributors.
|
||||
* 4. Neither the name of the University nor the names of its contributors
|
||||
* may be used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
||||
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
||||
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
/*
|
||||
** I'm renaming it to mystrtok() in order to avoid conflict with the
|
||||
** system which might have it
|
||||
** I also formatted to my coding style
|
||||
** 10/08/95, muquit@semcor.com
|
||||
*/
|
||||
|
||||
char *mutilsStrtok(char *s,char *delim)
|
||||
{
|
||||
register char
|
||||
*spanp;
|
||||
|
||||
register int
|
||||
c,
|
||||
sc;
|
||||
|
||||
char
|
||||
*tok;
|
||||
|
||||
static char
|
||||
*last;
|
||||
|
||||
|
||||
if (s == (char *) NULL && (s = last) == (char *) NULL)
|
||||
return ((char *) NULL);
|
||||
|
||||
/*
|
||||
** Skip (span) leading delimiters (s += strspn(s, delim), sort of).
|
||||
*/
|
||||
cont:
|
||||
c = *s++;
|
||||
for (spanp = (char *)delim; (sc = *spanp++) != 0;)
|
||||
{
|
||||
if (c == sc)
|
||||
goto cont;
|
||||
}
|
||||
|
||||
if (c == 0)
|
||||
{ /* no non-delimiter characters */
|
||||
last = (char *) NULL;
|
||||
return ((char *)NULL);
|
||||
}
|
||||
tok = s - 1;
|
||||
|
||||
/*
|
||||
* Scan token (scan for delimiters: s += strcspn(s, delim), sort of).
|
||||
* Note that delim must have one NUL; we stop if we see that, too.
|
||||
*/
|
||||
for (;;)
|
||||
{
|
||||
c = *s++;
|
||||
spanp = (char *)delim;
|
||||
do
|
||||
{
|
||||
if ((sc = *spanp++) == c)
|
||||
{
|
||||
if (c == 0)
|
||||
s = (char *) NULL;
|
||||
else
|
||||
s[-1] = '\0';
|
||||
last = s;
|
||||
return (tok);
|
||||
}
|
||||
} while (sc != 0);
|
||||
}
|
||||
/* NOTREACHED */
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** convert a string to upper case
|
||||
** borrowed from C snippets library (strupr.c)
|
||||
*/
|
||||
char *mutilsStrUpper(char *str)
|
||||
{
|
||||
char
|
||||
*s;
|
||||
|
||||
if (str)
|
||||
{
|
||||
for (s=str; *s; ++s)
|
||||
*s=toupper(*s);
|
||||
}
|
||||
|
||||
return (str);
|
||||
}
|
||||
|
||||
/*
|
||||
** convert a string to lower case
|
||||
** borrowed from C snippets library (strupr.c)
|
||||
*/
|
||||
char *mutilsStrLower(char *str)
|
||||
{
|
||||
char
|
||||
*s;
|
||||
|
||||
if (str)
|
||||
{
|
||||
for (s=str; *s; ++s)
|
||||
*s=tolower(*s);
|
||||
}
|
||||
|
||||
return (s);
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
#include "mutils.h"
|
||||
|
||||
int main (int argc,char **argv)
|
||||
{
|
||||
int
|
||||
rc;
|
||||
if (argc != 3)
|
||||
{
|
||||
(void) fprintf(stderr," usage: %s <string> <mask>\n",argv[0]);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
rc=mutilsIsinname(argv[1],argv[2]);
|
||||
if (rc)
|
||||
(void) fprintf(stderr,"%s %s fits\n",argv[1],argv[2]);
|
||||
return(0);
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
#include "mutils.h"
|
||||
|
||||
int main(int argc,char **argv)
|
||||
{
|
||||
char
|
||||
buf[BUFSIZ];
|
||||
|
||||
FILE
|
||||
*ifp,
|
||||
*ofp;
|
||||
|
||||
if (argc != 2)
|
||||
{
|
||||
(void) fprintf(stderr,"usage: %s file\n",argv[0]);
|
||||
return (1);
|
||||
}
|
||||
|
||||
(void) sprintf(buf,"%s.b64",argv[1]);
|
||||
ifp=fopen(argv[1],"r");
|
||||
if (ifp == NULL)
|
||||
{
|
||||
(void) fprintf(stderr,"could not open for reading: %s\n",argv[1]);
|
||||
return (1);
|
||||
}
|
||||
|
||||
ofp=fopen(buf,"w");
|
||||
if (ofp == NULL)
|
||||
{
|
||||
(void) fprintf(stderr,"could not open for writing: %s\n",buf);
|
||||
return (1);
|
||||
}
|
||||
|
||||
mutilsBase64Encode(ifp,ofp);
|
||||
|
||||
(void) fclose(ifp);
|
||||
(void) fclose(ofp);
|
||||
|
||||
|
||||
return (0);
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
#
|
||||
# for Singly linked list package.
|
||||
# muhammad a muquit
|
||||
# Aug-07-1998
|
||||
#
|
||||
CC= gcc
|
||||
DEFS= -DPACKAGE_NAME=\"\" -DPACKAGE_TARNAME=\"\" -DPACKAGE_VERSION=\"\" -DPACKAGE_STRING=\"\" -DPACKAGE_BUGREPORT=\"\" -DSTDC_HEADERS=1 -DHAVE_SYS_TYPES_H=1 -DHAVE_SYS_STAT_H=1 -DHAVE_STDLIB_H=1 -DHAVE_STRING_H=1 -DHAVE_MEMORY_H=1 -DHAVE_STRINGS_H=1 -DHAVE_INTTYPES_H=1 -DHAVE_STDINT_H=1 -DHAVE_UNISTD_H=1 -DHAVE_STRING_H=1 -DHAVE_STRINGS_H=1 -DHAVE_MEMORY_H=1 -DHAVE_MALLOC_H=1 -DHAVE_UNISTD_H=1 -DHAVE_CTYPE_H=1 -DHAVE_STDINT_H=1 -DHAVE_SYS_TYPES_H=1 -DHAVE_STDLIB_H=1 -DHAVE_FCNTL_H=1 -DHAVE_SYS_FILE_H=1 -DHAVE_FLOCK=1 -DHAVE_SOCKET=1 -DHAVE_HTONL=1 -DHAVE_GETHOSTNAME=1 -DHAVE_GETHOSTBYADDR=1 -DHAVE_YP_GET_DEFAULT_DOMAIN=1 -DHAVE_LIBNSL=1 -DHAVE_RES_SEARCH=1 -DHAVE_LIBRESOLV=1 -DHAVE_INET_ATON=1 -DHAVE_DN_SKIPNAME=1 -DHAVE_MKSTEMP=1 -DHAVE_OPENSSL=1
|
||||
AR= ar cq
|
||||
RANLIB= ranlib
|
||||
LIBNAME= libsll.a
|
||||
|
||||
INCLUDES= -I. -I/usr/include/malloc
|
||||
|
||||
# replace -O with -g in order to debug
|
||||
|
||||
DEFINES= $(INCLUDES) $(DEFS) -DSYS_UNIX=1
|
||||
CFLAGS= -O $(DEFINES)
|
||||
|
||||
SRCS = sll.c
|
||||
OBJS = sll.o
|
||||
|
||||
.c.o:
|
||||
rm -f $@
|
||||
$(CC) $(CFLAGS) -c $*.c
|
||||
|
||||
all: $(LIBNAME)
|
||||
|
||||
$(LIBNAME): $(OBJS)
|
||||
rm -f $@
|
||||
$(AR) $@ $(OBJS)
|
||||
$(RANLIB) $@
|
||||
|
||||
sll.o: sll.h
|
||||
|
||||
clean:
|
||||
rm -f $(OBJS) $(LIBNAME) core a.out
|
||||
|
||||
realclean:
|
||||
rm -f $(OBJS) $(LIBNAME) core a.out config.cache config.log config.status
|
||||
(cd examples/append;make clean)
|
||||
(cd examples/append_sorted;make clean)
|
||||
(cd examples/delete;make clean)
|
||||
(cd examples/insert;make clean)
|
||||
@@ -0,0 +1,419 @@
|
||||
#include "sll.h"
|
||||
|
||||
/*
|
||||
** private protos
|
||||
static void freeList(Sll **list);
|
||||
*/
|
||||
|
||||
/*
|
||||
** initList()
|
||||
** initialize a list
|
||||
**
|
||||
** Parameters:
|
||||
** Sll **list list to initialize
|
||||
**
|
||||
** Return Values:
|
||||
** none
|
||||
**
|
||||
** Limitations and Comments:
|
||||
** none
|
||||
**
|
||||
**
|
||||
** Development History:
|
||||
** who when why
|
||||
** ma_muquit@fccc.edu Aug-07-1998 first cut
|
||||
*/
|
||||
void initList(Sll **list)
|
||||
{
|
||||
(*list)=NULL;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** allocateNode()
|
||||
** allocate a new node.
|
||||
**
|
||||
** Parameters:
|
||||
** void *data a generic pointer to object data
|
||||
**
|
||||
** Return Values:
|
||||
** pointer to Sll if succeeds
|
||||
** NULL otherwise
|
||||
**
|
||||
** Limitations and Comments:
|
||||
** the caller must pass valid pointer to data.
|
||||
**
|
||||
** Development History:
|
||||
** who when why
|
||||
** ma_muquit@fccc.edu Aug-07-1998 first cut
|
||||
*/
|
||||
|
||||
Sll *allocateNode(void *data)
|
||||
{
|
||||
char
|
||||
*func="allocateNode() in sll.c";
|
||||
|
||||
Sll
|
||||
*sll;
|
||||
|
||||
sll=(Sll *) malloc(sizeof(Sll));
|
||||
if (sll == (Sll *) NULL)
|
||||
{
|
||||
(void) fprintf(stderr,"malloc failed at: %s\n",func);
|
||||
return ((Sll *) NULL);
|
||||
}
|
||||
|
||||
sll->data=data;
|
||||
sll->next=NULL;
|
||||
|
||||
return (sll);
|
||||
}
|
||||
|
||||
/*
|
||||
** appendNode()
|
||||
** appends a node to the end of a list
|
||||
**
|
||||
** Parameters:
|
||||
** Sll **head - modify the list
|
||||
** Sll **new - appends this node
|
||||
**
|
||||
** Return Values:
|
||||
** None
|
||||
**
|
||||
** Limitations and Comments:
|
||||
** new node must be allocated and initialized before passing it here
|
||||
**
|
||||
** Development History:
|
||||
** who when why
|
||||
** ma_muquit@fccc.edu Aug-07-1998 first cut
|
||||
*/
|
||||
|
||||
void appendNode(Sll **head,Sll **new)
|
||||
{
|
||||
Sll
|
||||
*tmp;
|
||||
|
||||
if (emptyList(*head) == TRUE)
|
||||
{
|
||||
(*head)=(*new);
|
||||
}
|
||||
else
|
||||
{
|
||||
for (tmp=(*head); tmp->next != NULL; tmp=tmp->next)
|
||||
;
|
||||
tmp->next=(*new);
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
** appendNodeSorted()
|
||||
** appends a node to the end of a list sorting by a user defined function
|
||||
**
|
||||
** Parameters:
|
||||
** Sll **head - append at the ends of this node
|
||||
** Sll **new - appends this node
|
||||
**
|
||||
** Return Values:
|
||||
** None
|
||||
**
|
||||
** Limitations and Comments:
|
||||
** new node must be allocated and initialized before passing it here
|
||||
** the function takes two arguments, void * each
|
||||
**
|
||||
** Development History:
|
||||
** who when why
|
||||
** ma_muquit@fccc.edu Aug-07-1998 first cut
|
||||
*/
|
||||
|
||||
void appendNodeSorted(Sll **head,Sll **new,Ifunc compFunc)
|
||||
{
|
||||
Sll
|
||||
*tmp;
|
||||
|
||||
if (emptyList(*head) == TRUE)
|
||||
{
|
||||
(*head)=(*new);
|
||||
}
|
||||
else
|
||||
{
|
||||
if ((*compFunc)((*head)->data,(*new)->data) > 0)
|
||||
{
|
||||
(*new)->next=(*head);
|
||||
(*head)=(*new);
|
||||
}
|
||||
else
|
||||
{
|
||||
for(tmp=(*head); tmp->next; tmp=tmp->next)
|
||||
{
|
||||
if ((*compFunc)(tmp->next->data,(*new)->data) > 0)
|
||||
break;
|
||||
}
|
||||
(*new)->next=tmp->next;
|
||||
tmp->next=(*new);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
** insertNode()
|
||||
** insert a node at the beginning of a list
|
||||
**
|
||||
** Parameters:
|
||||
** Sll **head - modify this list
|
||||
** Sll **new - appends this node
|
||||
**
|
||||
** Return Values:
|
||||
** None
|
||||
**
|
||||
** Limitations and Comments:
|
||||
** new node must be allocated and initialized before passing it here
|
||||
**
|
||||
** Development History:
|
||||
** who when why
|
||||
** ma_muquit@fccc.edu Aug-07-1998 first cut
|
||||
*/
|
||||
|
||||
void insertNode(Sll **head,Sll **new)
|
||||
{
|
||||
|
||||
(*new)->next=(*head);
|
||||
(*head)=(*new);
|
||||
}
|
||||
|
||||
/*
|
||||
** emptyList()
|
||||
** check if a list variable is NULL
|
||||
**
|
||||
** Parameters:
|
||||
** Sll *list list
|
||||
**
|
||||
** Return Values:
|
||||
** TRUE if empty
|
||||
** FALSE if not empty
|
||||
**
|
||||
** Limitations and Comments:
|
||||
** list must be allocated/initialized or initialized before calling
|
||||
**
|
||||
** Development History:
|
||||
** who when why
|
||||
** ma_muquit@fccc.edu Aug-07-1998 first cut
|
||||
*/
|
||||
|
||||
Bool emptyList(Sll *list)
|
||||
{
|
||||
return ((list == NULL) ? TRUE : FALSE);
|
||||
}
|
||||
|
||||
/*
|
||||
** delNode()
|
||||
** remove a node from a list
|
||||
**
|
||||
** Parameters:
|
||||
** Sll **head - list to modify
|
||||
** Sll *node - node to remove
|
||||
**
|
||||
** Return Values:
|
||||
** none
|
||||
**
|
||||
** Limitations and Comments:
|
||||
** list is modified
|
||||
**
|
||||
** Development History:
|
||||
** who when why
|
||||
** ma_muquit@fccc.edu Aug-07-1998 first cut
|
||||
*/
|
||||
void delNode(Sll **head,Sll *node)
|
||||
{
|
||||
if (emptyList(*head) == TRUE)
|
||||
return;
|
||||
|
||||
if ((*head) == node)
|
||||
(*head)=(*head)->next;
|
||||
else
|
||||
{
|
||||
Sll
|
||||
*l;
|
||||
for (l=(*head); l != NULL && l->next != node; l=l->next);
|
||||
if (l == NULL)
|
||||
return;
|
||||
else
|
||||
l->next=node->next;
|
||||
}
|
||||
freeNode(&node);
|
||||
}
|
||||
|
||||
/*
|
||||
** freeNode()
|
||||
** frees a node
|
||||
**
|
||||
** Parameters:
|
||||
** Sll **list node to free
|
||||
**
|
||||
** Return Values:
|
||||
** none
|
||||
**
|
||||
** Limitations and Comments:
|
||||
** if list is not null, it wil be freed. so list better point to a valid
|
||||
** location
|
||||
**
|
||||
** Development History:
|
||||
** who when why
|
||||
** ma_muquit@fccc.edu Aug-07-1998 first cut
|
||||
*/
|
||||
|
||||
void freeNode(Sll **list)
|
||||
{
|
||||
if (*list)
|
||||
{
|
||||
(void) free ((char *) (*list));
|
||||
(*list)=NULL;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
** getNthNode()
|
||||
** get nth node in a list
|
||||
**
|
||||
** Parameters:
|
||||
** Sll *list - the head list
|
||||
** int n - return the node
|
||||
** Return Values:
|
||||
** a pointer to the list at position n
|
||||
** NULL if there's no such node at posion n
|
||||
**
|
||||
** Limitations and Comments:
|
||||
** position starts at 1
|
||||
**
|
||||
** Development History:
|
||||
** who when why
|
||||
** ma_muquit@fccc.edu Aug-08-1998 frist cut
|
||||
*/
|
||||
|
||||
Sll *getNthNode(Sll *list,int n)
|
||||
{
|
||||
Sll
|
||||
*lp=NULL;
|
||||
int
|
||||
j=0;
|
||||
|
||||
for (lp=list; lp; lp=lp->next)
|
||||
{
|
||||
j++;
|
||||
if (j == n)
|
||||
{
|
||||
return (lp);
|
||||
}
|
||||
}
|
||||
|
||||
return ((Sll *) NULL);
|
||||
}
|
||||
|
||||
/*
|
||||
** destroyNode()
|
||||
** frees a node and the data associated with it
|
||||
**
|
||||
** Parameters:
|
||||
** Sll **list - modify this list
|
||||
** SLl *node - remove the node
|
||||
** void (*freeFunc)() - pointer to function to free the data
|
||||
**
|
||||
** Return Values:
|
||||
** none
|
||||
**
|
||||
** Limitations and Comments:
|
||||
** none
|
||||
**
|
||||
** Development History:
|
||||
** who when why
|
||||
** ma_muquit@fccc.edu Aug-08-1998 first cut
|
||||
*/
|
||||
|
||||
/*
|
||||
void destroyNode(Sll **list,Sll *node,void (*freeFunc)(void **))
|
||||
*/
|
||||
void destroyNode(Sll **list,Sll *node,Vfunc freeFunc)
|
||||
{
|
||||
if (emptyList(node) == FALSE)
|
||||
{
|
||||
/*
|
||||
** destroy the data
|
||||
*/
|
||||
if (freeFunc != NULL)
|
||||
(*freeFunc) (&(node->data));
|
||||
|
||||
delNode(list,node);
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
** destroyNodes()
|
||||
** destroy the entire linked list and the data
|
||||
**
|
||||
** Parameters:
|
||||
** Sll **head - head node of the list
|
||||
** freeFunc - function to free data
|
||||
**
|
||||
** Return Values:
|
||||
** none
|
||||
**
|
||||
** Limitations and Comments:
|
||||
** whole list and the data associated are freed
|
||||
**
|
||||
**
|
||||
** Development History:
|
||||
** who when why
|
||||
** ma_muquit@fccc.edu Aug-08-1998 first cut
|
||||
*/
|
||||
|
||||
/*
|
||||
void destroyNodes(Sll **head,void (*freeFunc)(void **))
|
||||
*/
|
||||
void destroyNodes(Sll **head,Vfunc freeFunc)
|
||||
{
|
||||
Sll
|
||||
*lp;
|
||||
while (*head)
|
||||
{
|
||||
lp=(*head);
|
||||
if (freeFunc != NULL)
|
||||
(*freeFunc) (&lp->data);
|
||||
(*head)=(*head)->next;
|
||||
(void) free((char *) lp);
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
** numNodes()
|
||||
** returns number of nodes in the list
|
||||
**
|
||||
** Parameters:
|
||||
** Sll **head - the head node of the list
|
||||
**
|
||||
** Return Values:
|
||||
** number of node/s
|
||||
**
|
||||
** Limitations and Comments:
|
||||
** traverse the whole list, so not efficient
|
||||
**
|
||||
** Development History:
|
||||
** who when why
|
||||
** ma_muquit@fccc.edu Aug-09-1998 first cut
|
||||
*/
|
||||
|
||||
int numNodes(Sll **head)
|
||||
{
|
||||
int
|
||||
n=0;
|
||||
|
||||
Sll
|
||||
*lp;
|
||||
|
||||
for (lp=(*head); lp; lp=lp->next)
|
||||
{
|
||||
n++;
|
||||
}
|
||||
|
||||
return (n);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,100 @@
|
||||
#ifndef SLL_H
|
||||
#define SLL_H
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
#if STDC_HEADERS
|
||||
#include <stdlib.h>
|
||||
#include <ctype.h>
|
||||
#include <string.h>
|
||||
#endif
|
||||
|
||||
#if HAVE_STRING_H
|
||||
#include <string.h>
|
||||
#endif
|
||||
|
||||
#if HAVE_MALLOC_H
|
||||
#include <malloc.h>
|
||||
#endif
|
||||
|
||||
#if TIME_WITH_SYS_TIME
|
||||
#include <sys/time.h>
|
||||
#include <time.h>
|
||||
#else
|
||||
#if HAVE_SYS_TIME_H
|
||||
#include <sys/time.h>
|
||||
#else
|
||||
#include <time.h>
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#ifdef WINNT
|
||||
#include <io.h>
|
||||
#include <fcntl.h>
|
||||
#endif
|
||||
|
||||
#include <math.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/stat.h>
|
||||
|
||||
|
||||
#ifdef WINNT
|
||||
#include <io.h>
|
||||
#include <share.h>
|
||||
#endif
|
||||
|
||||
|
||||
#define SLL_SUCCESS 0
|
||||
#define SLL_ERROR -1
|
||||
|
||||
#ifndef TRUE
|
||||
#define TRUE 1
|
||||
#endif
|
||||
|
||||
#ifndef FALSE
|
||||
#define FALSE 0
|
||||
#endif
|
||||
|
||||
typedef int Bool;
|
||||
typedef void (*Vfunc)(void **);
|
||||
typedef int (*Ifunc)(void *,void *);
|
||||
|
||||
|
||||
/*
|
||||
** the linked list structure
|
||||
*/
|
||||
|
||||
typedef struct _Sll
|
||||
{
|
||||
void
|
||||
*data; /* void pointer for user data */
|
||||
|
||||
struct _Sll
|
||||
*next; /* pointer to next node */
|
||||
} Sll;
|
||||
|
||||
/*
|
||||
** function prototypes
|
||||
*/
|
||||
Sll *allocateNode (void *data);
|
||||
void appendNodeSorted (Sll **head,Sll **new,Ifunc compFunc);
|
||||
void appendNode (Sll **list,Sll **new);
|
||||
void delNode (Sll **list,Sll *node);
|
||||
/*
|
||||
void destroyNode (Sll **list,Sll *node,
|
||||
void (*freeFunc)(void **));
|
||||
*/
|
||||
void destroyNode (Sll **list,Sll *node,Vfunc freeFunc);
|
||||
/*
|
||||
void destroyNodes (Sll **head,
|
||||
void (*freeFunc)(void **));
|
||||
*/
|
||||
void destroyNodes (Sll **head,Vfunc freeFunc);
|
||||
Bool emptyList (Sll *list);
|
||||
void freeNode (Sll **list);
|
||||
Sll *getNthNode (Sll *list,int n);
|
||||
void initList (Sll **list);
|
||||
void insertNode (Sll **list,Sll **new);
|
||||
int numNodes (Sll **head);
|
||||
|
||||
#endif /* SLL_H */
|
||||
Reference in New Issue
Block a user