index = index->next;
}
- return 0;
+ return 0;
+}
+
+int do_require( const char *package )
+{
+ int stat = 1;
+ lar_archive *ar;
+ lar_member *mb;
+
+ if( (ar = lar_find_archive(package)) != NULL )
+ {
+ if( (mb = lar_find_member(ar, package)) != NULL )
+ {
+ write(fileno(stdout), mb->data, mb->length);
+ lar_close_member(mb);
+ stat = 0;
+ }
+
+ lar_close(ar);
+ }
+
+ return stat;
}
int main( int argc, const char* argv[] )
if( argv[1] != NULL )
{
- if( (ar = lar_open(argv[1])) != NULL )
+ switch(argv[1][0])
{
- if( argv[2] )
- stat = do_print_member(ar, argv[2]);
- else
- stat = do_print_index(ar);
+ case 's':
+ if( (ar = lar_open(argv[2])) != NULL )
+ {
+ if( argv[3] != NULL )
+ stat = do_print_member(ar, argv[3]);
+ else
+ stat = do_print_index(ar);
- lar_close(ar);
- return stat;
- }
- else
- {
- LAR_DIE("Failed to open archive");
+ lar_close(ar);
+ }
+ else
+ {
+ LAR_DIE("Failed to open archive");
+ }
+
+ break;
+
+ case 'r':
+ stat = do_require(argv[2]);
+ break;
}
+
+ return stat;
}
else
{
- printf("Usage: lar <archive> [<member>]\n");
+ printf("Usage:\n");
+ printf("\tlar show <archive> [<member>]\n");
+ printf("\tlar require <package>\n");
+
return 1;
}
return 0;
}
-
idx_map = NULL;
for( i = 0; i < idx_length; \
- i += (sizeof(lar_index) - sizeof(char))
+ i += (sizeof(lar_index) - sizeof(char))
) {
idx_ptr = (lar_index *)malloc(sizeof(lar_index));
}
uint32_t lar_get_filename( lar_archive *ar,
- lar_index *idx_ptr, char *filename
+ lar_index *idx_ptr, char *filename
) {
if( idx_ptr->nlength >= LAR_FNAME_BUFFER )
LAR_DIE("Filename exceeds maximum allowed length");
member->data = &memberdata[idx_ptr->foffset % pgsz];
member->mmap = memberdata;
- member->mlen = idx_ptr->flength + ( idx_ptr->foffset % pgsz );
+ member->mlen = idx_ptr->flength + ( idx_ptr->foffset % pgsz );
return member;
}
int fd;
struct stat as;
lar_archive *ar;
-
+
if( stat(filename, &as) == -1 )
return NULL;
return 0;
}
+lar_archive * lar_find_archive( const char *package )
+{
+ int seg = 1;
+ int i, j, len;
+ struct stat s;
+ LAR_FNAME(buffer);
+
+ for( len = 0; package[len] != '\0'; len++ )
+ if( package[len] == '.' ) seg++;
+
+ while( seg > 0 )
+ {
+ for( i = 0, j = 1; (i < len) && (j <= seg); i++ )
+ {
+ if( package[i] == '.' ) {
+ if( j < seg ) j++; else break;
+ }
+
+ buffer[i] = ( package[i] == '.' ) ? '/' : package[i];
+ }
+
+ buffer[i+0] = '.'; buffer[i+1] = 'l'; buffer[i+2] = 'a';
+ buffer[i+3] = 'r'; buffer[i+4] = '\0';
+
+ if( (stat(buffer, &s) > -1) && (s.st_mode & S_IFREG) )
+ return lar_open(buffer);
+
+ seg--;
+ }
+
+ return NULL;
+}
+
+lar_member * lar_find_member( lar_archive *ar, const char *package )
+{
+ int len;
+ LAR_FNAME(buffer);
+
+ for( len = 0; package[len] != '\0'; len++ )
+ buffer[len] = ( package[len] == '.' ) ? '/' : package[len];
+
+ buffer[len+0] = '.'; buffer[len+1] = 'l'; buffer[len+2] = 'u';
+ buffer[len+3] = 'a'; buffer[len+4] = '\0';
+
+ return lar_open_member(ar, buffer);
+}