File: nsln.c

package info (click to toggle)
dpm-postgres 1.7.4.7-1
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 13,788 kB
  • ctags: 10,782
  • sloc: ansic: 146,136; sh: 13,362; perl: 11,142; python: 5,529; cpp: 5,113; sql: 1,790; makefile: 955; fortran: 113
file content (121 lines) | stat: -rw-r--r-- 2,574 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
/*
 * Copyright (C) 2003-2004 by CERN/IT/GD/CT
 * All rights reserved
 */
 
#ifndef lint
static char sccsid[] = "@(#)$RCSfile: nsln.c,v $ $Revision: 1.1.1.1 $ $Date: 2004/12/02 12:31:56 $ CERN IT-GD/CT Jean-Philippe Baud";
#endif /* not lint */

/*	nsln - make a symbolic link to a file or a directory */
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#if defined(_WIN32)
#include <winsock2.h>
#endif
#include "Cns.h"
#include "Cns_api.h"
#include "serrno.h"
extern	char	*getenv();
extern	int	optind;
int sflag;
main(argc, argv)
int argc;
char **argv;
{
	int c;
	int errflg = 0;
	char fullpath[CA_MAXPATHLEN+1];
	int i;
	char *lastarg;
	char *p;
	char path[CA_MAXPATHLEN+1];
	struct Cns_filestat statbuf;
#if defined(_WIN32)
	WSADATA wsadata;
#endif

	while ((c = getopt (argc, argv, "s")) != EOF) {
		switch (c) {
		case 's':
			sflag++;
			break;
		case '?':
			errflg++;
			break;
		default:
			break;
		}
	}
	if (! sflag)
		errflg++;
	if (errflg || optind >= argc) {
		fprintf (stderr,
		    "usage: %s -s file [link]\n\t%s -s file...directory\n",
		    argv[0], argv[0]);
		exit (USERR);
	}
	if (argc - optind > 2) {
		if (Cns_stat (argv[argc-1], &statbuf) < 0 ||
		    (statbuf.filemode & S_IFDIR) == 0) {
			fprintf (stderr, "target %s must be a directory\n",
			    argv[argc-1]);
			exit (USERR);
		}
	}
#if defined(_WIN32)
	if (WSAStartup (MAKEWORD (2, 0), &wsadata)) {
		fprintf (stderr, NS052);
		exit (SYERR);
	}
#endif
	argc -= optind;
	argv += optind;
	if (argc == 1)
		if (p = strrchr (argv[0], '/'))
			lastarg = p + 1;
		else
			lastarg = argv[0];
	else
		lastarg = argv[--argc];
	for (i = 0; i < argc; i++) {
		if (argc > 1)
			if (p = strrchr (argv[i], '/'))
				sprintf (path, "%s/%s", lastarg, p + 1);
			else
				sprintf (path, "%s/%s", lastarg, argv[i]);
		else
			strcpy (path, lastarg);
		if (*path != '/' && strstr (path, ":/") == NULL) {
			if ((p = getenv (CNS_HOME_ENV)) == NULL ||
			    strlen (p) + strlen (path) + 1 > CA_MAXPATHLEN) {
				fprintf (stderr, "%s: invalid path\n", path);
				errflg++;
				continue;
			} else
				sprintf (fullpath, "%s/%s", p, path);
		} else {
			if (strlen (path) > CA_MAXPATHLEN) {
				fprintf (stderr, "%s: %s\n", path,
				    sstrerror(SENAMETOOLONG));
				errflg++;
				continue;
			} else
				strcpy (fullpath, path);
		}
		if (Cns_symlink (argv[i], fullpath)) {
			fprintf (stderr, "%s: %s\n", path, sstrerror(serrno));
			errflg++;
		}
	}
#if defined(_WIN32)
	WSACleanup();
#endif
	if (errflg)
		exit (USERR);
	exit (0);
}