C: No such file or directory - c

when I hard code chemin in open(chemin, O_RDONLY) to a file name, the program works, but when I leave if to open(chemin, O_RDONLY) I get No such file or directory.
Why isn't chemin in type_fichier used?
When i use printf("%s", chemin) in type_fichier I get '
int type_fichier(char * chemin) {
int fp;
if ((fp = open(chemin, O_RDONLY)) == -1) { perror(""); exit(0); }
struct stat fileStat;
if(fstat(fp, &fileStat) < 0)
return 1;
switch(fileStat.st_mode & S_IFMT) {
case S_IFBLK: printf("block device\n"); break;
case S_IFCHR: printf("character device\n"); break;
case S_IFDIR: printf("directory\n"); break;
case S_IFIFO: printf("FIFO/pipe\n"); break;
case S_IFLNK: printf("symlink\n"); break;
case S_IFREG: printf("regular file\n"); break;
case S_IFSOCK: printf("socket\n"); break;
default: printf("unknown?\n"); break;
}
}
int main(int argc, char *argv[]) {
char fn = "file";
type_fichier(&fn);
}

Your mistake is here, you are allocating only 1 character for fn:
char fn = "file";
type_fichier(&fn);
You probably want:
const char *fn = "file";
type_fichier(fn);

You are using char variable for storing string literal instead of char array or an equivalent one.
int main(int argc, char *argv[]) {
char fn[] = "file";
type_fichier(fn);
}
there you go....

Related

How to use nested Switch with commandline arguments?

