My friend run this code it worked but when I ran this code in Linux I got a segmentation fault( He runs this code in Linux too) This program makes some file operations like copying, deleting, creating, at writing... Here is the code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
int main(int argc, char* argv[]) {
//if(strcmp(argv[1],"--help") == 0)
FILE *fp;
if(strcmp(argv[argc-1], "/h") == 0)
{
if(strcmp(argv[1], "create") == 0)
printf("To create a file do \"main create filename.txt\"\n");
else if(strcmp(argv[1], "remove") == 0)
printf("To remove a file do \"main remove filename.txt\"\n");
else if(strcmp(argv[1], "rename") == 0)
printf("To rename a file do \"main rename oldname.txt newname.txt\"\n");
else if(strcmp(argv[1], "copy") == 0)
printf("To copy a file do \"main copy source.txt destination.txt\"\n");
else if(strcmp(argv[1], "move") == 0)
printf("To move a file do \"main move filename.txt ../files/filename.txt\"\n");
else if(strcmp(argv[1], "append") == 0)
printf("To append a file do \"main append filename.txt helloworld!\"\n");
else if(strcmp(argv[1], "writeat") == 0)
printf("To write at a specific location to file do \"main writeat location filename.txt\"\n");
else if(strcmp(argv[1], "clear") == 0)
printf("To clear a file do \"main clear filename.txt\"\n");
else if(strcmp(argv[1], "display") == 0)
printf("To display a file do \"main display filename.txt\"\n");
return 0;
}
if(strcmp(argv[1], "create") == 0) {
fp = fopen(argv[2], "w");
}
else if(strcmp(argv[1], "remove") == 0) {
remove(argv[2]);
}
else if(strcmp(argv[1], "rename") == 0) {
rename(argv[2], argv[3]);
}
else if(strcmp(argv[1], "copy") == 0) {
char ch;
FILE *dest;
fp = fopen(argv[2], "r");
dest = fopen(argv[3], "w");
while ((ch = fgetc(fp)) != EOF)
fputc(ch, dest);
}
else if(strcmp(argv[1], "move") == 0) {
char ch;
FILE *dest;
fp = fopen(argv[2], "r");
dest = fopen(argv[3], "w");
while ((ch = fgetc(fp)) != EOF)
fputc(ch, dest);
fclose(fp);
fp = fopen(argv[2], "w");
remove(argv[2]);
}
else if(strcmp(argv[1], "append") == 0) {
char *ap;
ap = argv[3];
FILE *dest;
dest = fopen(argv[2], "a");
int i;
for( i = 0; i < strlen(ap); i )
{
fputc(argv[3][i], dest);
}
}
else if(strcmp(argv[1], "writeat") == 0) {
char *ap;
ap = argv[4];
FILE *dest;
dest = fopen(argv[2], "r b");
fseek(dest, strtol(argv[3], NULL, 10), SEEK_SET);
int i;
for( i = 0; i < strlen(ap); i )
{
fputc(argv[4][i], dest);
}
}
else if(strcmp(argv[1], "clear") == 0) {
fp = fopen(argv[2], "w");
}
else if(strcmp(argv[1], "display") == 0) {
char ch;
fp = fopen(argv[2], "r");
do {
/* Read single character from file */
ch = fgetc(fp);
/* Print character read on console */
putchar(ch);
} while(ch != EOF); /* Repeat this if last read character is not EOF */
/* Done with this file, close file to release resource */
fclose(fp);
}
}
So do you see a bug or a point to be improved. An important point I'm trying to make without using system calls.
CodePudding user response:
Please send input data (arguments)
The error means that you are trying to get into memory that does not belong to you, that is not allocated.
There are suspicions that you are passing 2 parameters to the program with 1 argument being "rename" , but the condition with this parameter includes 3 arguments:
else if(strcmp(argv[1], "rename") == 0) {
rename(argv[2], argv[3]);
}
CodePudding user response:
How argc amd **argv works:
argv[0] will always be the name of your executable, for example (on Unix/Linux):
$ ./file-manager /h create
argv[0] = "./file-manager"
argv[1] = "/h"
argv[2] = "create"
Always check the value of argc to check that the user has provided a valid number of command line arguments before proceeding.
if(strcmp(argv[argc-1], "/h") == 0)
{
if(strcmp(argv[1], "create") == 0)
...
Seems wrong to me:
argv[0] the name of the executable, given from the command line
argv[1] should be "/h"
argv[2] would be the command "create" and etc.
I also suggest to check that argc is >=2 before proceeding, or exit with an error displayed to the user.
The rest of the program, always check that fopen(...) succeds:
FILE *dest;
fp = fopen(argv[2], "r");
if (fp == NULL) {
/* fopen failed, exit program with some informative error
message to user */
exit(-1);
}
In case that fp == NULL it will cause your program to crash.
