blob: 7412a86fd124dd7a581a0227efa03e1d0c231f86 [file] [log] [blame]
Erik Andersene49d5ec2000-02-08 19:58:47 +00001/* vi: set sw=4 ts=4: */
Eric Andersenf6be9441999-10-13 21:12:06 +00002/*
3 * Mini ln implementation for busybox
4 *
Eric Andersen8ec10a92001-01-27 09:33:39 +00005 * Copyright (C) 1999,2000,2001 by Lineo, inc.
Eric Andersenc4996011999-10-20 22:08:37 +00006 * Written by Erik Andersen <andersen@lineo.com>, <andersee@debian.org>
Eric Andersenf6be9441999-10-13 21:12:06 +00007 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 *
22 */
23
Eric Andersencc8ed391999-10-05 16:24:54 +000024#include <stdio.h>
Eric Andersenf6be9441999-10-13 21:12:06 +000025#include <dirent.h>
Eric Andersened3ef502001-01-27 08:24:39 +000026#include <string.h>
27#include <stdlib.h>
Eric Andersencc8ed391999-10-05 16:24:54 +000028#include <errno.h>
Eric Andersened3ef502001-01-27 08:24:39 +000029#include <unistd.h>
Eric Andersencbe31da2001-02-20 06:14:08 +000030#include "busybox.h"
Eric Andersencbe31da2001-02-20 06:14:08 +000031
Eric Andersencc8ed391999-10-05 16:24:54 +000032
Mark Whitley59ab0252001-01-23 22:30:04 +000033static const int LN_SYMLINK = 1;
34static const int LN_FORCE = 2;
35static const int LN_NODEREFERENCE = 4;
Glenn L McGrathaa3908d2000-10-04 09:34:35 +000036
37/*
38 * linkDestName is where the link points to,
39 * linkSrcName is the name of the link to be created.
40 */
Eric Andersend69d2da2001-02-15 20:12:05 +000041static int fs_link(const char *link_destname, const char *link_srcname,
42 const int flag)
Glenn L McGrathaa3908d2000-10-04 09:34:35 +000043{
44 int status;
Eric Andersend69d2da2001-02-15 20:12:05 +000045 int src_is_dir;
46 char *src_name;
Glenn L McGrathaa3908d2000-10-04 09:34:35 +000047
Eric Andersend69d2da2001-02-15 20:12:05 +000048 if (link_destname==NULL)
Glenn L McGrathaa3908d2000-10-04 09:34:35 +000049 return(FALSE);
50
Eric Andersend69d2da2001-02-15 20:12:05 +000051 src_name = (char *) xmalloc(strlen(link_srcname)+strlen(link_destname)+1);
Glenn L McGrathaa3908d2000-10-04 09:34:35 +000052
Eric Andersend69d2da2001-02-15 20:12:05 +000053 if (link_srcname==NULL)
54 strcpy(src_name, link_destname);
Glenn L McGrathaa3908d2000-10-04 09:34:35 +000055 else
Eric Andersend69d2da2001-02-15 20:12:05 +000056 strcpy(src_name, link_srcname);
Glenn L McGrathaa3908d2000-10-04 09:34:35 +000057
58 if (flag&LN_NODEREFERENCE)
Eric Andersend69d2da2001-02-15 20:12:05 +000059 src_is_dir = is_directory(src_name, TRUE, NULL);
Glenn L McGrathaa3908d2000-10-04 09:34:35 +000060 else
Eric Andersend69d2da2001-02-15 20:12:05 +000061 src_is_dir = is_directory(src_name, FALSE, NULL);
Glenn L McGrathaa3908d2000-10-04 09:34:35 +000062
Eric Andersend69d2da2001-02-15 20:12:05 +000063 if ((src_is_dir==TRUE)&&((flag&LN_NODEREFERENCE)==0)) {
64 char* srcdir_name;
65
66 srcdir_name = xstrdup(link_destname);
67 strcat(src_name, "/");
68 strcat(src_name, get_last_path_component(srcdir_name));
69 free(srcdir_name);
Glenn L McGrathaa3908d2000-10-04 09:34:35 +000070 }
71
72 if (flag&LN_FORCE)
Eric Andersend69d2da2001-02-15 20:12:05 +000073 unlink(src_name);
Glenn L McGrathaa3908d2000-10-04 09:34:35 +000074
75 if (flag&LN_SYMLINK)
Eric Andersend69d2da2001-02-15 20:12:05 +000076 status = symlink(link_destname, src_name);
Glenn L McGrathaa3908d2000-10-04 09:34:35 +000077 else
Eric Andersend69d2da2001-02-15 20:12:05 +000078 status = link(link_destname, src_name);
Glenn L McGrathaa3908d2000-10-04 09:34:35 +000079
80 if (status != 0) {
Eric Andersend69d2da2001-02-15 20:12:05 +000081 perror_msg(src_name);
Glenn L McGrathaa3908d2000-10-04 09:34:35 +000082 return(FALSE);
83 }
84 return(TRUE);
85}
Eric Andersenf6be9441999-10-13 21:12:06 +000086
87extern int ln_main(int argc, char **argv)
Eric Andersencc8ed391999-10-05 16:24:54 +000088{
Eric Andersen13241df2000-10-04 16:02:53 +000089 int status = EXIT_SUCCESS;
Glenn L McGrathaa3908d2000-10-04 09:34:35 +000090 int flag = 0;
91 int opt;
92
Erik Andersene49d5ec2000-02-08 19:58:47 +000093 /* Parse any options */
Glenn L McGrathaa3908d2000-10-04 09:34:35 +000094 while ((opt=getopt(argc, argv, "sfn")) != -1) {
95 switch(opt) {
96 case 's':
97 flag |= LN_SYMLINK;
98 break;
99 case 'f':
100 flag |= LN_FORCE;
101 break;
102 case 'n':
103 flag |= LN_NODEREFERENCE;
104 break;
105 default:
Eric Andersen67991cf2001-02-14 21:23:06 +0000106 show_usage();
Eric Andersen815e9042000-06-06 16:15:23 +0000107 }
Erik Andersenfac10d72000-02-07 05:29:42 +0000108 }
Eric Andersend69d2da2001-02-15 20:12:05 +0000109 if (optind > (argc-1)) {
110 show_usage();
111 }
112 if (optind == (argc-1)) {
113 if (fs_link(argv[optind],
114 get_last_path_component(argv[optind]), flag)==FALSE)
115 status = EXIT_FAILURE;
116 }
Glenn L McGrathaa3908d2000-10-04 09:34:35 +0000117 while(optind<(argc-1)) {
118 if (fs_link(argv[optind], argv[argc-1], flag)==FALSE)
Eric Andersen13241df2000-10-04 16:02:53 +0000119 status = EXIT_FAILURE;
Glenn L McGrathaa3908d2000-10-04 09:34:35 +0000120 optind++;
Eric Andersen815e9042000-06-06 16:15:23 +0000121 }
Eric Andersend69d2da2001-02-15 20:12:05 +0000122 exit(status);
Eric Andersencc8ed391999-10-05 16:24:54 +0000123}
Erik Andersen029011b2000-03-04 21:19:32 +0000124
125/*
126Local Variables:
127c-file-style: "linux"
128c-basic-offset: 4
129tab-width: 4
130End:
131*/