Changeset 93 for trunk/src/sh_utils.c
- Timestamp:
- Feb 26, 2007, 10:48:51 PM (18 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/sh_utils.c
r76 r93 554 554 } 555 555 556 /* can't inline (AIX) 556 /* read a hexchar, return int value (0-15) 557 * can't inline (AIX) 557 558 */ 558 559 int sh_util_hexchar( const char c ) … … 567 568 else return -1; 568 569 /*@-charint@*/ 570 } 571 572 char * sh_util_charhex( unsigned char i ) 573 { 574 static char i2h[2]; 575 int j, k; 576 577 j = i / 16; 578 k = i - (j*16); 579 580 if (j < 10) i2h[0] = '0'+j; 581 else i2h[0] = 'A'+(j-10); 582 583 if (k < 10) i2h[1] = '0'+k; 584 else i2h[1] = 'A'+(k-10); 585 586 return i2h; 569 587 } 570 588 … … 1718 1736 char * retval; 1719 1737 size_t len; 1738 char * tmp; 1720 1739 1721 1740 SL_ENTER(_("sh_util_dirname")); 1722 1741 1723 1742 ASSERT_RET ((fullpath != NULL), _("fullpath != NULL"), (NULL)) 1724 1725 len = sl_strlen (fullpath); /* fullpath[i] is terminating '\0' */ 1726 1727 if (len > 1 && fullpath[len-1] == '/') /* skip trailing '/' */ 1743 ASSERT_RET ((*fullpath == '/'), _("*fullpath == '/'"), (NULL)) 1744 1745 retval = sh_util_strdup(fullpath); 1746 1747 tmp = retval; 1748 while (*tmp == '/') ++tmp; 1749 1750 /* (1) only leading slashes -- return exact copy 1751 */ 1752 if (*tmp == '\0') 1753 { 1754 SL_RETURN(retval, _("sh_util_dirname")); 1755 } 1756 1757 /* (2) there are non-slash characters, so delete trailing slashes 1758 */ 1759 len = sl_strlen (retval); /* retval[len] is terminating '\0' */ 1760 1761 while (len > 1 && retval[len-1] == '/') /* delete trailing slash */ 1762 { 1763 retval[len-1] = '\0'; 1764 --len; 1765 } 1766 1767 /* (3) now delete all non-slash characters up to the preceding slash 1768 */ 1769 while (len > 1 && retval[len-1] != '/') { 1770 retval[len-1] = '\0'; 1728 1771 --len; 1729 1730 while (len > 0) { 1772 } 1773 1774 /* (4a) only leading slashes left, so return this 1775 */ 1776 if (&(retval[len]) == tmp) 1777 { 1778 SL_RETURN(retval, _("sh_util_dirname")); 1779 } 1780 1781 /* (4b) strip trailing slash(es) of parent directory 1782 */ 1783 while (len > 1 && retval[len-1] == '/') { 1784 retval[len-1] = '\0'; 1731 1785 --len; 1732 if (fullpath[len] == '/')1733 {1734 if (len == 0) ++len; /* copy the '/' to output */1735 break;1736 }1737 1786 } 1738 1739 /* -- Not an absolute path. --1740 */1741 if ((len == 0) && (fullpath[len] != '/'))1742 {1743 SL_RETURN(NULL, _("sh_util_dirname"));1744 }1745 1746 retval = SH_ALLOC(len + 1);1747 (void) sl_strlcpy (retval, fullpath, len+1);1748 1749 1787 SL_RETURN(retval, _("sh_util_dirname")); 1788 1750 1789 } 1751 1790 … … 1754 1793 char * sh_util_basename(const char * fullpath) 1755 1794 { 1756 char * retval = NULL; 1757 char * tmp; 1758 char * c; 1759 size_t len; 1795 char * retval = NULL; 1796 const char * tmp; 1797 char * tmp2; 1798 char * c; 1799 size_t len; 1760 1800 1761 1801 SL_ENTER(_("sh_util_basename")); … … 1763 1803 ASSERT_RET ((fullpath != NULL), _("fullpath != NULL"), (NULL)) 1764 1804 1765 c = strrchr(fullpath, '/'); 1766 len = sl_strlen (c); 1767 1768 if (c == fullpath) 1769 { 1770 if (len <= 1) 1771 retval = sh_util_strdup(c); 1805 tmp = fullpath; while (*tmp == '/') ++tmp; 1806 if (*tmp == '\0') 1807 { 1808 retval = sh_util_strdup(fullpath); 1809 } 1810 else 1811 { 1812 tmp2 = sh_util_strdup(tmp); 1813 len = sl_strlen (tmp2); 1814 1815 while (len > 1 && tmp2[len-1] == '/') 1816 { 1817 tmp2[len-1] = '\0'; 1818 --len; 1819 } 1820 1821 c = strrchr(tmp2, '/'); 1822 if (c) 1823 { 1824 retval = sh_util_strdup(++c); 1825 SH_FREE(tmp2); 1826 } 1772 1827 else 1773 retval = sh_util_strdup(++c); 1774 } 1775 else 1776 { 1777 if (len > 1) 1778 { 1779 retval = sh_util_strdup(++c); 1780 } 1781 else 1782 { 1783 /* input ends in '/' */ 1784 tmp = sh_util_strdup(fullpath); 1785 tmp[strlen(tmp)-1] = '\0'; 1786 retval = sh_util_basename(tmp); 1787 SH_FREE(tmp); 1828 { 1829 retval = tmp2; 1788 1830 } 1789 1831 }
Note:
See TracChangeset
for help on using the changeset viewer.