How to create file inside a directory using C - c

I am trying to create a directory and a file inside the directory. Below is my code in C, but when I try to compile it, I got this error: invalid operands to binary / (have ‘const char *’ and ‘char *’)
char *directory = "my_dir";
struct stat dir = {0};
if(stat(directory, &dir) == -1)
{
mkdir(directory, 0755);
printf("created directory testdir successfully! \n");
}
int filedescriptor = open(directory/"my_log.txt", O_RDWR | O_APPEND | O_CREAT);
if (filedescriptor < 0)
{
perror("Error creating my_log file\n");
exit(-1);
}
thanks for help

use sprintf() or similar to create the pathFilename string:
char pathFile[MAX_PATHNAME_LEN];
sprintf(pathFile, "%s\\my_log.txt", directory );
then
int filedescriptor = open(pathFile, O_RDWR | O_APPEND | O_CREAT);
Note: If you are using linux, change \\ to / and MAX_PATHNAME_LEN to 260 (or whatever linux likes to use for that value.)
EDIT if you need to check that a directory exists before creating a file there, you can do something like this:
if (stat("/dir1/my_dir", &st) == -1) {
mkdir("/dir1/my_dir", 0700);
}
Read more here: stat, mkdir

you should do something such as:
char *filepath = malloc(strlen(directory) + strlen("my_log.txt") + 2);
filepath = strcpy(filepath, directory);
filepath = strcat(filepath, "/my_log.txt");
and then use filepath in the open function

Please Refer complete solution:
#include<stdio.h>
#include<string.h> // for string operations
#include<stdlib.h> //for malloc()
#include<fcntl.h> //for creat()
#include<sys/stat.h> //for struct stat, stat()
#include<unistd.h> //for close()
int main(int argc,const char **argv)
{
//variable declaration
int iFd = 0;
char *chDirName = NULL;
char *chFileName = NULL;
char *chFullPath = NULL;
struct stat sfileInfo;
//Argument Validation
if(argc != 3)
{
printf("[ERROR] Insufficient Arguments\n");
return(-1);
}
//argument processing
chDirName = (char *)malloc(sizeof(char));
chFileName = (char *)malloc(sizeof(char));
chFullPath = (char *)malloc(sizeof(char));
chDirName = strcpy(chDirName,argv[1]);
chFileName = strcpy(chFileName,argv[2]);
//create full path of file
sprintf(chFullPath,"%s/%s",chDirName,chFileName);
//check directory exists or not
if(stat(chDirName,&sfileInfo) == -1)
{
mkdir(chDirName,0700);
printf("[INFO] Directory Created: %s\n",chDirName);
}
//create file inside given directory
iFd = creat(chFullPath,0644);
if(iFd == -1)
{
printf("[ERROR] Unable to create file: %s\n",chFullPath);
free(chDirName);
free(chFileName);
free(chFullPath);
return(-1);
}
printf("[INFO] File Created Successfully : %s\n",chFullPath);
//close resources
close(iFd);
free(chDirName);
free(chFileName);
free(chFullPath);
return(0);
}
Run Program With giving two Command Line Arguments:
First Argument : DirectoryName
Second Argument : FileName
for example : after compiling
./executableName yourFolderName yourFileName

Related

making a new directory for my copied file in C

