Undefined Reference to 'main' when code is compiled - c

I created a C program which would create a directory and file.
I have tried to debug the error, but it didn't work
#include <dirent.h>
#include <errno.h>
#include<stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <stdlib.h>
create_dir(char* outputdir,char* str_outpath,char* value){
DIR* dir = opendir(outputdir);
FILE *f;
if (dir) {
/* Directory exists. */
closedir(dir);
} else if (ENOENT == errno) {
/* Directory does not exist. */
mkdir(outputdir, 0700);
closedir(dir);
printf("Successfully created the directory %s ", outputdir);
} else {
printf("Creation of the directory %s failed",outputdir);
/* opendir() failed for some other reason. */
}
f = fopen(str_outpath, "a");
fprintf(f,"%s",value);
fclose(f);
}
I want it to create a file and a directory successfully

As others have mentioned. You do not have a main function.
Also your create_dir function is missing a type. I'll assume it's void since you are not returning anything. This should compile.
#include <dirent.h>
#include <errno.h>
#include<stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <stdlib.h>
void create_dir(char* outputdir,char* str_outpath,char* value){
DIR* dir = opendir(outputdir);
FILE *f;
if (dir) {
/* Directory exists. */
closedir(dir);
} else if (ENOENT == errno) {
/* Directory does not exist. */
mkdir(outputdir, 0700);
closedir(dir);
printf("Successfully created the directory %s ", outputdir);
} else {
printf("Creation of the directory %s failed",outputdir);
/* opendir() failed for some other reason. */
}
f = fopen(str_outpath, "a");
fprintf(f,"%s",value);
fclose(f);
}
int main(){
char directory[] = "/users/me/documents/testdir";
char filepath[] = "testfile";
char data[] = "hello world";
create_dir(directory,filepath,data);
return 0;
}
I did not execute the code to check whether it works. I merely copied and pasted yours and called the function.

In C under most cases you need to have a main function. So in order to run your code you'll need to have something like this (assuming that you want to pass in the parameters from the command-line) underneath that function:
int main(int argc, char *argv[]) {
if (argc < 4) {
printf("Proper Usage is ./program otputdir str_outpath value\n");
return -1;
}
char *outputdir = argv[1];
char *str_outpath = argv[2];
char *value = argv[3];
create_dir(outputdir, str_outpath, value);
return 0;
}
EDIT: fixed an issue with not checking argc

Related

My own program who work as "which" command in linux - dont work great

I would like to make my own program who will be work as which command in linux. Original which command works that (example for pwd):
$ which pwd
/bin/pwd
but my program works that:
$ ./prog1 pwd
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games pwd
Can You correct my program? Source code:
#include <sys/param.h>
#include <sys/stat.h>
#include <errno.h>
#include <locale.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
int main(int argc, char *argv[])
{
char *systemPath, *path, *fileName;
struct stat statStruct;
if (argc < 2){
printf( "Nothing to do\n");
return -1;
}
if ((systemPath = getenv("PATH")) == NULL)
{
perror( "Not found PATH!\n ");
return -1;
}
fileName = argv[1];
printf("%s %s\n", systemPath, fileName);
while ((path = strsep(&systemPath, ":")) != NULL)
{
if ((stat(fileName, &statStruct) == 0) && S_ISREG(statStruct.st_mode) && (statStruct.st_mode & S_IRUSR))
{
printf("%s\n", fileName);
printf("%s\n", systemPath);
printf("%s\n", path);
}
}
return 0;
}
You find the candidate directory using strsep, storing it in path. But then you don't use this variable; the stat call just uses the base filename, which will therefore always be looked up in the current working directory.
You need to form the concatenation of path, a /, and the filename, and then stat that.
You will probably find snprintf useful.

C Directory and Subdirectory Recursion

