Command Line Argument processing in c - c

On running the below code, it stucks after displaying the argv[0], argv[1] and argv[2] line.
Further flow of code is blocked at this point, can any one help why it is stopping its execution or is it entering into an infinite loop.
#include <stdio.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <string.h>
#include <stdlib.h>
#include "p8log.h"
#include <errno.h>
int main(int argc, char* argv[])
{
char* PORT;
char* IPADDR;
printf("Arg Count=%d\n",argc);
printf("Arguments are=%s,%s,%s\n",argv[0],argv[1],argv[2]);
printf("HELLO");
PORT=argv[1],
printf("WORLD");
IPADDR=argv[2];
printf("START");
printf("port num=%s",PORT);
printf("IP ADDR=%s",IPADDR);
printf("END");
/* some algorithm of calculation */
return 0;
}
Execution
./file-exe 11111 127.0.0.1
Output
Arg Count=3
Arguments are=./file-exe,11111,127.0.0.1

fflush(NULL); is good to do after any output, if you want to make sure it prints to screen. printf is buffered, so it can get lost.
./a.out 11111 127.0.0.1
Arg Count=3
Arguments are=./a.out,11111,127.0.0.1
HELLO
WORLD
START
port num=11111
IP ADDR=127.0.0.1
END
works fine, you needed some \n to break up lines, like so..
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main(int argc, char* argv[])
{
char* PORT;
char* IPADDR;
printf("Arg Count=%d\n",argc);
printf("Arguments are=%s,%s,%s\n",argv[0],argv[1],argv[2]);
printf("HELLO\n");
PORT=argv[1],
printf("WORLD\n");
IPADDR=argv[2];
printf("START\n");
printf("port num=%s\n",PORT);
printf("IP ADDR=%s\n",IPADDR);
printf("END\n");
fflush(NULL);
/* some algorithm of calculation */
return 0;
}

Related

How can I modify the server/client to take the port number and/or host as an optional command line argument?

