Wanted: How to strip off pathnames to obtain filenames.

Lawrence V. Cipriani lvc at cbnews.ATT.COM
Sat Sep 3 02:37:23 AEST 1988


In article <194 at dcs.UUCP> wnp at dcs.UUCP (Wolf N. Paul) writes:
	[discussion of basename deleted]

>However, this is also very straightforward to do in C:
>#include <stdio.h>
>#include <string.h>
>main(argc, argv)
>int argc;
>char **argv;
>{
>	char *ptr;
>
>	ptr=strrchr(argv[1], '/');
>	*ptr++;
>	puts(ptr);
>}
>
>Wolf N. Paul * 3387 Sam Rayburn Run * Carrollton TX 75007 * (214) 306-9101

Almost.  It should be:

	#include <stdio.h>
	#include <string.h>

	main(argc, argv)
		int argc; char *argv[];
	{
		char *ptr;

		if ((ptr = strchr(argv[1], '/')) == NULL)
			ptr = argv[1];
		else
			ptr++;

		puts(ptr);
	}

strchr can return NULL when there are no chars in the string that match
the char you are searching for.

-- 
Larry Cipriani, AT&T Network Systems, Columbus OH, cbnews!lvc lvc at cbnews.ATT.COM



More information about the Comp.unix.questions mailing list