I tried to recursively get all files and folder list.But I can only get subdirectories of documents and inside of them.I can't get the other folders which in inside of subdirectory.
I don't know how to do it recursively.I hope you help me
#include <stdio.h>
#include <sys/types.h>
#include <dirent.h>
#include <windows.h>
#include <unistd.h>
#include <string.h>
void list(char *a);
void reader(char *path);
int
main (void)
{
DIR *dp;
struct dirent *ep;
dp = opendir ("C:\\Users\\pen\\Documents\\");
if (dp != NULL)
{
while (ep = readdir (dp)){
GetFileAttributes(ep->d_name);
if(FILE_ATTRIBUTE_DIRECTORY & GetFileAttributes(ep->d_name))
{
if (strcmp(".",ep->d_name)==0)
continue;
if (strcmp("..",ep->d_name)==0)
continue;
reader(ep->d_name);
}
}
closedir(dp);
}
else
perror ("Couldn't open the directory");
closedir(dp);
system("pause");
return 0;
}
void reader(char *path){
DIR *da;
struct dirent *ef;
da = opendir(path);
while (ef=readdir(da)){
printf ("%s\n",ef->d_name);
if(FILE_ATTRIBUTE_DIRECTORY & GetFileAttributes(ef->d_name))
{
if (strcmp(".",ef->d_name)==0)
continue;
if (strcmp("..",ef->d_name)==0)
continue;
reader(ef->d_name);
}
}
closedir(da);
}
1) In reader you need to call closedir(da); after the while loop.
2) every call to reader needs to have the absolute path you need to concatenate path
to ef->d_name and then call reader.
3) also to enable debugging you should call perror after a failed readdir call.

Problem reading directories in C

I am writing a simple C program that receives a directory as an argument and displays the files in this directory and also his subdirectories. I wrote a "recursive" function for doing that. But for an unknown reason, my program fails at the stat function. Here is my program :
#define _POSIX_SOURCE 1
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <dirent.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
void display_directory(char* path){
DIR* directory = opendir(path);
if( directory == NULL){
printf("opendir failure for %s\n", path);
exit(1);
}
struct dirent* dirent;
struct stat stat_info;
while((dirent = readdir(directory)) != NULL){
printf("[%s]\n", dirent->d_name);
if(stat(dirent -> d_name, &stat_info) == -1){
printf("readdir error for %s\n", dirent->d_name);
exit(1);
}
if(S_ISREG(stat_info.st_mode)){
printf("File: %s \n", dirent -> d_name);
}
if(S_ISDIR(stat_info.st_mode)){
if(strncmp(dirent->d_name, "..",2)){
printf("Directory : %s\n", dirent->d_name);
display_directory(dirent->d_name);
}
}
}
closedir(directory);
}
int main(int argc, char* argv[]){
char* path;
if(argc > 1){
path = argv[1];
} else {
path = ".";
}
display_directory(path);
return EXIT_SUCCESS;
}
For instance, if in my directory A, I have a1, a2, a3 and .., it reads first the .. directory, and when it reads the directory a1, the stat function fails.
Can someone tells me what is not correct with my code.
[EDIT] I included <errno.h> as many of you suggest and after running the program, I have the error Too many open files.
#define _POSIX_SOURCE 1
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <dirent.h>
#include <string.h>
#include <errno.h>
#include <sys/stat.h>
#include <sys/types.h>
void display_directory(char* path){
DIR* directory = opendir(path);
if( directory == NULL){
printf("opendir failure for %s --> %s\n", path, strerror(errno));
exit(1);
}
struct dirent* dirent;
struct stat stat_info;
while((dirent = readdir(directory)) != NULL){
printf("[%s]\n", dirent->d_name);
if(stat(dirent->d_name, &stat_info)){
printf("readdir error for %s ---> %s\n", dirent->d_name, strerror(errno));
continue;
}
if(S_ISREG(stat_info.st_mode)){
printf("Fichier : %s \n", dirent->d_name);
}
if(S_ISDIR(stat_info.st_mode)){
if(strncmp(dirent->d_name, "..",2)){
printf("Directory : %s\n", dirent->d_name);
display_directory(dirent->d_name);
}
}
}
closedir(directory);
}
int main(int argc, char* argv[]){
char* path;
if (argc > 2) {
fprintf(stderr, "Usage: %s [directory]\n", argv[0]);
exit(1);
}
path = argv[1];
display_directory(path);
return EXIT_SUCCESS;
}
The output of the program :
[..]
[mykill.c]
readdir error for mykill.c ---> No such file or directory
[.]
Directory : .
[..]
[.]
Directory : .
[..]
[.]
Directory : .
[..]
...
...
Directory : .
opendir failure for . --> Too many open files
mykill.c is a file in the directory that was passed as an argument.
I have a pretty good idea what's wrong, but I want to tell you how to debug this for yourself, first. Change this code ...
if(stat(dirent -> d_name, &stat_info) == -1){
printf("readdir error for %s\n", dirent->d_name);
exit(1);
}
... to read instead ...
if (stat(dirent->d_name, &stat_info)) {
printf("%s: %s\n", dirent->d_name, strerror(errno));
continue;
}
You will need to add to the include list
#include <errno.h>
Run the program again. If you don't see from the output what the problem is, then edit the COMPLETE, UNEDITED output into your question and we'll go from there.
if(S_ISDIR(stat_info.st_mode)){
if( !strcmp(dirent->d_name, ".")) continue;
if( !strcmp(dirent->d_name, "..")) continue;
printf("Directory : %s\n", dirent->d_name);
display_directory(dirent->d_name);
}
You are making stat only with filename (without full path), add full path to the filename or change working directory before calling stat.
Use nftw().

