How to create hard/soft links in C? - c

I need to create a hard link to an existing file or directory.
I also need to create a soft/symbolic link to an existing file or directory. This is part of a larger program which is shown below. Commands are executed by typing "-f pathname linkname" and each break argument is a different command. I believe the functions -f, -d and -h have been created correctly so far. However I am having trouble creating these links.
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/stat.h>
#include <errno.h>
#include <sys/types.h>
#include <string.h>
#include <fcntl.h>
//Handles filename, absolute pathname
//Need relative pathname for -f
int main(int argc, char **argv)
{
char command[200], flag[20], pathname[100], linkname[100];
struct stat st = {0};
char cmd[200];
char *token; //Pointer
int counter = 1; //Counter variable
FILE *fp;
char mode2[] = "0750"; //To set the permission of a file/path
long j;
char mode[] = "0640"; //To set the permission of a file/path
long i;
printf("Enter command: ");
fgets(cmd, 420, stdin);
//User input is tokenized to determine the proper commands are entered and executed
token = strtok(cmd, " "); //Input is tokenized by white spaces.
if(token == NULL)
{
printf("Error with command input.\n");
exit(EXIT_FAILURE);
}
strcpy(command, token);
token = strtok(NULL, " ");
if(token != NULL)
strcpy(flag, token);
token = strtok(NULL, " ");
if(token != NULL)
strcpy(pathname, token);
token = strtok(NULL, " ");
if(token != NULL)
strcpy(linkname, token);
//Switch statement to determine which command the user is choosing and execute that command.
switch(flag[1]) {
//The f case will create a file who's name is chosen by the user.
case 'f':
fp=fopen(pathname,"w");
fclose(fp);
char mode[] = "0640"; //Sets the permission of file to 0640.
i = strtol(mode, 0, 8);
if (chmod(pathname, i) < 0)
{
fprintf(stderr, "%s: error in chmod(%s, %s) - %d (%s)\n",
argv[0], pathname, mode, errno, strerror(errno));
return -1;
}
return 0;
break;
//The d case will create a new directory chosen by the user.
case 'd':
if (stat(pathname, &st) == -1) {
mkdir(pathname, 0750); //Directory is given permission 0750.
}
j = strtol(mode, 0, 8);
if (chmod (pathname,j) < 0)
{
fprintf(stderr, "%s: error in chmod(%s, %s) - %d (%s)\n",
argv[0], pathname, mode, errno, strerror(errno));
return -1;
}
return 0;
break;
//The h case will create a hardlink to an existing file.
case 'h':
char *pathname; //Existing file
char *linkname; //Name of desired hardlink
int hlink; //Stores path
hlink = link(pathname, linkname); //Links linkname to pathname
if (chmod (pathname,j) < 0)
{
fprintf(stderr, "%s: error linking(%s, %s) - %d (%s)\n",
argv[0], pathname, mode, errno, strerror(errno));
return -1;
}
return 0;
break;
//The s case will create a symbol link to an existing file.
case 's':
char *pathname;
char *linkname;
int slink;
slink = symlink(pathname, linkname); //Using the symlink function to create a symbolic link of pathname
if (chmod (pathname,j) < 0)
{
fprintf(stderr, "%s: error linking(%s, %s) - %d (%s)\n",
argv[0], pathname, mode, errno, strerror(errno));
return -1;
}
return 0;
break;
}
}

POSIX
http://pubs.opengroup.org/onlinepubs/009695399/functions/link.html
http://pubs.opengroup.org/onlinepubs/009695399/functions/symlink.html
#include <unistd.h>
char *path1 = "/home/cnd/mod1";
char *path2 = "/modules/pass1";
int status;
...
status = link (path1, path2);
Windows
https://learn.microsoft.com/en-us/windows/desktop/api/winbase/nf-winbase-createhardlinka
https://learn.microsoft.com/en-us/windows/desktop/api/winbase/nf-winbase-createsymboliclinka
BOOL fCreatedLink = CreateHardLink( pszNewLinkName,
pszExistingFileName,
NULL ); // reserved, must be NULL
if ( fCreatedLink == FALSE )
{
;// handle error condition
}

