Attachment 'lfc-se-ls.cc'
Download 1 // Dirty Hack in Perl to find all files on a particular SE -- C. Wissing, August 2006
2 // Translated to C -- A. Vogel, March 2008
3
4 #include <iostream>
5 #include <regex.h>
6 #include <sys/types.h>
7
8 #include "lfc_api.h"
9 #include "serrno.h"
10
11 int main(int argc, char *argv[])
12 {
13 const char *self = argv[0];
14 char *sename = 0;
15 char *pattern = 0;
16 regex_t preg;
17 char errbuf[1024];
18 int status = 0;
19
20 switch (argc) {
21 case 3:
22 pattern = argv[2];
23 if ((status = regcomp(&preg, pattern, REG_NOSUB))) {
24 regerror(status, &preg, errbuf, 1024);
25 std::cerr << self << ": " << errbuf << std::endl;
26 return 1;
27 }
28 /* no break */
29 case 2:
30 sename = argv[1];
31 break;
32 default:
33 std::cerr << "Usage: " << self << " SENAME [PATTERN]" << std::endl;
34 return 1;
35 }
36
37 std::cerr << "Counting files on " << sename;
38 if (pattern) std::cerr << " matching \"" << pattern << "\"";
39 std::cerr << " ..." << std::endl;
40
41 int nfiles = 0;
42 lfc_list list;
43 int flags = CNS_LIST_BEGIN;
44 struct lfc_filereplica *replica;
45
46 while ((replica = lfc_listreplicax(0, sename, 0, flags, &list))) {
47 flags = CNS_LIST_CONTINUE;
48
49 if (pattern) {
50 if ((status = regexec(&preg, replica->sfn, 0, 0, 0))) {
51 if (status != REG_NOMATCH) {
52 regerror(status, &preg, errbuf, 1024);
53 std::cerr << self << ": " << errbuf << std::endl;
54 }
55 } else {
56 std::cout << replica->sfn << std::endl;
57 ++nfiles;
58 }
59 } else {
60 ++nfiles;
61 }
62 }
63 if (serrno) std::cerr << argv[0] << ": " << sstrerror(serrno) << std::endl;
64
65 lfc_listreplicax(0, sename, 0, CNS_LIST_END, &list);
66 if (pattern) regfree(&preg);
67
68 std::cerr << "Found " << nfiles << " file";
69 if (nfiles != 1) std::cerr << "s";
70 std::cerr << std::endl;
71 }
Attached Files
To refer to attachments on a page, use attachment:filename, as shown below in the list of files. Do NOT use the URL of the [get] link, since this is subject to change and can break easily.You are not allowed to attach a file to this page.