File: fnode.c

package info (click to toggle)
limo 0.3.2-4
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 152 kB
  • ctags: 159
  • sloc: ansic: 1,666; makefile: 73
file content (254 lines) | stat: -rw-r--r-- 7,061 bytes parent folder | download | duplicates (4)
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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
/* $Id: fnode.c,v 1.5 1999/07/01 01:39:50 fraserm Exp $
   $Log: fnode.c,v $
   Revision 1.5  1999/07/01 01:39:50  fraserm
   word 0

   Revision 1.4  1999/05/30 11:35:05  fraserm
   added support for multiple sort chunks

   Revision 1.3  1999/05/26 23:51:37  fraserm
   added custom command stuff

   Revision 1.2  1999/05/16 19:48:20  fraserm
   changed n N and L to n +n N and +N

   Revision 1.1  1999/04/20 23:43:00  fraserm
   Initial revision

*/

/* file node utility functions */

#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <sys/stat.h>
#include <unistd.h>
#include <pwd.h>
#include <grp.h>
#include <limits.h>
#include "limo.h"

extern struct custcmd cmds[]; /* custom commands */
extern int numcustcmds; /* number of custom commands defined */


char *sort_chunks;

/* allocate a new file node, and populate the stat structure */
struct fnode *newfnode(int argc, char *argv[], char *name, int options)
{
  struct fnode *tmp;
  int len;
  int ll;
  int i;

  size_t ltsize = 0;
  char *ltbuf = NULL;

  /* allocate the base structure */
  if ((tmp = (struct fnode *) malloc(sizeof(struct fnode))) == NULL) {
    fprintf(stderr, "%s: newfnode: fnode: %s\n", argv[0], strerror(errno));
    return NULL;
  }
  tmp->s = NULL;
  tmp->username = tmp->groupname = NULL; /* make sure the names are empty */
  /* allocate space to store the name */
  if ((tmp->name = (char *) malloc((len = strlen(name) + 1))) == NULL) {
    fprintf(stderr, "%s: newfnode: name: %s\n", argv[0], strerror(errno));
    return NULL;
  }
  strcpy(tmp->name, name); /* copy the name there */
  tmp->namelen = len; /* note the file length as an optimization for later */
  /* allocate space to store the stat structure */
  if ((tmp->s = (struct stat *) malloc(sizeof(struct stat))) == NULL) {
    fprintf(stderr, "%s: newfnode: s: %s\n", argv[0], strerror(errno));
    return NULL;
  }
  tmp->linklen = 0;
  /* perform the stat */
  if (lstat(name, tmp->s) != 0) {
    fprintf(stderr, "%s: %s: %s\n", argv[0], name,
	    strerror(errno)); /* just whine, don't die */
    free(tmp->s);
    tmp->s = NULL; /* null it out so it's known to be bad */
  }
  else { /* stat worked, let's check if we need to get the user or group */
    if (options & GET_OWNER) {
      struct passwd *pwd;

      errno = 0;
      if ((pwd = getpwuid(tmp->s->st_uid)) == NULL) {
	if (errno == ENOMEM) {
	  fprintf(stderr, "%s: getpwuid: %s\n", argv[0], strerror(errno));
	  tmp->username = "getpwerr";
	}
	else {
	  if ((tmp->username = (char *) malloc(10)) == NULL) { /* ugly */
	    fprintf(stderr, "%s: malloc: %s\n", argv[0], strerror(errno));
	    tmp->username = "allocerr";
	  }
	  else {
	    sprintf(tmp->username, "%d", tmp->s->st_uid); /* use the numeric
							     in emergencies */
	  }
	}
      }
      else {
	if ((tmp->username = (char *) malloc(strlen(pwd->pw_name) + 1))
	    == NULL) {
	  fprintf(stderr, "%s: malloc: %s\n", argv[0], strerror(errno));
	  tmp->username = "allocerr";
	}
	else {
	  strcpy(tmp->username, pwd->pw_name);
	}
      }
    }
    if (options & GET_GROUP) {
      struct group *grp;

      errno = 0;
      if ((grp = getgrgid(tmp->s->st_gid)) == NULL) {
	if (errno == ENOMEM) {
	  fprintf(stderr, "%s: getgrgid: %s\n", argv[0], strerror(errno));
	  tmp->groupname = "getgrerr";
	}
	else {
	  if ((tmp->groupname = (char *) malloc(10)) == NULL) { /* ugly */
	    fprintf(stderr, "%s: malloc: %s\n", argv[0], strerror(errno));
	    tmp->groupname = "allocerr";
	  }
	  else {
	    sprintf(tmp->groupname, "%d", tmp->s->st_gid); /* use the numeric
							      in emergencies */
	  }
	}
      }
      else {
	if ((tmp->groupname = (char *) malloc(strlen(grp->gr_name) + 1))
	    == NULL) {
	  fprintf(stderr, "%s: malloc: %s\n", argv[0], strerror(errno));
	  tmp->groupname = "allocerr";
	}
	else {
	  strcpy(tmp->groupname, grp->gr_name);
	}
      }
    }
    if ((options & GET_LINK_TARGET) && S_ISLNK(tmp->s->st_mode)) {
      if ((ll = xreadlink(tmp->name, &ltbuf, ltsize)) < 0) {
	fprintf(stderr, "%s: readlink of %s: %s\n",
		argv[0], tmp->name, strerror(errno));
	return NULL;
      }
      if ((tmp->linktarget = (char *) malloc(ll+1)) == NULL) {
	fprintf(stderr, "%s: malloc: %s\n", argv[0], strerror(errno));
	tmp->linklen = 0;
      }
      else {
	strcpy(tmp->linktarget, ltbuf);
	tmp->linklen = ll; /* note link length as optimization for later */
      }
      free(ltbuf);
    }
  }
  /* get custom commands */
  if (numcustcmds > 0) {
    if ((tmp->custvals = (char **) malloc((sizeof (char **)) * numcustcmds))
	== NULL
	|| (tmp->custlens = (int *) malloc((sizeof (int *)) * numcustcmds))
	== NULL) {
      fprintf(stderr, "%s: malloc: %s\n", argv[0], strerror(errno));
      return NULL;
    }
    for (i = 0; i < numcustcmds; ++i) {
      tmp->custvals[i] = get_custom(argc, argv, &cmds[i], tmp->name);
      tmp->custlens[i] = strlen(tmp->custvals[i]);
    }
  }
  /*printf("st_rdev %x\n", tmp->s->st_rdev);*/
  /* printf("name %s namelen %d linklen %d\n", tmp->name, tmp->namelen,
     tmp->linklen); */
  return tmp;
}

