如何在C中创建硬/软链接?

时间:2018-12-04 19:23:23

标签: c symlink hardlink

我需要创建一个到现有文件或目录的硬链接。 我还需要创建到现有文件或目录的软/符号链接。这是较大程序的一部分,如下所示。通过键入“ -f pathname linkname”来执行命令,每个break参数是一个不同的命令。我相信到目前为止,函数-f,-d和-h已经正确创建。但是,我在创建这些链接时遇到了麻烦。

#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;
  }
}

1 个答案:

答案 0 :(得分:3)

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://docs.microsoft.com/en-us/windows/desktop/api/winbase/nf-winbase-createhardlinka

https://docs.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
}