home *** CD-ROM | disk | FTP | other *** search
/ Big Green CD 8 / BGCD_8_Dev.iso / NEXTSTEP / UNIX / Shells / zsh-3.0.5-MIHS / src / Src / compat.c < prev    next >
Encoding:
C/C++ Source or Header  |  1997-09-25  |  4.1 KB  |  190 lines

  1. /*
  2.  * $Id: compat.c,v 2.3 1996/10/15 20:16:35 hzoli Exp $
  3.  *
  4.  * compat.c - compatibiltiy routines for the deprived
  5.  *
  6.  * This file is part of zsh, the Z shell.
  7.  *
  8.  * Copyright (c) 1992-1996 Paul Falstad
  9.  * All rights reserved.
  10.  *
  11.  * Permission is hereby granted, without written agreement and without
  12.  * license or royalty fees, to use, copy, modify, and distribute this
  13.  * software and to distribute modified versions of this software for any
  14.  * purpose, provided that the above copyright notice and the following
  15.  * two paragraphs appear in all copies of this software.
  16.  *
  17.  * In no event shall Paul Falstad or the Zsh Development Group be liable
  18.  * to any party for direct, indirect, special, incidental, or consequential
  19.  * damages arising out of the use of this software and its documentation,
  20.  * even if Paul Falstad and the Zsh Development Group have been advised of
  21.  * the possibility of such damage.
  22.  *
  23.  * Paul Falstad and the Zsh Development Group specifically disclaim any
  24.  * warranties, including, but not limited to, the implied warranties of
  25.  * merchantability and fitness for a particular purpose.  The software
  26.  * provided hereunder is on an "as is" basis, and Paul Falstad and the
  27.  * Zsh Development Group have no obligation to provide maintenance,
  28.  * support, updates, enhancements, or modifications.
  29.  *
  30.  */
  31.  
  32. #include "zsh.h"
  33.  
  34. /* Return pointer to first occurence of string t *
  35.  * in string s.  Return NULL if not present.     */
  36.  
  37. #ifndef HAVE_STRSTR
  38. char *
  39. strstr(const char *s, const char *t)
  40. {
  41.     char *p1, *p2;
  42.  
  43.     for (; *s; s++) {
  44.         for (p1 = s, p2 = t; *p2; p1++, p2++)
  45.             if (*p1 != *p2)
  46.                 break;
  47.         if (!*p2)
  48.             return (char *)s;
  49.     }
  50.     return NULL;
  51. }
  52. #endif
  53.  
  54.  
  55. #ifndef HAVE_GETHOSTNAME
  56. int
  57. gethostname(char *name, int namelen)
  58. {
  59.     struct utsname uname_str;
  60.  
  61.     uname(&uname_str);
  62.     strncpy(hostnam, uname_str.nodename, 256);
  63.     return 0;
  64. }
  65. #endif
  66.  
  67.  
  68. #ifndef HAVE_GETTIMEOFDAY
  69. void
  70. gettimeofday(struct timeval *tv, struct timezone *tz)
  71. {
  72.     tv->tv_usec = 0;
  73.     tv->tv_sec = (long)time((time_t) 0);
  74. }
  75. #endif
  76.  
  77.  
  78. /* compute the difference between two calendar times */
  79.  
  80. #ifndef HAVE_DIFFTIME
  81. double
  82. difftime(time_t t2, time_t t1)
  83. {
  84.     return ((double)t2 - (double)t1);
  85. }
  86. #endif
  87.  
  88.  
  89. #ifndef HAVE_STRERROR
  90. extern char *sys_errlist[];
  91.  
  92. /* Get error message string associated with a particular  *
  93.  * error number, and returns a pointer to that string.    *
  94.  * This is not a particularly robust version of strerror. */
  95.  
  96. char *
  97. strerror(int errnum)
  98. {
  99.     return (sys_errlist[errnum]);
  100. }
  101. #endif
  102.  
  103.  
  104. /* This function will be changed to work the same way *
  105.  * as POSIX getcwd.  Then I'll use the system getcwd  *
  106.  * if configure finds one.                            */
  107.  
  108. /**/
  109. char *
  110. zgetcwd(void)
  111. {
  112.     static char buf0[PATH_MAX];
  113.     char *buf2 = buf0 + 1;
  114.     char buf3[PATH_MAX];
  115.     struct stat sbuf;
  116.     struct dirent *de;
  117.     DIR *dir;
  118.     ino_t ino, pino, rootino = (ino_t) ~ 0;
  119.     dev_t dev, pdev, rootdev = (dev_t) ~ 0;
  120.  
  121.     holdintr();
  122.     buf2[0] = '\0';
  123.     buf0[0] = '/';
  124.  
  125.     if (stat(buf0, &sbuf) >= 0) {
  126.     rootino = sbuf.st_ino;
  127.     rootdev = sbuf.st_dev;
  128.     }
  129.  
  130.     if (stat(".", &sbuf) < 0) {
  131.     noholdintr();
  132.     return ztrdup(".");
  133.     }
  134.  
  135.     pino = sbuf.st_ino;
  136.     pdev = sbuf.st_dev;
  137.  
  138.     for (;;) {
  139.     if (stat("..", &sbuf) < 0) {
  140.         chdir(buf0);
  141.         noholdintr();
  142.         return ztrdup(".");
  143.     }
  144.  
  145.     ino = pino;
  146.     dev = pdev;
  147.     pino = sbuf.st_ino;
  148.     pdev = sbuf.st_dev;
  149.  
  150.     if ((ino == pino && dev == pdev) ||
  151.         (ino == rootino && dev == rootdev)) {
  152.         chdir(buf0);
  153.         noholdintr();
  154.         return ztrdup(buf0);
  155.     }
  156.     dir = opendir("..");
  157.     if (!dir) {
  158.         chdir(buf0);
  159.         noholdintr();
  160.         return ztrdup(".");
  161.     }
  162.     chdir("..");
  163.     while ((de = readdir(dir))) {
  164.         char *fn = de->d_name;
  165.         /* Ignore `.' and `..'. */
  166.         if (fn[0] == '.' &&
  167.         (fn[1] == '\0' ||
  168.          (fn[1] == '.' && fn[2] == '\0')))
  169.         continue;
  170.         if (dev != pdev || (ino_t) de->d_ino == ino) {
  171.         lstat(fn, &sbuf);
  172.         if (sbuf.st_dev == dev && sbuf.st_ino == ino) {
  173.             strcpy(buf3, de->d_name);
  174.             break;
  175.         }
  176.         }
  177.     }
  178.     closedir(dir);
  179.     if (!de) {
  180.         noholdintr();
  181.         return ztrdup(".");
  182.     }
  183.     if (*buf2)
  184.         strcat(buf3, "/");
  185.     strcat(buf3, buf2);
  186.     strcpy(buf2, buf3);
  187.     }
  188. }
  189.  
  190.