home *** CD-ROM | disk | FTP | other *** search
/ Serving the Web / ServingTheWeb1995.disc1of1.iso / linux / slacksrce / d / libc / libc-4.6 / libc-4 / libc-linux / locale / time / mktime.c < prev   
Encoding:
C/C++ Source or Header  |  1994-03-18  |  4.2 KB  |  191 lines

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <ctype.h>
  5. #include <getopt.h>
  6. #include <locale.h>
  7. #include <localeinfo.h>
  8.  
  9. unsigned char *codename;
  10.  
  11. struct time_info tinfo;
  12.  
  13. unsigned char **lines[] =
  14. {
  15.   (unsigned char **) &tinfo.abbrev_wkday[0],
  16.   (unsigned char **) &tinfo.abbrev_wkday[1],
  17.   (unsigned char **) &tinfo.abbrev_wkday[2],
  18.   (unsigned char **) &tinfo.abbrev_wkday[3],
  19.   (unsigned char **) &tinfo.abbrev_wkday[4],
  20.   (unsigned char **) &tinfo.abbrev_wkday[5],
  21.   (unsigned char **) &tinfo.abbrev_wkday[6],
  22.   (unsigned char **) &tinfo.full_wkday[0],
  23.   (unsigned char **) &tinfo.full_wkday[1],
  24.   (unsigned char **) &tinfo.full_wkday[2],
  25.   (unsigned char **) &tinfo.full_wkday[3],
  26.   (unsigned char **) &tinfo.full_wkday[4],
  27.   (unsigned char **) &tinfo.full_wkday[5],
  28.   (unsigned char **) &tinfo.full_wkday[6],
  29.   (unsigned char **) &tinfo.abbrev_month[0],
  30.   (unsigned char **) &tinfo.abbrev_month[1],
  31.   (unsigned char **) &tinfo.abbrev_month[2],
  32.   (unsigned char **) &tinfo.abbrev_month[3],
  33.   (unsigned char **) &tinfo.abbrev_month[4],
  34.   (unsigned char **) &tinfo.abbrev_month[5],
  35.   (unsigned char **) &tinfo.abbrev_month[6],
  36.   (unsigned char **) &tinfo.abbrev_month[7],
  37.   (unsigned char **) &tinfo.abbrev_month[8],
  38.   (unsigned char **) &tinfo.abbrev_month[9],
  39.   (unsigned char **) &tinfo.abbrev_month[10],
  40.   (unsigned char **) &tinfo.abbrev_month[11],
  41.   (unsigned char **) &tinfo.full_month[0],
  42.   (unsigned char **) &tinfo.full_month[1],
  43.   (unsigned char **) &tinfo.full_month[2],
  44.   (unsigned char **) &tinfo.full_month[3],
  45.   (unsigned char **) &tinfo.full_month[4],
  46.   (unsigned char **) &tinfo.full_month[5],
  47.   (unsigned char **) &tinfo.full_month[6],
  48.   (unsigned char **) &tinfo.full_month[7],
  49.   (unsigned char **) &tinfo.full_month[8],
  50.   (unsigned char **) &tinfo.full_month[9],
  51.   (unsigned char **) &tinfo.full_month[10],
  52.   (unsigned char **) &tinfo.full_month[11],
  53.   (unsigned char **) &tinfo.ampm[0],
  54.   (unsigned char **) &tinfo.ampm[1],
  55.   (unsigned char **) &tinfo.date_time,
  56.   (unsigned char **) &tinfo.date,
  57.   (unsigned char **) &tinfo.time,
  58.   (unsigned char **) &tinfo.ut0,
  59.   (unsigned char **) &tinfo.tz,
  60.   NULL
  61. };
  62.  
  63. int parse (void);
  64.  
  65. int
  66. main (int argc, char *argv[])
  67. {
  68.   int i;
  69.   unsigned char *outname = "LC_TIME";
  70.  
  71.   while ((i = getopt (argc, argv, "o:")) != EOF)
  72.     {
  73.       switch (i)
  74.     {
  75.     case 'o':
  76.       outname = optarg;
  77.       break;
  78.     }
  79.     }
  80.  
  81.   if (argc - optind > 1)
  82.     {
  83.       (void) fprintf (stderr, "Usage: %s [-o out_file_name] [file]\n", argv[0]);
  84.       return 3;
  85.     }
  86.   else if ((argc - optind) == 1)
  87.     {
  88.       if (freopen (argv[optind], "r", stdin) == NULL)
  89.     {
  90.       perror (argv[optind]);
  91.       return 2;
  92.     }
  93.     }
  94.  
  95.   if (!parse ())
  96.     return 1;
  97.  
  98.   return !write_out (outname);
  99. }
  100.  
  101. void
  102. write_str (char *str, FILE * ofp)
  103. {
  104.   short int slen = strlen (str) + 1;
  105.  
  106.   (void) fwrite (&slen, sizeof (slen), 1, ofp);
  107.   (void) fwrite (str, sizeof (char), slen, ofp);
  108. }
  109.  
  110. int
  111. write_out (outname)
  112.      unsigned char *outname;
  113. {
  114.   unsigned char ***target = lines;
  115.   FILE *ofp = fopen (outname, "w");
  116.  
  117.   if (ofp == NULL)
  118.     return 0;
  119.  
  120.   while (*target != NULL)
  121.     {
  122.       write_str (**target, ofp);
  123.       target++;
  124.     }
  125. #ifndef NOGUARD
  126.   (void) fwrite (codename, sizeof (unsigned char), strlen (codename) + 1, ofp);
  127. #endif
  128.   (void) fclose (ofp);
  129.   return 1;
  130. }
  131.  
  132. unsigned char iline[1024];
  133.  
  134. int
  135. parse ()
  136. {
  137.   int codename_seen = 0;
  138.   int lineno = 0;
  139.   unsigned char ***target = lines;
  140.   unsigned char *cp;
  141.  
  142.   while (fgets (iline, sizeof (iline), stdin) != NULL)
  143.     {
  144.       lineno++;
  145.       if (iline[0] == '#')
  146.     continue;
  147.       if ((cp = strchr (iline, '\n')) == NULL)
  148.     {
  149.       (void) fprintf (stderr, "Line %d: buffer overflow\n", lineno);
  150.       return 0;
  151.     }
  152.       *cp = '\0';
  153.       if (strlen (iline) == 0)
  154.     continue;
  155.       if (!codename_seen)
  156.     {
  157.       if (grok_codename ())
  158.         {
  159.           codename_seen = 1;
  160.           continue;
  161.         }
  162.       else
  163.         return 0;
  164.     }
  165.       **target++ = strdup (iline);
  166.       if (*target == NULL)
  167.     return 1;
  168.     }
  169.   return 0;
  170. }
  171.  
  172. int
  173. grok_codename ()
  174. {
  175.   unsigned char *cp = iline;
  176.  
  177.   while (*cp && *cp == ' ' && *cp == '\t')
  178.     cp++;
  179.   if (*cp == '\0')
  180.     return 0;
  181.   if (strncmp (cp, "codeset ", 8) && strncmp (cp, "codeset\t", 8))
  182.     return 0;
  183.   cp += 8;
  184.   while (*cp && *cp == ' ' && *cp == '\t')
  185.     cp++;
  186.   if (*cp == '\0')
  187.     return 0;
  188.   codename = strdup (cp);
  189.   return 1;
  190. }
  191.