2011-05-22 21:36:34 -04:00
|
|
|
/* See LICENSE file for copyright and license details. */
|
2011-05-24 07:56:38 -04:00
|
|
|
#include <libgen.h>
|
2011-05-22 21:36:34 -04:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2011-05-24 16:39:20 -04:00
|
|
|
#include <unistd.h>
|
2012-04-23 09:50:47 -04:00
|
|
|
#include <stdbool.h>
|
|
|
|
|
|
|
|
#include "arg.h"
|
2011-05-22 21:36:34 -04:00
|
|
|
#include "util.h"
|
|
|
|
|
2012-04-23 09:50:47 -04:00
|
|
|
char *argv0;
|
|
|
|
|
|
|
|
void
|
|
|
|
usage(void)
|
|
|
|
{
|
|
|
|
eprintf("usage: %s [-ahz] [-s suffix] name [suffix]\n",
|
|
|
|
basename(argv0));
|
|
|
|
}
|
|
|
|
|
2011-05-22 21:36:34 -04:00
|
|
|
int
|
|
|
|
main(int argc, char *argv[])
|
|
|
|
{
|
2012-04-23 09:50:47 -04:00
|
|
|
char *s, *suffix = NULL;
|
|
|
|
size_t n, sn;
|
|
|
|
bool aflag = false, zflag = false;
|
|
|
|
|
|
|
|
ARGBEGIN {
|
|
|
|
case 'a':
|
|
|
|
aflag = true;
|
|
|
|
break;
|
|
|
|
case 's':
|
|
|
|
suffix = EARGF(usage());
|
|
|
|
break;
|
|
|
|
case 'z':
|
|
|
|
zflag = true;
|
|
|
|
break;
|
|
|
|
case 'h':
|
|
|
|
default:
|
|
|
|
usage();
|
|
|
|
} ARGEND;
|
|
|
|
|
|
|
|
if (argc < 1)
|
|
|
|
usage();
|
|
|
|
|
|
|
|
if (!aflag && argc == 2)
|
|
|
|
suffix = argv[1];
|
|
|
|
if (suffix)
|
|
|
|
sn = strlen(suffix);
|
|
|
|
|
|
|
|
for (; argc > 0; argc--, argv++) {
|
|
|
|
s = basename(argv[0]);
|
|
|
|
if (suffix) {
|
|
|
|
n = strlen(s) - sn;
|
|
|
|
if (!strcmp(&s[n], suffix))
|
|
|
|
s[n] = '\0';
|
|
|
|
}
|
|
|
|
printf("%s%c", s, (zflag)? '\0' : '\n');
|
|
|
|
|
|
|
|
if (!aflag)
|
|
|
|
break;
|
2011-05-22 21:36:34 -04:00
|
|
|
}
|
2012-04-23 09:50:47 -04:00
|
|
|
|
2011-05-22 21:36:34 -04:00
|
|
|
return EXIT_SUCCESS;
|
|
|
|
}
|
2012-04-23 09:50:47 -04:00
|
|
|
|