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.

143 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 - 2005 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); chdir(odir);
  44. if (strcmp(buf, "/")) strcat(buf,"/");
  45. strcat(buf, file);
  46. return buf;
  47. }
  48. int dir_empty(char * nam) {
  49. struct dirent **namelist; int n;
  50. n = scandir(nam, &namelist, 0, alphasort);
  51. if (n < 0) perror("scandir");
  52. while (n--)
  53. if ( strcmp(namelist[n]->d_name,".") &&
  54. strcmp(namelist[n]->d_name,"..") ) return 0;
  55. return 1;
  56. }
  57. int main(int argc, char ** argv) {
  58. char *fn, buf[FILENAME_MAX], *n;
  59. char realrootdir[FILENAME_MAX];
  60. char *package = NULL;
  61. char *rootdir = NULL;
  62. struct stat st;
  63. int opt, nodircheck = 0;
  64. int stripinfo = 0;
  65. int debug = 0;
  66. while ( (opt = getopt(argc, argv, "dDr:sp:")) != -1 ) {
  67. switch (opt) {
  68. case 'd':
  69. debug = 1;
  70. break;
  71. case 'D':
  72. nodircheck = 1;
  73. break;
  74. case 's':
  75. stripinfo = 1;
  76. break;
  77. case 'p':
  78. package = optarg;
  79. break;
  80. case 'r':
  81. strcpy(realrootdir, get_realname(optarg));
  82. rootdir = realrootdir;
  83. break;
  84. default:
  85. fprintf(stderr, "Usage: %s [-D] [-r rootdir] [-s] "
  86. "[-p pkg]\n", argv[0]);
  87. return 1;
  88. }
  89. }
  90. if ( rootdir != NULL ) chdir(rootdir);
  91. while ( fgets(buf, FILENAME_MAX, stdin) != NULL ) {
  92. if (stripinfo) {
  93. fn = strpbrk(buf, "\t ");
  94. if ( fn++ == NULL ) continue;
  95. } else fn = buf;
  96. if ( (n = strchr(fn, '\n')) != NULL ) *n = '\0';
  97. if ( *fn == 0 ) continue;
  98. if ( !lstat(fn,&st) )
  99. {
  100. if ( !S_ISDIR(st.st_mode) || nodircheck || dir_empty(fn) )
  101. {
  102. fn = get_realname(fn);
  103. if (rootdir) {
  104. if (! strncmp(rootdir, fn, strlen(rootdir))) {
  105. fn += strlen(rootdir);
  106. } else {
  107. if (debug) printf("DEBUG: Outside "
  108. "root (%s): %s.\n",
  109. rootdir, fn);
  110. continue;
  111. }
  112. }
  113. if (! package) printf("%s\n", fn);
  114. else printf("%s: %s\n", package, fn);
  115. } else {
  116. if (debug) printf("DEBUG: Non-empty dir: %s.\n", fn);
  117. }
  118. } else {
  119. if (debug) printf("DEBUG: Can't stat file: %s.\n", fn);
  120. }
  121. }
  122. return 0;
  123. }