I want to have a successful copy file program, where I can have the result: ./a.out 1.c examples3/16.c to be successful, where I can successfully make a new directory for my copied file. How can you do this, because I get errors trying to do it. I can copy the 1.c file successfully in one place, and I can copy the file into an existing directory but not successful in copying the file into a newly made directory. How can you do this and fix this problem?
code:
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
#define BUFFERSIZE 4096
#define COPYMODE 0644
int file_exist (char *filename)
{
struct stat buffer;
return (stat (filename, &buffer) == 0);
}
int main(int ac, char *av[])
{
int ch; //ch - character no.
struct stat sb;
char directory[120];
FILE *source, *target;
/* from http://stackoverflow.com/questions/30215462/how-to-get-the-source-files-the-file-which-i-want-to-copy-and-the-copied-file/30217023#30217023 */
if (ac <= 2) {
printf("Enter source and destination file names\n");
exit(EXIT_FAILURE);
}
if ( strcmp(av[1], av[2]) ==0 )
{
printf("the files are the same\n");
exit(1);
}
if (file_exist (av[2]))
{
printf ("the destination file exists\n");
exit(1);
}
//printf("which directory do you want to send your destination file?");
//scanf("%s", directory);
char serv_name[1000];
mkdir("newdir/", S_IRWXU | S_IRWXG | S_IRWXO);
snprintf(serv_name, sizeof(serv_name), "newdir/%s", av[2]);
FILE* f = fopen(serv_name, "w");
if (f < 0) {
perror("CLIENT:\n");
exit(1);
}
source = fopen(av[1], "r");//getting and opening source file
if( source == NULL ) {
printf("Press any key to exit...\n");
exit(EXIT_FAILURE);
}
target = fopen(av[2], "w");//getting and opening destination file
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");
/* from http://stackoverflow.com/questions/30215462/how-to-get-the-source-files-the-file-which-i-want-to-copy-and-the-copied-file/30217023#30217023 */
if (stat(av[1], &sb) == -1) {
perror("stat");
exit(1); //exit(EXIT_SUCCESS);
}
else
if (chmod(av[2], sb.st_mode & 07777))//http://stackoverflow.com/questions/18032574/how-can-i-copy-permissions-from-a-file-that-already-exists
{
perror("chmod");
}
printf("Source File: %s, Inode number: %d, Mode: 0x%04X\n", av[1], (unsigned)sb.st_ino, (unsigned)sb.st_mode);
if (stat(av[2], &sb) == -1) {
perror("stat");
exit(1);
}
char *str;
str = (char *) malloc(15);
strcpy(str, av[2]);
if (stat(av[2], &sb) == -1) {//http://stackoverflow.com/questions/7430248/creating-a-new-directory-in-c
mkdir(av[2], 0700);
}
printf("Destination File: %s, inode number: %d, Address = %u, Mode: 0x%04X\n", av[2], (unsigned)sb.st_ino, str, (unsigned)sb.st_mode);
free(str);
return 0;
}
I can successfully make a new directory for my copied file. [...] but not successful in copying the file into a new directory or different
The problem may come from the directory creation:
If you call you program with: ./a.out foo bar
snprintf(serv_name, sizeof(serv_name), "newdir/%s", av[2]);
FILE* f = fopen(serv_name, "w");
Your program will try to open newdir/bar: OK
If you call you program with: ./a.out foo /path/to/bar
snprintf(serv_name, sizeof(serv_name), "newdir/%s", av[2]);
FILE* f = fopen(serv_name, "w");
Your program will try to open newdir//path/to/bar: That will certainly fail.
So your problem comes from:
snprintf(serv_name, sizeof(serv_name), "newdir/%s", av[2]);
FILE* f = fopen(serv_name, "w");
if (f < 0) {
perror("CLIENT:\n");
exit(1);
}
You cannot open f if the tree directory is not correct.

How to iterate in a directory and stop at a particular level using C?

I am trying to print the names of all the processes currently in the system, in the terminal. For that I have to get into all the directories named after the process ID in the "proc" directory. So I am looping till before the "acpi" directory and trying to read the status file in each process directory. But I don't exactly understand how to read a file in a directory which is inside a directory. On running my code below :
#include <stdio.h>
#include <unistd.h>
#include <sys/stat.h>
#include <stdlib.h>
#include <dirent.h>
int main(int argc, const char *argv[])
{
DIR* FD;
struct dirent* in_file;
FILE *process_file;
char ch, pname[1024];
int i=0;
FD = opendir ("/proc");
while ((in_file = readdir(FD)))
{
if (strcmp (in_file->d_name, "acpi") == 0)
break;
else
{
opendir(in_file->d_name);
process_file = fopen("status", "r");
while( ( ch = fgetc(process_file) ) != '\n' )
{
pname[i] = ch;
i++;
}
printf(" %s \n",pname);
fclose(process_file);
closedir(in_file->d_name);
}
}
closedir(FD);
return 0;
}
I get the error :
myps.c: In function ‘main’:
myps.c:38:13: warning: passing argument 1 of ‘closedir’ from incompatible pointer type
closedir(in_file->d_name);
^
In file included from myps.c:5:0:
/usr/include/dirent.h:149:12: note: expected ‘struct DIR *’ but argument is of type ‘char *’
extern int closedir (DIR *__dirp) __nonnull ((1));
^
This is a good example of when to use a recursive function.
The function would take a directory name, open that directory, and loop through the results. For each result that is not . or .., call the stat function to get status on each entry. Then use the S_ISREG and S_ISDIR macros against the file mode to see if it's a regular file or a directory. If it's a directory, build a new string from the parent directory and the one you just found and pass that to the recursive function call.
So the function would look something like this:
void processDirectory(char dirname[])
{
struct stat statbuf;
DIR *dir;
struct dirent *de;
char *subdirname;
int rval, ;
if ((dir = opendir(dirname)) == NULL) {
perror("Failed to open directory %s", dirname);
exit(1);
}
while ((errno = 0, de = readdir(dir)) != NULL) {
rval = stat(de->d_name, &statbuf);
if (rval == -1) {
perror("stat failed");
exit(1);
}
if (S_ISREG(statbuf.st_mode)) {
// process as a regular file
} else if (S_ISDIR(statbuf.st_mode)) {
if (strcmp(de->d_name, ".") && strcmp(de->d_name, "..")) {
subdirname = malloc(strlen(dirname) + strlen(de->d_name) + 2);
if (subdirname == NULL) {
perror("malloc failed");
exit(1);
}
strcpy(subdirname, dirname);
strcat(subdirname, "/");
strcat(subdirname, de->d_name);
processDirectory(subdirname);
free(subdirname);
}
}
}
if (errno && (errno != ENOENT)) {
perror("Failed to read directory %s", dirname);
exit(1);
}
closedir(dir);
}
To solve the error, save the directory pointer you open. Then use that to close the directory.
DIR *process_dir = opendir(in_file->d_name);
closedir(process_dir);