Related

Linux terminal tool dosent run one of the getopt commands

So I have a simple terminal tool here that makes use of getopt but when I run it everything runs smoothly except "cast -s" it returns "cast: invalid option -- 's'" but it fails silently without any error messages. Ive tried it on different hardware, operating systems and more but it dosent seem to work just right
#include <errno.h>
#include <getopt.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <dirent.h>
#define DATA_SIZE 1000
void print_help(void)
{
printf("Help\n");
printf("> cast -d (deletes file)\n");
printf("> cast -r (renames file)\n");
printf("> cast -c (create new file)\n");
printf("> cast -s (scans for file in directory)\n");
printf("________________________________________\n");
printf("Find an error or a bug? please submit it in the issues section on github\n");
}
int main(int argc, char **argv)
{
int option_val = 0;
int opt_delete = 0;
int opt_help = 0;
int opt_rename = 0;
int opt_create = 0;
int opt_scan = 0;
while ((option_val = getopt(argc, argv, "dhrc")) != -1) {
switch (option_val) {
case 'd':
char filename[65]; //Hope your filename isnt bigger than this
printf("Filename or path to file: ");
scanf("%s", filename); // checks to see if your filename isnt bigger than specified
if (remove(filename) != 0)
{
fprintf(stderr, "Errno: %d\n", errno);
perror("Error msg");
} else printf("%s, deleted.\n", filename);
opt_delete = 1;
break;
case 's':
{
DIR *dp;
struct dirent *dirp;
if (argc != 2) {
fprintf(stderr, "usage: s directory_name\n");
return 1;
}
if ((dp = opendir(argv[1])) == NULL) {
perror("cant copen");
return 2;
}
while((dirp = readdir(dp)) != NULL)
printf("%s\n", dirp->d_name);
closedir(dp);
return 0;
}
opt_scan = 1;
break;
case 'r':
char file[65], new[65];
printf("File: ");
scanf("%s", file);
printf("New name: ");
scanf("%s", new);
if (rename(file, new) != 0)
{
fprintf(stderr, "Errno: %d\n", errno);
perror("Error msg");
} else printf("%s --> %s", file, new);
opt_rename = 1;
break;
case 'c':
FILE *f = fopen("Castdocument.txt", "w+");
fprintf(f, "Finished with maybe no errors? Rename this file to whatever you would like and change the filename extension with ""cast -r""");
printf("File created! (Check your home directory for ""Castdocument.txt"" file and modify that to fit your needs)");
fclose(f);
opt_create = 1;
break;
case 'h':
opt_help = 1;
break;
default: /* '?' */
//print_help();
}
}
if (opt_delete) {
printf("\n");
} if (opt_rename) {
printf("\n");
} if (opt_help) {
print_help();
} if (opt_create) {
printf("\n");
}
}
Ive attempted to run this on a different system to no avail

could not stat file - c

I need to stat a file to get the size of it. I also need to provide the name of the file as a command line argument. This is my code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
int main (int argc, char* argv[])
{
int N = 300;
int L = 1000;
char Nseq[N][L];
FILE *myfile;
char *token;
const char s[2] = ",";
char *line;
int lenline;
char filename[100];
strcpy(filename, "/path/");
char name[100];
strcpy(name, argv[1]);
strcat(filename, name);
strcat(filename, ".txt");
printf("%s\n", filename);
int err;
struct stat st;
int n = 0;
err = stat(filename,&st);
if (err < 0) {
printf("could not stat file %s", filename);
exit(1);
}
lenline = st.st_size + 1;
line = malloc(lenline);
myfile = fopen(filename, "r");
if (myfile == NULL) {
printf("could not open file %s", filename);
exit(1);
}
while (fgets(line, lenline, myfile) != NULL) {
token = strtok(line, s);
while (token != NULL && n<N) {
strcpy(Nseq[n], token);
printf("%s\t%u\n", token, n);
token = strtok(NULL, s);
n++;
}
}
fclose(myfile);
return 0;
}
The output I get is:
/path/file.txt
could not stat file /path/file.txt
Does anyone know why is this happening?
how can I fix it?
thank you!
The manual page of stat (2) says: On success, zero (0) is returned. On error, -1 is returned, and errno is set appropriately.
You don't actually use errno and basically are causing your own error message to be a rather unhelpful variant of "something went wrong".
Actually use errno, implicitly by calling
perror("stat");
or explicitly by calling
fprintf(stderr, "could not stat file %s: %s", filename, strerror(errno));
The underlying problem, most likely, is that you're prepending /path and appending .txt and there is no actual file at the path you're constructing before calling stat. If you focus only on successfully stating the file, try this:
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <sys/stat.h>
int main (int argc, char** argv) {
const char* filename = argv[1];
printf("Calling stat(%s)...", filename);
int err;
struct stat st;
err = stat(filename, &st);
if (err < 0) {
printf("failed with error %d (%s)\n", err, strerror(errno));
return err;
} else {
printf("succeeded\n");
return 0;
}
}
At the very least you'll see exactly why stat is failing and it will help show why you're code isn't working properly.

