~ubuntu-branches/ubuntu/karmic/xpuzzles/karmic

« back to all changes in this revision

Viewing changes to xpanex/file.c

  • Committer: Bazaar Package Importer
  • Author(s): Adam Conrad
  • Date: 2005-08-22 16:29:42 UTC
  • mfrom: (2.1.1 sarge)
  • Revision ID: james.westby@ubuntu.com-20050822162942-tdtf178zkpd3xxdg
Tags: 7.1.3-1ubuntu1
Update {build-,}depends for xorg -> mesa transition.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
#  Taken from xlock, many authors...
 
3
#
 
4
#                   All Rights Reserved
 
5
#
 
6
#  Permission to use, copy, modify, and distribute this software and
 
7
#  its documentation for any purpose and without fee is hereby granted,
 
8
#  provided that the above copyright notice appear in all copies and
 
9
#  that both that copyright notice and this permission notice appear in
 
10
#  supporting documentation, and that the name of the author not be
 
11
#  used in advertising or publicity pertaining to distribution of the
 
12
#  software without specific, written prior permission.
 
13
#
 
14
#  This program is distributed in the hope that it will be "useful",
 
15
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
 
16
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 
17
#
 
18
*/
 
19
 
 
20
#include <stdio.h>
 
21
#include <stdlib.h>
 
22
#include <string.h>
 
23
#include <sys/stat.h>
 
24
 
 
25
static FILE *
 
26
carefulOpen(char *fileName, const char *type)
 
27
{
 
28
        FILE       *fp = (FILE *) NULL;
 
29
        int         s;
 
30
        struct stat fileStat;
 
31
 
 
32
        s = stat(fileName, &fileStat);
 
33
        if ((s >= 0 && S_ISREG(fileStat.st_mode)) || (s < 0 && type[0] == 'w')) {
 
34
                if ((fp = fopen(fileName, type)) == NULL)
 
35
                        return (FILE *) NULL;
 
36
        } else {
 
37
                return (FILE *) NULL;
 
38
        }
 
39
 
 
40
        return fp;
 
41
}
 
42
 
 
43
static int
 
44
readable(char *fileName)
 
45
{
 
46
        FILE       *fp;
 
47
 
 
48
        if ((fp = carefulOpen(fileName, "r")) == NULL)
 
49
                return 0;
 
50
        (void) fclose(fp);
 
51
        return 1;
 
52
}
 
53
 
 
54
#ifdef WINVER
 
55
#define LASTDELIM '\\'
 
56
#else
 
57
#ifdef VMS
 
58
#define LASTDELIM ']'
 
59
#else
 
60
#define LASTDELIM '/'
 
61
#endif
 
62
#endif
 
63
static char *
 
64
baseName(char *fileName)
 
65
{
 
66
        int i;
 
67
 
 
68
        for (i = strlen(fileName); i >= 0; i--) {
 
69
                if (fileName[i] == LASTDELIM
 
70
#ifdef WINVER
 
71
                        ||  fileName[i] == '/'
 
72
#endif
 
73
                        )
 
74
                  return &fileName[i + 1];
 
75
        }
 
76
        return fileName;
 
77
}
 
78
 
 
79
char *
 
80
findFile(char *fileName)
 
81
{
 
82
        if (readable(fileName))
 
83
                return fileName;
 
84
        else {
 
85
                char *temp = baseName(fileName);
 
86
 
 
87
                if (readable(temp))
 
88
                        return temp;
 
89
        }
 
90
        return NULL;
 
91
}