home *** CD-ROM | disk | FTP | other *** search
/ The UNIX CD Bookshelf / OREILLY_TUCB_UNIX_CD.iso / upt / examples / SOURCES / REN / I386 / REN. next >
Encoding:
Text File  |  1998-07-24  |  5.3 KB  |  180 lines

  1. --- Makefile.rtr    Fri Jan 31 19:59:13 1997
  2. +++ Makefile    Tue Feb  4 17:18:22 1997
  3. @@ -1,10 +1,13 @@
  4.  all:    ren ren.1
  5.  
  6. -ren:    ren.c
  7. -    $(CC) -o ren $(CFLAGS) ren.c -lbsd
  8. +ren:    ren.c scandir.o
  9. +    $(CC) -o ren $(CFLAGS) ren.c scandir.o
  10.  install:    all
  11.      install -s ren <installdir>/bin
  12.      install ren.1 <installsharedir>/man/man1
  13. +
  14. +scandir.o:    scandir.c
  15. +    $(CC) $(CFLAGS) -c scandir.c
  16.  
  17.  clean:
  18.      rm -f core a.out ren ren.o
  19. --- scandir.c.rtr    Tue Feb  4 17:12:16 1997
  20. +++ scandir.c    Tue Feb  4 17:12:16 1997
  21. @@ -0,0 +1,136 @@
  22. +/*
  23. + * Copyright (c) 1983 Regents of the University of California.
  24. + * All rights reserved.
  25. + *
  26. + * Redistribution and use in source and binary forms are permitted provided
  27. + * that: (1) source distributions retain this entire copyright notice and
  28. + * comment, and (2) distributions including binaries display the following
  29. + * acknowledgement:  ``This product includes software developed by the
  30. + * University of California, Berkeley and its contributors'' in the
  31. + * documentation or other materials provided with the distribution and in
  32. + * all advertising materials mentioning features or use of this software.
  33. + * Neither the name of the University nor the names of its contributors may
  34. + * be used to endorse or promote products derived from this software without
  35. + * specific prior written permission.
  36. + * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
  37. + * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
  38. + * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  39. + */
  40. +
  41. +/* Changes for SCO compatability by Ready-to-Run Software, Inc.  */
  42. +
  43. +
  44. +#if defined(LIBC_SCCS) && !defined(lint)
  45. +static char sccsid[] = "@(#)scandir.c    5.9 (Berkeley) 6/24/90";
  46. +#endif /* LIBC_SCCS and not lint */
  47. +
  48. +#define NULL 0
  49. +#define bcopy(s1,s2,n)  memcpy(s2,s1,n)
  50. +
  51. +/*
  52. + * Scan the directory dirname calling select to make a list of selected
  53. + * directory entries then sort using qsort and compare routine dcomp.
  54. + * Returns the number of entries and a pointer to a list of pointers to
  55. + * struct dirent (through namelist). Returns -1 if there were any errors.
  56. + */
  57. +
  58. +#include <sys/types.h>
  59. +#include <sys/stat.h>
  60. +#ifdef USE_NDIR
  61. +#include <sys/ndir.h>
  62. +
  63. +#define dirent direct
  64. +#else
  65. +#include <dirent.h>
  66. +#endif
  67. +
  68. +/*
  69. + * The DIRSIZ macro gives the minimum record length which will hold
  70. + * the directory entry.  This requires the amount of space in struct dirent
  71. + * without the d_name field, plus enough space for the name with a terminating
  72. + * null byte (dp->d_namlen+1), rounded up to a 4 byte boundary.
  73. + * null byte (strlen(dp->d_name)+1), rounded up to a 4 byte boundary.
  74. + */
  75. +#undef DIRSIZ
  76. +#ifdef USE_NDIR
  77. +#define DIRSIZ(dp) \
  78. +    ((sizeof (struct dirent) - (MAXNAMLEN+1)) + (((dp)->d_namlen+1 + 3) &~ 3))
  79. +#else
  80. +#define DIRSIZ(dp) \
  81. +    ((sizeof (struct dirent) - 1) + ((strlen((dp)->d_name)+1 + 3) &~ 3))
  82. +#endif
  83. +
  84. +scandir(dirname, namelist, select, dcomp)
  85. +    char *dirname;
  86. +    struct dirent ***namelist;
  87. +    int (*select)(), (*dcomp)();
  88. +{
  89. +    register struct dirent *d, *p, **names;
  90. +    register int nitems;
  91. +    struct stat stb;
  92. +    long arraysz;
  93. +    DIR *dirp;
  94. +
  95. +    if ((dirp = opendir(dirname)) == NULL)
  96. +        return(-1);
  97. +    if (fstat(dirp->dd_fd, &stb) < 0)
  98. +        return(-1);
  99. +
  100. +    /*
  101. +     * estimate the array size by taking the size of the directory file
  102. +     * and dividing it by a multiple of the minimum size entry. 
  103. +     */
  104. +    arraysz = (stb.st_size / 24);
  105. +    names = (struct dirent **)malloc(arraysz * sizeof(struct dirent *));
  106. +    if (names == NULL)
  107. +        return(-1);
  108. +
  109. +    nitems = 0;
  110. +    while ((d = readdir(dirp)) != NULL) {
  111. +        if (select != NULL && !(*select)(d))
  112. +            continue;    /* just selected names */
  113. +        /*
  114. +         * Make a minimum size copy of the data
  115. +         */
  116. +        p = (struct dirent *)malloc(DIRSIZ(d));
  117. +        if (p == NULL)
  118. +            return(-1);
  119. +        p->d_ino = d->d_ino;
  120. +        p->d_reclen = d->d_reclen;
  121. +#ifdef USE_NDIR
  122. +        p->d_namlen = d->d_namlen;
  123. +        bcopy(d->d_name, p->d_name, p->d_namlen + 1);
  124. +#else
  125. +        bcopy(d->d_name, p->d_name, strlen(d->d_name) + 1);
  126. +#endif
  127. +        /*
  128. +         * Check to make sure the array has space left and
  129. +         * realloc the maximum size.
  130. +         */
  131. +        if (++nitems >= arraysz) {
  132. +            if (fstat(dirp->dd_fd, &stb) < 0)
  133. +                return(-1);    /* just might have grown */
  134. +            arraysz = stb.st_size / 12;
  135. +            names = (struct dirent **)realloc((char *)names,
  136. +                arraysz * sizeof(struct dirent *));
  137. +            if (names == NULL)
  138. +                return(-1);
  139. +        }
  140. +        names[nitems-1] = p;
  141. +    }
  142. +    closedir(dirp);
  143. +    if (nitems && dcomp != NULL)
  144. +        qsort(names, nitems, sizeof(struct dirent *), dcomp);
  145. +    *namelist = names;
  146. +    return(nitems);
  147. +}
  148. +
  149. +/*
  150. + * Alphabetic order comparison routine for those who want it.
  151. + */
  152. +alphasort(d1, d2)
  153. +    void *d1, *d2;
  154. +{
  155. +    return(strcmp((*(struct dirent **)d1)->d_name,
  156. +        (*(struct dirent **)d2)->d_name));
  157. +}
  158. --- ren.c.rtr    Tue Feb  4 17:22:52 1997
  159. +++ ren.c    Tue Feb  4 17:25:25 1997
  160. @@ -1,7 +1,8 @@
  161.  #include <stdio.h>
  162.  #include <unistd.h>
  163.  #include <sys/file.h>
  164. -#include <sys/dir.h>
  165. +#include <dirent.h>
  166. +#define direct dirent
  167.  
  168.  #define MAXWILD 20
  169.  #define MAXREP 40
  170. @@ -526,7 +527,8 @@
  171.                          tempnames[-(cur->ftorep)] :
  172.                          dot[cur->ftorep]->d_name);
  173.                      cur->status = DONE;
  174. -                    if (rename(fromname, cur->repname)) {
  175. +                    if (link(fromname, cur->repname) || 
  176. +                                            unlink(fromname)) {
  177.                          fputs("Strange. Can not rename '", stderr);
  178.                          fputs(fromname, stderr);
  179.                          fputs("' to '", stderr);
  180.