Redirecting execvp path

I'm trying to write a simple code which execute a program from subfolders from a input file and print thr result into a output file.
My problem is that when i execute the program it keeps failing on me. since the execvp command is trying to look for an exe named "a.out" on the wrong location. in (desktop rather than searching the correct path address).
here's the code. please help me out :)
pid_t runner;
char enter[] = "/home/demo/Desktop/OS/Ex1/Ex12/code/input.txt"; // input file
char path[] = "/home/demo/Desktop/OS/Ex1/Ex12/Ex1/ronen/"; correct path
char *r [] = {"./a.out", NULL};
int savedFD = dup(0);
int sever2Fd=dup(1);
int fdin = open(enter,O_RDONLY);
int fdout = open ("output.txt", O_CREAT | O_RDWR, 0466);
dup2(fdin, 0);
dup2(fdout, 1);
if ((runner = fork()) < 0) {perror("could not make fork");}
else if (runner == 0) {
if (execvp(r[0],r) < 0 ) {printf("Failed!\n");}
} else if (runner != 0) {
waitpid(runner,0,0);
dup2(savedFD, 0);
dup2(sever2Fd, 1);
printf("done\n");
}
close(fdin);close(fdout);
The answer was simple.
"chdir(wanted path)"
int dirchange = chdir(argv[1]);

Root-SUID C wrapper debugging

