home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1992 March / Source_Code_CD-ROM_Walnut_Creek_March_1992.iso / usenet / compsrcs / misc / volume02 / checkl.ink < prev    next >
Encoding:
Internet Message Format  |  1991-08-27  |  5.1 KB

  1. From mipos3!omepd!intelisc!littlei!uunet!husc6!necntc!ncoast!allbery Fri Mar 11 19:02:20 PST 1988
  2. Article 325 of comp.sources.misc:
  3. Path: td2cad!mipos3!omepd!intelisc!littlei!uunet!husc6!necntc!ncoast!allbery
  4. From: jason@violet.berkeley.edu (Jason Venner)
  5. Newsgroups: comp.sources.misc
  6. Subject: v02i072: checklink - check for symlinks in limbo
  7. Message-ID: <7489@ncoast.UUCP>
  8. Date: 9 Mar 88 23:22:50 GMT
  9. Sender: allbery@ncoast.UUCP
  10. Reply-To: jason@violet.berkeley.edu (Jason Venner)
  11. Lines: 171
  12. Approved: allbery@ncoast.UUCP
  13. Comp.sources.misc: Volume 2, Issue 72
  14. Submitted-By: "Jason Venner" <jason@violet.berkeley.edu>
  15. Archive-Name: checklink
  16.  
  17. Comp.sources.misc: Volume 2, Issue 72
  18. Submitted-By: "Jason Venner" <jason@violet.berkeley.edu>
  19. Archive-Name: checklink
  20.  
  21. This is the first pass of a program that walks through the directories
  22. listed on the command link, and prints out the names of the
  23. unresolvable symbolic links, and what they point to on stdout.
  24. Error messages are reported on stderr.
  25. the exit code is the sum of the unresolved symbolic links + sum of non
  26. fatal errors.
  27. It has only tested on BSD systems.  [So how many non-BSD systems have symbolic
  28. links?  ++bsa]
  29.  
  30. #--------------------------------CUT HERE-------------------------------------
  31. #! /bin/sh
  32. #
  33. # This is a shell archive.  Save this into a file, edit it
  34. # and delete all lines above this comment.  Then give this
  35. # file to sh by executing the command "sh file".  The files
  36. # will be extracted into the current directory owned by
  37. # you with default permissions.
  38. #
  39. # The files contained herein are:
  40. #
  41. # -rw-rw-rw-   1 allbery  uucp        3108 Mar  9 18:17 checklink.c
  42. #
  43. echo 'x - checklink.c'
  44. if test -f checklink.c; then echo 'shar: not overwriting checklink.c'; else
  45. sed 's/^X//' << '________This_Is_The_END________' > checklink.c
  46. X#include    <stdio.h>
  47. X#include    <sys/types.h>
  48. X#include    <sys/dir.h>
  49. X#include    <sys/stat.h>
  50. X#include    <sys/file.h>
  51. X
  52. Xchar    *name;
  53. X
  54. Xmain( argc, argv )
  55. Xint    argc;
  56. Xchar    **argv;
  57. X{
  58. X
  59. X    char    *dir;
  60. X    char    *path;
  61. X    char    cwd[MAXNAMLEN+1];
  62. X    char*    getcwd();
  63. X    char*    rindex();
  64. X    int    err_count;
  65. X
  66. X    name = argv[0];
  67. X    cwd[MAXNAMLEN] = '\0';
  68. X    if( !getwd( cwd ) ) {
  69. X        fprintf( stderr, "%s:unable to get current working directory, %s", name, cwd );
  70. X        exit( 1 );
  71. X    }
  72. X    for( argc--, argv++, err_count = 0; argc; argc--, argv++ ) {
  73. X        if( chdir( argv[0] ) == -1 ) {
  74. X            fprintf( stderr, "%s:unable to chdir to %s\n", name, dir );
  75. X            perror( "" );
  76. X            err_count++;
  77. X        }
  78. X        if( (dir = rindex( argv[0], '/' )) ) {
  79. X            *dir++ = '\0';
  80. X            path = argv[0];
  81. X        } else {
  82. X            dir = argv[0];
  83. X            path = ".";
  84. X        }
  85. X        err_count += check_link( dir, path );
  86. X        if( chdir( cwd ) == -1 ) {
  87. X            fprintf( stderr, "%s:unable to chdir to %s\n", name, cwd );
  88. X            perror( "" );
  89. X            exit( 2 );
  90. X        }
  91. X    }
  92. X    exit( err_count );
  93. X}
  94. X
  95. Xcheck_link( top, path )
  96. Xchar    *top;
  97. Xchar    *path;
  98. X{
  99. X
  100. X    DIR    *dptr;
  101. X    DIR*    opendir();
  102. X    char    cwd[MAXNAMLEN+1];
  103. X    char    link_name[MAXNAMLEN+1];
  104. X    char*    sprintf();
  105. X    extern    char    *name;
  106. X    int    err_count;
  107. X    int    link_size;
  108. X    off_t    offset;
  109. X    off_t    telldir();
  110. X    struct    direct    *dent;
  111. X    struct    direct*    readdir();
  112. X    struct    stat    stat;
  113. X
  114. X    (void) sprintf( cwd, "%s/%s", path, top );
  115. X    if( !(dptr = opendir( "." )) ) {
  116. X        fprintf( stderr, "%s:unable to open directory %s", name, cwd );
  117. X        perror( "" );
  118. X        return 1;
  119. X    }
  120. X    if( !readdir( dptr )  || !readdir( dptr ) ) {
  121. X        fprintf( stderr, "%s:unable to read '.' or '..' in %s\n",
  122. X                  name, cwd );
  123. X        perror( "" );
  124. X        (void) closedir( dptr );
  125. X        return 1;
  126. X    }
  127. X    err_count = 0;
  128. X    link_name[MAXNAMLEN] = '\0';
  129. X    while( (dent = readdir( dptr )) ) {
  130. X        if( lstat( dent->d_name, &stat ) == -1 ) {
  131. X            fprintf( stderr, "%s:unable to lstat %s/%s", name, cwd, dent->d_name );
  132. X            perror( "" );
  133. X            err_count++;
  134. X            continue;
  135. X        }
  136. X        switch( stat.st_mode & S_IFMT ) {
  137. X        case    S_IFDIR:
  138. X            if( (offset = telldir( dptr )) == -1 ) {
  139. X                fprintf( stderr, "%s:unable to telldir on %s\n", cwd );
  140. X                perror( "" );
  141. X                err_count++;
  142. X                continue;
  143. X            }
  144. X            (void) closedir( dptr );
  145. X            if( chdir( dent->d_name ) == -1 ) {
  146. X                fprintf( stderr, "%s:unable to chdir to %s/%s", name, cwd, dent->d_name );
  147. X                perror( "" );
  148. X                return ++err_count;
  149. X            }
  150. X            err_count += check_link( dent->d_name, cwd );
  151. X            if( chdir( ".." ) == -1 && chdir( cwd ) == -1 ) {
  152. X                fprintf( stderr, "%s:unable to chdir up to %s", name, cwd);
  153. X                return ++err_count;
  154. X            }
  155. X            if( !(dptr = opendir( "." )) ) {
  156. X                fprintf( stderr, "%s:unable to reopendir %s", name, cwd );
  157. X                perror( "" );
  158. X                return ++err_count;
  159. X            }
  160. X            seekdir( dptr, offset, 0 );
  161. X            break;
  162. X        case    S_IFLNK:
  163. X            if( (link_size = readlink( dent->d_name, link_name, MAXNAMLEN )) == -1 ) {
  164. X                fprintf( stderr, "%s:unable to readlink on %s/%s", cwd, dent->d_name );
  165. X                perror( "" );
  166. X                err_count++;
  167. X                continue;
  168. X            }
  169. X            link_name[link_size] = '\0';
  170. X            if( access( link_name, F_OK ) == -1 ) {
  171. X                printf( "%s/%s %s\n", cwd, dent->d_name, link_name );
  172. X                err_count++;
  173. X            }
  174. X            break;
  175. X        default:
  176. X            break;
  177. X        }
  178. X    }
  179. X    (void) closedir( dptr );
  180. X    return err_count;
  181. X}
  182. ________This_Is_The_END________
  183. if test `wc -c < checklink.c` -ne 3108; then
  184.     echo 'shar: checklink.c was damaged during transit (should have been 3108 bytes)'
  185. fi
  186. fi        ; : end of overwriting check
  187. exit 0
  188.  
  189.  
  190.