keep npartitions independent of byte order
[unix-history] / usr / src / libexec / bugfiler / bugfiler.c
CommitLineData
7172eb74 1/*
771d5f21 2 * Copyright (c) 1983, 1986 Regents of the University of California.
7172eb74
DF
3 * All rights reserved. The Berkeley software License Agreement
4 * specifies the terms and conditions for redistribution.
5 */
6
7#ifndef lint
8char copyright[] =
771d5f21 9"@(#) Copyright (c) 1983, 1986 Regents of the University of California.\n\
7172eb74
DF
10 All rights reserved.\n";
11#endif not lint
12
524aa063 13#ifndef lint
771d5f21 14static char sccsid[] = "@(#)bugfiler.c 5.6 (Berkeley) 86/11/25";
7172eb74 15#endif not lint
524aa063 16
b7520dd0 17/*
771d5f21
KB
18 * Bug report processing program, designed to be invoked
19 * through aliases(5).
b7520dd0 20 */
771d5f21
KB
21#include <bug.h>
22#include <sys/time.h>
b7520dd0 23#include <stdio.h>
ae422d11
SL
24#include <pwd.h>
25
771d5f21
KB
26extern char *optarg; /* getopt arguments */
27extern int optind;
b7520dd0 28
771d5f21
KB
29int lfd; /* lock file descriptor */
30short do_redist = YES; /* redistribut BR */
31char bfr[MAXBSIZE], /* general I/O buffer */
32 tmpname[sizeof(TMP_BUG) + 5]; /* temp bug file */
b7520dd0 33
771d5f21
KB
34main(argc,argv)
35int argc;
36char **argv;
b7520dd0 37{
771d5f21
KB
38 register struct passwd *pwd; /* bugs password entry */
39 register int ch; /* getopts char */
40 register short do_ack = YES; /* acknowledge bug report */
41 struct passwd *getpwnam();
42
43 while ((ch = getopt(argc,argv,"ar")) != EOF)
44 switch((char)ch) {
45 case 'a':
46 do_ack = NO;
b7520dd0 47 break;
771d5f21
KB
48 case 'r':
49 do_redist = NO;
80765673 50 break;
771d5f21 51 case '?':
b7520dd0 52 default:
771d5f21 53 error("usage: bugfiler [-ar] [maildir]",CHN);
b7520dd0 54 }
80765673 55
771d5f21
KB
56 if (!(pwd = getpwnam(BUGS_ID)))
57 error("bugs person %s is unknown",BUGS_ID);
80765673 58
771d5f21
KB
59 argv += optind;
60 if (*argv) { /* change to argument directory */
61 if (chdir(*argv))
62 error("can't move to %s.",*argv);
63 } /* change to bugs home directory */
64 else if (chdir(pwd->pw_dir))
65 error("can't move to %s.",pwd->pw_dir);
80765673 66
771d5f21
KB
67 if (setreuid(0,pwd->pw_uid))
68 error("can't set id to %s.",BUGS_ID);
80765673 69
771d5f21
KB
70 umask(2); /* everything is 664 */
71 seterr();
72 logit();
73 make_copy();
80765673 74
771d5f21
KB
75 if (access(LOCK_FILE,R_OK) || (lfd = open(LOCK_FILE,O_RDONLY,0)) < 0)
76 error("can't read lock file %s.",LOCK_FILE);
80765673 77
771d5f21
KB
78 gethead();
79 process();
80765673 80
771d5f21
KB
81 if (setuid(0,0))
82 error("can't set id to root.",CHN);
80765673 83
771d5f21
KB
84 if (do_ack)
85 reply();
86 if (do_redist)
87 redist();
80765673 88
771d5f21
KB
89 unlink(tmpname);
90 exit(OK);
b7520dd0
RC
91}
92
d93ebe7f 93/*
771d5f21
KB
94 * make_copy --
95 * make a copy of the bug report
d93ebe7f 96 */
771d5f21
KB
97static
98make_copy()
d93ebe7f 99{
771d5f21
KB
100 register int cnt, /* read return value */
101 tfd; /* temp file descriptor */
102 char *mktemp(), *strcpy();
103
104 /* use O_EXCL, since may not be able to get a lock file */
105 for (cnt = 0;cnt < 20;++cnt)
106 if ((tfd = open(mktemp(strcpy(tmpname,TMP_BUG)),O_WRONLY | O_CREAT | O_EXCL,0664)) >= 0) {
107 while ((cnt = read(fileno(stdin),bfr,sizeof(bfr))) != ERR && cnt)
108 write(tfd,bfr,cnt);
109 close(tfd);
3b28bda3
MK
110 return;
111 }
771d5f21 112 error("unable to make copy using %s.\n",tmpname);
b7520dd0
RC
113}
114
115/*
771d5f21
KB
116 * logit --
117 * log this run of the bugfiler
b7520dd0 118 */
771d5f21
KB
119static
120logit()
80765673 121{
771d5f21
KB
122 struct timeval tp;
123 struct timezone tzp;
124 char *ctime();
80765673 125
771d5f21
KB
126 if (gettimeofday(&tp,&tzp))
127 error("unable to get time of day.",CHN);
128 fprintf(stderr,"\n>>> BUGFILER <<<\n\t%s",ctime(&tp.tv_sec));
b7520dd0 129}