basename.c (108419) | basename.c (197804) |
---|---|
1/* 2 * Copyright (c) 1997 Todd C. Miller <Todd.Miller@courtesan.com> 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright --- 17 unchanged lines hidden (view full) --- 26 */ 27 28#if 0 29#ifndef lint 30static char rcsid[] = "$OpenBSD: basename.c,v 1.4 1999/05/30 17:10:30 espie Exp $"; 31#endif /* not lint */ 32#endif 33#include <sys/cdefs.h> | 1/* 2 * Copyright (c) 1997 Todd C. Miller <Todd.Miller@courtesan.com> 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright --- 17 unchanged lines hidden (view full) --- 26 */ 27 28#if 0 29#ifndef lint 30static char rcsid[] = "$OpenBSD: basename.c,v 1.4 1999/05/30 17:10:30 espie Exp $"; 31#endif /* not lint */ 32#endif 33#include <sys/cdefs.h> |
34__FBSDID("$FreeBSD: head/lib/libc/gen/basename.c 108419 2002-12-30 01:41:14Z marcel $"); | 34__FBSDID("$FreeBSD: head/lib/libc/gen/basename.c 197804 2009-10-06 14:05:57Z rwatson $"); |
35 36#include <errno.h> 37#include <libgen.h> 38#include <stdlib.h> 39#include <string.h> 40#include <sys/param.h> 41 42char * | 35 36#include <errno.h> 37#include <libgen.h> 38#include <stdlib.h> 39#include <string.h> 40#include <sys/param.h> 41 42char * |
43basename(path) | 43basename_r(path, bname) |
44 const char *path; | 44 const char *path; |
45 char *bname; |
|
45{ | 46{ |
46 static char *bname = NULL; | |
47 const char *endp, *startp; 48 | 47 const char *endp, *startp; 48 |
49 if (bname == NULL) { 50 bname = (char *)malloc(MAXPATHLEN); 51 if (bname == NULL) 52 return(NULL); 53 } 54 | |
55 /* Empty or NULL string gets treated as "." */ 56 if (path == NULL || *path == '\0') { 57 (void)strcpy(bname, "."); 58 return(bname); 59 } 60 61 /* Strip trailing slashes */ 62 endp = path + strlen(path) - 1; --- 14 unchanged lines hidden (view full) --- 77 if (endp - startp + 2 > MAXPATHLEN) { 78 errno = ENAMETOOLONG; 79 return(NULL); 80 } 81 (void)strncpy(bname, startp, endp - startp + 1); 82 bname[endp - startp + 1] = '\0'; 83 return(bname); 84} | 49 /* Empty or NULL string gets treated as "." */ 50 if (path == NULL || *path == '\0') { 51 (void)strcpy(bname, "."); 52 return(bname); 53 } 54 55 /* Strip trailing slashes */ 56 endp = path + strlen(path) - 1; --- 14 unchanged lines hidden (view full) --- 71 if (endp - startp + 2 > MAXPATHLEN) { 72 errno = ENAMETOOLONG; 73 return(NULL); 74 } 75 (void)strncpy(bname, startp, endp - startp + 1); 76 bname[endp - startp + 1] = '\0'; 77 return(bname); 78} |
79 80char * 81basename(path) 82 const char *path; 83{ 84 static char *bname = NULL; 85 86 if (bname == NULL) { 87 bname = (char *)malloc(MAXPATHLEN); 88 if (bname == NULL) 89 return (NULL); 90 } 91 return (basename_r(path, bname)); 92} |
|