-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxargs.c
104 lines (97 loc) · 2.28 KB
/
xargs.c
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
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <errno.h>
#include <getopt.h>
// https://github.com/ishworgiri1999
//Basic xargs implemented on C Program
int status = EXIT_SUCCESS;
void work(char **args, int tflag)
{
pid_t pid;
pid = fork();
if (pid < 0)
{
fprintf(stderr, "forkfailed");
}
else if (pid == 0) //child
{
if (tflag == 1) //for printing actual code
{
for (int i = 0; args[i] != NULL; i++)
{
fprintf(stderr, "%s ", args[i]);
}
fprintf(stderr, "\n");
}
//execute command
execvp(args[0], args);
fprintf(stderr, "Unknown command\n");
exit(EXIT_FAILURE);
}
else //parent
{
if (waitpid(pid, &status, 0) == -1)
{
fprintf(stderr, "wait pid failed");
}
}
}
int main(int argc, char *argv[])
{
int nflag = 0;
int tflag = 0;
int nlines = 50; //default lines
int option;
while ((option = getopt(argc, argv, "n:t")) != -1)
{
switch (option)
{
case 'n':
nflag = 1;
if (atoi(optarg))
nlines = atoi(optarg);
break;
case 't':
tflag = 1;
break;
default:
return -1;
}
}
char *args[nlines + 2]; //args for execvp
if (optind >= argc) //if no command given
args[0] = "/bin/echo";
else //if option given
args[0] = argv[optind];
size_t len = 0; //for getline
int n_read; //for getline
while (1)
{
int i;
for (i = 1; i <= nlines; i++)
{
args[i] = NULL;
if ((n_read = getline(&args[i], &len, stdin)) == -1)
{ //nothing more to read
break;
} //removing new line
args[i][strlen(args[i]) - 1] = '\0';
}
//remaining args to null
for (int j = i; j <= nlines + 1; j++)
{
args[j] = NULL;
}
//calling actual function for fork and execvp
work(args, tflag);
if (n_read <= 0)
{ //if nothing to read end
break;
}
}
return status;
}