~ubuntu-branches/ubuntu/trusty/util-linux/trusty-proposed

« back to all changes in this revision

Viewing changes to lib/at.c

  • Committer: Bazaar Package Importer
  • Author(s): LaMont Jones
  • Date: 2011-05-11 08:38:31 UTC
  • mfrom: (1.3.10 upstream)
  • mto: (1.6.3 upstream) (4.5.5 sid)
  • mto: This revision was merged to the branch mainline in revision 82.
  • Revision ID: james.westby@ubuntu.com-20110511083831-tty7wnezw55fmrn4
ImportĀ upstreamĀ versionĀ 2.19.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Portable xxxat() functions.
 
3
 *
 
4
 * Copyright (C) 2010 Karel Zak <kzak@redhat.com>
 
5
 */
 
6
#include <stdio.h>
 
7
#include <stdlib.h>
 
8
#include <fcntl.h>
 
9
#include <sys/stat.h>
 
10
 
 
11
#include "at.h"
 
12
 
 
13
int fstat_at(int dir, const char *dirname, const char *filename,
 
14
                                struct stat *st, int nofollow)
 
15
{
 
16
#ifdef HAVE_FSTATAT
 
17
        return fstatat(dir, filename, st,
 
18
                        nofollow ? AT_SYMLINK_NOFOLLOW : 0);
 
19
#else
 
20
        char path[PATH_MAX];
 
21
        int len;
 
22
 
 
23
        len = snprintf(path, sizeof(path), "%s/%s", dirname, filename);
 
24
        if (len < 0 || len + 1 > sizeof(path))
 
25
                return -1;
 
26
 
 
27
        return nofollow ? lstat(path, st) : stat(path, st);
 
28
#endif
 
29
}
 
30
 
 
31
int open_at(int dir, const char *dirname, const char *filename, int flags)
 
32
{
 
33
#ifdef HAVE_FSTATAT
 
34
        return openat(dir, filename, flags);
 
35
#else
 
36
        char path[PATH_MAX];
 
37
        int len;
 
38
 
 
39
        len = snprintf(path, sizeof(path), "%s/%s", dirname, filename);
 
40
        if (len < 0 || len + 1 > sizeof(path))
 
41
                return -1;
 
42
 
 
43
        return open(path, flags);
 
44
#endif
 
45
}
 
46
 
 
47
FILE *fopen_at(int dir, const char *dirname, const char *filename, int flags,
 
48
                        const char *mode)
 
49
{
 
50
        int fd = open_at(dir, dirname, filename, flags);
 
51
 
 
52
        if (fd < 0)
 
53
                return NULL;
 
54
 
 
55
        return fdopen(fd, mode);
 
56
}
 
57
 
 
58
#ifdef TEST_PROGRAM
 
59
#include <err.h>
 
60
#include <errno.h>
 
61
#include <sys/types.h>
 
62
#include <dirent.h>
 
63
#include <string.h>
 
64
 
 
65
int main(int argc, char *argv[])
 
66
{
 
67
        DIR *dir;
 
68
        struct dirent *d;
 
69
        char *dirname;
 
70
 
 
71
        if (argc != 2) {
 
72
                fprintf(stderr, "usage: %s <directory>\n", argv[0]);
 
73
                exit(EXIT_FAILURE);
 
74
        }
 
75
        dirname = argv[1];
 
76
 
 
77
        dir = opendir(dirname);
 
78
        if (!dir)
 
79
                err(EXIT_FAILURE, "%s: open failed", dirname);
 
80
 
 
81
        while ((d = readdir(dir))) {
 
82
                struct stat st;
 
83
                FILE *f;
 
84
 
 
85
                printf("%32s ", d->d_name);
 
86
 
 
87
                if (fstat_at(dirfd(dir), dirname, d->d_name, &st, 0) == 0)
 
88
                        printf("%16jd bytes ", st.st_size);
 
89
                else
 
90
                        printf("%16s bytes ", "???");
 
91
 
 
92
                f = fopen_at(dirfd(dir), dirname, d->d_name, O_RDONLY, "r");
 
93
                printf("   %s\n", f ? "OK" : strerror(errno));
 
94
                if (f)
 
95
                        fclose(f);
 
96
        }
 
97
        closedir(dir);
 
98
        return EXIT_SUCCESS;
 
99
}
 
100
#endif