Get only the files included in a directory in c / Ubuntu

I have to create a listing of the files contained inside a specific directory, I have done the code below(part of a bigger programm), but I would like my programm to ignore any possible folders that could be included inside the directory.
#include <stdio.h>
#include <sys/types.h>
#include <dirent.h>
int main ()
{
DIR *dirptr;
struct dirent *entry;
dirptr = opendir ("synchedFolder");
if (dirptr != NULL)
{
while (entry = readdir (dirptr))
{
if(strcmp(entry->d_name,"..")!=0 && strcmp(entry->d_name,".")!=0)
puts (entry->d_name);
}
(void) closedir (dirptr);
}
else
perror ("ERROR opening directory");
}
If you want to list only files, but no directories, you have to add the following check:
entry->d_type == DT_REG
or
entry->d_type != DT_DIR
There's stat() and lstat() and the return value for stat. In the latter, look out for the S_ISDIR macro.
Short answer is the dirent structure includes the necessary information:
if ( entry->d_type == DT_REG)
Check stat (or lstat)
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
/* int main (void){ */
int main (int argc, char **argv){
int i,result=0;
struct stat buf;
/* print_S_I_types(); */
for (i=1; i < argc; i++){
if (lstat(argv[i], &buf) < 0) {
fprintf(stderr, "something went wrong with %s, but will continue\n",
argv[i]);
continue;
} else {
if S_ISREG(buf.st_mode){
printf("argv[%d] is normal file\n",i);
}else {
printf("argv[%d] is not normal file\n",i);
}
}
}
return 0;
}
Working code for listing files (without directories):
#include <stdio.h>
#include <dirent.h>
#include <stdlib.h>
int main()
{
DIR *dir;
struct dirent *ent;
if ((dir = opendir ("/home/images")) != NULL)
{
/* print all the files and directories within directory */
while ((ent = readdir (dir)) != NULL)
{
if(ent->d_type!= DT_DIR)
{
printf ("%s\n", ent->d_name);
}
}
closedir (dir);
}
else
{
/* could not open directory */
perror ("");
return EXIT_FAILURE;
}
}

creating a temporary folder in tmp folder c language Mac OS X

How can i create a temporary folder in /tmp directory.
Try the mkdtemp function.
char *tmpdir;
strcpy (template, "/tmp/myprog.XXXXXX");
tmpdir = mkdtemp (template);
if (!tmpdir) {
// Error out here
}
printf ("Temporary directory created : %s", tmpdir);
Since I can't change/improve other's answers yet, I'm writing one myself.
I'd use stat and mkdir. For example:
#include <errno.h> // for errno
#include <stdio.h> // for printf
#include <stdlib.h> // for EXIT_*
#include <string.h> // for strerror
#include <sys/stat.h> // for stat and mkdir
int main() {
const char *mydir = "/tmp/mydir";
struct stat st;
if (stat(mydir, &st) == 0) {
printf("%s already exists\n", mydir);
return EXIT_SUCCESS;
}
if (mkdir(mydir, S_IRWXU|S_IRWXG) != 0) {
printf("Error creating directory: %s\n", strerror(errno));
return EXIT_FAILURE;
}
printf("%s successfully created\n", mydir);
return EXIT_SUCCESS;
}

Resources