#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <signal.h>
#include <errno.h>
#include <libgen.h>
#define LINESZ 2048
#define STDERR 2
#define P_locale "/usr/lib/locale/"
#define L_locale sizeof(P_locale)
#define MESSAGES "/LC_MESSAGES/"
static char *syserr(void);
static void usage(void);
static int mymkdir(char *);
static void clean(int);
static char *cmdname;
static char *workp;
int
main(argc, argv)
int argc;
char *argv[];
{
int c;
char *ifilep;
char *ofilep;
char *localep;
char *localedirp;
char *outfilep;
FILE *fp_inp;
FILE *fp_outp;
char *bufinp, *bufworkp;
int *bufoutp;
char *msgp;
int num_msgs;
int iflag;
int oflag;
int nitems;
char *pathoutp;
struct stat buf;
unsigned size;
int i;
localep = (char *)NULL;
num_msgs = 0;
iflag = 0;
oflag = 0;
if (cmdname = strrchr(argv[0], '/'))
++cmdname;
else
cmdname = argv[0];
if (argc < 3 && argc > 6)
usage();
while ((c = getopt(argc, argv, "oi:")) != EOF) {
switch (c) {
case 'o':
oflag++;
break;
case 'i':
iflag++;
localep = optarg;
break;
case '?':
usage();
break;
}
}
ifilep = argv[optind];
ofilep = argv[optind + 1];
if (iflag && oflag && argc != 6)
usage();
if (iflag && ! oflag && argc != 5)
usage();
if (! iflag && oflag && argc != 4)
usage();
if (! iflag && ! oflag && argc != 3)
usage();
if (localep) {
size = L_locale + strlen(localep) +
sizeof(MESSAGES) + strlen(ofilep);
if ((pathoutp = malloc(2 * (size + 1))) == NULL) {
(void)fprintf(stderr, "%s: malloc error (size = %d)\n",
cmdname, size);
exit(1);
}
localedirp = pathoutp + size + 1;
(void)strcpy(pathoutp, P_locale);
(void)strcpy(&pathoutp[L_locale - 1], localep);
(void)strcat(pathoutp, MESSAGES);
(void)strcpy(localedirp, pathoutp);
(void)strcat(pathoutp, ofilep);
}
if (! oflag) {
if (iflag) {
if (access(pathoutp, 0) == 0) {
(void)fprintf(stderr, "%s: Message file \"%s\" already exists;\ndid not overwrite it\n", cmdname, pathoutp);
if (localep)
free(pathoutp);
exit(1);
}
}
else
if (access(ofilep, 0) == 0) {
(void)fprintf(stderr, "%s: Message file \"%s\" already exists;\ndid not overwrite it\n", cmdname, ofilep);
if (localep)
free(pathoutp);
exit(1);
}
}
if ((fp_inp = fopen(ifilep, "r")) == NULL) {
(void)fprintf(stderr, "%s: %s: %s\n",
cmdname, ifilep, syserr());
exit(1);
}
if ((bufinp = malloc(2 * LINESZ)) == NULL) {
(void)fprintf(stderr, "%s: malloc error (size = %d)\n",
cmdname, 2 * LINESZ);
exit(1);
}
bufworkp = bufinp + LINESZ;
if (sigset(SIGINT, SIG_IGN) == SIG_DFL)
(void)sigset(SIGINT, clean);
workp = tempnam(".", "xx");
if ((fp_outp = fopen(workp, "a+")) == NULL) {
(void)fprintf(stderr, "%s: %s: %s\n", cmdname, workp, syserr());
if (localep)
free(pathoutp);
free(bufinp);
exit(1);
}
for(;;) {
if (!fgets(bufinp, LINESZ, fp_inp)) {
if (!feof(fp_inp)) {
(void)fprintf(stderr,"%s: %s: %s\n",
cmdname, ifilep, syserr());
free(bufinp);
if (localep)
free(pathoutp);
exit(1);
}
break;
}
if(*(bufinp+strlen(bufinp)-1) != '\n') {
(void)fprintf(stderr, "%s: %s: data base file: error on line %d\n", cmdname, ifilep, num_msgs);
free(bufinp);
exit(1);
}
*(bufinp + strlen(bufinp) -1) = (char)0;
num_msgs++;
(void)strccpy(bufworkp, bufinp);
nitems = strlen(bufworkp) + 1;
if (fwrite(bufworkp, sizeof(*bufworkp), nitems, fp_outp) != nitems) {
(void)fprintf(stderr, "%s: %s: %s\n",
cmdname, workp, syserr());
exit(1);
}
}
free(bufinp);
(void)fclose(fp_outp);
if ((fp_outp = fopen(workp, "r")) == NULL) {
(void)fprintf(stderr, "%s: %s: %s\n", cmdname, workp, syserr());
exit(1);
}
if ((stat(workp, &buf)) != 0) {
(void)fprintf(stderr, "%s: %s: %s\n", cmdname, workp, syserr());
}
size = sizeof(int) + num_msgs * sizeof(int) + buf.st_size;
if ( (bufoutp = (int *)malloc((uint)size)) == NULL ) {
(void)fprintf(stderr, "%s: malloc error (size = %d)\n",
cmdname, size);
exit(1);
}
bufinp = (char *)bufoutp;
if ( (fread(bufinp + sizeof(int) + num_msgs * sizeof(int), sizeof(*bufinp), buf.st_size, fp_outp)) != buf.st_size ) {
free(bufinp);
(void) fprintf(stderr, "%s: %s: %s\n", cmdname, workp, syserr());
}
(void) fclose(fp_outp);
(void) unlink(workp);
free(workp);
msgp = bufinp + sizeof(int) + num_msgs * sizeof(int);
*bufoutp = num_msgs;
*(bufoutp + 1) = (bufinp + sizeof(int) + num_msgs * sizeof(int)) - bufinp;
for(i = 2; i <= num_msgs; i++) {
*(bufoutp + i) = (msgp + strlen(msgp) + 1) - bufinp;
msgp = msgp + strlen(msgp) + 1;
}
if (iflag) {
outfilep = pathoutp;
if (mymkdir(localedirp) == 0) {
free(bufinp);
if (localep)
free(pathoutp);
exit(1);
}
}
else
outfilep = ofilep;
if ((fp_outp = fopen(outfilep, "w")) == NULL) {
(void)fprintf(stderr, "%s: %s: %s\n",
cmdname, outfilep, syserr());
free(bufinp);
if (localep)
free(pathoutp);
exit(1);
}
if (fwrite((char *)bufinp, sizeof(*bufinp), size, fp_outp) != size) {
(void)fprintf(stderr, "%s: %s: %s\n",
cmdname, ofilep, syserr());
free(bufinp);
if (localep)
free(pathoutp);
exit(1);
}
free(bufinp);
if (localep)
free(pathoutp);
return (0);
}
static char *
syserr()
{
return (strerror(errno));
}
static void
usage()
{
(void)fprintf(stderr, "Usage: %s [-o] inputstrings outputmsgs\n",
cmdname);
(void)fprintf(stderr, " %s [-o] [-i locale] inputstrings outputmsgs\n", cmdname);
exit(1);
}
static int
mymkdir(localdir)
char *localdir;
{
char *dirp;
char *s1 = localdir;
char *path;
if ((path = malloc(strlen(localdir)+1)) == NULL)
return(0);
*path = '\0';
while( (dirp = strtok(s1, "/")) != NULL ) {
s1 = (char *)NULL;
(void)strcat(path, "/");
(void)strcat(path, dirp);
if (access(path, 3) == 0)
continue;
if (mkdir(path, 0777) == -1) {
(void)fprintf(stderr, "%s: %s: %s\n",
cmdname, path, syserr());
free(path);
return(0);
}
}
free(path);
return(1);
}
static void
clean(int sig)
{
(void)sigset(SIGINT, SIG_IGN);
if (workp)
(void) unlink(workp);
exit(1);
}