VirtualBox

Changeset 51 in kBuild for trunk/src/kmk/util.c


Ignore:
Timestamp:
Apr 7, 2003 1:30:32 AM (22 years ago)
Author:
bird
Message:

kMk and porting to kLib.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/kmk/util.c

    r35 r51  
    55#ifndef lint
    66static char rcsid[] = "$FreeBSD: src/usr.bin/make/util.c,v 1.5.2.2 2001/02/13 03:13:58 will Exp $";
     7#define KLIBFILEDEF rcsid
    78#endif
    89
     
    2728    static char buf[100];
    2829    if (e < 0 || e >= sys_nerr) {
    29         sprintf(buf, "Unknown error %d", e);
    30         return buf;
     30        sprintf(buf, "Unknown error %d", e);
     31        return buf;
    3132    }
    3233    else
    33         return sys_errlist[e];
     34        return sys_errlist[e];
    3435}
    3536#endif
     
    5051
    5152    if (str == NULL)
    52         return NULL;
     53        return NULL;
    5354    len = strlen(str) + 1;
    5455    if ((p = emalloc(len)) == NULL)
    55         return NULL;
     56        return NULL;
    5657
    5758    return memcpy(p, str, len);
     
    6061#endif
    6162
    62 #if defined(sun) || defined(__hpux) || defined(__sgi) || defined(__EMX__) || (defined(OS2) && defined(__IBMC__))
     63#ifndef USE_KLIB
     64#if defined(sun) || defined(__hpux) || defined(__sgi) || defined(__EMX__)
    6365
    6466int
     
    7577
    7678    if (ptr == NULL)
    77         return -1;
     79        return -1;
    7880
    7981    p = ptr;
    8082
    8183    while (*name)
    82         *p++ = *name++;
     84        *p++ = *name++;
    8385
    8486    *p++ = '=';
    8587
    8688    while (*value)
    87         *p++ = *value++;
     89        *p++ = *value++;
    8890
    8991    *p = '\0';
     
    9395    return len;
    9496}
     97#endif
    9598#endif
    9699
     
    157160
    158161/* strrcpy():
    159  *      Like strcpy, going backwards and returning the new pointer
     162 *      Like strcpy, going backwards and returning the new pointer
    160163 */
    161164static char *
     
    166169
    167170    while (len)
    168         *--ptr = str[--len];
     171        *--ptr = str[--len];
    169172
    170173    return (ptr);
     
    185188    /* find the inode of root */
    186189    if (stat("/", &st_root) == -1) {
    187         (void) sprintf(pathname,
    188                         "getwd: Cannot stat \"/\" (%s)", strerror(errno));
    189         return (NULL);
     190        (void) sprintf(pathname,
     191                        "getwd: Cannot stat \"/\" (%s)", strerror(errno));
     192        return (NULL);
    190193    }
    191194    pathbuf[MAXPATHLEN - 1] = '\0';
     
    196199    /* find the inode of the current directory */
    197200    if (lstat(".", &st_cur) == -1) {
    198         (void) sprintf(pathname,
    199                         "getwd: Cannot stat \".\" (%s)", strerror(errno));
    200         return (NULL);
     201        (void) sprintf(pathname,
     202                        "getwd: Cannot stat \".\" (%s)", strerror(errno));
     203        return (NULL);
    201204    }
    202205    nextpathptr = strrcpy(nextpathptr, "../");
     
    205208    for (;;) {
    206209
    207         /* look if we found root yet */
    208         if (st_cur.st_ino == st_root.st_ino &&
    209             DEV_DEV_COMPARE(st_cur.st_dev, st_root.st_dev)) {
    210             (void) strcpy(pathname, *pathptr != '/' ? "/" : pathptr);
    211             return (pathname);
    212         }
    213 
    214         /* open the parent directory */
    215         if (stat(nextpathptr, &st_dotdot) == -1) {
    216             snprintf(pathname, sizeof(pathname),
    217                             "getwd: Cannot stat directory \"%s\" (%s)",
    218                             nextpathptr, strerror(errno));
    219             return (NULL);
    220         }
    221         if ((dp = opendir(nextpathptr)) == NULL) {
    222              snprintf(pathname, sizeof(pathname),
    223                             "getwd: Cannot open directory \"%s\" (%s)",
    224                             nextpathptr, strerror(errno));
    225             return (NULL);
    226         }
    227 
    228         /* look in the parent for the entry with the same inode */
    229         if (DEV_DEV_COMPARE(st_dotdot.st_dev, st_cur.st_dev)) {
    230             /* Parent has same device. No need to stat every member */
    231             for (d = readdir(dp); d != NULL; d = readdir(dp))
    232                 if (d->d_fileno == st_cur.st_ino)
    233                     break;
    234         }
    235         else {
    236             /*
    237              * Parent has a different device. This is a mount point so we
    238              * need to stat every member
    239              */
    240             for (d = readdir(dp); d != NULL; d = readdir(dp)) {
    241                 if (ISDOT(d->d_name) || ISDOTDOT(d->d_name))
    242                     continue;
    243                 (void) strcpy(cur_name_add, d->d_name);
    244                 if (lstat(nextpathptr, &st_next) == -1) {
    245                     snprintf(pathname, sizeof(pathname), "getwd: Cannot stat \"%s\" (%s)",
    246                                     d->d_name, strerror(errno));
    247                     (void) closedir(dp);
    248                     return (NULL);
    249                 }
    250                 /* check if we found it yet */
    251                 if (st_next.st_ino == st_cur.st_ino &&
    252                     DEV_DEV_COMPARE(st_next.st_dev, st_cur.st_dev))
    253                     break;
    254             }
    255         }
    256         if (d == NULL) {
    257             (void) sprintf(pathname, "getwd: Cannot find \".\" in \"..\"");
    258             (void) closedir(dp);
    259             return (NULL);
    260         }
    261         st_cur = st_dotdot;
    262         pathptr = strrcpy(pathptr, d->d_name);
    263         pathptr = strrcpy(pathptr, "/");
    264         nextpathptr = strrcpy(nextpathptr, "../");
    265         (void) closedir(dp);
    266         *cur_name_add = '\0';
     210        /* look if we found root yet */
     211        if (st_cur.st_ino == st_root.st_ino &&
     212            DEV_DEV_COMPARE(st_cur.st_dev, st_root.st_dev)) {
     213            (void) strcpy(pathname, *pathptr != '/' ? "/" : pathptr);
     214            return (pathname);
     215        }
     216
     217        /* open the parent directory */
     218        if (stat(nextpathptr, &st_dotdot) == -1) {
     219            snprintf(pathname, sizeof(pathname),
     220                            "getwd: Cannot stat directory \"%s\" (%s)",
     221                            nextpathptr, strerror(errno));
     222            return (NULL);
     223        }
     224        if ((dp = opendir(nextpathptr)) == NULL) {
     225             snprintf(pathname, sizeof(pathname),
     226                            "getwd: Cannot open directory \"%s\" (%s)",
     227                            nextpathptr, strerror(errno));
     228            return (NULL);
     229        }
     230
     231        /* look in the parent for the entry with the same inode */
     232        if (DEV_DEV_COMPARE(st_dotdot.st_dev, st_cur.st_dev)) {
     233            /* Parent has same device. No need to stat every member */
     234            for (d = readdir(dp); d != NULL; d = readdir(dp))
     235                if (d->d_fileno == st_cur.st_ino)
     236                    break;
     237        }
     238        else {
     239            /*
     240             * Parent has a different device. This is a mount point so we
     241             * need to stat every member
     242             */
     243            for (d = readdir(dp); d != NULL; d = readdir(dp)) {
     244                if (ISDOT(d->d_name) || ISDOTDOT(d->d_name))
     245                    continue;
     246                (void) strcpy(cur_name_add, d->d_name);
     247                if (lstat(nextpathptr, &st_next) == -1) {
     248                    snprintf(pathname, sizeof(pathname), "getwd: Cannot stat \"%s\" (%s)",
     249                                    d->d_name, strerror(errno));
     250                    (void) closedir(dp);
     251                    return (NULL);
     252                }
     253                /* check if we found it yet */
     254                if (st_next.st_ino == st_cur.st_ino &&
     255                    DEV_DEV_COMPARE(st_next.st_dev, st_cur.st_dev))
     256                    break;
     257            }
     258        }
     259        if (d == NULL) {
     260            (void) sprintf(pathname, "getwd: Cannot find \".\" in \"..\"");
     261            (void) closedir(dp);
     262            return (NULL);
     263        }
     264        st_cur = st_dotdot;
     265        pathptr = strrcpy(pathptr, d->d_name);
     266        pathptr = strrcpy(pathptr, "/");
     267        nextpathptr = strrcpy(nextpathptr, "../");
     268        (void) closedir(dp);
     269        *cur_name_add = '\0';
    267270    }
    268271} /* end getwd */
     
    336339
    337340    if (sigaction(s, &sa, &osa) == -1)
    338         return SIG_ERR;
     341        return SIG_ERR;
    339342    else
    340         return osa.sa_handler;
    341 }
    342 
    343 #endif
     343        return osa.sa_handler;
     344}
     345
     346#endif
Note: See TracChangeset for help on using the changeset viewer.

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette