Changeset 93 for trunk/src/sh_utils.c


Ignore:
Timestamp:
Feb 26, 2007, 10:48:51 PM (18 years ago)
Author:
rainer
Message:

Add check for PCI ROMs; fix ticket #51 (symlinks in root directory reported with leading double slash).

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/sh_utils.c

    r76 r93  
    554554}
    555555
    556 /* can't inline (AIX)
     556/* read a hexchar, return int value (0-15)
     557 * can't inline (AIX)
    557558 */
    558559int sh_util_hexchar( const char c )
     
    567568  else return -1;
    568569  /*@-charint@*/
     570}
     571
     572char * 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;
    569587}
    570588
     
    17181736  char * retval;
    17191737  size_t len;
     1738  char * tmp;
    17201739
    17211740  SL_ENTER(_("sh_util_dirname"));
    17221741
    17231742  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';
    17281771    --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';
    17311785    --len;
    1732     if (fullpath[len] == '/')
    1733       {
    1734         if (len == 0) ++len; /* copy the '/' to output */
    1735         break;
    1736       }
    17371786  }
    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 
    17491787  SL_RETURN(retval, _("sh_util_dirname"));
     1788
    17501789}
    17511790
     
    17541793char * sh_util_basename(const char * fullpath)
    17551794{
    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;
    17601800
    17611801  SL_ENTER(_("sh_util_basename"));
     
    17631803  ASSERT_RET ((fullpath != NULL), _("fullpath != NULL"), (NULL))
    17641804
    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        }
    17721827      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;
    17881830        }
    17891831    }
Note: See TracChangeset for help on using the changeset viewer.