home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Usenet 1994 October
/
usenetsourcesnewsgroupsinfomagicoctober1994disk1.iso
/
altsrc
/
articles
/
10884
< prev
next >
Wrap
Text File
|
1994-07-16
|
33KB
|
1,087 lines
Path: wupost!psuvax1!news.pop.psu.edu!news.cac.psu.edu!newsserver.jvnc.net!howland.reston.ans.net!EU.net!sunic!news.funet.fi!hydra.Helsinki.FI!usenet
From: lars.wirzenius@helsinki.fi (Lars Wirzenius)
Newsgroups: alt.sources
Subject: Publib liw-modules (part 06/07)
Followup-To: alt.sources.d
Date: 16 Jul 1994 20:28:48 GMT
Organization: University of Helsinki, Department of Computer Science
Lines: 1073
Message-ID: <309fu0$gum@hydra.Helsinki.FI>
References: <309fre$gu4@hydra.Helsinki.FI>
NNTP-Posting-Host: hydra.helsinki.fi
Keywords: Publib, library, ANSI C
Archive-name: liw-modules/part06
Submitted-by: lars.wirzenius@helsinki.fi
Publib is a library of reusable C functions, and a mechanism for building
the library.
---- Cut Here and unpack ----
#!/bin/sh
# this is part 6 of a multipart archive
# do not concatenate these parts, unpack them in order with /bin/sh
# file ./strutil/strins.c continued
#
CurArch=6
if test ! -r s2_seq_.tmp
then echo "Please unpack part 1 first!"
exit 1; fi
( read Scheck
if test "$Scheck" != $CurArch
then echo "Please unpack part $Scheck next!"
exit 1;
else exit 0; fi
) < s2_seq_.tmp || exit 1
echo "x - Continuing file ./strutil/strins.c"
sed 's/^X//' << 'SHAR_EOF' >> ./strutil/strins.c
X * strins.c -- insert a string at the beginning of another string
X *
X * Part of publib. See man page for more information
X * "@(#)publib-strutil:strins.c,v 1.2 1994/02/05 17:08:44 liw Exp"
X */
X
X#include <assert.h>
X#include <string.h>
X#include "publib/strutil.h"
X
Xchar *strins(char *tgt, const char *src) {
X size_t srclen;
X
X assert(tgt != NULL);
X assert(src != NULL);
X assert(!memoverlap(tgt, strlen(tgt)+strlen(src)+1, src, strlen(src)+1));
X
X srclen = strlen(src);
X memmove(tgt + srclen, tgt, strlen(tgt) + 1); /* +1 for '\0' */
X memcpy(tgt, src, srclen);
X
X return tgt;
X}
SHAR_EOF
echo "File ./strutil/strins.c is complete"
chmod 0644 ./strutil/strins.c || echo "restore of ./strutil/strins.c fails"
echo "x - extracting ./strutil/strltrim.3 (Text)"
sed 's/^X//' << 'SHAR_EOF' > ./strutil/strltrim.3 &&
X.\" part of publib
X.\" "@(#)publib-strutil:strltrim.3,v 1.1.1.1 1994/02/03 17:25:29 liw Exp"
X.\"
X.TH STRLTRIM 3 "C Programmer's Manual" Publib "C Programmer's Manual"
X.SH NAME
Xstrltrim \- remove leading whitespace from string
X.SH SYNOPSIS
X.nf
X#include <publib.h>
Xchar *\fBstrltrim\fR(char *\fIs\fR);
X.SH DESCRIPTION
X\fIstrltrim\fR removes all leading whitespace characters from the
Xbeginning of a string, by moving everything starting from the first
Xnon-whitespace character to the beginning of the string. As whitespace
Xis counted everything for which \fIisspace\fR(3) returns true.
X.SH "RETURN VALUE"
X\fIstrltrim\fR returns its argument.
X.SH EXAMPLE
XTo remove all indentation from all lines in a program, you might do
Xthe following:
X.sp 1
X.nf
X.in +5
X#include <publib.h>
X
Xint main(void) {
X char line[512];
X
X while (fgets(line, sizeof(line), stdio) != NULL) {
X strltrim(line);
X printf("%s", line);
X }
X return 0;
X}
X.in -5
X.SH "SEE ALSO"
Xpublib(3), strrtrim(3), strtrim(3), isspace(3)
X.SH AUTHOR
XLars Wirzenius (lars.wirzenius@helsinki.fi)
SHAR_EOF
chmod 0644 ./strutil/strltrim.3 || echo "restore of ./strutil/strltrim.3 fails"
echo "x - extracting ./strutil/strltrim.c (Text)"
sed 's/^X//' << 'SHAR_EOF' > ./strutil/strltrim.c &&
X/*
X * strltrim.c -- remove leading whitespace from string
X *
X * Part of publib. See man page for more information
X * "@(#)publib-strutil:strltrim.c,v 1.1.1.1 1994/02/03 17:25:29 liw Exp"
X */
X
X#include <assert.h>
X#include <ctype.h>
X#include <string.h>
X
X#include "publib/strutil.h"
X
Xchar *strltrim(char *s) {
X char *t;
X
X assert(s != NULL);
X for (t = s; isspace(*t); ++t)
X continue;
X memmove(s, t, strlen(t)+1); /* +1 so that '\0' is moved too */
X return s;
X}
SHAR_EOF
chmod 0644 ./strutil/strltrim.c || echo "restore of ./strutil/strltrim.c fails"
echo "x - extracting ./strutil/strmaxcpy.3 (Text)"
sed 's/^X//' << 'SHAR_EOF' > ./strutil/strmaxcpy.3 &&
X.\" part of publib
X.\" "@(#)publib-strutil:strmaxcpy.3,v 1.1.1.1 1994/02/03 17:25:29 liw Exp"
X.\"
X.TH STRMAXCPY 3 "C Programmer's Manual" Publib "C Programmer's Manual"
X.SH NAME
Xstrmaxcpy \- copy at most a given number of characters of string
X.SH SYNOPSIS
X.nf
X#include <publib.h>
Xchar *\fBstrmaxcpy\fR(char *\fItgt\fR, const char *\fIsrc\fR, size_t \fIn\fR);
X.SH DESCRIPTION
X\fIstrmaxcpy\fR copies up to \fIn-1\fR characters from the beginning of
X\fIsrc\fR to \fItgt\fR, then adds a '\\0'. \fIn\fR must be at least 1.
XThe target string must be large enough to hold the result.
X.PP
XNote that unlike \fIstrncpy\fR(3), this function always terminates the
Xresult with '\\0'. It also doesn't fill the result with extra '\\0'
Xcharacters.
X.SH "RETURN VALUE"
X\fIstrmaxcpy\fR returns its first argument.
X.SH EXAMPLE
XTo print out the first 69 characters of a string, you might do the
Xfollowing (although familiarity with printf's format string might
Xbe more useful in this case).
X.sp 1
X.nf
X.in +5
X#include <stdio.h>
X#include <publib.h>
X
Xvoid print42(const char *string) {
X char copy[43]; /* 42 + '\\0' */
X
X puts(strmaxcpy(copy, string, sizeof(copy)));
X}
X.in -5
X.SH "SEE ALSO"
Xpublib(3), strncpy(3)
X.SH AUTHOR
XLars Wirzenius (lars.wirzenius@helsinki.fi)
SHAR_EOF
chmod 0644 ./strutil/strmaxcpy.3 || echo "restore of ./strutil/strmaxcpy.3 fails"
echo "x - extracting ./strutil/strmaxcpy.c (Text)"
sed 's/^X//' << 'SHAR_EOF' > ./strutil/strmaxcpy.c &&
X/*
X * strmaxcpy.c -- copy at most a given number of characters of string
X *
X * Part of publib. See man page for more information
X * "@(#)publib-strutil:strmaxcpy.c,v 1.2 1994/02/05 17:08:44 liw Exp"
X */
X
X#include <assert.h>
X#include <string.h>
X#include "publib/strutil.h"
X
Xchar *strmaxcpy(char *tgt, const char *src, size_t n) {
X assert(tgt != NULL);
X assert(src != NULL);
X assert(n > 0);
X assert(!memoverlap(tgt, n+1, src, strlen(src)+1));
X
X *tgt = '\0';
X strncat(tgt, src, n);
X
X return tgt;
X}
SHAR_EOF
chmod 0644 ./strutil/strmaxcpy.c || echo "restore of ./strutil/strmaxcpy.c fails"
echo "x - extracting ./strutil/strmove.3 (Text)"
sed 's/^X//' << 'SHAR_EOF' > ./strutil/strmove.3 &&
X.\" part of publib
X.\" "@(#)publib-strutil:strmove.3,v 1.1 1994/06/20 20:30:18 liw Exp"
X.\"
X.TH STRMOVE 3 "C Programmer's Manual" Publib "C Programmer's Manual"
X.SH NAME
Xstrmove \- make a copy of a string, handling overlapping strings
X.SH SYNOPSIS
X.nf
X#include <publib.h>
Xchar *\fBstrmove\fR(char *\fItgt\fR, const char *\fIsrc\fR);
X.SH DESCRIPTION
X\fIstrmove\fR copies the string \fIsrc\fR to \fItgt\fR, just like
X\fIstrcpy\fR(3), but handles overlapping moves correctly (cf. \fImemcpy\fR(3)
Xvs. \fImemmove\fR(3)).
X.SH "RETURN VALUE"
X\fIstrmove\fR returns \fItgt\fR.
X.SH "SEE ALSO"
Xpublib(3), strcpy(3), memcpy(3), memmove(3)
X.SH AUTHOR
XLars Wirzenius (lars.wirzenius@helsinki.fi)
SHAR_EOF
chmod 0644 ./strutil/strmove.3 || echo "restore of ./strutil/strmove.3 fails"
echo "x - extracting ./strutil/strrev.3 (Text)"
sed 's/^X//' << 'SHAR_EOF' > ./strutil/strrev.3 &&
X.\" part of publib
X.\" "@(#)publib-strutil:strrev.3,v 1.3 1994/06/20 20:30:30 liw Exp"
X.\"
X.TH STRREV 3 "C Programmer's Manual" Publib "C Programmer's Manual"
X.SH NAME
Xstrrev \- reverse a string in place
X.SH SYNOPSIS
X.nf
X#include <publib.h>
Xchar *\fBstrrev\fR(char *\fIstr\fR);
X.SH DESCRIPTION
X\fIstrrev\fR reverses the argument string in place, i.e., it swaps
Xthe \fIi\fRth character from the beginning with the \fIi\fRth
Xcharacter from the end.
X.SH "RETURN VALUE"
X\fIstrrev\fR returns its argument.
X.SH EXAMPLE
XReversing "dlrow, elloh" would be done like the following.
X.sp 1
X.nf
X.in +5
Xchar str[] = "dlrow, elloh";
X
Xputs(strrev(str));
X.in -5
X.sp 1
X.fi
XThis would output "hello, world".
XNote that using the string literal as the argument would be an error,
Xsince it is not allowable to modify string literals.
X.SH BUGS
XDoes not automatically detect palindromes, nor automatically return
Xwithout doing anything.
X.SH "SEE ALSO"
Xpublib(3), memrev(3)
X.SH AUTHOR
XLars Wirzenius (lars.wirzenius@helsinki.fi)
SHAR_EOF
chmod 0644 ./strutil/strrev.3 || echo "restore of ./strutil/strrev.3 fails"
echo "x - extracting ./strutil/strmove.c (Text)"
sed 's/^X//' << 'SHAR_EOF' > ./strutil/strmove.c &&
X/*
X * strmove.c -- copy a string to another, possibly overlapping place
X *
X * Part of publib. See man page for more information
X * "@(#)publib-strutil:strmove.c,v 1.1 1994/06/20 20:30:20 liw Exp"
X */
X
X#include <assert.h>
X#include <string.h>
X#include "publib/strutil.h"
X
Xchar *strmove(char *tgt, const char *src) {
X assert(tgt != NULL);
X assert(src != NULL);
X
X return memmove(tgt, src, strlen(src) + 1);
X}
SHAR_EOF
chmod 0644 ./strutil/strmove.c || echo "restore of ./strutil/strmove.c fails"
echo "x - extracting ./strutil/strmtrim.3 (Text)"
sed 's/^X//' << 'SHAR_EOF' > ./strutil/strmtrim.3 &&
X.\" part of publib
X.\" "@(#)publib-strutil:strmtrim.3,v 1.1 1994/06/20 20:30:21 liw Exp"
X.\"
X.TH STRMTRIM 3 "C Programmer's Manual" Publib "C Programmer's Manual"
X.SH NAME
Xstrmtrim \- replace multiple white spaces with single blanks within string
X.SH SYNOPSIS
X.nf
X#include <publib.h>
Xchar *\fBstrmtrim\fR(char *\fIstr\fR);
X.SH DESCRIPTION
X\fIstrmtrim\fR will replace every run of whitespace characters
X(as defined by \fIisspace\fR(3)) with a single blank. It will not
Xtouch leading and trailing whitespace (use \fIstrltrim\fR(3) and
X\fIstrrtrim\fR(3) for those).
X.SH "RETURN VALUE"
X\fIstrmtrim\fR will return the value of its argument.
X.SH "SEE ALSO"
Xpublib(3), strtrim(3), strltrim(3), strrtrim(3)
X.SH AUTHOR
XLars Wirzenius (lars.wirzenius@helsinki.fi)
SHAR_EOF
chmod 0644 ./strutil/strmtrim.3 || echo "restore of ./strutil/strmtrim.3 fails"
echo "x - extracting ./strutil/strmtrim.c (Text)"
sed 's/^X//' << 'SHAR_EOF' > ./strutil/strmtrim.c &&
X/*
X * strmtrim.c -- replace multiple white spaces with single blanks within string
X *
X * Part of publib. See man page for more information.
X * "@(#)publib-strutil:strmtrim.c,v 1.1 1994/06/20 20:30:22 liw Exp"
X */
X
X#include <assert.h>
X#include <ctype.h>
X#include "publib/strutil.h"
X
Xchar *strmtrim(char *str) {
X char *s, *t, *u;
X
X assert(str != NULL);
X
X for (s = str; isspace(*s); ++s)
X continue;
X
X t = s;
X for (;;) {
X if (!isspace(*t)) {
X *s = *t;
X if (*t == '\0')
X break;
X ++s;
X ++t;
X } else {
X u = t;
X while (isspace(*++t))
X continue;
X if (*t == '\0') {
X while ((*s++ = *u++) != '\0')
X continue;
X break;
X }
X *s++ = ' ';
X }
X }
X
X return str;
X}
SHAR_EOF
chmod 0644 ./strutil/strmtrim.c || echo "restore of ./strutil/strmtrim.c fails"
echo "x - extracting ./strutil/strndup.3 (Text)"
sed 's/^X//' << 'SHAR_EOF' > ./strutil/strndup.3 &&
X.\" part of publib
X.\" "@(#)publib-strutil:strndup.3,v 1.1 1994/06/20 20:30:23 liw Exp"
X.\"
X.TH STRNDUP 3 "C Programmer's Manual" Publib "C Programmer's Manual"
X.SH NAME
Xstrndup \- duplicate part of a string
X.SH SYNOPSIS
X.nf
X#include <publib.h>
Xchar *\fBstrndup\fR(const char *\fIstr\fR, size_t \fIn\fR);
X.SH DESCRIPTION
X\fIstrndup\fR will make a duplicate of the \fIn\fR first characters
Xof \fIstr\fR, using \fImalloc\fR(3) to allocate memory for the
Xduplicate. The caller is supposed to free the duplicate's memory
Xwhen no longer needed.
X.SH "RETURN VALUE"
X\fIstrndup\fR will return a pointer to the duplicate, or NULL if no
Xmemory could be allocated.
X.SH "SEE ALSO"
Xpublib(3), strdup(3)
X.SH AUTHOR
XLars Wirzenius (lars.wirzenius@helsinki.fi)
SHAR_EOF
chmod 0644 ./strutil/strndup.3 || echo "restore of ./strutil/strndup.3 fails"
echo "x - extracting ./strutil/strndup.c (Text)"
sed 's/^X//' << 'SHAR_EOF' > ./strutil/strndup.c &&
X/*
X * strndup.c -- duplicate at most a given beginning of a string
X *
X * Part of publib. See man page for more information.
X * "@(#)publib-strutil:strndup.c,v 1.1 1994/06/20 20:30:24 liw Exp"
X */
X
X#include <assert.h>
X#include <string.h>
X#include <stdlib.h>
X#include <stdarg.h>
X#include "publib/strutil.h"
X#include "publib/errormsg.h"
X
Xchar *strndup(const char *str, size_t n) {
X char *dup;
X size_t len;
X
X len = strlen(str);
X if (n > len)
X n = len;
X
X dup = malloc(n+1);
X if (dup == NULL) {
X __publib_error("malloc failed");
X return NULL;
X }
X
X memcpy(dup, str, n);
X dup[n] = '\0';
X return dup;
X}
SHAR_EOF
chmod 0644 ./strutil/strndup.c || echo "restore of ./strutil/strndup.c fails"
echo "x - extracting ./strutil/stroverlap.3 (Text)"
sed 's/^X//' << 'SHAR_EOF' > ./strutil/stroverlap.3 &&
X.\" part of publib
X.\" "@(#)publib-strutil:stroverlap.3,v 1.1 1994/06/20 20:30:25 liw Exp"
X.\"
X.TH STROVERLAP 3 "C Programmer's Manual" Publib "C Programmer's Manual"
X.SH NAME
Xstroverlap \- check whether two strings overlap
X.SH SYNOPSIS
X.nf
X#include <publib.h>
Xint \fBstroverlap\fR(const char *\fIs\fR, const char *\fIt\fR);
X.SH DESCRIPTION
X\fIstroverlap\fR checks whether the storage used by two strings
Xoverlap (i.e., if they even partially stored in the same place
Xin memory).
X.SH "RETURN VALUE"
X\fIstroverlap\fR returns 0 for no overlap, nonzero for any overlap
Xat all.
X.SH "SEE ALSO"
Xpublib(3), memoverlap(3)
X.SH AUTHOR
XLars Wirzenius (lars.wirzenius@helsinki.fi)
SHAR_EOF
chmod 0644 ./strutil/stroverlap.3 || echo "restore of ./strutil/stroverlap.3 fails"
echo "x - extracting ./strutil/stroverlap.c (Text)"
sed 's/^X//' << 'SHAR_EOF' > ./strutil/stroverlap.c &&
X/*
X * stroverlap.c -- check whether two strings overlap
X *
X * Part of publib. See man page for more information
X * "@(#)publib-strutil:stroverlap.c,v 1.1 1994/06/20 20:30:28 liw Exp"
X */
X
X#include <assert.h>
X#include <string.h>
X#include "publib/strutil.h"
X
Xint stroverlap(const char *s, const char *t) {
X assert(s != NULL);
X assert(t != NULL);
X return memoverlap(s, strlen(s)+1, t, strlen(t)+1);
X}
SHAR_EOF
chmod 0644 ./strutil/stroverlap.c || echo "restore of ./strutil/stroverlap.c fails"
echo "x - extracting ./strutil/strrev.c (Text)"
sed 's/^X//' << 'SHAR_EOF' > ./strutil/strrev.c &&
X/*
X * strrev.c -- reverse a string in place
X *
X * Part of publib. See man page for more information
X * "@(#)publib-strutil:strrev.c,v 1.1.1.1 1994/02/03 17:25:30 liw Exp"
X */
X
X#include <assert.h>
X#include "publib/strutil.h"
X
Xchar *strrev(char *s) {
X char c, *t, *origs = s;
X
X assert(s != NULL);
X
X for (t = s+strlen(s); s < t; ++s, --t) {
X c = *s;
X *s = *t;
X *t = c;
X }
X return origs;
X}
SHAR_EOF
chmod 0644 ./strutil/strrev.c || echo "restore of ./strutil/strrev.c fails"
echo "x - extracting ./strutil/strright.3 (Text)"
sed 's/^X//' << 'SHAR_EOF' > ./strutil/strright.3 &&
X.\" part of publib
X.\" "@(#)publib-strutil:strright.3,v 1.1 1994/06/20 20:30:31 liw Exp"
X.\"
X.TH STRRIGHT 3 "C Programmer's Manual" Publib "C Programmer's Manual"
X.SH NAME
Xstrright \- return a pointer to the beginning of the rightmost n chars in a string
X.SH SYNOPSIS
X.nf
X#include <publib.h>
Xchar *\fBstrright\fR(const char *\fIs\fR, size_t \fIn\fR);
X.SH DESCRIPTION
X\fIstrright\fR will return a pointer to the first of the \fIn\fR rightmost
Xcharacters (not counting the '\\0') in the string \fIs\fR. It does \fInot\fR
Xmake a copy of the string, but will return a pointer into the argument
Xstring.
X.SH "SEE ALSO"
Xpublib(3)
X.SH AUTHOR
XLars Wirzenius (lars.wirzenius@helsinki.fi)
SHAR_EOF
chmod 0644 ./strutil/strright.3 || echo "restore of ./strutil/strright.3 fails"
echo "x - extracting ./strutil/strright.c (Text)"
sed 's/^X//' << 'SHAR_EOF' > ./strutil/strright.c &&
X/*
X * strright.c -- return a pointer to the beginning of the rightmost n chars
X *
X * Part of publib. See man page for more information
X * "@(#)publib-strutil:strright.c,v 1.1 1994/06/20 20:30:32 liw Exp"
X */
X
X#include <assert.h>
X#include <string.h>
X#include "publib/strutil.h"
X
Xchar *strright(const char *s, size_t n) {
X size_t len;
X
X assert(s != NULL);
X len = strlen(s);
X if (n > len)
X n = 0;
X return (char *)s + (len - n);
X}
SHAR_EOF
chmod 0644 ./strutil/strright.c || echo "restore of ./strutil/strright.c fails"
echo "x - extracting ./strutil/strrot13.3 (Text)"
sed 's/^X//' << 'SHAR_EOF' > ./strutil/strrot13.3 &&
X.\" part of publib
X.\" "@(#)publib-strutil:strrot13.3,v 1.1 1994/02/05 17:09:25 liw Exp"
X.\"
X.TH STRROT13 3 "C Programmer's Manual" Publib "C Programmer's Manual"
X.SH NAME
Xstrrot13 \- encrypt or decrypt string using rot13
X.SH SYNOPSIS
X.nf
X#include <publib.h>
Xchar *\fBstrrot13\fR(char *\fIstr\fR);
X.SH DESCRIPTION
X\fIstrrot13\fR converts the argument string using rot13, i.e., it
Xreplaces each letter a with n, n with a, b with o, o with b, and
Xso on. Converting twice results in the original string. Non-letter
Xcharacters are not converted.
X.PP
XThe rot13 encryption method is used commonly on USENET to hide
Xoffensive text, or spoilers in discussions about movies or books,
Xor in other similar occasions.
X.SH "RETURN VALUE"
X\fIstrrot13\fR returns its argument.
X.SH "SEE ALSO"
Xpublib(3), crypt(3)
X.SH AUTHOR
XLars Wirzenius (lars.wirzenius@helsinki.fi)
SHAR_EOF
chmod 0644 ./strutil/strrot13.3 || echo "restore of ./strutil/strrot13.3 fails"
echo "x - extracting ./strutil/strrot13.c (Text)"
sed 's/^X//' << 'SHAR_EOF' > ./strutil/strrot13.c &&
X/*
X * strrot13.c -- encrypt string with rot13
X *
X * Part of publib. See man page for more information
X * "@(#)publib-strutil:strrot13.c,v 1.1.1.1 1994/02/03 17:25:31 liw Exp"
X */
X
X#include <assert.h>
X#include "publib/strutil.h"
X
X#define N 26
X
X
X/* Warning: this code assumes ASCII */
Xchar *strrot13(char *str) {
X char *s = str;
X
X while (*s != '\0') {
X if ((*s >= 'a' && *s <= 'm') || (*s >= 'A' && *s <= 'M'))
X *s += 13;
X else if ((*s >= 'n' && *s <= 'z') || (*s >= 'N' && *s <= 'Z'))
X *s -= 13;
X ++s;
X }
X return str;
X}
SHAR_EOF
chmod 0644 ./strutil/strrot13.c || echo "restore of ./strutil/strrot13.c fails"
echo "x - extracting ./strutil/strrstr.3 (Text)"
sed 's/^X//' << 'SHAR_EOF' > ./strutil/strrstr.3 &&
X.\" part of publib
X.\" "@(#)publib-strutil:strrstr.3,v 1.1.1.1 1994/02/03 17:25:29 liw Exp"
X.\"
X.TH STRRSTR 3 "C Programmer's Manual" Publib "C Programmer's Manual"
X.SH NAME
Xstrrstr \- locate last occurence of substring
X.SH SYNOPSIS
X.nf
X#include <publib.h>
Xchar *\fBstrrstr\fR(const char *\fIstr\fR, const char *\fIpat\fR);
X.SH DESCRIPTION
X\fIstrrstr\fR finds the last occurence of the string \fIpat\fR in
Xthe string \fIstr\fR. The terminating '\\0' characters are not
Xcompared.
X.SH "RETURN VALUE"
X\fIstrrstr\fR returns a pointer to the first character of the substring,
Xor \fBNULL\fR if the substring is not found.
X.SH EXAMPLE
XTo print out everything on each line starting with the last occurence
Xof "/* " on each line, you might use the following code:
X.sp 1
X.nf
X.in +5
X#include <stdio.h>
X#include <publib.h>
X
Xint main(void) {
X char *p, line[512];
X
X while (fgets(line, sizeof(line), stdin) != NULL) {
X p = strrstr(line, "/* ");
X if (p != NULL)
X printf("%s", p);
X }
X return 0;
X}
X.in -5
X.SH "SEE ALSO"
Xpublib(3), string(3), strstr(3)
X.SH AUTHOR
XLars Wirzenius (lars.wirzenius@helsinki.fi)
SHAR_EOF
chmod 0644 ./strutil/strrstr.3 || echo "restore of ./strutil/strrstr.3 fails"
echo "x - extracting ./strutil/strrstr.c (Text)"
sed 's/^X//' << 'SHAR_EOF' > ./strutil/strrstr.c &&
X/*
X * strrstr.c -- find last occurence of string in another string
X *
X * Part of publib. See man page for more information.
X * "@(#)publib-strutil:strrstr.c,v 1.1.1.1 1994/02/03 17:25:29 liw Exp"
X */
X
X#include <assert.h>
X#include <string.h>
X#include "publib/strutil.h"
X
Xchar *strrstr(const char *str, const char *pat) {
X size_t len, patlen;
X const char *p;
X
X assert(str != NULL);
X assert(pat != NULL);
X
X len = strlen(str);
X patlen = strlen(pat);
X
X if (patlen > len)
X return NULL;
X for (p = str + (len - patlen); p > str; --p)
X if (*p == *pat && strncmp(p, pat, patlen) == 0)
X return (char *) p;
X return NULL;
X}
SHAR_EOF
chmod 0644 ./strutil/strrstr.c || echo "restore of ./strutil/strrstr.c fails"
echo "x - extracting ./strutil/strrtrim.3 (Text)"
sed 's/^X//' << 'SHAR_EOF' > ./strutil/strrtrim.3 &&
X.\" part of publib
X.\" "@(#)publib-strutil:strrtrim.3,v 1.1.1.1 1994/02/03 17:25:30 liw Exp"
X.\"
X.TH STRRTRIM 3 "C Programmer's Manual" Publib "C Programmer's Manual"
X.SH NAME
Xstrrtrim \- remove trailing whitespace
X.SH SYNOPSIS
X.nf
X#include <publib.h>
Xchar *\fBstrrtrim\fR(char *\fIs\fR);
X.SH DESCRIPTION
X\fIstrrtrim\fR removes all trailing whitespace characters from the
Xend of a string. As whitespace is counted everything for which
X\fIisspace\fR(3) returns true.
X.SH "RETURN VALUE"
X\fIstrltrim\fR returns its argument.
X.SH EXAMPLE
XTo remove whitespace from the end of all lines, you might do the
Xfollowing:
X.sp 1
X.nf
X.in +5
X#include <publib.h>
X
Xint main(void) {
X char line[512];
X
X while (fgets(line, sizeof(line), stdio) != NULL) {
X strrtrim(line);
X printf("%s", line);
X }
X return 0;
X}
X.in -5
X.SH "SEE ALSO"
Xpublib(3), strtrim(3), strltrim(3), isspace(3)
X.SH AUTHOR
XLars Wirzenius (lars.wirzenius@helsinki.fi)
SHAR_EOF
chmod 0644 ./strutil/strrtrim.3 || echo "restore of ./strutil/strrtrim.3 fails"
echo "x - extracting ./strutil/strrtrim.c (Text)"
sed 's/^X//' << 'SHAR_EOF' > ./strutil/strrtrim.c &&
X/*
X * strrtrim.c -- remove trailing whitespace from a string
X *
X * Part of publib. See man page for more information
X * "@(#)publib-strutil:strrtrim.c,v 1.3 1994/07/16 12:11:02 liw Exp"
X */
X
X#include <assert.h>
X#include <ctype.h>
X#include <string.h>
X
X#include "publib/strutil.h"
X
Xchar *strrtrim(char *s) {
X char *t, *tt;
X
X assert(s != NULL);
X
X for (tt = t = s; *t != '\0'; ++t)
X if (!isspace(*t))
X tt = t+1;
X *tt = '\0';
X
X return s;
X}
SHAR_EOF
chmod 0644 ./strutil/strrtrim.c || echo "restore of ./strutil/strrtrim.c fails"
echo "x - extracting ./strutil/strset.3 (Text)"
sed 's/^X//' << 'SHAR_EOF' > ./strutil/strset.3 &&
X.\" part of publib
X.\" "@(#)publib-strutil:strset.3,v 1.1 1994/06/20 20:30:35 liw Exp"
X.\"
X.TH STRSET 3 "C Programmer's Manual" Publib "C Programmer's Manual"
X.SH NAME
Xstrset \- set all characters in a string to a given character
X.SH SYNOPSIS
X.nf
X#include <publib.h>
Xchar *\fBstrset\fR(char *\fIstr\fR, int \fIc\fR);
X.SH DESCRIPTION
X\fIstrset\fR will set all characters (before the terminating '\\0') in
Xthe string \fIstr\fR to \fIc\fR.
X.SH "RETURN VALUE"
X\fIstrset\fR returns its first argument.
X.SH "SEE ALSO"
Xpublib(3), memset(3)
X.SH AUTHOR
XLars Wirzenius (lars.wirzenius@helsinki.fi)
SHAR_EOF
chmod 0644 ./strutil/strset.3 || echo "restore of ./strutil/strset.3 fails"
echo "x - extracting ./strutil/strset.c (Text)"
sed 's/^X//' << 'SHAR_EOF' > ./strutil/strset.c &&
X/*
X * strset.c -- set all characters in a string to a given character
X *
X * Part of publib. See man page for more information
X * "@(#)publib-strutil:strset.c,v 1.1 1994/06/20 20:30:36 liw Exp"
X */
X
X#include <assert.h>
X#include <string.h>
X#include "publib/strutil.h"
X
Xchar *strset(char *str, int c) {
X char *s;
X assert(str != NULL);
X
X for (s = str; *s != '\0'; ++s)
X *s = c;
X return str;
X}
SHAR_EOF
chmod 0644 ./strutil/strset.c || echo "restore of ./strutil/strset.c fails"
echo "x - extracting ./strutil/strshuffle.3 (Text)"
sed 's/^X//' << 'SHAR_EOF' > ./strutil/strshuffle.3 &&
X.\" part of publib
X.\" "@(#)publib-strutil:strshuffle.3,v 1.1 1994/06/20 20:30:37 liw Exp"
X.\"
X.TH STRSHUFFLE 3 "C Programmer's Manual" Publib "C Programmer's Manual"
X.SH NAME
Xstrshuffle \- make the characters in a string be in random order
X.SH SYNOPSIS
X.nf
X#include <publib.h>
Xchar *\fBstrshuffle\fR(char *\fIstr\fR);
X.SH DESCRIPTION
X\fIstrshuffle\fR will make the characters in a string be in random
Xorder.
X.SH "RETURN VALUE"
X\fIstrshuffle\fR will return its argument.
X.SH "SEE ALSO"
Xpublib(3), memshuffle(3)
X.SH AUTHOR
XLars Wirzenius (lars.wirzenius@helsinki.fi)
SHAR_EOF
chmod 0644 ./strutil/strshuffle.3 || echo "restore of ./strutil/strshuffle.3 fails"
echo "x - extracting ./strutil/strshuffle.c (Text)"
sed 's/^X//' << 'SHAR_EOF' > ./strutil/strshuffle.c &&
X/*
X * strshuffle.c -- make the characters in a string be in random order
X *
X * Part of publib. See man page for more information
X * "@(#)publib-strutil:strshuffle.c,v 1.1 1994/06/20 20:30:38 liw Exp"
X */
X
X#include <assert.h>
X#include <string.h>
X#include <stdlib.h>
X#include "publib/strutil.h"
X
X
Xchar *strshuffle(char *str) {
X assert(str != NULL);
X return memshuffle(str, 1, strlen(str));
X}
SHAR_EOF
chmod 0644 ./strutil/strshuffle.c || echo "restore of ./strutil/strshuffle.c fails"
echo "x - extracting ./strutil/strsplit.3 (Text)"
sed 's/^X//' << 'SHAR_EOF' > ./strutil/strsplit.3 &&
X.\" part of publib
X.\" "@(#)publib-strutil:strsplit.3,v 1.2 1994/02/19 20:58:36 liw Exp"
X.\"
X.TH STRSPLIT 3 "C Programmer's Manual" Publib "C Programmer's Manual"
X.SH NAME
Xstrsplit \- split string into words
X.SH SYNOPSIS
X.nf
X#include <publib.h>
Xint \fBstrsplit\fR(char *\fIsrc\fR, char **\fIwords\fR, int \fImaxw\fR, const char *\fIsep\fR);
X.SH DESCRIPTION
X\fIstrsplit\fR splits the \fIsrc\fR string into words separated by one
Xor more of the characters in \fIsep\fR (or by whitespace characters, as
Xspecified by \fIisspace\fR(3), if \fIsep\fR is the empty string).
XPointers to the words are stored in successive elements in the array
Xpointed to by \fIwords\fR. No more than \fImaxw\fR pointers are stored.
XThe input string is modifed by replacing the separator character
Xfollowing a word with '\\0'. However, if there are more than \fImaxw\fR
Xwords, only \fImaxw\fR-1 words will be returned, and the \fImaxw\fRth
Xpointer in the array will point to the rest of the string. If
X\fImaxw\fR is 0, no modification is done. This can be used for counting
Xhow many words there are, e.g., so that space for the word pointer table
Xcan be allocated dynamically.
X.PP
Xstrsplit splits the src string into words separated by one or more
Xof the characters in sep (or by whitespace characters, as defined by
Xisspace(3), if sep is the empty string). The src string is modified
Xby replacing the separator character after each word with '\\0'. A
Xpointer to each word is stored into successive elements of the
Xarray words. If there are more than maxw words, a '\\0' is stored
Xafter the first maxw-1 words only, and the words[maxw-1] will contain
Xa pointer to the rest of the string after the word in words[maxw-2].
X
X.SH "RETURN VALUE"
X\fIstrsplit\fR returns the total number of words in the input string.
X.SH EXAMPLE
XAssuming that words are separated by white space, to count the number
Xof words on a line, one might say the following.
X.sp 1
X.nf
X.in +5
Xn = strsplit(line, NULL, 0, "");
X.in -5
X.PP
XTo print out the fields of a colon-separated list (such as PATH, or a
Xline from /etc/passwd or /etc/group), one might do the following.
X.sp 1
X.nf
X.in +5
Xchar *fields[15];
Xint i, n;
X
Xn = strsplit(list, fields, 15, ":");
Xif (n > 15)
X n = 15;
Xfor (i = 0; i < n; ++i)
X printf("field %d: %s\\n", i, fields[i]);
X.in -5
X.PP
XIn real life, one would of course prefer to not restrict the number of
Xfields, so one might either allocated the pointer table dynamically
X(first counting the number of words using something like the first
Xexample), or realize that since it is the original string that is
Xbeing modified, one can do the following:
X.sp 1
X.nf
X.in +5
Xchar *fields[15];
Xint i, n;
X
Xdo {
X n = strsplit(list, fields, 15, ":");
X if (n > 15)
X n = 15;
X for (i = 0; i < n; ++i)
X printf("field %d: %s\\n", i, fields[i]);
X list = field[n-1] + strlen(field[n-1]);
X} while (n == 15);
X.in -5
X.SH "SEE ALSO"
Xpublib(3), strtok(3)
X.SH AUTHOR
XThe idea for this function came from C-News source code by Henry Spencer
Xand Geoff Collyer. Their function is very similar, but this
Ximplementation is by Lars Wirzenius (lars.wirzenius@helsinki.fi)
SHAR_EOF
chmod 0644 ./strutil/strsplit.3 || echo "restore of ./strutil/strsplit.3 fails"
echo "x - extracting ./strutil/strsplit.c (Text)"
sed 's/^X//' << 'SHAR_EOF' > ./strutil/strsplit.c &&
X/*
X * strsplit.c -- split a string into "words"
X *
X * Part of publib. See man page for more information
X * "@(#)publib-strutil:strsplit.c,v 1.3 1994/02/19 20:58:38 liw Exp"
X */
X
X#include <assert.h>
X#include <ctype.h>
X#include <string.h>
X#include <stdlib.h>
X
X#include "publib/strutil.h"
X
Xstatic char *find_nonsep(const char *s, const char *sep);
Xstatic char *find_sep(const char *s, const char *sep);
X
Xint strsplit(char *s, char **words, int maxw, const char *sep) {
X char *start, *end;
X int count;
X
X assert(s != NULL);
X assert(words != NULL || maxw == 0);
X assert(sep != NULL);
X assert(!memoverlap(s, strlen(s)+1, sep, strlen(sep)+1));
X
X count = 0;
X end = s;
X while ((start = find_nonsep(end, sep)) != NULL) {
X end = find_sep(start, sep);
X if (count == maxw-1 && find_nonsep(end, sep) != NULL) {
X words[count] = start;
X
X }
X if (count < maxw) {
X *end++ = '\0';
X words[count] = start;
X }
X ++count;
X }
X
X return count;
X}
X
X
X/* Find first character that is not a separator, starting with the character
X at s. Return NULL if not found. */
Xstatic char *find_nonsep(const char *s, const char *sep) {
X if (sep != NULL)
X s += strspn(s, sep);
X else
X while (isspace(*s))
X ++s;
X return *s == '\0' ? NULL : (char *) s;
X}
X
X
X/* Find first character that is a separator, starting with the character
X at s. Treat '\0' as a separator, so that the call always succeeds. */
Xstatic char *find_sep(const char *s, const char *sep) {
X if (sep != NULL)
X s += strcspn(s, sep);
X else
X while (*s != '\0' && !isspace(*s))
X ++s;
X return (char *) s;
X}
SHAR_EOF
chmod 0644 ./strutil/strsplit.c || echo "restore of ./strutil/strsplit.c fails"
echo "x - extracting ./strutil/strsub.3 (Text)"
sed 's/^X//' << 'SHAR_EOF' > ./strutil/strsub.3 &&
X.\" part of publib
X.\" "@(#)publib-strutil:strsub.3,v 1.1.1.1 1994/02/03 17:25:30 liw Exp"
X.\"
X.TH STRSUB 3 "C Programmer's Manual" Publib "C Programmer's Manual"
X.SH NAME
Xstrsub \- substitute first occurence of pattern with another string
X.SH SYNOPSIS
X.nf
X#include <publib.h>
Xchar *\fBstrsub\fR(char *\fIstr\fR, const char *\fIpat\fR, const char *\fIsub\fR);
X.SH DESCRIPTION
X\fIstrsub\fR finds the first occurence of the pattern \fIpat\fR in the
Xstring \fIstr\fR (using a method similar to \fIstrstr\fR(3), i.e., no
Xregular expressions), and replaces it with \fIsub\fR.
XIf \fIpat\fR does not occur in \fIstr\fR, no substitution is made.
X.PP
XOf course, if \fIsub\fR is an empty string, the pattern is deleted from
Xthe string.
X.SH "RETURN VALUE"
X\fIstrsub\fR returns a pointer to the first character after the substitution,
Xor NULL if no substitution was made.
X.SH EXAMPLE
XTo substitute up to two occurences of "foo" with "bar" in a line,
Xone might do the following.
X.sp 1
X.nf
X.in +5
Xp = strsub(line, "foo", "bar");
Xif (p != NULL)
X strsub(line, "foo", "bar");
X.in -5
X.SH "SEE ALSO"
Xpublib(3), strstr(3), strgsub(3)
X.SH AUTHOR
XLars Wirzenius (lars.wirzenius@helsinki.fi)
SHAR_EOF
chmod 0644 ./strutil/strsub.3 || echo "restore of ./strutil/strsub.3 fails"
echo "x - extracting ./strutil/strsub.c (Text)"
sed 's/^X//' << 'SHAR_EOF' > ./strutil/strsub.c &&
X/*
X * strsub.c -- substitute first occurence of pattern with another string
X *
X * Part of publib. See man page for more information
X * "@(#)publib-strutil:strsub.c,v 1.1.1.1 1994/02/03 17:25:30 liw Exp"
X */
X
X#include <assert.h>
X#include <string.h>
X#include "publib/strutil.h"
X
Xchar *strsub(char *str, const char *pat, const char *sub) {
X size_t lenpat, lensub, lenstr;
X
X assert(str != NULL);
X assert(pat != NULL);
X assert(*pat != '\0');
X assert(sub != NULL);
X
X str = strstr(str, pat);
X if (str == NULL)
X return NULL;
X
X lenstr = strlen(str);
X lenpat = strlen(pat);
X lensub = strlen(sub);
X
X /* make room for substituted string, or remove slack after it */
X if (lensub != lenpat)
X memmove(str + lensub, str + lenpat, lenstr + 1 - lenpat);
X
X memcpy(str, sub, lensub);
X return str + lensub;
X}
SHAR_EOF
chmod 0644 ./strutil/strsub.c || echo "restore of ./strutil/strsub.c fails"
echo "x - extracting ./strutil/strtabify.3 (Text)"
sed 's/^X//' << 'SHAR_EOF' > ./strutil/strtabify.3 &&
X.\" part of publib
X.\" "@(#)publib-strutil:strtabify.3,v 1.1 1994/06/20 20:30:39 liw Exp"
X.\"
X.TH STRTABIFY 3 "C Programmer's Manual" Publib "C Programmer's Manual"
X.SH NAME
Xstrtabify \- convert runs of spaces and tabs to tabs
SHAR_EOF
echo "End of part 6"
echo "File ./strutil/strtabify.3 is continued in part 7"
echo "7" > s2_seq_.tmp
exit 0