Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
I am trying to implement a LINUX shell in C but when I am trying to run a command like this : sort -u < in.txt > out.txt it says : sort: cannot read: '<': No such file or directory . I also tried other commands like ls -l > out.txt etc but it keep telling me the same thing. Here is my code :
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/wait.h>
#include <fcntl.h>
#include <sys/types.h>
#define LENGTH 1024
int main(){
char line[LENGTH];
char* argv[100];
char* path= "/bin/";
char fullPath[30];
int option;
char* inputFile;
char* outputFile;
while(1){
printf("mysh3>");
if(!fgets(line,LENGTH,stdin)){
break;
}
size_t length = strlen(line);
if(line[length-1]=='\n')
line[length-1] = '\0';
}
char* token;
token = strtok(line," ");
int i = 0;
while(token!=NULL){
argv[i]=token;
token = strtok(NULL," ");
i++;
}
if(argv[1]==">"){
option=1;
outputFile=argv[2];
}
else if(argv[2]==">"){
option = 1;
outputFile=argv[3];
}
else if(argv[1]=="<" && argv[3]==">"){
option=2;
inputFile=argv[2];
outputFile=argv[4];
}
else if(argv[2]=="<" && argv[4]==">"){
option=2;
inputFile=argv[3];
outputFile=argv[5];
}
else if(argv[1]=="<" && argv[3]==">>"){
option=3;
inputFile=argv[2];
outputFile=argv[4];
}
else if(argv[2]=="<" && argv[4]==">>"){
option=3;
inputFile=argv[3];
outputFile=argv[5];
}
argv[i]=NULL;
strcpy(fullPath, path);
strcat(fullPath, argv[0]);
for(int i = 0; i < strlen(fullPath); i++){
if(fullPath[i]=='\n'){
fullPath[i]='\0';
}
}
int ret=forkEx2(argv,inputFile,outputFile,fullPath,option);
if(ret==-1) {
return -1;
}
int forkEx2(char* argv[],char* inputFile,char* outputFile,char
fullPath[],int option){
int fd;
pid_t pid,waitPid;
pid = fork();
if(pid<0){
perror("ERROR: Fork failed.\n");
return -1;
}
else if(pid==0){
if(option==1){
fd=open(outputFile,O_CREAT | O_TRUNC | O_WRONLY, 0600); //if the file does not exist the system create it. Clear all data from the file. Open the file for write only.Store its file descriptor in fd
dup2(fd,STDOUT_FILENO); //replace the standar out with the file
close(fd);
}
else if(option==2){
fd=open(inputFile,O_RDONLY,0600); //open the file for read only
dup2(fd,STDIN_FILENO); //replace the standar in with the file
close(fd);
fd=open(outputFile,O_CREAT | O_TRUNC | O_WRONLY, 0600); //if the file does not exist the system create it. Clear all data from the file. Open the file for write only
dup2(fd,STDOUT_FILENO); //replace the standar out with the file
close(fd);
}
else if(option==3){
fd=open(inputFile,O_RDONLY,0600); //open the file for read only
dup2(fd,STDIN_FILENO); //replace the standar in with the file
close(fd);
fd=open(outputFile, O_APPEND | O_WRONLY, 0600); //append at the end of the file. Open the file for write only
dup2(fd,STDOUT_FILENO); //replace the standar out with the file
close(fd);
}
execvp(fullPath,argv);
perror("ERROR: Child should never arrive here.\n");
}
else{
waitPid = wait(&waitPid);
if (waitPid == -1) {
perror("ERROR: Waitpid failed.\n");
return -1;
}
printf("Parent: Finished pid %d.\n", waitPid);
}
}
}
Any help is appreciated.
let's take that case (it's the same in all cases):
else if(argv[2]=="<" && argv[4]==">"){
option=2;
inputFile=argv[3];
outputFile=argv[5];
}
First, you're comparing strings using ==: that doesn't work. You have to use strcmp or you're comparing the pointers.
Then, you're extracting input file & output file all right, but you forget to remove the redirection arguments from argv. You have to filter them out (by shifting the rest of the argument array each time you encounter one redirection argument)
Aside: you should initialize your argv array or you may meet undefined behaviour if there are not enough arguments.
Related
So im trying to redirect the I/O to read command from file then when user runs the output command it will print the compiled command to output file.
For example on the terminal:
./run 2 < test.txt // This would take file using dup and take the input
Then when you want to output the compile:
./run 1 > output.txt // and it would put into an output file
So far i know how to output to a file but my problem is with the input. how do i get the command from the file using the dup2() function? I tried researching this but no luck.
#include <stdlib.h>
#include <stdio.h>
#include <fcntl.h>
#include <string.h>
#include <unistd.h>
char inputForOutput[100];
void functionOutput(int argc, char **argv){
int ofd; //Init of file desc.
ofd = open(argv[1], O_CREAT|O_TRUNC|O_WRONLY);
dup2(ofd, 1);//Duplicates to stdout
system("ls");//Copies commnd given to output_file
}
//Function is called when argument number is == 1
void functionInput(int argc, char **argv){
FILE *ifd;
printf("\n %s \n ", argv[2]);
ifd = fopen(argv[2] , "r");
if (ifd == NULL){
perror("No file found");
exit(1);
}
fscanf(ifd,"%s",inputForOutput);
printf("\n**%s**\n",inputForOutput);
}
int main(int argc, char **argv)
{
int output;
int input;
output = strcmp("1", argv[1]);
input = strcmp("2" ,argv[1]);
if (output == 0 ) { //Fail safe for number of arguments
functionOutput(argc, argv);
}
else if ( input == 0){
functionInput(argc, argv);
}
else{
fprintf(stderr, "How to use: %s function output_file\n", argv[0]); // FAIL SAFE IF INPUT DOES NOT MATCH BOTH FUNCTIONS
}
return 0;
}
To redirect input and output, use this format
myprogram > out.txt < in.txt //read from in.txt, write to out.txt
myprogram < in.txt > out.txt //read from in.txt, write to out.txt
myprogram < in.txt //redirect stdin only
myprogram > out.txt //redirect stdout only
myprogram //no redirection
...
This should work with any program. Example:
int main(void)
{
char buf[1000];
if(fgets(buf, sizeof(buf), stdin))
printf("write: %s\n", buf);
return 0;
}
To redirect stdin/stdout in the program, use the standard method
freopen("output.txt", "w", stdout);
printf("Testing...");
fclose(stdout);
freopen("input.txt", "r", stdin);
char buf[100];
fgets(buf, sizeof(buf), stdin);
fclose(stdin);
Alternatively, set FILE *fin = stdin; FILE* fout = stdout; to redirect the opposite way.
Next, to write a program using argv elements, always test argc first. The code below shows an example.
#include <stdio.h>
#include <string.h>
int redirect(int argc, char **argv, int *index)
{
//no more redirection!
if(*index >= argc)
return 1;
//not enough parameters
if(*index + 1 >= argc)
{
printf("wrong usage\n");
return 0;
}
if(strcmp(argv[*index], "<") == 0)
{
*index++; //next parameter is to redirect input
if(!freopen(argv[*index], "r", stdin))
printf("error, redirect input failed");
}
else if(strcmp(argv[*index], ">") == 0)
{
*index++; //next parameter is to redirect output
if(!freopen(argv[*index], "w", stdout))
printf("error, redirect output failed");
}
else
{
printf("wrong usage\n");
return 0;
}
return 1;
}
int main(int argc, char **argv)
{
int index = 1;
if(!redirect(argc, argv, &index))
return 1;
if(!redirect(argc, argv, &index))
return 1;
//read
char buf[1000];
if(fgets(buf, sizeof(buf), stdin))
{
//write
printf("write: %s\n", buf);
}
fclose(stdin);
fclose(stdout);
return 0;
}
With functionOutput() you have a good first attempt at capturing the output of a system command to a file. Actually, that is the function called when the first argument is 1, so you might want to update your comment. Also, you're creating a file with the name stored in argv[1], which we already know is 1 so it's probably not doing what you expect, and you probably want:
ofd = open(argv[2], O_CREAT|O_TRUNC|O_WRONLY);
With functionInput() you're reading the first non-whitespace entry from the file. If you're telling it to read the file which you output using the functionOutput() function, that is likely to be (some of) the name of the first file which was listed by ls.
I'm finding it unclear what you're wanting to do which isn't that. If you want to find out what the command was which you ran to generate the output, that information is not available from the file itself, because you didn't write it there. If that's what you want, you may want to consider writing the command as the first line of the file, followed by the output. Then when you read it, you can assume that the first line is the command run, followed by the output of that command.
If I understand your question, and you want to run your program in essentially two different modes, (1) you want to take input if there is input to be taken on stdin; and (2) if there is no input waiting, you want to do an output, then select/pselect or poll are what you are looking for.
For example select allows you to check whether there is input ready to be read on a file descriptor (or set of descriptors) and it will return the number of descriptors with input waiting (or -1 and set errno on error). You could simply use the STDIN_FILENO (a/k/a fd 0) to check if there is input on stdin, e.g.
#include <stdio.h>
#include <unistd.h> /* for STDIN_FILENO */
#include <sys/select.h> /* for pselect */
int input (int filedes)
{
fd_set set;
/* declare/initialize zero timeout */
struct timespec timeout = { .tv_sec = 0 };
/* Initialize the file descriptor set. */
FD_ZERO (&set);
FD_SET (filedes, &set);
/* check whether input is ready on filedes */
return pselect (filedes + 1, &set, NULL, NULL, &timeout, NULL);
}
int main (void)
{
if (input (STDIN_FILENO))
puts ("doing input routine");
else
puts ("doing output routine");
return 0;
}
(note: from the man page "select() uses a timeout that is a struct timeval (with seconds and microseconds), while pselect() uses a struct timespec (with seconds and nanoseconds).")
Example Use/Output
$ ./bin/select_peekstdin < file
doing input routine
$ ./bin/select_peekstdin
doing output routine
follwing code has written to open a file and write data to terminal using sysyem calls in linux.
To read the value of the file descriptor (fd) it should assign a value. As we know in if else statement, from if part else part or else if part one part will implement at a time. So according to following code fd will have a value only at else if line. But when I pass a file name and run this program it opens the file. File opening is happen in while loop from read(() system call. But while loop is in else part and since file descriptor can't have any value theoretically. So how does the read function get recognize the file exactly? This is confusing me.
#include <stdio.h>
#include <fcntl.h>
#include <stdlib.h>
#include <unistd.h>
#define SIZE 10
int main(int argc, char *argv[])
{
int fd,n;
char buff[SIZE];
if(argc != 2)
{
printf("USAGE : %s\n",argv[0] );
exit(1);
}
else if ((fd = open(argv[1],0)) == -1)
{
perror("STATUS");
exit(1);
}
else
{
while((n = read(fd,buff,SIZE)) > 0)
{
write(1,buff,SIZE);
}
close(fd);
}
}
Following happens here:
Let's suppose the program is started with xyz.txt on the command line and let's suppose the xyz.txt file does exist:
if(argc != 2)
{
// we don't get here because argc == 2
printf("USAGE : %s\n",argv[0] );
exit(1);
}
else if ((fd = open(argv[1],0)) == -1) // the statement in the if clause will therefore
// be executed, fd will be something different
// from -1 because open succeeded
{
perror("STATUS"); // therefore we dont ge here either
exit(1);
}
else
{ // instead we get here and
while((n = read(fd,buff,SIZE)) > 0) // everything works as expected
{
write(1,buff,SIZE);
}
close(fd);
}
I'm trying to write a C program, that make user able to write stuff in a file. My Problem is that after making and running the program the file stay empty ?? any idea how can I solve this.
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
// the user should give a file to write the file
int main (int argc , char**argv)
{
int fd; // file descriptor
char ret; // the character
int offset;
if(argc != 2) {
printf("You have to give the name or the path of the file to work with \n");
printf("Exiting the program \n")
return -1;
}
fd = open (argv[1], O_WRONLY/*write*/|O_CREAT/*create if not found */, S_IRUSR|S_IWUSR/*user can read and write*/);
if (fd == -1) {
printf("can'T open the file ");
return -1;
}
printf("At wich position you want to start ");
scanf("%d",&offset);
lseek(fd,offset,SEEK_SET);
while(1) {
ret = getchar();
if(ret == '1') {
printf("closing the file");
close (fd);
return 1;
}
else
write (fd,red, sizeof(char));
}
return 0;
}
thanks in advance for you help.
I have made some changes,this should work:
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
int main (int argc , char**argv)
{
int fd; // file descriptor
char ret; // the character
int offset;
if(argc != 2){
printf("You have to give the name or the path of the file to work with \n");
printf("Exiting the program \n"); **//There was ';' missing here**
return -1;
}
fd = open (argv[1], O_WRONLY|O_CREAT,S_IRUSR|S_IWUSR);
if (fd == -1) {
printf("can'T open the file ");
return -1;
}
printf("At wich position you want to start ");
scanf("%d",&offset);
lseek(fd,offset,SEEK_SET);
while(1){
ret = getchar();
if(ret == '1'){
printf("closing the file");
close (fd);
return 1;
}
else
write (fd,&ret, sizeof(char)); **//red has been changed to &ret**
}
return 0;
}
One error I can notice, the call of write function:
write (fd,red, sizeof(char));
should be:
write (fd, &red, sizeof(char));
You forgot & before red, write need address.
syntax of write: int write( int handle, void *buffer, int nbyte );
This will cause an undefined behavior in your code at run time
Edit: in write function you are using red that is not defined, I think it should be ret variable in your code. correct it as write (fd, &ret, sizeof(char));
second, you forgot ; after printf("Exiting the program \n") in if, but I also think its mistake while posting question as you says you are getting run time error.
side note: If you are using gcc compiler then you can use gcc -Wall -pedantic to generate warnings
It should be:
write (fd,&ret, sizeof(char));
write takes the pointer to the memory position, and since ret is a single char, you need to pass a pointer to it.
So this is one of the first programs I've ever used some self created error checks however for some reason when I compile this and run it using:
./file test1.txt test2.txt 10
I get absolutely an error suggesting that the output file exists and I've checked the file and it doesn't even when I change the name of the output file (second argument) I get nothing. Anyone who can help? I've been racking my brain for ages now. This is a UNIX homework assignment I'm compiling and running in Gentoo. I have it running in a VB and have a linked folder between my windows and linux OS's.
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdlib.h>
#define BUFFT 25
int main (int argc, char *argv[])
{
int count;
int readin;
int writeout;
printf ("This program was called \"%s\".\n",argv[0]);
if (argc > 1)
{
for (count = 1; count < argc; count++)
{
printf("argv[%d] = %s\n", count, argv[count]);
}
}
else
{
perror("The command had no arguments.\n");
exit(-1);
}
// check correct number of arguments parsed //
if (argc == 4)
{
printf("There are the correct number of arguments(4)\n");
}
else
{
perror("Not enough arguments! please try again \n");
exit(-1);
}
//Check original file is there//
int openFD = open(argv[1], O_RDWR);
if (openFD <0)
{
perror("Error unable to read file \n");
exit(-1);
}
//Check existence of output file, if it doesn't exist create it//
int CheckFile = open(argv[2], O_RDONLY);
if (CheckFile < 0)
{
perror("Error output file already exists \n");
exit(-1);
}
else
{
int CheckFile = open(argv[2], O_CREAT);
printf("The file has successfully been created \n");
}
//Create buffer
int bufsize = atoi(argv[3]);
char *calbuf;
calbuf = calloc(bufsize, sizeof(char));
//Read text from original file and print to output//
readin = read(openFD, calbuf, BUFFT);
if (readin < 0){
perror("File read error");
exit(-1);
}
writeout = write(openFD,bufsize,readin);
if (writeout <0){
perror("File write error");
exit(-1);
}
return 0;
}
The open call for HANDLE CheckFile is printing Error File Exists. This is your problem. You are printing out the wrong statement when Output File Is Not Found and moreover you are exiting which prevents the code to create any.
int CheckFile = open(argv[2], O_RDONLY);
if (CheckFile < 0)
{
//Means the file doesn't exist
int CheckFile = open(argv[2], O_CREAT);
// Check for errors here
}
And why are you trying to do this::
writeout = write(openFD,bufsize,readin);
when your HANDLE TO OUTPUT FILE IS CheckFile
int CheckFile = open(argv[2], O_RDONLY);
if (CheckFile < 0)
{
perror("Error output file already exists \n");
A negative return from open means that the file could not be opened, most likely because it does not exist ... it does not mean that the file already exists. Failing to open the input file certainly does not mean that the output file already exists. Please check your code more carefully for obvious errors, e.g.,
int CheckFile = open(argv[2], O_CREAT);
printf("The file has successfully been created \n");
Here you don't check the return code.
Take a look at this fragment of your code:
int CheckFile = open(argv[2], O_RDONLY);
if (CheckFile < 0)
{
perror("Error output file already exists \n");
exit(-1);
}
You are saying trying to open a file in READ ONLY MODE. From what I read in your question, it's not an error if the file doesn't exist, but in the code you are validating the opposite, if the file doesn't exists, throw an error (in fact your message error is incorrect here).
Double check your logic and you will find your solution.
Got to use lseek function in this program below... Program is simply copying file (that already exist). I wanned to copy the existing file with the chars from the end of file
for example: Sorce_File.txt contains:"1 2 3" after copy Target_File.txt contains:"3 2 1"
I'm pretty sure it's simple problem but couldn't find out since 2 days how to do it
#include <fcntl.h>
#include <stdio.h>
#define MAX 512
int main(int argc, char* argv[]){
char buf[MAX];
int desc_sorc, desc_targ;
int lbajt;
if (argc<3){
argv[0];
exit(1);
}
desc_sorc = open(argv[1], O_RDONLY);
if (desc_sorc == -1){
}
desc_targ = creat(argv[2], 0640);
if (desc_targ == -1){
exit(1);
}
while((lbajt = read(desc_sorc, buf, MAX)) > 0){
if (lbajt == -1) {
perror("position error");
exit(1);}
if (write(desc_targ, buf, lbajt) == -1)
{
exit(1);
}
}
if (lbajt == -1){
exit(1);
}
if (close(desc_sorc) == -1 || close(desc_targ) == -1){
exit(1);
}
exit(0);
}
int desc_sorc, desc_targ;
You don't actually initialize these to anything. Anywhere.
EDIT: Now that you've fixed that, have you actually tested it again?
You are missing the equivalent of strrev(...) in there to reverse the string you write out as well starting from the end of the source file and reading backwards or writing from the end of the target file back to the beginning.
The actual implementation is left as an exercise to the reader.