summaryrefslogtreecommitdiffstats
path: root/dirtree.c
blob: b8c28f052b62e216e16b187e26c2d61e3f729266 (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
#ident "$Id$"
/* ----------------------------------------------------------------------- *
 *   
 *   Copyright 2001 H. Peter Anvin - All Rights Reserved
 *
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, Inc., 675 Mass Ave, Cambridge MA 02139,
 *   USA; either version 2 of the License, or (at your option) any later
 *   version; incorporated herein by reference.
 *
 * ----------------------------------------------------------------------- */

/*
 * dirtree.c
 *
 * Scan the input directory and create the appropriate structure
 */

#include <inttypes.h>
#include <dirent.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <ctype.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <string.h>
#include "dirtree.h"
#include "xmalloc.h"

extern const char *program;

static int
dos_mangle_name(const char *name, char *buf, uint32_t numtail)
{
  const char *dosforbidden =
    "\001\002\003\004\005\006\007\010\011\012\013\014\015\016\017"
    "\020\021\022\023\024\025\026\027\030\031\032\033\034\035\036\037"
    " \"\'\\/[]|<>,*$+";
  const char *dot;
  char *cp, *endp;
  char xc;
  const char numtail_str[9];

  dot = strrchr(name, '.');	/* Find last dot */
  if ( dot == name )
    dot = NULL;			/* Leading dot? */

  cp = buf;  endp = buf+11;
  memset(buf, ' ', 11);
  *endp = '\0';

  for ( ; *name ; name++ ) {
    xc = *name;
    if ( xc == '.' ) {
      if ( name == dot ) {
	/* Last dot */
	cp = buf+8;
	cp[0] = ' ';
	cp[1] = ' ';
	cp[2] = ' ';
	continue;
      } else {
	xc = '_';
      }
    }

    if ( islower(xc) ) {
      xc = toupper(xc);
    }

    if ( strchr(dosforbidden, xc) ) {
      xc = '_';
    }

    if ( (unsigned char)xc == 0xe5 && cp == buf ) {
      xc = 0x05;		/* Only applies to the first character... */
    }

    if ( cp < endp )
      *cp++ = xc;
  }

  if ( numtail ) {
    int nc = sprintf(numtail_str, "~%X", numtail);
    cp = buf+8-nc;
    while ( cp > buf && cp[-1] == ' ' ) cp--;
    memcpy(cp, numtail_str, nc);
  }

  return 0;
}


struct direntry *
make_dir_tree(const char *root, int *dirsizeptr, struct direntry *parent)
{
  DIR *dirp;
  struct dirent *de;
  struct direntry *thisent;
  struct direntry *tree = NULL;
  struct direntry **link = &tree;
  int rootlen = strlen(root);
  int namelen;
  int dirsize;

  dirsize = 0;

  dirp = opendir(root);
  if ( !dirp )
    return NULL;

  while ( (de = readdir(dirp)) != NULL ) {
    if ( !strcmp(de->d_name, ".") || !strcmp(de->d_name, "..") )
      continue;			/* Ignore . and .. */

    thisent = xmalloc(sizeof(struct direntry));
    memset(thisent, 0, sizeof(struct direntry));

    thisent->name = xstrdup(de->d_name);
    namelen = strlen(thisent->name);

    thisent->path = xmalloc(rootlen+namelen+3);
    memcpy(thisent->path, root, rootlen);
    thisent->path[rootlen] = '/';
    memcpy(thisent->path+rootlen+1, thisent->name, namelen+1);

    thisent->parent = parent;

    if ( stat(thisent->path, &thisent->st) ) {
      fprintf(stderr, "%s: Cannot stat file: %s\n", program,
	      thisent->path);
      free(thisent->name);
      free(thisent->path);
      free(thisent);
      continue;
    }

    if ( S_ISDIR(thisent->st.st_mode) ) {
      /* Recursion: see recursion */
      thisent->dir = make_dir_tree(thisent->path, &thisent->dirsize, thisent);
      thisent->attribute = 0x10; /* Directory */
    } else if ( !S_ISREG(thisent->st.st_mode) ) {
      /* Some kind of special file (not symbolic link, we used stat,
	 not lstat.)  This isn't representable, so just ignore it. */
      fprintf(stderr, "%s: Ignoring special file: %s\n", program,
	      thisent->path);
      free(thisent->name);
      free(thisent->path);
      free(thisent);
      continue;
    } else {
      thisent->dir = NULL;
      thisent->attribute = 0x00; /* File */
    }

    if ( !(thisent->st.st_mode & (S_IWUSR|S_IWGRP|S_IWOTH)) ) {
      thisent->attribute |= 0x01; /* Readonly */
    }

    thisent->next = NULL;

    /* Generate mangled DOS filename (eventually) */
    dos_mangle_name(thisent->name, thisent->mangled_name, 0);

    /* Add to linked list */
    *link = thisent;
    link = &thisent->next;
    dirsize++;
  }

  closedir(dirp);

  if (dirsizeptr)
    *dirsizeptr = dirsize;

  return tree;
}