This is substr.c in view mode; [Download] [Up]
/*
* substr string first [last]
* Returns the substring of string firsting at the specified postion.
* Negative values mean count from the end.
*/
#include <stdio.h>
#include <strings.h>
main( argc, argv )
int argc;
char *argv[];
{
char *string;
int first, last, len;
if( !(argc==4 || argc==3 ) ) {
fprintf( stderr, "Usage: %s string first [last]\n", argv[0] );
fprintf( stderr, "Command given was: " );
while( argc-- > 0 )
fprintf( stderr, "%s ", *argv++ );
fprintf( stderr, "\n" );
exit(-1);
}
string = argv[1];
len = strlen( string );
first = atoi( argv[2] );
if( argc == 4 )
last = atoi( argv[3] );
else
last = len;
if( first<0 )
first = len+first+1;
if( last<0 )
last = len+last+1;
if( first>len || first>last || first==0 )
string[0] = '\0'; /* return an empty string */
else {
if( last < len )
string[last] = '\0';
string += first-1;
}
printf( "%s", string );
exit(0);
}
These are the contents of the former NiCE NeXT User Group NeXTSTEP/OpenStep software archive, currently hosted by Netfuture.ch.