home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / x / volume10 / xcal / part03 / xcal_cal.c next >
C/C++ Source or Header  |  1990-12-18  |  5KB  |  206 lines

  1. #ifndef lint
  2. static char *sccsid = "@(#)xcal_cal.c    1.1 (Hillside Systems) 7/14/90";
  3. #endif  /* lint */
  4. /***
  5.  
  6. * program name:
  7.     xcal_cal.c
  8. * function:
  9.     read files generated by xcal and produce a file compatible
  10.     with the standard "calendar" utility
  11. * switches:
  12.     -d dir    use "dir" instead of "Calendar"
  13.     -f file use "file" instead of .xcal
  14.     -m    copy all of multi-line entries
  15. * history:
  16.     Written July, 1990
  17.     Ed Gould
  18.     mt Xinu, Inc.
  19. * (C) Copyright: 1990 mt Xinu, Inc.
  20.     
  21.     Permission to use, copy, modify, and distribute this software
  22.     and its documentation for any purpose is hereby granted to
  23.     anyone, provided that the above copyright notice appear in
  24.     all copies and that both that copyright notice and this
  25.     permission notice appear in supporting documentation, and
  26.     that the names of Ed Gould and mt Xinu, Inc. not be used
  27.     in advertising or publicity pertaining to distribution of
  28.     the software without specific, written prior permission.
  29.     mt Xinu, Inc. makes no representations about the suitability
  30.     of this software for any purpose.  It is provided "as is"
  31.     without express or implied warranty.
  32.  
  33.     Ed Gould and mt Xinu, Inc. DISCLAIM ALL WARRANTIES WITH
  34.     REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES
  35.     OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL Ed Gould
  36.     or mt Xinu, Inc. BE LIABLE FOR ANY SPECIAL, INDIRECT OR
  37.     CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING
  38.     FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
  39.     CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
  40.     OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
  41.     SOFTWARE.
  42.  
  43. ***/
  44. #include <stdio.h>
  45. #include <ctype.h>
  46. #include <sys/param.h>
  47. #include <sys/types.h>
  48. #include <sys/dir.h>
  49. #include <time.h>
  50. #include <strings.h>
  51.  
  52. char    *directory    = "Calendar";
  53. char    *file        = ".xcal";
  54. char    *home;
  55. char    usedir[MAXPATHLEN];
  56. char    usefile[MAXPATHLEN];
  57. char    line[BUFSIZ];
  58.  
  59. char    *months[]    = {"jan", "feb", "mar", "apr", "may", "jun",
  60.                "jul", "aug", "sep", "oct", "nov", "dec"};
  61.  
  62. int    debug;
  63. int    mflag;
  64.  
  65. FILE    *out;
  66.  
  67. void
  68. main(argc, argv)
  69.     char **argv;
  70. {
  71.     register int c;
  72.     time_t now;
  73.     register struct tm *lt;
  74.     char *getenv();
  75.  
  76.     while((c = getopt(argc, argv, "d:f:m#")) != EOF) {
  77.         extern char *optarg;
  78.  
  79.         switch (c) {
  80.  
  81.         case 'd':
  82.             directory = optarg;
  83.             break;
  84.  
  85.         case 'f':
  86.             file = optarg;
  87.             break;
  88.  
  89.         case 'm':
  90.             mflag++;
  91.             break;
  92.  
  93.         case '#':
  94.             debug++;
  95.             break;
  96.  
  97.         case '?':
  98.         default:
  99.             fprintf(stderr, "usage: %s [-d dir] [-f file]\n",
  100.                                 argv[0]);
  101.             exit(1);
  102.         }
  103.     }
  104.     home = getenv("HOME");
  105.     /*
  106.      * For both directory and output file, 
  107.      *
  108.      *   /...    is absolute path
  109.      *   ./...    is relative to current directory
  110.      *
  111.      * otherwise, relative to $HOME, if available
  112.      */
  113.     if(file[0] == '/' || (file[0] == '.' && file[1] == '/') ||
  114.        home == NULL)
  115.         strcpy(usefile, file);
  116.     else
  117.         sprintf(usefile, "%s/%s", home, file);
  118.     if(debug)
  119.         fprintf(stderr, "output to %s\n", usefile);
  120.     if((out = fopen(usefile, "w")) == NULL) {
  121.         perror(usefile);
  122.         exit(1);
  123.     }
  124.     now = time((time_t *)NULL);
  125.     lt = localtime(&now);
  126.     calfiles(directory, lt->tm_year + 1900, lt->tm_mon, lt->tm_mday);
  127.     if(lt->tm_mday >= 25 ) {
  128.         /*
  129.          * close to end of month: include next month, too
  130.          */
  131.         if(lt->tm_mon == 11)
  132.             calfiles(directory, lt->tm_year + 1900 + 1, 0, 1);
  133.         else
  134.             calfiles(directory, lt->tm_year + 1900,
  135.                             lt->tm_mon + 1, 1);
  136.     }
  137. }
  138.  
  139. calfiles(dir, year, thismonth, today)
  140.     register char *dir;
  141. {
  142.     register DIR *dp;
  143.     register char *to;
  144.     register char *from;
  145.     register struct direct *d;
  146.     char day[3];
  147.     char month[4];
  148.  
  149.     if(dir[0] == '/' || (dir[0] == '.' && dir[1] == '/') ||
  150.        home == NULL)
  151.         sprintf(usedir, "%s/xy%4d", dir, year);
  152.     else
  153.         sprintf(usedir, "%s/%s/xy%4d", home, dir, year);
  154.     if(debug)
  155.         fprintf(stderr, "looking in directory %s\n", usedir);
  156.     if((dp = opendir(usedir)) == NULL) {
  157.         perror(usedir);
  158.         exit(1);
  159.     }
  160.     while((d = readdir(dp)) != NULL) {
  161.         register FILE *in;
  162.  
  163.         if(d->d_name[0] != 'x' || d->d_name[1] != 'c' ||
  164.            !isascii(d->d_name[2]) || !isdigit(d->d_name[2]))
  165.             continue;
  166.         sprintf(usefile, "%s/%s", usedir, d->d_name);
  167.         if(debug)
  168.             fprintf(stderr, "looking in file %s\n", usefile);
  169.         if((in = fopen(usefile, "r")) == NULL) {
  170.             if(debug)
  171.                 perror(usefile);
  172.             continue;
  173.         }
  174.         from = &d->d_name[2];
  175.         to = day;
  176.         while(isascii(*from) && isdigit(*from))
  177.             *to++ = *from++;
  178.         *to = '\0';
  179.         to = month;
  180.         while(isascii(*from) && !isdigit(*from)) {
  181.             if(isupper(*from))
  182.                 *from = tolower(*from);
  183.             *to++ = *from++;
  184.         }
  185.         *to = '\0';
  186.         if(strcmp(month, months[thismonth]) != 0 ||
  187.            atoi(day) < today) {
  188.             if(debug)
  189.                 fprintf(stderr, "\tskipped - date\n");
  190.             fclose(in);
  191.             continue;
  192.         }
  193.         while(fgets(line, sizeof(line), in) != NULL) {
  194.             if((to = index(line, '\n')) != NULL)
  195.                 *to = '\0';
  196.             if(debug)
  197.                 fprintf(stderr, "==>\t%s %s\t%s\n", month,
  198.                                 day, line);
  199.             fprintf(out, "%s %s\t%s\n", month, day, line);
  200.             if(mflag == 0)
  201.                 break;
  202.         }
  203.         fclose(in);
  204.     }
  205. }
  206.