void fnode_free(struct fnode *fn)
{
  if (fn->s != NULL) {
    free(fn->s);
  }
  free(fn->name);
  if (fn->username != NULL) {
    free(fn->username);
  }
  if (fn->groupname != NULL) {
    free(fn->groupname);
  }
  free(fn);
}

/* generic compare routine called by qsort() */
int fnode_compare(struct fnode **first, struct fnode **second)
{
  int mult = 1;
  char *i;
  struct fnode *a = *first, *b = *second;
  int rv = 0; /* return value */

  for (i = sort_chunks; rv == 0 && *i != '\0'; ++i) {
    /* decide what to return based on the current sort_chunk */
    switch (*i) {
    case SORT_DIR_ASC:
      mult = 1;
      break;
    case SORT_DIR_DESC:
      mult = -1;
      break;
    case NAME:
      rv = mult * strcmp(a->name, b->name);
      break;
    case SIZEBLOCKS:
      rv = mult * ((long)a->s->st_blocks - (long)b->s->st_blocks);
      break;
    case BYTESSHORT:
      rv = mult * ((long)a->s->st_size - (long)b->s->st_size);
      break;
    case DESCSHORT:
      rv = mult * ((int)(a->s->st_mode & S_IFMT)
		   - (int)(b->s->st_mode & S_IFMT));
      break;
    case MODESHORT:
      rv = mult * ((int)a->s->st_mode - (int)b->s->st_mode);
      break;
    case OWNERNUMBER:
      rv = mult * ((int)a->s->st_uid - (int)b->s->st_uid);
      break;
    case OWNERNAME:
      rv = mult * strcmp(a->username, b->username);
      break;
    case GROUPNUMBER:
      rv = mult * ((int)a->s->st_gid - (int)b->s->st_gid);
      break;
    case GROUPNAME:
      rv = mult * strcmp(a->groupname, b->groupname);
      break;
    case INODE:
      rv = mult * ((long)a->s->st_ino - (long)b->s->st_ino);
      break;
    case ACCESSSHORT:
      rv = mult * ((long)a->s->st_atime - (long)b->s->st_atime);
      break;
    case MODIFYSHORT:
      rv = mult * ((long)a->s->st_mtime - (long)b->s->st_mtime);
      break;
    case CHANGESHORT:
      rv = mult * ((long)a->s->st_ctime - (long)b->s->st_ctime);
      break;
    default:
      rv = 0;
    }
  }
  return rv;
}