File: huh.c

package info (click to toggle)
orville-write 2.55-2.2
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 556 kB
  • ctags: 317
  • sloc: ansic: 3,494; sh: 2,927; makefile: 155
file content (132 lines) | stat: -rw-r--r-- 2,443 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#include "write.h"
#include <pwd.h>
#include <sys/stat.h>

int keep= 0, silent= 0;


/* MY_DIR - Return name of my home directory, or NULL if I can't find it.
 */

char *my_dir()
{
char *r;
struct passwd *pwd;

    /* Try the environment variable first */
    if ((r= getenv("HOME")) != NULL) return r;

    /* Try the password file */
    setpwent();
    pwd= getpwuid(getuid());
    endpwent();
    if (pwd != NULL) return pwd->pw_dir;

    /* Give up */
    return NULL;
}


/* RECORD_ON -- return true if the user has his record flag on.  We don't
 * really care, but we want to tell him about it if this is the reason he
 * hasn't any messages.
 */

int record_on()
{
struct utmp *ut;
struct wrttmp wt;
char *tty;
long pos;

    /* Open the utmp file */
    setutent();

    /* Open the wrttmp file */
    if (init_wstream(O_RDONLY)) return 1;

    /* Get tty name */
    if (getdevtty()) exit(1);
    tty= mydevname+5;

    /* Find our entry in the utmp file */
    if ((ut= find_utmp(tty)) == NULL || ut->ut_name[0] == '\0') return 1;

    /* Find the entry in the wrttmp file */
    find_wrttmp(tty, ut->ut_time, &wt, &pos);

    /* Close utmp file */
    endutent();

    return (wt.wrt_record != 'n');
}


int main(int argc, char **argv)
{
char fname[200];
char *dir;
FILE *fp;
int ch;
int i,j;
struct stat st;

    progname= leafname(argv[0]);
    readconfig(NULL);

    for (i= 1; i < argc; i++)
    {
	if (argv[i][0] == '-')
	    for (j= 1; argv[i][j] != '\0'; j++)
		switch(argv[i][j])
		{
		case 'k':
			keep= 1;
			break;
		case 's':
			silent= 1;
		default:
			goto usage;
		}
	else
	    goto usage;
    }

    if ((dir= my_dir()) == NULL)
    {
	fprintf(stderr,"%s: cannot find your home directory\n",progname);
	exit(1);
    }

    sprintf(fname,"%.180s/.lastmesg",dir);

    if ((fp= fopen(fname,"r")) == NULL)
    {
	printf("No recorded messages%s.\n", (silent || record_on()) ? "":
	    " (do \042mesg -r y\042 to record future messages)");
	exit(0);
    }

    if (fstat(fileno(fp), &st) || st.st_uid != getuid())
    {
    	printf("Incorrect ownership of %s\n",fname);
    	exit(1);
    }

    /* Unlink the file -- do it now to minimize conflicts with incoming
     * messages.
     */
    if (!keep) unlink(fname);

    /* Display the file */
    if (!silent)
	while ((ch= getc(fp)) != EOF)
	{
	    if (ch != '\007') putchar(ch);
	}

    exit(0);

usage: fprintf(stderr,"usage: %s [-ks]\n", progname);
    exit(1);
}