File: ntdir.c

package info (click to toggle)
ntfs 971218-4
  • links: PTS
  • area: main
  • in suites: hamm, slink
  • size: 692 kB
  • ctags: 670
  • sloc: ansic: 7,774; sh: 1,509; makefile: 232
file content (217 lines) | stat: -rw-r--r-- 4,553 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
/*
 *  ntdir.c
 *
 *  Copyright (C) 1995-1997 Martin von Lwis
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <stdio.h>
#include <ctype.h>
#ifdef HAVE_GETOPT_H
#include <getopt.h>
#else
#define getopt_long(a,v,o,ol,x)        getopt(a,v,o)
#endif
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include "types.h"
#include "struct.h"
#include "util.h"
#include "support.h"
#include "nttools.h"
#include "dir.h"
#include "inode.h"

char *short_opts="lf:B:hUdnp1V";
#ifdef HAVE_GETOPT_H
struct option options[]={
	{"filesystem",1,0,'f'},
	{"long",0,0,'l'},
	{"dos",0,0,'d'},
	{"nt",0,0,'n'},
	{"posix",0,0,'p'},
	{"bias",1,0,'B'},
	{"help",0,0,'h'},
	{"unsorted",0,0,'U'},
	{"8859-1",0,0,'1'},
	{"version",0,0,'V'},
	{0,0,0,0}
};
#endif

char usage_str[]=
"ntdir: displays the contents of a NTFS directory\n"
"ntdir [OPTIONS] path\n"
"  --filesystem, -f device   Use device\n"
"  --dos, -d                 Display only short names\n"
"  --nt, -n                  Display only long names (default)\n"
"  --posix, -p               Display all but hidden files\n"
"  --long, -l                Display all files\n"
"  --unsorted, -U            Do not sort names alphabetically\n"
"  --8859-1, -1              Display names using ISO-8859-1\n"
"                            (default is UTF-8)\n"
"  --help, -h                Display this message\n"
;


void usage(void)
{
	fprintf(stderr,usage_str);
}

int ntfs_printcb(ntfs_u8 *entry,void *param)
{
	ntfs_volume *vol=(ntfs_volume*)param;
	int flags=NTFS_GETU8(entry+0x51);
	int show_hidden=0,to_lower=0;
	char *name;
	int name_len,i;
	switch(vol->ngt){
	case ngt_dos:
		if((flags & 2) == 0)
			return 0;
		break;
	case ngt_nt:
		if(flags==2)
			return 0;
		if((flags & 2) == 2)
			to_lower=1;
		break;
	case ngt_posix:
		to_lower=1;
		break;
	case ngt_full:
		show_hidden=1;
		break;
	}
	if(!show_hidden && ((NTFS_GETU8(entry+0x48) & 2) == 2))
		return 0;
	/* last access time? */
	print_time(NTFS_GETU64(entry+0x20));
	/* file size and mft record number */
	printf(" %10d %5d ",NTFS_GETU32(entry+0x40),NTFS_GETU32(entry));
	if(ntfs_encodeuni(vol,(ntfs_u16*)(entry+0x52),NTFS_GETU8(entry+0x50),
			   &name,&name_len))
	{
		printf("(name print error)\n");
		return 0;
	}
	if(to_lower)
		for(i=0;i<name_len;i++)
			putchar(tolower(name[i]));
				
	else
		printf("%s",name);
	free(name);
	putchar('\n');
	return 0;
}

/* display the directory ino */
void ntfs_print_dir(ntfs_inode* ino)
{
	long long fileno;
	int first;
	char item[1000];
	ntfs_iterate_s walk;
	walk.type=BY_POSITION;
	walk.dir=ino;
	walk.u.pos=0;
	walk.result=item;
	for(fileno=0,first=1;1;)
	{
		/* get the next entry */
		if(!ntfs_getdir_byposition(&walk))return;
		/* on the first iteration, we won't get any data */
		if(first){
			first=0;
			continue;
		}
		ntfs_printcb(item,ino->vol);
	}
}

void ntfs_print_dir_unsorted(ntfs_inode* ino)
{
	ntfs_u32 ph=0,pl=0;
	int error;
	while(1){
		if((error=ntfs_getdir_unsorted(ino,&ph,&pl,
					       ntfs_printcb,ino->vol)))
			fprintf(stderr,"Dir error %x\n",error);
		if(ph==0xFFFFFFFF)
			return;
	}
}

/* find file name in directory ino, return MFT record number if found */
int main(int argc,char *argv[])
{
	int c;
	char *device=0;
	char *name;
	ntfs_volume *volume;
	ntfs_inode ino;
	int inum,bias=0;
	unsigned int type=ngt_nt;
	unsigned int charset=nct_utf8;
	int unsorted=0;
	extern int opterr,optind;
	extern char* optarg;

	opterr=1;
	while((c=getopt_long(argc,argv,short_opts,options,NULL))>0)
		switch(c)
		{
		case 'f': device=optarg;break;
		case 'd': type=ngt_dos;break;
		case 'n': type=ngt_nt;break;
		case 'p': type=ngt_posix;break;
		case 'l': type=ngt_full;break;
		case 'B': bias=strtol(optarg,NULL,0);break;
		case 'h': usage();exit(0);break;
		case 'U': unsorted=1;break;
		case '1': charset=nct_iso8859_1;break;
		case 'V': printf("ntdir " NTFS_VERSION "\n");exit(0);break;
		default: usage();exit(1);
		}
	name=argv[optind];
	volume=ntfs_open_volume(device,bias,1,0);
	if(!volume)return 1;
	volume->ngt=type;
	volume->nct=charset;
	/* walk the directory */
	inum=5;
	do{
		char *next;
		if(ntfs_init_inode(&ino,volume,inum))
			fprintf(stderr,"error opening %s\n",name);
		if(!name || !*name)break;
		next=strchr(name,'/');
		if(next){
			*next='\0';
			next++;
		}
		inum=ntfs_find_file(&ino,name);
		if(inum==-1){
			printf("%s not found\n",name);
			return 1;
		}
		name=next;
	}while(1);
	if(unsorted)
		ntfs_print_dir_unsorted(&ino);
	else
		ntfs_print_dir(&ino);
	return 0;
}

/*
 * Local variables:
 * c-file-style: "linux"
 * End:
 */