I want to use the default host name localhost and port 8080 when I don't specify the arguments.
server.c
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <dirent.h>
#include <sys/wait.h>
#include <assert.h>
#include "command.h"
#include "send.h"
#include "receive.h"
#define DEFAULT_PORT 8080
void checkError(int status)
{
if (status < 0) {
fprintf(stderr, "Process %d: socket error: [%s]\n", getpid(),strerror(errno));
exit(-1);
}
}
void handleNewConnection(int chatSocket);
int main(int argc,char* argv[])
{
short int port;
port = atoi(argv[1]); //When the argument is specified
(...)
client.c
(...)
void doLSCommand(int sid);
void doExitCommand(int sid);
void doGETCommand(int sid);
void doPUTCommand(int sid);
void doSIZECommand(int sid);
int doMGETCommand(int sid);
int main(int argc,char* argv[])
{
char fName[BUF_SIZE];
char* host;
short int port;
host = atoi(argv[1]); //when argument specified in command line
port = atoi(argv[2]); //when argument specified in command line
(...)
I used atoi to get the arguments when they're provided but I don't know how to set defaults if they are not provided.
There are two arguments to the main function: argc and argv.
argc is a number of command line arguments passed and argv is the array of command line arguments.
See: https://www.gnu.org/software/libc/manual/html_node/Program-Arguments.html
In your case, you can check the argc first for how many arguments has the user passed to your program and act accordingly.
So, for the host it could be:
char* host;
if (argc > 1) {
host = argv[1];
}
else {
host = DEFAULT_HOST;
}
and for the port number:
int port;
if (argc > 2) {
port = atoi(argv[2]);
}
else {
port = DEFAULT_PORT;
}

c - can't understand pthread_join()

I can not figure out where I'm wrong, after running the code arrived in the for where it runs the pthread_join() , many pthread_join() return with value 3 instead of 0. Furthermore, printing the value of i is not always consistent and this causes segmentation fault and printing several times of the same position.
Code modified as required in the comments
all the includes are for other parts of the program. Testing only this piece of code creates segmentation fault at error 3 on pthread_join()
#define _POSIX_C_SOURCE 200809L
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <assert.h>
#include <string.h>
#include <signal.h>
#include <pthread.h>
#include <errno.h>
#include <config.h>
#include <sys/select.h>
#include <ctype.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/un.h>
void *threadF(){
printf("hello\n");
pthread_exit((void*)0);
}
int main(int argc, char *argv[]) {
FILE *fileconf=fopen(argv[2],"r");
if(fileconf==NULL){
fprintf(stderr, "Fopen\n",argv[2]);
return -1;
}
set_conf(fileconf); //parse fileconf and set THREADSINPOOL correctly
pthread_t array[THREADSINPOOL];
int i,err,s=0;
for(i=0;i<THREADSINPOOL;i++){
if((err=pthread_create(&array[i],NULL,&threadF,NULL))!=0){
fprintf(stderr,"thread\n");
exit(errno);
}
}
int tmp;
for(i=0;i<THREADSINPOOL;i++){
tmp=pthread_join(array[i],(void *)&s);
printf("thread: %lu terminated\n tmp: %d\n",array[i],tmp);
}
return 0;
}
The problem is that you are passing the address of an int to a function that expects the address of a void *. On a 64-bit system, there's a good chance that an int is only 32-bits whereas a void * is 64-bits. So pthread_join ends up writing 64-bits into a location that is only big enough for 32-bits. The result is that you overwrite memory that shouldn't being changed, and all sorts of undefined behavior follows.
Here's a way to write the code so that the second argument to pthread_join is actually a pointer to a void *
for (i = 0; i < THREADSINPOOL; i++)
{
void *value;
if (pthread_join(array[i], &value) == 0)
printf("thread %d returned %" PRIiPTR "\n", i, (intptr_t)value);
else
printf("thread %d failed\n", i);
}

Linux command-line redirection with 2 c-files

I'm just new to the piping I/O functions within Linux.
2 c-files were made, the first sends data:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <unistd.h>
int main(int argc, char* argv[])
{
int i = 0;
for(;;)
{
printf("\nSent number: %d",i);
i++;
sleep(1);
fflush(stdout);
}
return 0;
}
The second files receives the printed number and displays it:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <unistd.h>
int main(int argc, char* argv[])
{
int x;
for(;;)
{
scanf("%d",&x);
printf("Received number: %d\n",x);
sleep(1);
fflush(stdout);
}
return 0;
}
Finally I try to redirect the data from the first file to the second with:
./send_test.out | ./rcv_test.out
The Terminal prints repeatedly: "Received number: 0", what am I doing wrong?
Also, how can I have to terminal windows for both programs running simultaneously while directing the output from the sender to the receiver?
Thanks in advance
You are not "sending" the number in a format the the receiver can understand.
Try removing all text except the %d from the sender's formatting string.
Also, you should check the return value of scanf() before relying on it.

gzip with execlp in fork process

I have a problem i don't know how to solve in my code. I have to compress with gzip several arguments received form the command-line.
But i have to introduce in the command line the route and not the file. The sample i have prepared is working well but i'm indicating the name of the file and this is not correct.
Can you help me how to indicate the route and not the file? in the execlp. The route is argv[] but i don't know exactly how to build the sentence.
The code is:
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/time.h>
#include <unistd.h>
int main(int argc, char *argv[])
{
int p,pid[p];
int fills;
int ret=0;
char msg[100];
int status;
char filename[30];
if (argc>1)
fills=atoi(argv[p]);
if (argc==1)
{
printf("Error");
exit(1);
}
// Creem N parĂ metres
for(p=1; p<argc; p++)
{
pid[p] = fork();
if (pid[p]<0)
error("Error");
if (pid[p]==0)
{
memset(filename,0,sizeof(filename));
snprintf(filename,30,"ex1a.c");
ret=execlp("gzip", "gzip", "-9", "-f", filename, NULL);
if (ret < 0)
{
exit(EXIT_FAILURE);
}
printf("Process %d created process %d compressed file %s \n",getppid(),getpid(),argv[p]);
exit(p);
}
else
wait(status);
}
exit(EXIT_SUCCESS);
}

Whether a given file argument is a directory or not. C

I am looking for a peace of code to check if the argument I pass to my program is a directory or not. So far I found this:
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
struct stat buf;
stat(argv[1],&buf);
exit(0);
}
But it does not really help me.
Use:
if(S_ISDIR(buf.st_mode))
printf(" Its a directoy\n");
else
printf("Its a file\n");
after stat(argv[1],&buf); call

Resources