Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
I'm writing a program that checks who the user is, if it's the correct user logged in it will upload data to my FTP server. It encrypts the data using tar, than openssl. I think that part is correct however when passing system commands to the terminal I'm having an issue with strcpy. I keep getting an error that "there are to many arguments to function strcpy. Specifically where it says "strcpy(encrypt, "tar -cf /home/%s", p);" I think it's because I'm passing the variable p to it. Could anyone assist. Thank you.
int main(int argc, char *argv[])
{
// checks to see who the user is
char *p=getenv("USER");
if (p=NULL) return EXIT_FAILURE;
printf("User is %s\n", p);
char *usr;
char *encrypt;
char *upload;
//encrypts folder using tar, then openssl AES 256 bit
strcpy(encrypt, "tar -cf /home/%s", p);
system(encrypt);
strcpy(encrypt, "openssl enc -aes-256-cbc -in FILE.NAME -out file.enc");
system(encrypt);
// uploads data to ftp server using curl
strcpy(upload, "curl -T /PATH/ SERVER_ADDRESS");
system(upload);
return 0;
}
you used strcpy incorrectly as compiler log the error
this is strcpy prototype
char * strcpy ( char * destination, const char * source );
if you want to copy from source to destination when source is a pattern ->
you need to replace strcpy with sprintf or snprintf
for more detail you can check this https://linux.die.net/man/3/snprintf
additional, you haven't allocateed for
char *usr;
char *encrypt;
char *upload;
use malloc or calloc for these things before use
Related
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 2 years ago.
Improve this question
Im writing this program in c89 that reads the files however i keep getting segmentation fault
ive run it by valgrind --leak-check=full --show-leak-kinds=all -v ./program and it pointed to the two sscanf lines leaks
to run the program, it is two file names
./program cat.txt dog.txt
and complies by a make file
I have free'd them and set to null after and i still get these issues, i have attached a picture of my [![valgrind errors here][1]][1]
int main(int argc, char* argv[]){
char* cat = (char*)malloc(12 * sizeof (char));
char* dog = (char*)malloc(12 * sizeof (char));
/*float sleepTime;*/
/*if (argc != 4)
{
printf("Error - Enter file names");
}*/
sscanf(argv[1], "%s", cat);
sscanf(argv[2], "%s", dog);
sleepTime = atof(argv[3]);
readFile(cat);
readFile(dog);
free(cat);
free(dog);
cat = NULL;
dog = NULL;
}
i tried to comment things out, however still no luck
any help would be appreciated
Ok from looking at the valgrind log aswell as reading what you wrote I gather this:
You are building with a makefile, which should set the compiler arguments dog.txt and cat.txt, which are then written cat and dog.
The valgrind log says Address 0x0 is not ... in main when calling sscanf().
This means that there is a null pointer as it is pointing to an invalid memory address, being used in the sscanf() call.
So either the make build fails to properly compile with the arguments or the memory allocation of cat and dog is not successful.
.
To check validity:
I would recommend writing some assertions or writing a function to check if a pointer is vaild and call it and check your pointers, namely cat , dog and argv[index] just before calling sscanf().
https://ptolemy.berkeley.edu/~johnr/tutorials/assertions.html
https://wiki.sei.cmu.edu/confluence/display/c/MEM10-C.+Define+and+use+a+pointer+validation+function
A good and simple example of pointer validation from the above link:
int valid(void *ptr) {
return (ptr != NULL); // 1 valid, 0 invalid
}
To fix eventual makefile issues I think we would need to see the makefile in question, but if that can't be done for some reason then these might be of interest:
Command-line arguments via Makefile
How to pass argument from Makefile to a program?
Edit:
I checked the updated valgrind log on the question and noticed that the reachable memory that is in readFile().
To fix that issue you would need to define a function that properly closes the file. As you never close the files, leaving them hanging in memory causing your reachable memory warning.
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 3 years ago.
Improve this question
Is it ok to leave the parameters unused?
I'm trying to run the function like this:
void encoder();
void encoder(int argc, FILE* inputFP, FILE* outputFP);
Is there a need for å secound function, one for dealing with stdio and one for dealing with files? When I try to run
void encoder(int argc, FILE* inputFP, FILE* outputFP); without any arguments I get errors:
error: too few arguments to function ‘void encoder(int, FILE*, FILE*)’
encoder();```
Is it ok to have unused parameters in C function?
Yes.
Still, why not have encoder() take two FILE* instead of the names, then do (pseudo code):
main(argc, argv)
FILE * fp_in, * fp_out
if argc == 2
fp_in = open argv[1], "r"
else
f = stdin
if argc == 3
fp_out = open argv[3], "w"
else
fp_out = stdout
encoder fp_in, fp_out
To answer the general question in the title - yes, it's okay not to use all the parameters passed to a function, although it may be confusing to future maintainers.
For this specific case, instead of passing an argument count, I'd recommend using special names for the input and output files to indicate standard streams. It's a common convention among *nix command line utilities to use "-" to specify reading from standard input:
xmllint --format some_ugly_wad_of_xml | vi -
The above command pretty prints some XML and then passes the pretty-printed version to vi to edit (something I've had to do a lot lately). So instead of passing an argc, just pass the filenames and examine them to determine what to do:
if ( !strcmp( inputFile, "-" ) )
inputStream = stdin;
else
inputStream = fopen( inputFile, "r" );
This way you're not leaving any parameters "unused".
It is better to open files and read all content and fetch to variables. So you can free (close) the file and dance. After close the file, freely make stuff.
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
The error says no such file or directory i moved the file to the project folder
int main(int argc, const char * argv[]) {
int i;
char singleline[150];
FILE *file;
file = fopen("test.txt", "r");
puts(singleline);
if( file == NULL ) {
perror("Error: ");}
fclose (file);
}
return 0;
}
I guess you really need help debugging why this is happening to you.
Try adding some more code to your routine to help you determine what is going on. One thing to try is to call getcwd.
#include <unistd.h>
...
char buf[PATH_MAX];
printf("cwd: %s\n", getcwd(buf, sizeof(buf)));
...
This should report to you where your program thinks it is running from.
You report you get the following output:
cwd: /Users/ahmedhossam/Library/Developer/Xcode/DerivedData/assembler-doinswpyuiekhhemczblkainroaw/Build/Products/Debug `ֿ_\377
Start with that first, and I am guessing the next steps will become obvious to you.
The reported current working directory (hence, getcwd) is not your project folder. You can copy your file to that strange directory, or you can use chdir to change your working directory to be your project folder, or you can specify the absolute path to your file as suggested below.
You can avoid the problem altogether by specifying the absolute path to your file.
file = fopen("/the/absolute/path/to/test.txt", "r");
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
while understanding the parameters of main function i.e, int argc, char* argv[]
i wrote a piece of code to understand these parameters.
#include <stdio.h>
int main(int argc,char*argv[])
{
printf("test\n");
printf("%d %c",argc,*argv[argc-1]);
return 0;
}
This prints
test
1 F
here I don't understand why there is F as output. I mean how this is executed to result in output as F ?
I read about these perameters and main function at here and here. But still I don't understand how these works.
please explain .
EDIT: as mentioned in comments if I change the code to
printf("%d %s",argc,argv[argc-1]);
Now i'm getting the whole path of the file F:\file path
so does it mean argv[0] is the location of the file in drive?
It is not defined in the C standard, but on Unix argv[0] is the name of the executable. Then argv[1] the first argument, etc. I think that this is also true, most of the time, on Microsoft's Dos and their Windowing OSes.
In simple words:
When you give your commandline the instruction to run your program, you can append some text, which can be accessed in your program.
#include <stdio.h>
int main(int argc,char*argv[])
{
printf("This is the path or name of your programm: ");
printf("%s\n", argv[0]);
if(argc > 1) {
printf("This is the first argument you gave your programm: ");
printf("%s\n", argv[1]);
}
if(argc > 2) {
printf("This is the second argument you gave your programm: ");
printf("%s\n", argv[2]);
}
return 0;
}
Try to run this example with:
<path_to_the_programm> Hallo Welt
You will see argc is an integer, which tells you how many arguments you gave the program. I hope it helped you.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
I am trying to create a call to system using custom parameters. However I think that I am incorrectly malloc-ing the size of the final char*.
So I instead get a Failed to Open source file error during the system call. Am I doing something wrong in terms of syntax?
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(void)
{
char program_name[] = "/usr/local/bin/some_program";
char argument_1[] = "foo";
char argument_2[] = "foo2";
char space[] = " ";
char *runProgram = malloc( strlen(program_name) + strlen(argument_1)+
strlen(argument_2) + 2*strlen(space) + 1);
strcpy(runProgram, program_name);
strcat(runProgram, space);
strcat(runProgram, argument_1);
strcat(runProgram, space);
strcat(runProgram, argument_2);
system(runProgram);
free(runProgram);
exit(0);
}
When I run your code, your string appears to contain exactly what it needs to to be called. I do have a suggestion to simplify building it though:
...
int ret=0;
int len = strlen(program_name) + strlen(argument_1)+ strlen(argument_2) + 2*strlen(space) + 1;
char *runProgram = malloc( len);
ret = snprintf(runProgram, len, "%s %s %s", program_name, argument_1, argument_2);
if(ret < 0)
{
//handle if truncation occurred (returns -1 for truncation)
}
if(ret >= len)
{
//use runProgram buffer
}
system(runProgram);
free(runProgram);
//exit(0);
return 0;
...
Have you verified the chmod settings on the file to be executed are correct?
chmod +x /usr/local/bin/some_program
EDIT:
To address concern in comments regarding variations in implementation/documentation for snprintf(). When discussing standard C functions, the hope would be that implementations of the same function would be equal across platforms, but for snprintf(), it appears that some implementations are more equal than others...
1) snprintf some have referred to this as Microsoft's broken implementation.
2) snprintf from skrenta.com
3) snprintf from opengroup.org.
4) snprintf from linux.die.net, (includes glibc references, macros and other comments)
Your code should work fine, printf("cmd is '%s'\n", runProgram); to check if generated command can be run in shell and produces expected results:
command string is correct
/usr/local/bin/some_program exists and has correct permissions
some_program does not fail