-
Notifications
You must be signed in to change notification settings - Fork 0
/
bbcls.c
131 lines (111 loc) · 2.6 KB
/
bbcls.c
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
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include "common.h"
char perm_string[4];
char infile[256];
int hidden,longfiles;
char *parse_permissions( permission )
int permission;
{
strcpy( perm_string, "---" );
/* first owner */
if ( (permission & 4) != 0 ) perm_string[0]='r';
if ( (permission & 2) != 0 ) perm_string[1]='w';
if ( (permission & 1) != 0 ) perm_string[2]='x';
return perm_string;
}
int syntax( void )
{
printf( "Usage: bbcls -i <infile> [-alh]\n" );
}
int parse_cli( argc, argv )
int argc;
char **argv;
{
int i;
while ( 1 )
{
i=getopt( argc, argv, "i:alh?" );
if (i == -1 ) /* no more parameters */
break;
switch (i)
{
case 'i' :
strncpy(infile,optarg,256);
break;
case 'a' :
hidden=1;
break;
case 'l' :
longfiles=1;
break;
case 'h' :
case '?' :
syntax();
exit (0);
default :
printf ("Illegal option specified: %c\n",i);
syntax();
exit (1);
}
}
if ( strlen( infile ) == 0 )
{
printf( "Input file not specified\n" );
syntax();
exit (1);
}
if ( optind < argc )
{
printf( "Extra parameters supplied\n" );
syntax();
exit (1);
}
return optind;
}
int main( argc, argv )
int argc;
char **argv;
{
unsigned char *dblock;
int parameters;
int success,i;
FILE *inhandle;
directory_type *directory;
char permissions[12];
dblock=(char *) calloc( 1, 260 );
directory=(directory_type *) calloc( 144, sizeof( directory_type ) );
parameters=parse_cli(argc,argv);
inhandle=fopen( infile, "r" );
if ( inhandle == 0 )
{
printf( "Failed to open file %s\n", argv[1] );
return 1;
}
success=bbc_list_directory( directory, inhandle );
if ( success == -1 )
{
printf( "Failed to read directory\n" );
fclose( inhandle );
return 1;
}
for ( i=0; i < success; i++ )
{
strcpy(permissions,"-");
strcat(permissions,parse_permissions(directory[i].owner_permissions));
strcat(permissions,parse_permissions(directory[i].group_permissions));
strcat(permissions,parse_permissions(directory[i].world_permissions));
if ( longfiles == 1 )
{
printf( "%s 0 %-9d%-9d%8d %s\n", permissions, directory[i].owner,\
directory[i].group, directory[i].size, directory[i].name);
}
else
{
printf( "%s\n", directory[i].name );
}
}
return 0;
}