home *** CD-ROM | disk | FTP | other *** search
/ rtsi.com / 2014.01.www.rtsi.com.tar / www.rtsi.com / OS9 / TOP / USR / SRC / vcron.t.Z / vcron.t / VCRON / misc.c < prev    next >
Text File  |  1988-11-15  |  6KB  |  325 lines

  1. /* $Source: /usr/src/local/vix/cron/misc.c,v $
  2.  * $Revision: 1.5 $
  3.  * $Log:    misc.c,v $
  4.  * Revision 1.5  87/05/02  17:34:04  paul
  5.  * baseline for mod.sources release
  6.  * 
  7.  * Revision 1.4  87/04/02  16:54:45  paul
  8.  * added BSD/ATT differences in be_different()
  9.  *   (another idea from rs@mirror)
  10.  * 
  11.  * Revision 1.3  87/02/11  00:55:10  paul
  12.  * added strcmp_until; moved get_shell into user.c
  13.  * 
  14.  * Revision 1.2  87/02/02  19:25:01  paul
  15.  * various
  16.  * 
  17.  * Revision 1.1  87/01/26  23:47:57  paul
  18.  * Initial revision
  19.  * 
  20.  * vix 15jan87 [added TIOCNOTTY, thanks csg@pyramid]
  21.  * vix 30dec86 [written]
  22.  */
  23.  
  24. /* Copyright 1987 by Vixie Enterprises
  25.  * All rights reserved
  26.  *
  27.  * Distribute freely, except: don't sell it, don't remove my name from the
  28.  * source or documentation (don't take credit for my work), mark your changes
  29.  * (don't get me blamed for your possible bugs), don't alter or remove this
  30.  * notice.  Commercial redistribution is negotiable; contact me for details.
  31.  *
  32.  * Send bug reports, bug fixes, enhancements, requests, flames, etc., and
  33.  * I'll try to keep a version up to date.  I can be reached as follows:
  34.  * Paul Vixie, Vixie Enterprises, 329 Noe Street, San Francisco, CA, 94114,
  35.  * (415) 864-7013, {ucbvax!dual,ames,ucsfmis,lll-crg,sun}!ptsfa!vixie!paul.
  36.  */
  37.  
  38.  
  39. #include "cron.h"
  40. # include <time.h>
  41. # include <sgstat.h>
  42. # include <types.h>
  43.  
  44.  
  45. char *
  46. savestr(str)
  47.     char    *str;
  48. {
  49.     char    *malloc(), *strcpy();
  50.     char    *temp;
  51.  
  52.     temp = malloc((unsigned) (strlen(str) + 1));
  53.     (void) strcpy(temp, str);
  54.     return temp;
  55. }
  56.  
  57.  
  58. int
  59. nocase_strcmp(left, right)
  60.     char    *left;
  61.     char    *right;
  62. {
  63.     while (*left && (MkLower(*left) == MkLower(*right)))
  64.     {
  65.         left++;
  66.         right++;
  67.     }
  68.     return MkLower(*left) - MkLower(*right);
  69. }
  70.  
  71.  
  72. int
  73. strcmp_until(left, right, until)
  74. REG    char    *left;
  75. REG    char    *right;
  76. REG    char    until;
  77. {
  78.     REG int    diff;
  79.  
  80. #if DEBUGGING
  81.     Debug(DPARS|DEXT, "strcmp_until(%s,%s,%c) ... ", left, right, until);
  82. #endif
  83.  
  84.     while (*left && *left != until && *left == *right)
  85.     {
  86.         left++;
  87.         right++;
  88.     }
  89.  
  90.     if (    (*left=='\0' || *left == until) 
  91.         &&    (*right=='\0' || *right == until)
  92.        )
  93.         diff = 0;
  94.     else
  95.         diff = *left - *right;
  96.  
  97. #if DEBUGGING
  98.     Debug(DPARS|DEXT, "%d\n", diff);
  99. #endif
  100.  
  101.     return diff;
  102. }
  103.  
  104.  
  105. /* strdtb(s) - delete trailing blanks in string 's' and return new length
  106.  */
  107. int
  108. strdtb(s)
  109.     REG char    *s;
  110. {
  111.     REG char    *x = s;
  112.  
  113.     /* scan forward to the null
  114.      */
  115.     while (*x)
  116.         x++;
  117.  
  118.     /* scan backward to either the first character before the string,
  119.      * or the last non-blank in the string, whichever comes first.
  120.      */
  121.     do    {x--;}
  122.     while (x >= s && isspace(*x));
  123.  
  124.     /* one character beyond where we stopped above is where the null
  125.      * goes.
  126.      */
  127.     *++x = '\0';
  128.  
  129.     /* the difference between the position of the null character and
  130.      * the position of the first character of the string is the length.
  131.      */
  132.     return x - s;
  133. }
  134.  
  135.  
  136. int
  137. set_debug_flags(flags)
  138.     char    *flags;
  139. {
  140.     /* debug flags are of the form    flag[,flag ...]
  141.      *
  142.      * if an error occurs, print a message to stdout and return FALSE.
  143.      * otherwise return TRUE after setting ERROR_FLAGS.
  144.      */
  145.  
  146. #if !DEBUGGING
  147.  
  148.     printf("this program was compiled without debugging enabled\n");
  149.     return FALSE;
  150.  
  151. #else /* DEBUGGING */
  152.  
  153.     REG char    *_pc = flags;
  154.  
  155.     DEBUG_FLAGS = 0;
  156.  
  157.     while (*_pc)
  158.     {
  159.         REG char    **test;
  160.         REG int    mask;
  161.  
  162.         /* try to find debug flag name in our list.
  163.          */
  164.         for (    test = DEBUG_FLAG_NAMES, mask = 1;
  165.             *test && strcmp_until(*test, _pc, ',');
  166.             test++, mask <<= 1
  167.             )
  168.             ;
  169.  
  170.         if (!*test)
  171.         {
  172.             fprintf(stderr,
  173.                 "unrecognized debug flag <%s> <%s>\n",
  174.                 flags, _pc);
  175.             return FALSE;
  176.         }
  177.  
  178.         DEBUG_FLAGS |= mask;
  179.  
  180.         /* skip to the next flag
  181.          */
  182.         while (*_pc && *_pc != ',')
  183.             _pc++;
  184.         if (*_pc == ',')
  185.             _pc++;
  186.     }
  187.  
  188.     if (DEBUG_FLAGS)
  189.     {
  190.         REG int    flag;
  191.  
  192.         fprintf(stderr, "debug flags enabled:");
  193.  
  194.         for (flag = 0;  flag < DCOUNT;  flag++)
  195.             if (DEBUG_FLAGS & (1 << flag))
  196.                 fprintf(stderr, " %s", DEBUG_FLAG_NAMES[flag]);
  197.         fprintf(stderr, "\n");
  198.     }
  199.  
  200.     return TRUE;
  201.  
  202. #endif /* DEBUGGING */
  203. }
  204.  
  205.  
  206. void
  207. set_cron_uid()
  208. {
  209.     if (ERR == setuid(ROOT_UID))
  210.     {
  211.         perror("seteuid");
  212.         exit(ERROR_EXIT);
  213.     }
  214. }
  215.  
  216.  
  217. /* get_char(file) : like getc() but increment LINE_NUMBER on newlines
  218.  */
  219. int
  220. get_char(file)
  221.     FILE    *file;
  222. {
  223.     REG int    ch;
  224.  
  225.     ch = getc(file);
  226.     if (ch == '\n')
  227.         Set_LineNum(LINE_NUMBER + 1)
  228.     return ch;
  229. }
  230.  
  231.  
  232. /* unget_char(ch, file) : like ungetc but do LINE_NUMBER processing
  233.  */
  234. void
  235. unget_char(ch, file)
  236.     int    ch;
  237.     FILE    *file;
  238. {
  239.     ungetc(ch, file);
  240.     if (ch == '\n')
  241.         Set_LineNum(LINE_NUMBER - 1)
  242. }
  243.  
  244.  
  245. /* get_string(str, max, file, termstr) : like fgets() but
  246.  *        (1) has terminator string which should include \n
  247.  *        (2) will always leave room for the null
  248.  *        (3) uses get_char() so LINE_NUMBER will be accurate
  249.  *        (4) returns EOF or terminating character, whichever
  250.  */
  251. int
  252. get_string(string, size, file, terms)
  253.     char    *string;
  254.     int    size;
  255.     FILE    *file;
  256.     char    *terms;
  257. {
  258.     REG int    ch;
  259.     char    *index();
  260.  
  261.     while (EOF != (ch = get_char(file)) && !index(terms, ch))
  262.         if (size > 1)
  263.         {
  264.             *string++ = (char) ch;
  265.             size--;
  266.         }
  267.  
  268.     if (size > 0)
  269.         *string = '\0';
  270.  
  271.     return ch;
  272. }
  273.  
  274.  
  275. /* skip_comments(file) : read past comment (if any)
  276.  */
  277. void
  278. skip_comments(file)
  279.     FILE    *file;
  280. {
  281.     REG int    ch;
  282.  
  283.     while (EOF != (ch = get_char(file)))
  284.     {
  285.         /* ch is now the first character of a line.
  286.          */
  287.  
  288.         while (ch == ' ' || ch == '\t')
  289.             ch = get_char(file);
  290.  
  291.         if (ch == EOF)
  292.             break;
  293.  
  294.         /* ch is now the first non-blank character of a line.
  295.          */
  296.  
  297.         if (ch != '\n' && ch != '#')
  298.             break;
  299.  
  300.         /* ch must be a newline or comment as first non-blank
  301.          * character on a line.
  302.          */
  303.  
  304.         while (ch != '\n' && ch != EOF)
  305.             ch = get_char(file);
  306.  
  307.         /* ch is now the newline of a line which we're going to
  308.          * ignore.
  309.          */
  310.     }
  311.     unget_char(ch, file);
  312. }
  313.  
  314. #if DEBUGGING
  315. void
  316. Debug(mask, a, b, c, d, e ,f)
  317. int mask;
  318. char *a;
  319. {
  320.   if ( (DEBUG_FLAGS & (mask) ) == (mask) )
  321.         printf(a, b, c, d, e, f);
  322. }
  323. #endif
  324.  
  325.