summaryrefslogtreecommitdiff
path: root/package/sash/src/cmd_uclinux.c
blob: bf66c45294b33433baa4504547d2d3b13bff7c96 (plain)
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

#include "sash.h"

#include <fcntl.h>
#include <sys/types.h>

#include <sys/stat.h>
#include <dirent.h>
#include <pwd.h>
#include <grp.h>
#include <time.h>
#include <unistd.h>

#if 0
char psbuf[256];
char name[40];
int pid, state;
char statec;

void
do_ps(argc, argv)
	char	**argv;
{
	int i;
	int h;
	int max;
	FILE * f;
	DIR * d;
	struct dirent * de;
	int l;
	
	printf("  PID TTY STAT  TIME COMMAND\n");
	
	
	d = opendir("/proc");
	if (!d)
		return;
	
	while (de = readdir(d)) {
		for(i=0;i<strlen(de->d_name);i++)
			if (!isdigit(de->d_name[i]))
				goto next;
		
		sprintf(psbuf, "/proc/%s/stat", de->d_name);
		h = open(psbuf, O_RDONLY);
		
		if (h==-1)
			continue;
			
		l = read(h, psbuf, 255);
		if (l<=0) {
			perror("Unable to read status");
			close(h);
			continue;
		}
		
		psbuf[l] = '\0';
		psbuf[255] = '\0';
		
		
		if (sscanf(psbuf, 
			"%d %s %c",
			&pid, name, &statec)<3)
			{
			perror("Unable to parse status");
			close(h);
			continue;
		}
		
		state = statec;
		
		close(h);
		
		sprintf(psbuf, "/proc/%s/cmdline", de->d_name);
		h = open(psbuf, O_RDONLY);
		
		if (h == -1) {
			perror("Unable to open cmdline");
			continue;
		}
		
		l = read(h, psbuf, 255);
		if (l < 0) {
			perror("Unable to read cmdline");
			close(h);
			continue;
		}
		
		close(h);
		
		psbuf[255] = psbuf[l] = '\0';
		
		printf("%5d %3s %c     --:-- %s\n", pid, "", state, psbuf);
	next:
	}
	
	closedir(d);
}
#endif

void
do_cat(argc, argv)
	char	**argv;
{
	int	fd;
	char	*name;
	size_t	l;
	char	buf[256];

	while (argc-- > 1) {
		if (intflag) {
			return;
		}
		name = *(++argv);

		fd = open(name, O_RDONLY);
		if (fd < 0) {
			perror(name);
			return;
		}

		while ((l = read(fd, buf, sizeof(buf))) > 0) {
			fwrite(buf, 1, l, stdout);
		}
		close(fd);
	}
}