A C program that copies all the content a directory including files and folders

The last Code I just posted now works. That is, it is able to copy all files from one directory to another. But now, I wanted to update it in such a way that it copies also directories including it contents be it files or folders.
Here is what I did so far, but this has been unable to accomplish my dream.
I really don't know what is wrong with the code.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <dirent.h>
#include <fcntl.h>
#include <unistd.h>
#define Max 8192
int copy_files(char *src, char *dest);
int copy_dir(char *srcpath, char *destpath);
int copy_dir(char *srcpath, char *destpath)
{
DIR *sdp = NULL;
DIR *ddp = NULL;
struct dirent *entry;
struct stat sb;
char tempsrc[strlen(srcpath)+1];
char tempdest[strlen(destpath)+1];
strcat(srcpath, "/");
strcat(destpath, "/");
strcpy(tempdest, destpath);
strcpy(tempsrc, srcpath);
if( (sdp = opendir(srcpath)) == NULL )
{
printf ("%s is not an existing directory\n", srcpath);
return 0;
}
else
{
while( (entry = readdir(sdp)) )
{
stat(entry->d_name, &sb);
// printf("Cannot open directory\n");
// exit(EXIT_FAILURE);
switch (sb.st_mode & S_IFMT)
{
case S_IFREG:
{
strcat(tempdest, entry->d_name);
strcat(tempsrc, entry->d_name);
copy_files(tempsrc, tempdest);
strcpy(tempdest, destpath);
strcpy(tempsrc, srcpath);
break;
}
case S_IFDIR:
{
strcat(tempsrc, entry->d_name);
strcat(tempdest, entry->d_name);
mkdir(tempdest, 0777);
ddp = opendir(tempdest);
copy_dir(tempsrc, tempdest);
strcpy(tempdest, destpath);
strcpy(tempsrc, srcpath);
break;
}
}
}
closedir(sdp);
closedir(ddp);
return 1;
}
}
int copy_files(char *src, char *dest)
{
int sfd, dfd, ret_in, ret_out;
char buff[Max];
if ( (sfd = open(src, O_RDONLY)) == -1 )
{
printf("Error while reading %s\n", src);
perror(src);
exit(1);
}
if ( (dfd = creat(dest, 0644)) == -1 )
{
printf("Error while creating %s\n", dest);
perror(dest);
exit(1);
}
while( (ret_in = read(sfd, &buff, Max)) > 0 )
{
ret_out = write (dfd, &buff, ret_in);
if (ret_out != ret_in)
{
printf("write error to %s", dest);
perror(dest);
exit(1);
}
if (ret_in == -1)
{
printf("read error from %s", src);
perror(src);
exit(1);
}
}
close(sfd);
close(dfd);
return 1;
}
int main(int argc, char *argv[])
{
int i;
if (argc != 3)
{
printf ("Usage: Programme_name src dest\n e.g. ./cp src dest\n");
exit(1);
}
char *srcp = argv[1];
char *destp = argv[2];
if (srcp[0] == '/' && destp[0] == '/')
{
for (i = 1; i <= strlen(destp); i++)
destp[(i-1)] = destp[i];
for (i = 1; i <= strlen(srcp); i++)
srcp[(i-1)] = srcp[i];
copy_dir(srcp, destp);
}
else if (srcp[0] != '/' && destp[0] == '/') //./ass1 test /t2
{
for (i = 1; i <= strlen(destp); i++)
destp[i-1] = destp[i];
strcat(destp, "/");
strcat(destp, srcp);
copy_files(srcp, destp);
}
else
{
printf ("Usage: Programme_name src dest\n e.g. ./cp src dest\n");
exit(1);
}
}
You are indefinitely adding /. to the temporary source and destination paths when the directory entry . is read, which is present in all directories. Instead, you should skip the . and .. entries.
Another error is the dimensioning of the temporary paths:
char tempsrc[strlen(srcpath)+1];
char tempdest[strlen(destpath)+1];
The arrays are made just long enough to hold the original paths, though sub-directory names are then appended, overflowing the arrays. Better:
char tempsrc[PATH_MAX];
char tempdest[PATH_MAX];

