summaryrefslogtreecommitdiff
path: root/setup/dirsetup.c
blob: 38bcd2b2926cb53e2dfdb88da6e4e5dda6c2eb7a (plain)
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
/*
 *
 * This file is part of Open Sound System.
 *
 * Copyright (C) 4Front Technologies 1996-2008.
 *
 * This this source file is released under GPL v2 license (no other versions).
 * See the COPYING file included in the main directory of this source
 * distribution for the license terms and conditions.
 *
 */

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <dirent.h>
#include <time.h>

#if PATH_MAX == -1                                      
#undef PATH_MAX                                           
#endif
#ifndef PATH_MAX                                          
#define PATH_MAX 1024                                     
#endif

char *srcdir = NULL, *blddir = NULL;
int verbose = 0, copy_files = 0;
struct stat bld_stat, src_stat;

static void
copy_file (char *sname, char *tname, char *pname, int native_make)
{
  char *p;
  int in_fd, out_fd, l;

  unsigned char buf[4096];

  if (strcmp (pname, ".depend") == 0)
    return;

  if (!native_make)
    if (strncmp (pname, "Makefile", 8) == 0)
      return;

  if (strlen (pname) > 6)
    {
      p = pname + strlen (pname) - 6;	/* Seek to the _cfg.[c-h] suffix */
      if (strcmp (p, "_cfg.c") == 0)
	return;
      if (strcmp (p, "_cfg.h") == 0)
	return;
    }

  if (!copy_files)
  {
     symlink (sname, tname);
     return;
  }

  if ((in_fd=open(sname, O_RDONLY, 0))==-1)
     {
	     perror(sname);
	     exit(-1);
     }

  if ((out_fd=creat(tname, 0644))==-1)
     {
	     perror(tname);
	     exit(-1);
     }

  while ((l=read(in_fd, buf, sizeof(buf)))>0)
  {
	  if (write(out_fd, buf, l)!=l)
	     {
		     perror(tname);
		     exit(-1);
	     }
  }

  if (l==-1)
     {
	     perror(sname);
	     exit(-1);
     }

  close(in_fd);
  close(out_fd);
}

static void
copy_tree (char *fromdir, char *tgtdir, int native_make)
{
  DIR *dir;
  struct dirent *de;
  struct stat st;

  if (tgtdir != NULL)
    {
      mkdir (tgtdir, 0700);
    }

  if ((dir = opendir (fromdir)) == NULL)
    {
      fprintf (stderr, "Bad source directory %s\n", fromdir);
      return;
    }

  while ((de = readdir (dir)) != NULL)
    {
      char sname[PATH_MAX+20], tname[PATH_MAX+20];

      sprintf (sname, "%s/%s", fromdir, de->d_name);
      if (tgtdir != NULL)
	sprintf (tname, "%s/%s", tgtdir, de->d_name);
      else
	sprintf (tname, "%s", de->d_name);

      if (stat (sname, &st) == -1)
	{
	  perror (sname);
	  exit (-1);
	}

      if ((st.st_dev == bld_stat.st_dev) && (st.st_ino == bld_stat.st_ino)) continue;
      if ((st.st_dev == src_stat.st_dev) && (st.st_ino == src_stat.st_ino)) continue;

      if (S_ISDIR (st.st_mode))
	{
	  if (de->d_name[0] != '.')
	    {
	      char tmp[PATH_MAX+20];
	      int is_native = 0;
	      struct stat st2;

	      sprintf (tmp, "%s/.nativemake", sname);
	      if (stat (tmp, &st2) != -1)
		is_native = 1;

	      sprintf (tmp, "%s/.nocopy", sname);
	      if (stat (tmp, &st2) == -1)
		copy_tree (sname, tname, is_native);
	    }
	}
      else
	{
	  copy_file (sname, tname, de->d_name, native_make);
	}
    }

  closedir (dir);
}

int
main (int argc, char *argv[])
{
  int i;
  FILE *f;

  time_t t;
  struct tm *tm;

  if (argc < 3)
    {
      fprintf (stderr, "%s: Bad usage\n", argv[0]);
      exit (-1);
    }

  srcdir = argv[1];
  blddir = argv[2];

  if (stat (blddir, &bld_stat) == -1)
    {
      perror (blddir);
      exit (-1);
    }

  if (stat (srcdir, &src_stat) == -1)
    {
      perror (srcdir);
      exit (-1);
    }

  for (i = 3; i < argc; i++)
    {
      if (strcmp (argv[i], "-v") == 0)
	{
	  verbose++;
	  continue;
	}

      if (strcmp (argv[i], "-c") == 0)
	{
	  copy_files = 1;
	  continue;
	}
    }

  f = fopen (".nocopy", "w");
  fclose (f);
  copy_tree (srcdir, NULL, 0);

#if 0
  if ((f = fopen ("timestamp.h", "w")) == NULL)
    {
      perror ("timestamp.h");
      exit (-1);
    }

  time (&t);
  tm = gmtime (&t);
  fprintf (f, "#define OSS_COMPILE_DATE \"%04d%02d%02d%02d%02d\"\n",
	   tm->tm_year + 1900,
	   tm->tm_mon + 1, tm->tm_mday, tm->tm_hour, tm->tm_min);

  fclose (f);
#endif

  exit (0);
}