Dash:
[t2-trunk.git] / misc / tools-source / fl_wrparse.c
blobad89ef0560db1f05ef10db6ef0c5b7accbcd80ea
1 /*
2 * --- T2-COPYRIGHT-NOTE-BEGIN ---
3 * This copyright note is auto-generated by ./scripts/Create-CopyPatch.
4 *
5 * T2 SDE: misc/tools-source/fl_wrparse.c
6 * Copyright (C) 2004 - 2005 The T2 SDE Project
7 * Copyright (C) 1998 - 2003 ROCK Linux Project
8 *
9 * More information can be found in the files COPYING and README.
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; version 2 of the License. A copy of the
14 * GNU General Public License can be found in the file COPYING.
15 * --- T2-COPYRIGHT-NOTE-END ---
18 #define _GNU_SOURCE
20 #include <string.h>
21 #include <stdio.h>
22 #include <sys/stat.h>
23 #include <unistd.h>
24 #include <dirent.h>
26 char * get_realname(char * origname) {
27 static char buf[FILENAME_MAX];
28 char *file, odir[FILENAME_MAX];
30 if (! strcmp(origname, "/")) return "/";
32 strcpy(buf,origname);
33 if ( (file=strrchr(buf,'/')) == NULL ) {
34 file = origname;
35 strcpy(buf, ".");
36 } else {
37 *file=0; file++;
38 file = origname + (file-buf);
41 getcwd(odir, FILENAME_MAX); chdir(buf);
42 getcwd(buf, FILENAME_MAX); chdir(odir);
44 if (strcmp(buf, "/")) strcat(buf,"/");
45 strcat(buf, file);
47 return buf;
50 int dir_empty(char * nam) {
51 struct dirent **namelist; int n;
52 n = scandir(nam, &namelist, 0, alphasort);
53 if (n < 0) perror("scandir");
54 while (n--)
55 if ( strcmp(namelist[n]->d_name,".") &&
56 strcmp(namelist[n]->d_name,"..") ) return 0;
57 return 1;
60 int main(int argc, char ** argv) {
61 char *fn, buf[FILENAME_MAX], *n;
62 char realrootdir[FILENAME_MAX];
63 char *package = NULL;
64 char *rootdir = NULL;
65 struct stat st;
66 int opt, nodircheck = 0;
67 int stripinfo = 0;
68 int debug = 0;
70 while ( (opt = getopt(argc, argv, "dDr:sp:")) != -1 ) {
71 switch (opt) {
72 case 'd':
73 debug = 1;
74 break;
76 case 'D':
77 nodircheck = 1;
78 break;
80 case 's':
81 stripinfo = 1;
82 break;
84 case 'p':
85 package = optarg;
86 break;
88 case 'r':
89 strcpy(realrootdir, get_realname(optarg));
90 rootdir = realrootdir;
91 break;
93 default:
94 fprintf(stderr, "Usage: %s [-D] [-r rootdir] [-s] "
95 "[-p pkg]\n", argv[0]);
96 return 1;
100 if ( rootdir != NULL ) chdir(rootdir);
102 while ( fgets(buf, FILENAME_MAX, stdin) != NULL ) {
104 if (stripinfo) {
105 fn = strpbrk(buf, "\t ");
106 if ( fn++ == NULL ) continue;
107 } else fn = buf;
108 if ( (n = strchr(fn, '\n')) != NULL ) *n = '\0';
109 if ( *fn == 0 ) continue;
111 if ( lstat(fn,&st) ) {
112 if (debug) fprintf(stderr, "DEBUG: Can't stat file: %s.\n", fn);
113 continue;
116 if ( S_ISDIR(st.st_mode) ) {
117 if (!nodircheck && dir_empty(fn) ) {
118 if (debug) fprintf(stderr, "DEBUG: Non-empty dir: %s.\n", fn);
119 continue;
123 fn = get_realname(fn);
124 if (rootdir) {
125 if (! strncmp(rootdir, fn, strlen(rootdir))) {
126 fn += strlen(rootdir);
127 } else {
128 if (debug) fprintf(stderr, "DEBUG: Outside "
129 "root (%s): %s.\n",
130 rootdir, fn);
131 continue;
135 if (! package) printf("%s\n", fn);
136 else printf("%s: %s\n", package, fn);
138 return 0;