mirror of the now-defunct rocklinux.org
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

140 lines
3.4 KiB

  1. /*
  2. * --- ROCK-COPYRIGHT-NOTE-BEGIN ---
  3. *
  4. * This copyright note is auto-generated by ./scripts/Create-CopyPatch.
  5. * Please add additional copyright information _after_ the line containing
  6. * the ROCK-COPYRIGHT-NOTE-END tag. Otherwise it might get removed by
  7. * the ./scripts/Create-CopyPatch script. Do not edit this copyright text!
  8. *
  9. * ROCK Linux: rock-src/misc/tools-source/fl_wrparse.c
  10. * ROCK Linux is Copyright (C) 1998 - 2003 Clifford Wolf
  11. *
  12. * This program is free software; you can redistribute it and/or modify
  13. * it under the terms of the GNU General Public License as published by
  14. * the Free Software Foundation; either version 2 of the License, or
  15. * (at your option) any later version. A copy of the GNU General Public
  16. * License can be found at Documentation/COPYING.
  17. *
  18. * Many people helped and are helping developing ROCK Linux. Please
  19. * have a look at http://www.rocklinux.org/ and the Documentation/TEAM
  20. * file for details.
  21. *
  22. * --- ROCK-COPYRIGHT-NOTE-END ---
  23. */
  24. #define _GNU_SOURCE
  25. #include <string.h>
  26. #include <stdio.h>
  27. #include <sys/stat.h>
  28. #include <unistd.h>
  29. #include <dirent.h>
  30. char * get_realname(char * origname) {
  31. static char buf[FILENAME_MAX];
  32. char *file, odir[FILENAME_MAX];
  33. if (! strcmp(origname, "/")) return "/";
  34. strcpy(buf,origname);
  35. if ( (file=strrchr(buf,'/')) == NULL ) {
  36. file = origname;
  37. strcpy(buf, ".");
  38. } else {
  39. *file=0; file++;
  40. file = origname + (file-buf);
  41. }
  42. getcwd(odir, FILENAME_MAX); chdir(buf);
  43. getcwd(buf, FILENAME_MAX); strcat(buf,"/");
  44. strcat(buf, file); chdir(odir);
  45. return buf;
  46. }
  47. int dir_empty(char * nam) {
  48. struct dirent **namelist; int n;
  49. n = scandir(nam, &namelist, 0, alphasort);
  50. if (n < 0) perror("scandir");
  51. while (n--)
  52. if ( strcmp(namelist[n]->d_name,".") &&
  53. strcmp(namelist[n]->d_name,"..") ) return 0;
  54. return 1;
  55. }
  56. int main(int argc, char ** argv) {
  57. char *fn, buf[FILENAME_MAX], *n;
  58. char realrootdir[FILENAME_MAX];
  59. char *package = NULL;
  60. char *rootdir = NULL;
  61. struct stat st;
  62. int opt, nodircheck = 0;
  63. int stripinfo = 0;
  64. int debug = 0;
  65. while ( (opt = getopt(argc, argv, "dDr:sp:")) != -1 ) {
  66. switch (opt) {
  67. case 'd':
  68. debug = 1;
  69. break;
  70. case 'D':
  71. nodircheck = 1;
  72. break;
  73. case 's':
  74. stripinfo = 1;
  75. break;
  76. case 'p':
  77. package = optarg;
  78. break;
  79. case 'r':
  80. strcpy(realrootdir, get_realname(optarg));
  81. rootdir = realrootdir;
  82. break;
  83. default:
  84. fprintf(stderr, "Usage: %s [-D] [-r rootdir] [-s] "
  85. "[-p pkg]\n", argv[0]);
  86. return 1;
  87. }
  88. }
  89. if ( rootdir != NULL ) chdir(rootdir);
  90. while ( fgets(buf, FILENAME_MAX, stdin) != NULL ) {
  91. if (stripinfo) {
  92. fn = strpbrk(buf, "\t ");
  93. if ( fn++ == NULL ) continue;
  94. } else fn = buf;
  95. if ( (n = strchr(fn, '\n')) != NULL ) *n = '\0';
  96. if ( *fn == 0 ) continue;
  97. if ( !lstat(fn,&st) )
  98. {
  99. if ( !S_ISDIR(st.st_mode) || nodircheck || dir_empty(fn) )
  100. {
  101. fn = get_realname(fn);
  102. if (rootdir) {
  103. if (! strncmp(rootdir, fn, strlen(rootdir))) {
  104. fn += strlen(rootdir);
  105. } else {
  106. if (debug) printf("DEBUG: Outside "
  107. "root (%s): %s.\n",
  108. rootdir, fn);
  109. continue;
  110. }
  111. }
  112. if (! package) printf("%s\n", fn);
  113. else printf("%s: %s\n", package, fn);
  114. } else {
  115. if (debug) printf("DEBUG: Non-empty dir: %s.\n", fn);
  116. }
  117. } else {
  118. if (debug) printf("DEBUG: Can't stat file: %s.\n", fn);
  119. }
  120. }
  121. return 0;
  122. }