aboutsummaryrefslogtreecommitdiff
path: root/coreutils
diff options
context:
space:
mode:
Diffstat (limited to 'coreutils')
-rw-r--r--coreutils/hostid.c28
-rw-r--r--coreutils/logname.c40
-rw-r--r--coreutils/tty.c42
-rw-r--r--coreutils/wc.c162
-rw-r--r--coreutils/whoami.c44
-rw-r--r--coreutils/yes.c41
6 files changed, 357 insertions, 0 deletions
diff --git a/coreutils/hostid.c b/coreutils/hostid.c
new file mode 100644
index 000000000..f8d5862ae
--- /dev/null
+++ b/coreutils/hostid.c
@@ -0,0 +1,28 @@
1/*
2 * Mini hostid implementation for busybox
3 *
4 * Copyright (C) 2000 Edward Betts <edward@debian.org>.
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 *
20 */
21
22#include "internal.h"
23#include <stdio.h>
24
25extern int hostid_main(int argc, char **argv) {
26 printf ("%lx\n", gethostid());
27 exit( TRUE);
28}
diff --git a/coreutils/logname.c b/coreutils/logname.c
new file mode 100644
index 000000000..5c8275ab4
--- /dev/null
+++ b/coreutils/logname.c
@@ -0,0 +1,40 @@
1/*
2 * Mini logname implementation for busybox
3 *
4 * Copyright (C) 2000 Edward Betts <edward@debian.org>.
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 *
20 */
21
22#include "internal.h"
23#include <stdio.h>
24
25static const char logname_usage[] = "logname\n\n"
26"Print the name of the current user.\n";
27
28extern int logname_main(int argc, char **argv) {
29 char *cp;
30
31 if (argc > 1) usage (logname_usage);
32
33 cp = getlogin ();
34 if (cp) {
35 puts (cp);
36 exit (TRUE);
37 }
38 fprintf (stderr, "%s: no login name\n", argv[0]);
39 exit (FALSE);
40}
diff --git a/coreutils/tty.c b/coreutils/tty.c
new file mode 100644
index 000000000..83abaffb5
--- /dev/null
+++ b/coreutils/tty.c
@@ -0,0 +1,42 @@
1/*
2 * Mini tty implementation for busybox
3 *
4 * Copyright (C) 2000 Edward Betts <edward@debian.org>.
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 *
20 */
21
22#include "internal.h"
23#include <stdio.h>
24#include <sys/types.h>
25
26static const char tty_usage[] = "tty\n\n"
27"Print the file name of the terminal connected to standard input.\n"
28"\t-s\tprint nothing, only return an exit status\n";
29
30extern int tty_main(int argc, char **argv) {
31 char *tty;
32
33 if (argc > 1) {
34 if (argv[1][0] != '-' || argv[1][1] != 's') usage (tty_usage);
35 }
36 else {
37 tty = ttyname (0);
38 if (tty) puts (tty);
39 else puts ("not a tty");
40 }
41 exit (isatty (0) ? TRUE : FALSE);
42}
diff --git a/coreutils/wc.c b/coreutils/wc.c
new file mode 100644
index 000000000..a1e2fca56
--- /dev/null
+++ b/coreutils/wc.c
@@ -0,0 +1,162 @@
1/*
2 * Mini wc implementation for busybox
3 *
4 * by Edward Betts <edward@debian.org>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 *
20 */
21
22#include "internal.h"
23#include <stdio.h>
24
25static const char wc_usage[] = "wc [OPTION]... [FILE]...\n\n"
26"Print line, word, and byte counts for each FILE, and a total line if\n"
27"more than one FILE is specified. With no FILE, read standard input.\n"
28"\t-c\tprint the byte counts\n"
29"\t-l\tprint the newline counts\n"
30"\t-L\tprint the length of the longest line\n"
31"\t-L\tprint the length of the longest line\n"
32"\t-w\tprint the word counts\n";
33
34static int total_lines, total_words, total_chars, max_length;
35static int print_lines, print_words, print_chars, print_length;
36
37void print_counts (int lines, int words, int chars, int length,
38 const char *name) {
39 char const *space = "";
40 if (print_lines) {
41 printf ("%7d", lines);
42 space = " ";
43 }
44 if (print_words) {
45 printf ("%s%7d", space, words);
46 space = " ";
47 }
48 if (print_chars) {
49 printf ("%s%7d", space, chars);
50 space = " ";
51 }
52 if (print_length)
53 printf ("%s%7d", space, length);
54 if (*name)
55 printf (" %s", name);
56 putchar ('\n');
57}
58
59static void wc_file(FILE *file, const char *name)
60{
61 int lines, words, chars, length;
62 int in_word = 0, linepos = 0;
63 int c;
64 lines = words = chars = length = 0;
65 while ((c = getc(file)) != EOF) {
66 chars++;
67 switch (c) {
68 case '\n':
69 lines++;
70 case '\r':
71 case '\f':
72 if (linepos > length)
73 length = linepos;
74 linepos = 0;
75 goto word_separator;
76 case '\t':
77 linepos += 8 - (linepos % 8);
78 goto word_separator;
79 case ' ':
80 linepos++;
81 case '\v':
82 word_separator:
83 if (in_word) {
84 in_word = 0;
85 words++;
86 }
87 break;
88 default:
89 linepos++;
90 in_word = 1;
91 break;
92 }
93 }
94 if (linepos > length)
95 length = linepos;
96 if (in_word)
97 words++;
98 print_counts (lines, words, chars, length, name);
99 total_lines += lines;
100 total_words += words;
101 total_chars += chars;
102 if (length > max_length)
103 max_length = length;
104 fclose(file);
105 fflush(stdout);
106}
107
108int wc_main(int argc, char **argv) {
109 FILE *file;
110 total_lines = total_words = total_chars = max_length = 0;
111 print_lines = print_words = print_chars = print_length = 0;
112
113 while (--argc && **(++argv) == '-') {
114 while (*++(*argv))
115 switch (**argv) {
116 case 'c':
117 print_chars = 1;
118 break;
119 case 'l':
120 print_lines = 1;
121 break;
122 case 'L':
123 print_length = 1;
124 break;
125 case 'w':
126 print_words = 1;
127 break;
128 default:
129 usage (wc_usage);
130 }
131 }
132
133 if (!print_lines && !print_words && !print_chars && !print_length)
134 print_lines = print_words = print_chars = 1;
135
136 if (argc == 0) {
137 wc_file(stdin, "");
138 exit(TRUE);
139 }
140 else if (argc == 1) {
141 file = fopen(*argv, "r");
142 if (file == NULL) {
143 perror(*argv);
144 exit(FALSE);
145 }
146 wc_file(file, *argv);
147 }
148 else {
149 while (argc-- > 0 && *argv != '\0' && strlen(*argv)) {
150 file = fopen(*argv, "r");
151 if (file == NULL) {
152 perror(*argv);
153 exit(FALSE);
154 }
155 wc_file(file, *argv);
156 argv++;
157 }
158 print_counts (total_lines, total_words, total_chars,
159 max_length, "total");
160 }
161 exit(TRUE);
162}
diff --git a/coreutils/whoami.c b/coreutils/whoami.c
new file mode 100644
index 000000000..7fd5d01b2
--- /dev/null
+++ b/coreutils/whoami.c
@@ -0,0 +1,44 @@
1/*
2 * Mini whoami implementation for busybox
3 *
4 * Copyright (C) 2000 Edward Betts <edward@debian.org>.
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 *
20 */
21
22#include "internal.h"
23#include <stdio.h>
24#include <pwd.h>
25
26static const char whoami_usage[] = "whoami\n\n"
27"Print the user name associated with the current effective user id.\n"
28"Same as id -un.\n";
29
30extern int whoami_main(int argc, char **argv) {
31 struct passwd *pw;
32 uid_t uid;
33
34 if (argc > 1) usage (whoami_usage);
35
36 uid = geteuid ();
37 pw = getpwuid (uid);
38 if (pw) {
39 puts (pw->pw_name);
40 exit (TRUE);
41 }
42 fprintf (stderr, "%s: cannot find username for UID %u\n", argv[0], (unsigned) uid);
43 exit (FALSE);
44}
diff --git a/coreutils/yes.c b/coreutils/yes.c
new file mode 100644
index 000000000..96d6257d0
--- /dev/null
+++ b/coreutils/yes.c
@@ -0,0 +1,41 @@
1/*
2 * Mini yes implementation for busybox
3 *
4 * Copyright (C) 2000 Edward Betts <edward@debian.org>.
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 *
20 */
21
22#include "internal.h"
23#include <stdio.h>
24
25extern int yes_main(int argc, char **argv) {
26 int i;
27 if (argc == 1)
28 while (1)
29 if (puts ("y") == EOF) {
30 perror ("yes");
31 exit(FALSE);
32 }
33
34 while (1)
35 for (i = 1; i < argc; i++)
36 if (fputs (argv[i], stdout) == EOF || putchar (i == argc - 1 ? '\n' : ' ') == EOF) {
37 perror ("yes");
38 exit(FALSE);
39 }
40 exit(TRUE);
41}