Run Constant Number of Threads at a Time in C

I am trying to copy files from one directory to another using pthreads. Each thread is responsible for copying exactly one file. The maximum number of threads is specified via a command line argument.
What I want to do is IF the current threads is less than the max threads, create a thread to do work. ELSE, wait for the current threads and when one of them finishes, decrease the number of current threads.
I can't figure out how to wait for a thread via pthread_join without blocking the main thread.
Here is what I have so far:
#include <stdlib.h>
#include <stdio.h>
#include <dirent.h>
#include <stdlib.h>
#include <stdio.h>
#include <dirent.h>
#include <sys/stat.h>
#include <string.h>
#include <errno.h>
#include <fcntl.h>
#include <pthread.h>
#define MAXNAME 80
#define R_FLAGS O_RDONLY
#define W_FLAGS (O_WRONLY | O_CREAT)
#define W_PERMS (S_IRUSR | S_IWUSR)
#define KILOBYTE 0.001
void *copyfilepass(void *arg);
int main(int argc, char* argv[]){
int num_threads; //number of threads to execute in parallel
int cur_threads = 0; //number of threads currently executing
char filename[MAXNAME]; //will temporarily hold the file name
DIR* source; //pointer to the source directory
DIR* dest; //pointer to the destination directory
struct dirent* dentry; //pointer to the internal structure of the source directory
struct stat fst; //stats for each file in a directory
int error;
void *status;
//***BEGIN ERROR CHECKING***
if (argc != 4) {
fprintf(stderr, "Usage: %s sourceDir destDir numThreads\n", argv[0]);
return 1;
}
//check if source directory name is too long
if ( snprintf(filename, MAXNAME, "%s", argv[1]) == MAXNAME ) {
fprintf(stderr, "Source directory name %s too long\n", argv[1]);
return 1;
}
//check if destination directory name is too long
if ( snprintf(filename, MAXNAME, "%s", argv[2]) == MAXNAME ) {
fprintf(stderr, "Source directory name %s too long\n", argv[2]);
return 1;
}
//check if we can successfully open the source directory
if( (source = opendir(argv[1])) == NULL ) {
fprintf(stderr, "Error opening source directory %s\n", argv[1]);
return 1;
}
//check if we can successfully open the destination directory
if( (dest = opendir(argv[2])) == NULL ) {
fprintf(stderr, "Error opening destination directory %s\n", argv[2]);
return 1;
}
//***END ERROR CHECKING***
num_threads = atoi(argv[3]);
while( (dentry = readdir(source)) != NULL ){
//source path
char* path = (char*)malloc(sizeof(char) * (strlen(dentry->d_name) + strlen(argv[1]) + 2)); //need '.' + '/' + '\0'
sprintf(path, "%s%c%s", argv[1], '/', dentry->d_name);
//destination path
char* dest_path = (char*)malloc(sizeof(char) * (strlen(dentry->d_name) + strlen(argv[2]) + 2)); //need '.' + '/' + '\0'
sprintf(dest_path, "%s%c%s", argv[2], '/', dentry->d_name);
if(!stat(path, &fst)){ //stat() return 0 if successful
if(S_ISREG(fst.st_mode)){
int args[3];
pthread_t tid;
if ( (args[0] = open(path, R_FLAGS)) == -1 ) {
fprintf(stderr, "Failed to open source file %s: %s\n", path, strerror(errno));
continue;
}
if ( (args[1] = open(dest_path, W_FLAGS, W_PERMS)) == -1 ) {
fprintf(stderr, "Failed to open destination file %s: %s\n", dest_path, strerror(errno));
continue;
}
if(cur_threads < num_threads) {
++cur_threads;
if ( (error = pthread_create((&tid), NULL, copyfilepass, args)) ) {
--cur_threads;
fprintf(stderr, "Failed to create thread: %s\n", strerror(error));
tid = pthread_self(); /* cannot be value for new thread */
}
printf("file: %.03fKB %s\n", (fst.st_size * KILOBYTE), path);
}
}
}
}
//close directory
closedir(source);
return 0;
}
Better than spawning and reaping threads is to just create a fixed-size pool at the beginning and have them all consume from a work queue. This will reduce overhead and simplify your code.
By the way, using threads to solve this problem may not improve performance, depending on the filesystem you're operating on. Food for thought.

