I doubt that print_usage will ever appear in out libc. However, here
is a hacked up version…
#ifdef __USAGE
%C - test print_usage
#endif /* __USAGE */
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/debug.h>
#include <sys/elf.h>
#include <sys/procfs.h>
#include <unistd.h>
void print_usage(void)
{
Elf32_Ehdr ehdr;
Elf32_Shdr *shdr = NULL;
char *usage = NULL, *shstrtab = NULL;
int fd, i;
/* get a file descriptor to our executable */
if ( -1 == (fd = _cmdfd()) )
return;
/* read Elf Header */
if ( sizeof(ehdr) != read(fd, &ehdr, sizeof(ehdr)) ) {
return;
}
/* Check ELF magic */
if ( ehdr.e_ident[0] != 0x7f
|| ehdr.e_ident[1] != ‘E’
|| ehdr.e_ident[2] != ‘L’
|| ehdr.e_ident[3] != ‘F’ ) {
return;
}
/* read section table */
if ( (shdr = malloc( ehdr.e_shentsize * ehdr.e_shnum )) == NULL ) {
goto end;
}
if ( -1 == lseek( fd, ehdr.e_shoff, SEEK_SET ) )
goto end;
if ( ehdr.e_shentsize * ehdr.e_shnum != read( fd, shdr, ehdr.e_shentsize * ehdr.e_shnum ) )
goto end;
/* read string index */
if ( (shstrtab = malloc( shdr[ehdr.e_shstrndx].sh_size )) == NULL ) {
goto end;
}
if ( -1 == lseek( fd, shdr[ehdr.e_shstrndx].sh_offset, SEEK_SET ) )
goto end;
if ( shdr[ehdr.e_shstrndx].sh_size != read( fd, shstrtab, shdr[ehdr.e_shstrndx].sh_size ) ) {
goto end;
}
/* look through each section until we find the “QNX_usage” section */
for ( i = 1; i < ehdr.e_shnum; i++ ) {
if ( !strcmp( &shstrtab[shdr_.sh_name],“QNX_usage”))
break;
}
if ( i == ehdr.e_shnum )
goto end;
if ( (usage = malloc( shdr.sh_size )) == NULL )
goto end;
if ( -1 == lseek( fd, shdr.sh_offset, SEEK_SET ) )
goto end;
if ( shdr.sh_size != read( fd, usage, shdr.sh_size ) )
goto end;
write( STDERR_FILENO, usage, shdr.sh_size );
end:
if ( shdr )
free( shdr );
if ( shstrtab )
free( shstrtab );
if ( usage )
free( usage );
}
int main(void)
{
print_usage();
return 0;
}
Warren Peece <warren@nospam.com> wrote:_
→ Ping!
“Warren Peece” <> warren@nospam.com> > wrote in message
news:8vbrji$re7$> 1@inn.qnx.com> …
| I noticed that QNX6 provides the “use” and “usemsg” utilities, which is
great.
| However the function print_usage() which I used extensively in QNX4 seems to
| not have made it across quite yet. Any chance of me getting a pre-release
copy
| of it or enough info on how you do the use messages to code it myself?
(perhaps
| a snippet from “use.c”?) I hate having to maintain multiple sets of usage
| descriptions. Thanks in advance.
|
| -Warren
–
cburgess@qnx.com