Path: utzoo!utgpu!water!watmath!clyde!att!cbnews!lvc From: lvc@cbnews.ATT.COM (Lawrence V. Cipriani) Newsgroups: comp.unix.questions Subject: Re: Wanted: How to strip off pathnames to obtain filenames. Keywords: Any efficient way to generate "pr filenames" from "usend pathnames"? Message-ID: <1071@cbnews.ATT.COM> Date: 2 Sep 88 16:37:23 GMT References: <5968@ihlpf.ATT.COM> <194@dcs.UUCP> Reply-To: lvc@cbnews.ATT.COM (Lawrence V. Cipriani) Organization: AT&T Bell Laboratories, Columbus Lines: 42 In article <194@dcs.UUCP> wnp@dcs.UUCP (Wolf N. Paul) writes: [discussion of basename deleted] >However, this is also very straightforward to do in C: >#include >#include >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 #include 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@cbnews.ATT.COM