Opening a file using relative path

The following code is supposed to work as follows: print the list of the files in a directory, and print the content of each .c file.
it works fine when executed in UNIX for the same directory: ./a.out ./
However, I was not able to make it work for ./a.out ../differentDir execution.
I know that if the absolute path is provided as an argument, I could use argv[1] for that. However, when it is provided in a form of a relative path I am lost.
#include <sys/types.h>
#include <dirent.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#define BUFFSIZE 32768
int main(int argc, char **argv) {
char buf[BUFFSIZE];
DIR *dp;
struct dirent *dirp;
char filename[80];
int name_length;
FILE *fp;
if (argc != 2) {
fprintf(stderr, "usage: %s dir_name\n", argv[0]);
exit(1);
}
if ((dp = opendir(argv[1])) == NULL ) {
fprintf(stderr, "can't open '%s'\n", argv[1]);
exit(1);
}
while ((dirp = readdir(dp)) != NULL ){
printf("%s\n", dirp->d_name);
memset(filename, '\0', sizeof(filename));
strcpy(filename, dirp->d_name);
printf(" ** %s ", filename);
name_length = strlen(filename);
printf(" name_length=%d \n", name_length);
if (findC(filename)) // checking if the file has a .c extension
{
fp=fopen(filename, "r");
if (fp == NULL)
fprintf(stderr, "Can't open .C file!\n");
else
{// if the file was opened successfuly:
do
{
fgets(buf,BUFFSIZE,fp); // reading each line until buffer is full or until reaching whitespace
buf[strlen(buf)-1]='\0'; // removing the trailing whitespace from the buffer
puts(buf);
}
while (!feof(fp));
printf("\n\n");
fclose(fp);
}
}
}
closedir(dp);
return(0);
}
/*FindC method gets a c-string that represents a file name; returns 1 if the file ends with .C extension, else returns 0*/
int findC(char * name)
{
int len = strlen(name);
if (len>=2 && name[len-2]=='.' && tolower(name[len-1])=='c')
return 1;
return 0;
}
Upon opening the file to read, the file pathname needs to also be relative.
// Form prefix for complete relative file name
char filename[MAXPATH];
strcpy(filename, argv[1]);
// append '/' if directory path does not end in '/'
if (TBD_code(filename)) {
strcat(filename, "/");
}
char *end = filename[strlen(filename)];
while ((dirp = readdir(dp)) != NULL ){
printf("%s\n", dirp->d_name);
if (findC(dirp->d_name)) {
// append filename to prefix
strcpy(end, dirp->d_name);
fp=fopen(filename, "r");
...
You can use realpath(argv1...) like in this example. realpath will return the absolute path for a relative path.
#include <stdlib.h>
#include <stdio.h>
#include <limits.h>
int main(int argc, char **argv) {
char *path = "../..";
char buff[PATH_MAX + 1]; /* not sure about the "+ 1" */
char *res = realpath(path, buff);
if (res) {
printf("This source is at %s.\n", buff);
} else {
perror("realpath");
exit(EXIT_FAILURE);
}
return 0;
}
To include the desired behavior in your program, you can use realpathin your code:
#include <stdlib.h>
#include <stdio.h>
#include <limits.h>
#include <ctype.h>
#include <dirent.h>
#include <string.h>
#define BUFFSIZE 32768
int main(int argc, char **argv) {
char buf[BUFFSIZE];
DIR *dp;
struct dirent *dirp;
char filename[80];
int name_length;
FILE *fp;
char buff[PATH_MAX + 1]; /* not sure about the "+ 1" */
if (argc != 2) {
fprintf(stderr, "usage: %s dir_name\n", argv[0]);
exit(1);
}
char *res = realpath(argv[1], buff);
if ((dp = opendir(res)) == NULL ) {
fprintf(stderr, "can't open '%s'\n", argv[1]);
exit(1);
}
while ((dirp = readdir(dp)) != NULL ){
printf("%s\n", dirp->d_name);
memset(filename, '\0', sizeof(filename));
strcpy(filename, dirp->d_name);
printf(" ** %s ", filename);
name_length = strlen(filename);
printf(" name_length=%d \n", name_length);
if (findC(filename)) // checking if the file has a .c extension
{
fp=fopen(filename, "r");
if (fp == NULL)
fprintf(stderr, "Can't open .C file!\n");
else
{// if the file was opened successfuly:
do
{
fgets(buf,BUFFSIZE,fp); // reading each line until buffer is full or until reaching whitespace
buf[strlen(buf)-1]='\0'; // removing the trailing whitespace from the buffer
puts(buf);
}
while (!feof(fp));
printf("\n\n");
fclose(fp);
}
}
}
closedir(dp);
return(0);
}
/*FindC method gets a c-string that represents a file name; returns 1 if the file ends with .C extension, else returns 0*/
int findC(char * name)
{
int len = strlen(name);
if (len>=2 && name[len-2]=='.' && tolower(name[len-1])=='c')
return 1;
return 0;
}
You could first change to the directory chdir either with relative or absolute path and the get the absolute path via the getcwd
#include <sys/types.h>
#include <string.h>
#include <dirent.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#define BUFFSIZE 32768
#define PATH_SIZE 512
int main(int argc, char **argv) {
char buf[BUFFSIZE];
char path[PATH_SIZE];
DIR *dp;
struct dirent *dirp;
char filename[80];
int name_length, r;
FILE *fp;
if (argc != 2) {
fprintf(stderr, "usage: %s dir_name\n", argv[0]);
exit(1);
}
strcpy(path, argv[1]);
r = chdir(path);
if( r != 0 )
{
printf("Invalid path '%s'\n",path);
exit(1);
}
getcwd(path,PATH_SIZE);
if ((dp = opendir(path)) == NULL ) {
fprintf(stderr, "can't open '%s'\n", argv[1]);
exit(1);
}
while ((dirp = readdir(dp)) != NULL ){
printf("%s\n", dirp->d_name);
memset(filename, '\0', sizeof(filename));
strcpy(filename, dirp->d_name);
printf(" ** %s ", filename);
name_length = strlen(filename);
printf(" name_length=%d \n", name_length);
if (findC(filename)) // checking if the file has a .c extension
{
fp=fopen(filename, "r");
if (fp == NULL)
fprintf(stderr, "Can't open .C file!\n");
else
{// if the file was opened successfuly:
do
{
fgets(buf,BUFFSIZE,fp); // reading each line until buffer is full or until reaching whitespace
buf[strlen(buf)-1]='\0'; // removing the trailing whitespace from the buffer
puts(buf);
}
while (!feof(fp));
printf("\n\n");
fclose(fp);
}
}
}
closedir(dp);
return(0);
}
/*FindC method gets a c-string that represents a file name; returns 1 if the file ends with .C extension, else returns 0*/
int findC(char * name)
{
int len = strlen(name);
if (len>=2 && name[len-2]=='.' && tolower(name[len-1])=='c')
return 1;
return 0;
}

Resources