home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / x / volume13 / xcal / part04 / xcal_cal.c next >
C/C++ Source or Header  |  1991-05-12  |  5KB  |  215 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. #ifdef SYSV
  49. #include <dirent.h>
  50. #include <string.h>
  51. #else
  52. #include <strings.h>
  53. #endif
  54. #include <sys/dir.h>
  55. #include <time.h>
  56.  
  57.  
  58. #ifdef SYSV
  59. #define    index    strchr
  60. #endif
  61. char    *directory    = "Calendar";
  62. char    *file        = ".xcal";
  63. char    *home;
  64. char    usedir[MAXPATHLEN];
  65. char    usefile[MAXPATHLEN];
  66. char    line[BUFSIZ];
  67.  
  68. char    *months[]    = {"jan", "feb", "mar", "apr", "may", "jun",
  69.                "jul", "aug", "sep", "oct", "nov", "dec"};
  70.  
  71. int    debug;
  72. int    mflag;
  73.  
  74. FILE    *out;
  75.  
  76. void
  77. main(argc, argv)
  78.     char **argv;
  79. {
  80.     register int c;
  81.     time_t now;
  82.     register struct tm *lt;
  83.     char *getenv();
  84.  
  85.     while((c = getopt(argc, argv, "d:f:m#")) != EOF) {
  86.         extern char *optarg;
  87.  
  88.         switch (c) {
  89.  
  90.         case 'd':
  91.             directory = optarg;
  92.             break;
  93.  
  94.         case 'f':
  95.             file = optarg;
  96.             break;
  97.  
  98.         case 'm':
  99.             mflag++;
  100.             break;
  101.  
  102.         case '#':
  103.             debug++;
  104.             break;
  105.  
  106.         case '?':
  107.         default:
  108.             fprintf(stderr, "usage: %s [-d dir] [-f file]\n",
  109.                                 argv[0]);
  110.             exit(1);
  111.         }
  112.     }
  113.     home = getenv("HOME");
  114.     /*
  115.      * For both directory and output file, 
  116.      *
  117.      *   /...    is absolute path
  118.      *   ./...    is relative to current directory
  119.      *
  120.      * otherwise, relative to $HOME, if available
  121.      */
  122.     if(file[0] == '/' || (file[0] == '.' && file[1] == '/') ||
  123.        home == NULL)
  124.         strcpy(usefile, file);
  125.     else
  126.         sprintf(usefile, "%s/%s", home, file);
  127.     if(debug)
  128.         fprintf(stderr, "output to %s\n", usefile);
  129.     if((out = fopen(usefile, "w")) == NULL) {
  130.         perror(usefile);
  131.         exit(1);
  132.     }
  133.     now = time((time_t *)NULL);
  134.     lt = localtime(&now);
  135.     calfiles(directory, lt->tm_year + 1900, lt->tm_mon, lt->tm_mday);
  136.     if(lt->tm_mday >= 25 ) {
  137.         /*
  138.          * close to end of month: include next month, too
  139.          */
  140.         if(lt->tm_mon == 11)
  141.             calfiles(directory, lt->tm_year + 1900 + 1, 0, 1);
  142.         else
  143.             calfiles(directory, lt->tm_year + 1900,
  144.                             lt->tm_mon + 1, 1);
  145.     }
  146. }
  147.  
  148. calfiles(dir, year, thismonth, today)
  149.     register char *dir;
  150. {
  151.     register DIR *dp;
  152.     register char *to;
  153.     register char *from;
  154.     register struct direct *d;
  155.     char day[3];
  156.     char month[4];
  157.  
  158.     if(dir[0] == '/' || (dir[0] == '.' && dir[1] == '/') ||
  159.        home == NULL)
  160.         sprintf(usedir, "%s/xy%4d", dir, year);
  161.     else
  162.         sprintf(usedir, "%s/%s/xy%4d", home, dir, year);
  163.     if(debug)
  164.         fprintf(stderr, "looking in directory %s\n", usedir);
  165.     if((dp = opendir(usedir)) == NULL) {
  166.         perror(usedir);
  167.         exit(1);
  168.     }
  169.     while((d = readdir(dp)) != NULL) {
  170.         register FILE *in;
  171.  
  172.         if(d->d_name[0] != 'x' || d->d_name[1] != 'c' ||
  173.            !isascii(d->d_name[2]) || !isdigit(d->d_name[2]))
  174.             continue;
  175.         sprintf(usefile, "%s/%s", usedir, d->d_name);
  176.         if(debug)
  177.             fprintf(stderr, "looking in file %s\n", usefile);
  178.         if((in = fopen(usefile, "r")) == NULL) {
  179.             if(debug)
  180.                 perror(usefile);
  181.             continue;
  182.         }
  183.         from = &d->d_name[2];
  184.         to = day;
  185.         while(isascii(*from) && isdigit(*from))
  186.             *to++ = *from++;
  187.         *to = '\0';
  188.         to = month;
  189.         while(isascii(*from) && !isdigit(*from)) {
  190.             if(isupper(*from))
  191.                 *from = tolower(*from);
  192.             *to++ = *from++;
  193.         }
  194.         *to = '\0';
  195.         if(strcmp(month, months[thismonth]) != 0 ||
  196.            atoi(day) < today) {
  197.             if(debug)
  198.                 fprintf(stderr, "\tskipped - date\n");
  199.             fclose(in);
  200.             continue;
  201.         }
  202.         while(fgets(line, sizeof(line), in) != NULL) {
  203.             if((to = index(line, '\n')) != NULL)
  204.                 *to = '\0';
  205.             if(debug)
  206.                 fprintf(stderr, "==>\t%s %s\t%s\n", month,
  207.                                 day, line);
  208.             fprintf(out, "%s %s\t%s\n", month, day, line);
  209.             if(mflag == 0)
  210.                 break;
  211.         }
  212.         fclose(in);
  213.     }
  214. }
  215.