I am new to C and this is a simple wrapper I wrote to run execute scripts as different user. I understand I can do visudo in etc/sudoers but, I already did this and I don't want it to go to waste, also it will help me improve writing in C. I seem to ha The problem is I am having errors when I compile it. My operating system is Ubuntu 12.04.03 LTS. Can someone help me fix these errors?
rootsuidwrapper.c: In function ‘trusted’:
rootsuidwrapper.c:60:15: warning: assignment makes pointer from integer without a cast [enabled by default]
rootsuidwrapper.c: In function ‘main’:
rootsuidwrapper.c:116:48: error: too many arguments to function ‘stat’
/usr/include/x86_64-linux-gnu/sys/stat.h:211:12: note: declared here
It would be nice if someone could fix these errors and give me the working code. Also, I would like to know what I did wrong.
* This program must be run as root to work.
*/
#if !defined(lint) && !defined(SABER) || defined(RCS_HDRS)
#endif /* !lint && !SABER || RCS_HDRS */
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <netdb.h>
#include <sys/stat.h>
#define TRUSTED_GROUP "trusted"
typedef enum { false = 0, true } bool;
#ifdef __STDC__
bool trusted(char *whoami)
#else
bool trusted(whoami)
char *whoami;
#endif /* __STDC__ */
{
char *user;
char host[BUFSIZ + 1];
char domain[BUFSIZ + 1];
struct hostent *hp;
/*
* Figure out whether this user on this host in this domain is
* trusted.
*/
/*
* Determine our domain name
*/
(void) memset(domain, '\0', sizeof(domain));
getdomainname(domain, sizeof(domain) - 1);
/*
* Figure out our fully canonicalized hostname
*/
(void) memset(host, '\0', sizeof(host));
gethostname(host, sizeof(host) - 1);
if ((hp = gethostbyname(host)) == NULL) {
strcat(host, ".");
strcat(host, domain);
fprintf(stderr,
"%s: WARNING: can't canonlicalize hostname; assuming %s.\n",
whoami, host);
}
else {
strcpy(host, hp->h_name);
}
/*
* Get login name of current user
*/
if ((user = cuserid(NULL)) == NULL) {
fprintf(stderr, " %s: You do not seem to be in the passwd file!\n",
whoami);
return(false);
}
/*
* Look this triple up in the trusted netgroup
*/
return ((innetgr(TRUSTED_GROUP, host, user, domain) == 1) ? true : false);
}
#ifdef __STDC__
main(int argc, char *argv[])
#else
main(argc, argv)
int argc;
char *argv[];
#endif /* __STDC__ */
{
char *whoami;
int ouruid; /* uid we set to run chown and chmod */
int proguid; /* uid we are chowning program to */
char *filename;
struct stat statbuf;
int error = 0;
if (whoami = strrchr(argv[0], '/'))
whoami ++;
else
whoami = argv[0];
if (argc == 3)
proguid = atoi(argv[2]);
else if (argc == 2)
proguid = 0;
else {
fprintf(stderr, "usage: %s filename [proguid]\n", whoami);
exit(1);
}
filename = argv[1];
if (trusted(whoami))
ouruid = 0;
else
ouruid = getuid();
if (setuid(ouruid) == -1) {
fprintf(stderr, "%s: Warning: setuid(%d) failed: ", whoami, ouruid);
perror(NULL);
exit(1);
}
if (stat(filename, &statbuf, sizeof(struct stat)) == -1) {
fprintf(stderr, "%s: failure statting %s: ", whoami, filename);
perror(NULL);
exit(1);
}
if (chown(filename, proguid, -1) == -1) {
error++;
fprintf(stderr, "%s: chown %d %s failed: ", whoami, proguid, filename);
perror(NULL);
fprintf(stderr, "continuing...\n");
}
if (chmod(filename, statbuf.st_mode | S_ISUID)) {
error++;
fprintf(stderr, "%s: chmod u+s %s failed: ", whoami, filename);
perror(NULL);
}
return(error);
}
Help is appreciated,
NAME
stat, fstat, lstat - get file status
SYNOPSIS
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
int stat(const char *path, struct stat *buf);
int fstat(int filedes, struct stat *buf);
int lstat(const char *path, struct stat *buf);
Remove your third parameter in the call to stat(). Your code should then be:
if (stat(filename, &statbuf) == -1) {
There is no need to tell stat() the size of the buffer because stat() expects a struct stat * which has a fixed size.
For the compiler warning:
cuserid() returns a pointer to a character (char*). When any function returns a pointer, and you want to place the return value into a buffer, then you have to put the return value into a specific place in the buffer, usually the beginning. Specifically, you should use:
*user = cuserid(NULL);
if(user == NULL)
Remember, cuserid() returns a pointer to a single character. Therefore, the return value of the function should go into a single character - that is, *user or user[0]. When the above code is used, the compiler shouldn't complain. Then you place the result of cuserid(NULL) into user, from the first byte to the rest of the memory allocated.

C: strange behavior with nftw()

I've this code:
#include <ftw.h>
#include <stdio.h>
#include <string.h>
int nftw_stat(const char *path, const struct stat *stat, int flags,
struct FTW *ftw)
{
if (strcmp(path, "/home/pf/.gvfs\0") == 0) {
printf("nftw()\n");
printf("mode = %d\n", stat->st_mode);
printf("size = %d\n", (int) stat->st_size);
}
return 0;
}
int main()
{
if (nftw("/home/pf", &nftw_stat, 1, FTW_PHYS)) {
perror("nftw");
return 2;
}
}
If I execute it normally, it returns the same way as stat() function:
mode = 16704 (S_IFDIR | S_IRUSR | S_IXUSR)
size = 0
But when I execute it with sudo, it returns this:
mode = 16832 (S_IFDIR | S_IRWXU)
size = 4096
What happens? If I use stat() with sudo it give me the Permission denied error. This happens only with .gvfs directory, whose permissions are 500 (dr-x------). If sudo can't read with stat() , why it works with nftw()? :|
What's probably happening is that stat has failed on the directory, but you are printing the values of the stat structure regardless, meaning you get rubbish. You need to check the value of the typeflag, which you call "flags" in your nftw_stat routine to make sure that stat has successfully set the stat structure.
int nftw_stat(const char *path, const struct stat *stat, int typeflag,
struct FTW *ftw)
{
if (typeflag == FTW_NS) {
printf("stat failed on %s\n", path);
return 1;
}
if (strcmp(path, "/home/pf/.gvfs\0") == 0) {
printf("nftw()\n");
printf("mode = %d\n", stat->st_mode);
printf("size = %d\n", (int) stat->st_size);
}
return 0;
}

Resources