I have my switch case and statements inside my main function as follows:
int main(int argc, char *argv[])
{
int c;
while((c = getopt(argc,argv,"ABS"))!=-1)
{
switch(c)
{
case 'A':
flag = 0;
printf("open the port\n");
struct can_frame frame_rd;
open_port("vcan0");
printf("vcan0 port is opened");
fflush(stdout);
create_file();
while(1)
{
read_port(&frame_rd);
}
break;
case 'B':
flag = 1;
printf("open the port\n");
open_port("vcan0");
printf("vcan0 port is opened");
fflush(stdout);
create_binfile();
while(1)
{
read_port(&frame_rd);
}
break;
}
}
Now I want to make use of nested switch when user passes an argument -S inside the case A can i do it as follows? Is the following procedure correct?
int main(int argc, char *argv[])
{
int c;
while((c = getopt(argc,argv,"ABS"))!=-1)
{
switch(c)
{
case 'A':
switch(c)
{
case 'S':
size = 100;
break;
}
flag = 0;
printf("open the port\n");
struct can_frame frame_rd;
open_port("vcan0");
printf("vcan0 port is opened");
fflush(stdout);
create_file();
while(1)
{
read_port(&frame_rd);
}
break;
case 'B':
flag = 1;
printf("open the port\n");
open_port("vcan0");
printf("vcan0 port is opened");
fflush(stdout);
create_binfile();
while(1)
{
read_port(&frame_rd);
}
break;
}
}
In the above code can I use same switch(c) for the nested case also , is the usage of nested switch correct?Thanks in advance.
No, within the outer swich case 'A', variable c can be thought of as constant, therefore your nested switch will not find a case to match and does not have a default case.
If you know your arguments are in a particular order, you can address them directly e.g. argv[1] and argv[2] for the first and second arguments.

Parsing options having a common flag in C

I have a C program where I accept multiple arguments. Here, I have a common flag d for both data-store and disk. Is there a way that I can check for the flags in-order and get the value of store before I check with case d. I've tried various ways like adding a while loop before this to check for s and then enter this loop etc.
static void
ParseOptions(int argc, char* argv[])
{
int c, option_index;
int ind = 0;
while((c = getopt_long(argc, argv, "s:d:",
long_options, &option_index))!= -1) {
ind = optind;
switch(c) {
case 's':
optionStore = true;
store = strdup(optarg);
break;
case 'd':
if(strcmp(store,"datastore") == 0){
printf("In datastore\n");
datastore = strdup(optarg);
}
else if(strcmp(store,"disk") == 0){
printf("In disk\n");
disk = strdup(optarg);
}
break;
default:
exit(-1);
}
}
}
Not sure how to go about this.
You need to store optarg returned for flag d in a temporary variable, and use it after the loop exits to set either disk or datastore:
char *temp_disk_or_datastore;
while((c = getopt_long(argc, argv, "s:d:",
long_options, &option_index))!= -1) {
ind = optind;
switch(c) {
case 's':
optionStore = true;
store = strdup(optarg);
break;
case 'd':
temp_disk_or_datastore = strdup(optarg);
break;
default:
exit(-1);
}
}
if (store == NULL) {
printf("Missing storage option");
exit(-1);
}
if(strcmp(store,"datastore") == 0){
printf("In datastore\n");
datastore = temp_disk_or_datastore;
}
else if(strcmp(store,"disk") == 0){
printf("In disk\n");
disk = temp_disk_or_datastore;
}

how to get the source file's (the file which I want to copy) and the copied file's information in C

I want to get the source file information, when I want to copy the source file and then get the destination file information, when the source file has already being copied. The problem with the code is that I can't copy and get the source and destination file information.
How could you be able to fix my code to copy a file and get source and destination information?
Code:
#define BUFFER 100 // ** increased - file path can get pretty long
#define BUFFERSIZE 4096
#define COPYMODE 0644
void oops(char *, char *);
int file_exist(char *filename)
{
struct stat buffer;
return (stat (filename, &buffer) == 0);
}
int main(int argc, char *argv[])
{
char ch, source_file[20], target_file[20];
FILE *source, *target;
// printf("Enter name of file to copy\n");
// fgets(source_file, 20, stdin);
source_file = argv[20];
source = fopen(source_file, "r");
if( source == NULL )
{
printf("Press any key to exit...\n");
exit(EXIT_FAILURE);
}
printf("Enter name of target file\n");
fgets(target_file, 20 , stdin);
target = fopen(target_file, "w");
if( target == NULL )
{
fclose(source);
printf("Press any key to exit...\n");
exit(EXIT_FAILURE);
}
while( ( ch = fgetc(source) ) != EOF )
fputc(ch, target);
printf("File copied successfully.\n");
fclose(source);
fclose(target);
struct stat sb;
if (argc != 2) {
fprintf(stderr, "Usage: %s <pathname>\n", argv[0]);
exit(EXIT_FAILURE);
}
if (stat(argv[1], &sb) == -1) {
perror("stat");
exit(EXIT_SUCCESS);
}
printf("File type: ");
switch (sb.st_mode & S_IFMT) {
case S_IFBLK: printf("block device\n"); break;
case S_IFCHR: printf("character device\n"); break;
case S_IFDIR: printf("directory\n"); break;
case S_IFIFO: printf("FIFO/pipe\n"); break;
case S_IFLNK: printf("symlink\n"); break;
case S_IFREG: printf("regular file\n"); break;
case S_IFSOCK: printf("socket\n"); break;
default: printf("unknown?\n"); break;
}
printf("I-node number: %ld\n", (long) sb.st_ino);
exit(EXIT_SUCCESS);
}
void oops(char *s1, char *s2)
{
fprintf(stderr,"Error: %s ", s1);
perror(s2);
exit(1);
}
I am unsure where your difficulty lies, apart from errors mentioned in comment. I've simplified your code, removing the bitfield masks as I don't have their definitions.
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
int main(int argc, char *argv[])
{
int ch; // <--- int not char
struct stat sb;
FILE *source, *target;
if (argc < 3) {
printf("Enter two args: source and destination file names\n");
exit(EXIT_FAILURE);
}
source = fopen(argv[1], "r");
if( source == NULL ) {
printf("Press any key to exit...\n");
exit(EXIT_FAILURE);
}
target = fopen(argv[2], "w");
if( target == NULL ) {
fclose(source);
printf("Press any key to exit...\n");
exit(EXIT_FAILURE);
}
while( ( ch = fgetc(source) ) != EOF )
fputc(ch, target);
fclose(source);
fclose(target);
printf("File copied successfully.\n");
if (stat(argv[1], &sb) == -1) {
perror("stat");
exit(EXIT_SUCCESS);
}
printf("File %s type: 0x%04X Mode: 0x%04X\n", argv[1], (unsigned)sb.st_ino, (unsigned)sb.st_mode);
if (stat(argv[2], &sb) == -1) {
perror("stat");
exit(EXIT_SUCCESS);
}
printf("File %s type: 0x%04X Mode: 0x%04X\n", argv[2], (unsigned)sb.st_ino, (unsigned)sb.st_mode);
return 0;
}
Program output:
>test test.c test2.c
File copied successfully.
File test.c type: 0x0000 Mode: 0x81B6
File test2.c type: 0x0000 Mode: 0x81B6

getopt is not working for one of my parameters

I have to programm a TCP/UDP Server/Client software.
Possible arguments: -u: UDP -t: TCP -l Server -p: [Port] -h [IP]
I wrote a function printflags, to see if everything works fine.
The u-, t-, l- and p-Options work fine. But my IP is everytime NULL.
Where is the problem?
#include <ctype.h>
#include <stdio.h>
#include <getopt.h>
#include <stdlib.h>
#include <unistd.h>
int printflags(int, int, int, char *,char *);
int main(int argc, char *argv[]){
int uflag=0;
int tflag=0;
int lflag=0;
char *pvalue = NULL;
char *hvalue = NULL;
int c;
opterr = 0;
while((c = getopt (argc, argv, "utlhp:")) != -1)
{
switch(c)
{
case 'u':
uflag = 1;
break;
case 't':
tflag = 1;
break;
case 'l':
lflag = 1;
break;
case 'p':
pvalue = optarg;
break;
case 'h':
hvalue = optarg;
break;
case ':':
fprintf(stderr, "case :");
case '?':
if(optopt == 'p' || optopt == 'h')
fprintf(stderr, "Option '-%c' requires an argument.\n", optopt);
else if (isprint(optopt))
fprintf(stderr, "Unknown option character '-%c'.\n", optopt);
else
fprintf(stderr, "Unknown option character '%x'.\n", optopt);
return 1;
default:
abort();
}
}
printflags(uflag, tflag, lflag, pvalue, hvalue);
return 0;
}
int printflags(int uflag, int tflag, int lflag, char* pv, char *hv){
printf("-u UDP: %d\n", uflag);
printf("-t TCP: %d\n", tflag);
printf("-l Listen Socket - Server: %d\n", lflag);
printf("-p Port: %s\n", pv);
printf("-h IP: %s\n", hv);
return 0;
}
Your option string should be "utlh:p:". You need a colon after each letter that takes an optarg.
Your parameter to getopt() needs a colon after the h to signify that -h needs an argument.
while((c = getopt (argc, argv, "utlh:p:")) != -1)
// ^ --- here

ERROR: stat: No Such File Or Directory, opendir() and stat() in C programming

Hey and thanks for reading.
I am making a program which takes 1 argument (directory) and reads all the files in the directory used opendir()/readdir(), and displays the file type (reg, link, directory etc) using stat. I am receiving the error "No Such file or Directory" when I execute me program in the shell (I am using redhat linux). Here is my code:
#define _BSD_SOURCE
#include <stdio.h>
#include <dirent.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[]) {
DIR *dirp;
struct dirent* dent;
struct stat info;
//If no args
if(argc == 1){
argv[1] = ".";
dirp=opendir(argv[1]); // specify directory here: "." is the "current directory"
do {
dent = readdir(dirp);
if (dent)
{
//////////////////////////////////////////////////////////////////////////
if (stat(dent->d_name, &info) == -1) {
perror("stat");
exit(EXIT_FAILURE);
}
switch (info.st_mode & S_IFMT) {
case S_IFBLK: printf("block device\n"); break;
case S_IFCHR: printf("character device\n"); break;
case S_IFDIR: printf("dir "); break;
case S_IFIFO: printf("FIFO/pipe\n"); break;
case S_IFLNK: printf("lnk "); break;
case S_IFREG: printf("reg "); break;
case S_IFSOCK: printf("socket\n"); break;
default: printf("unknown?\n"); break;
}
//////////////////////////////////////////////////////////////////////////
printf("%s \n", dent->d_name);
}
} while (dent);
closedir(dirp);
}
////////////////////////////////////////////////////////////////////////////////////////////////
//If specified directory
if(argc > 1){
dirp=opendir(argv[1]); // specify directory here: "." is the "current directory"
do {
dent = readdir(dirp);
if (dent)
{
/////////////////////////////////////////////////////////////////////////////////////
if (stat(dent->d_name, &info) == -1) {
perror("stat");
exit(EXIT_FAILURE);
}
switch (info.st_mode & S_IFMT) {
case S_IFBLK: printf("block device\n"); break;
case S_IFCHR: printf("character device\n"); break;
case S_IFDIR: printf("dir "); break;
case S_IFIFO: printf("FIFO/pipe\n"); break;
case S_IFLNK: printf("lnk "); break;
case S_IFREG: printf("reg "); break;
case S_IFSOCK: printf("socket\n"); break;
default: printf("unknown?\n"); break;
}
//////////////////////////////////////////////////////////////////////////////////////
// printf("%s\n", argv[1]);
printf("%s \n", dent->d_name);
}
} while (dent);
closedir(dirp);
}
return 0;
}
Any ideas? I'm a bit stuck.
Thanks for your input
Also, are files of type "Link" going to be output using stat, or do I have to use lstat? Not sure how to use lstat in this situation, if I change the struct type to "struct lstat info" it throws an error.
dent->d_name is the name of the file relative to your current directory (e.g "/home/barney/myfile.txt") not the absolute full path to the file (e.g. /home/barney/sources/myfile.txt), which is the one expected by stat.
This is why stat cant find the path. print dent->d_name before each call to stat to observe those incorrect paths.
Edit:
You can try chdir() to change your working directory to argv[1]

Resources