| 1 |
/* $Id: xml_ls.c,v 1.17 2005/03/19 01:18:02 mgrouch Exp $ */ |
| 2 |
|
| 3 |
/* |
| 4 |
|
| 5 |
XMLStarlet: Command Line Toolkit to query/edit/check/transform XML documents |
| 6 |
|
| 7 |
Copyright (c) 2002-2004 Mikhail Grushinskiy. All Rights Reserved. |
| 8 |
|
| 9 |
Permission is hereby granted, free of charge, to any person obtaining a copy |
| 10 |
of this software and associated documentation files (the "Software"), to deal |
| 11 |
in the Software without restriction, including without limitation the rights |
| 12 |
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 13 |
copies of the Software, and to permit persons to whom the Software is |
| 14 |
furnished to do so, subject to the following conditions: |
| 15 |
|
| 16 |
The above copyright notice and this permission notice shall be included in |
| 17 |
all copies or substantial portions of the Software. |
| 18 |
|
| 19 |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 20 |
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 21 |
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 22 |
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 23 |
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 24 |
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 25 |
THE SOFTWARE. |
| 26 |
|
| 27 |
*/ |
| 28 |
|
| 29 |
#include <config.h> |
| 30 |
|
| 31 |
#include <sys/types.h> |
| 32 |
#include <sys/stat.h> |
| 33 |
#include <dirent.h> |
| 34 |
#include <unistd.h> |
| 35 |
#include <stdio.h> |
| 36 |
#include <stdlib.h> |
| 37 |
#include <string.h> |
| 38 |
#include <time.h> |
| 39 |
|
| 40 |
#include <libxml/xmlmemory.h> |
| 41 |
#include <libxml/c14n.h> |
| 42 |
|
| 43 |
#include "xmlstar.h" |
| 44 |
#include "escape.h" |
| 45 |
|
| 46 |
#if !HAVE_LSTAT |
| 47 |
# if HAVE_STAT |
| 48 |
# define lstat stat |
| 49 |
# else |
| 50 |
/* TODO: #ifdef out code that uses stat instead */ |
| 51 |
# error "lstat() or stat() required" |
| 52 |
# endif |
| 53 |
#endif |
| 54 |
|
| 55 |
#ifndef S_ISLNK |
| 56 |
# define S_ISLNK(m) 0 |
| 57 |
#endif |
| 58 |
|
| 59 |
#ifndef S_ISSOCK |
| 60 |
# define S_ISSOCK(m) 0 |
| 61 |
#endif |
| 62 |
|
| 63 |
/** |
| 64 |
* Print small help for command line options |
| 65 |
*/ |
| 66 |
void |
| 67 |
lsUsage(int argc, char **argv, exit_status status) |
| 68 |
{ |
| 69 |
extern void fprint_ls_usage(FILE* o, const char* argv0); |
| 70 |
extern const char more_info[]; |
| 71 |
FILE *o = (status == EXIT_SUCCESS)? stdout : stderr; |
| 72 |
fprint_ls_usage(o, argv[0]); |
| 73 |
fprintf(o, "%s", more_info); |
| 74 |
exit(status); |
| 75 |
} |
| 76 |
|
| 77 |
|
| 78 |
const char * |
| 79 |
get_file_type(mode_t mode) |
| 80 |
{ |
| 81 |
if (S_ISREG(mode)) return "f"; /* regular file */ |
| 82 |
else if (S_ISDIR(mode)) return "d"; /* directory */ |
| 83 |
else if (S_ISCHR(mode)) return "c"; /* character device */ |
| 84 |
else if (S_ISBLK(mode)) return "b"; /* block device */ |
| 85 |
else if (S_ISLNK(mode)) return "l"; /* symlink */ |
| 86 |
else if (S_ISFIFO(mode)) return "p"; /* fifo */ |
| 87 |
else if (S_ISSOCK(mode)) return "s"; /* socket */ |
| 88 |
else return "u"; /* unknown */ |
| 89 |
} |
| 90 |
|
| 91 |
const char * |
| 92 |
get_file_perms(mode_t mode) |
| 93 |
{ |
| 94 |
int i; |
| 95 |
static char perms[10]; |
| 96 |
|
| 97 |
strcpy(perms, "---------"); |
| 98 |
|
| 99 |
for(i=0; i < sizeof perms - 1; i+=3) |
| 100 |
{ |
| 101 |
if(mode &(S_IRUSR>>i)) |
| 102 |
perms[i+0] = 'r'; |
| 103 |
|
| 104 |
if(mode &(S_IWUSR>>i)) |
| 105 |
perms[i+1] = 'w'; |
| 106 |
|
| 107 |
if(mode &(S_IXUSR>>i)) |
| 108 |
perms[i+2] = 'x'; |
| 109 |
} |
| 110 |
|
| 111 |
#ifdef S_ISUID |
| 112 |
if((mode & S_ISUID)) |
| 113 |
perms[2] = 's'; |
| 114 |
#endif |
| 115 |
|
| 116 |
#ifdef S_ISGID |
| 117 |
if((mode & S_ISGID)) |
| 118 |
perms[5] = 's'; |
| 119 |
#endif |
| 120 |
|
| 121 |
#ifdef S_ISVTX |
| 122 |
if((mode & S_ISVTX)) |
| 123 |
perms[8] = 't'; |
| 124 |
#endif |
| 125 |
|
| 126 |
return(perms); |
| 127 |
} |
| 128 |
|
| 129 |
int |
| 130 |
xml_print_dir(const char* dir) |
| 131 |
{ |
| 132 |
DIR *dirp; |
| 133 |
struct dirent *d; |
| 134 |
struct stat stats; |
| 135 |
int num_files = 0; |
| 136 |
|
| 137 |
if((dirp = opendir(dir)) == NULL) |
| 138 |
return(-1); |
| 139 |
|
| 140 |
chdir(dir); |
| 141 |
|
| 142 |
while((d = readdir(dirp)) != NULL) |
| 143 |
{ |
| 144 |
xmlChar *xml_str; |
| 145 |
char atime[20]; |
| 146 |
char mtime[20]; |
| 147 |
int size_len; |
| 148 |
|
| 149 |
if ((d->d_name == NULL) || !strcmp(d->d_name, ".") || !strcmp(d->d_name, "..")) |
| 150 |
continue; |
| 151 |
|
| 152 |
if(lstat(d->d_name, &stats) != 0) |
| 153 |
{ |
| 154 |
fprintf(stderr, "couldn't stat: %s\n", d->d_name); |
| 155 |
} |
| 156 |
|
| 157 |
#if defined (__MINGW32__) |
| 158 |
/* somehow atime is -1 on Windows XP when the atime is in future */ |
| 159 |
if (stats.st_atime < 0) stats.st_atime = 0; |
| 160 |
/* somehow mtime is -1 on Windows XP when the mtime is in future */ |
| 161 |
if (stats.st_mtime < 0) stats.st_mtime = 0; |
| 162 |
#endif |
| 163 |
|
| 164 |
/* format time as per ISO 8601 */ |
| 165 |
strftime(atime, sizeof atime, "%Y%m%dT%H%M%SZ", gmtime(&stats.st_atime)); |
| 166 |
strftime(mtime, sizeof mtime, "%Y%m%dT%H%M%SZ", gmtime(&stats.st_mtime)); |
| 167 |
|
| 168 |
xml_str = xml_C11NNormalizeAttr((const xmlChar *) d->d_name); |
| 169 |
printf("<%s p=\"%s\" a=\"%s\" m=\"%s\" s=\"", |
| 170 |
get_file_type(stats.st_mode), get_file_perms(stats.st_mode), |
| 171 |
atime, mtime); |
| 172 |
size_len = printf("%lu", (unsigned long) stats.st_size); |
| 173 |
printf("\"%.*s", 16-size_len, " "); |
| 174 |
printf(" n=\"%s\"/>\n", xml_str); |
| 175 |
num_files++; |
| 176 |
xmlFree(xml_str); |
| 177 |
|
| 178 |
} /* end of for loop */ |
| 179 |
|
| 180 |
closedir(dirp); |
| 181 |
return num_files; |
| 182 |
} |
| 183 |
|
| 184 |
int |
| 185 |
lsMain(int argc, char** argv) |
| 186 |
{ |
| 187 |
const char *dir = "."; |
| 188 |
int files; |
| 189 |
|
| 190 |
if (argc == 3) { |
| 191 |
if (strcmp(argv[2], "--help") == 0) |
| 192 |
lsUsage(argc, argv, EXIT_SUCCESS); |
| 193 |
else |
| 194 |
dir = argv[2]; |
| 195 |
} else if (argc > 3) { |
| 196 |
lsUsage(argc, argv, EXIT_BAD_ARGS); |
| 197 |
} |
| 198 |
|
| 199 |
printf("<dir>\n"); |
| 200 |
files = xml_print_dir(dir); |
| 201 |
printf("</dir>\n"); |
| 202 |
return (files >= 0)? EXIT_SUCCESS : EXIT_FAILURE; |
| 203 |
} |
| 204 |
|