strcpy (User, pw->pw_name); strcpy (RealUser, User);
strncpy (User, pw->pw_name, MAX_UNAME-1); strncpy (RealUser, User, MAX_UNAME);
  check_error __P ((char *)),  parse_args __P ((int c, char *v[], char **const f));static int replace_cmd __P ((void));static voidusage (char *msg){  fprintf (stderr, "%s: usage error: %s\n", ProgramName, msg);  fprintf (stderr, "usage:\t%s [-u user] file\n", ProgramName);  fprintf (stderr, "\t%s [-u user] { -e | -l | -r }\n", ProgramName);  fprintf (stderr, "\t\t(default operation is replace, per 1003.2)\n");  fprintf (stderr, "\t-e\t(edit user's crontab)\n");  fprintf (stderr, "\t-l\t(list user's crontab)\n");  fprintf (stderr, "\t-r\t(delete user's crontab)\n");  exit (ERROR_EXIT);}intmain (int argc, char *argv[]){  int exitstatus;  char *config_file;		/* Name of our configuration file;  NULL if none */  Pid = getpid ();  ProgramName = argv[0];#if defined(POSIX)  setlocale (LC_ALL, "");#endif#if HAVE_SETLINEBUF  setlinebuf (stderr);#endif /*HAVE_SETLINEBUF */  parse_args (argc, argv, &config_file);	/* sets many globals, opens a file */  read_config (config_file, &allow_only_root, &log_syslog, &allow_file,	       &deny_file, &crondir, &spool_dir, &log_file, &syscrontab,	       &lastrun_file, &pidfile, &mailprog, &mailargs);  set_cron_cwd ();  if (!allowed (User))    {      fprintf (stderr,	       "You (%s) are not allowed to use this program (%s)\n",	       User, ProgramName);      fprintf (stderr, "See crontab(1) for more information\n");      log_it (RealUser, Pid, "AUTH", "crontab command not allowed");      exit (ERROR_EXIT);    }  exitstatus = OK_EXIT;  switch (Option)    {    case opt_list:      list_cmd ();      break;    case opt_delete:      delete_cmd ();      break;    case opt_edit:      edit_cmd ();      break;    case opt_replace:      if (replace_cmd () < 0)	exitstatus = ERROR_EXIT;      break;    }  exit (0); /*NOTREACHED*/}static voidparse_args (int argc, char *argv[], char **const config_file_p){  int argch;#ifdef CONFIG_FILE  *config_file_p = CONFIG_FILE;#else  *config_file_p = NULL;#endif  if (!(pw = getpwuid (getuid ())))    {      fprintf (stderr, "%s: your UID isn't in the passwd file.\n",	       ProgramName);      fprintf (stderr, "bailing out.\n");      exit (ERROR_EXIT);    }  strcpy (User, pw->pw_name);  strcpy (RealUser, User);  Filename[0] = '\0';  Option = opt_unknown;  while (EOF != (argch = getopt (argc, argv, "u:lerx:")))    {      switch (argch)	{	case 'x':	  if (!set_debug_flags (optarg))	    usage ("bad debug option");	  break;	case 'u':	  if (getuid () != ROOT_UID)	    {	      fprintf (stderr, "must be privileged to use -u\n");	      exit (ERROR_EXIT);	    }	  if (!(pw = getpwnam (optarg)))	    {	      fprintf (stderr, "%s:  user `%s' unknown\n",		       ProgramName, optarg);	      exit (ERROR_EXIT);	    }	  (void) strcpy (User, optarg);	  break;	case 'l':	  if (Option != opt_unknown)	    usage ("only one operation permitted");	  Option = opt_list;	  break;	case 'r':	  if (Option != opt_unknown)	    usage ("only one operation permitted");	  Option = opt_delete;	  break;	case 'e':	  if (Option != opt_unknown)	    usage ("only one operation permitted");	  Option = opt_edit;	  break;	default:	  usage ("unrecognized option");	}    }  endpwent ();  if (Option != opt_unknown)    {      if (argv[optind] != NULL)	{	  usage ("no arguments permitted after this option");	}    }  else    {      if (argv[optind] != NULL)	{	  Option = opt_replace;	  (void) strncpy (Filename, argv[optind], sizeof (Filename) - 1);	  Filename[sizeof (Filename) - 1] = '\0';	}      else	{	  usage ("file name must be specified for replace");	}    }  if (Option == opt_replace)    {      /* we have to open the file here because we're going to       * chdir(2) into /var/cron before we get around to       * reading the file.       */      if (!strcmp (Filename, "-"))	{	  NewCrontab = stdin;	}      else	{	  /* relinquish the setuid status of the binary during	   * the open, lest nonroot users read files they should	   * not be able to read.  we can't use access() here	   * since there's a race condition.  thanks go out to	   * Arnt Gulbrandsen <agulbra@pvv.unit.no> for spotting	   * the race.	   */	  if (swap_uids () < OK)	    {	      perror ("swapping uids");	      exit (ERROR_EXIT);	    }	  if (!(NewCrontab = fopen (Filename, "r")))	    {	      perror (Filename);	      exit (ERROR_EXIT);	    }	  if (swap_uids () < OK)	    {	      perror ("swapping uids back");	      exit (ERROR_EXIT);	    }	}    }  Debug (DMISC, ("user=%s, file=%s, option=%s\n",		 User, Filename, Options[(int) Option]));}static voidlist_cmd (void){  char n[MAX_FNAME];  FILE *f;  int ch;  log_it (RealUser, Pid, "LIST", User);  (void) snprintf (n, MAX_FNAME, CRON_TAB (User));  if (!(f = fopen (n, "r")))    {      if (errno == ENOENT)	fprintf (stderr, "no crontab for %s\n", User);      else	perror (n);      exit (ERROR_EXIT);    }  /* file is open. copy to stdout, close.   */  Set_LineNum (1);  while (EOF != (ch = get_char (f)))    putchar (ch);  fclose (f);}static voiddelete_cmd (void){  char n[MAX_FNAME];  log_it (RealUser, Pid, "DELETE", User);  (void) snprintf (n, MAX_FNAME, CRON_TAB (User));  if (unlink (n))    {      if (errno == ENOENT)	fprintf (stderr, "no crontab for %s\n", User);      else	perror (n);      exit (ERROR_EXIT);    }  poke_daemon ();}static voidcheck_error (char *msg){  CheckErrorCount++;  fprintf (stderr, "\"%s\":%d: %s\n", Filename, LineNumber - 1, msg);}