1() = evalfile ("inc.sl");
2
3testing_feature ("posdir");
4
5private define test_listdir (dir)
6{
7   variable i, file, files;
8
9   files = listdir (dir);
10   if (length(files))
11     {
12	failed ("listdir returned %d files for new dir", length(files));
13     }
14
15   _for i (1, 4, 1)
16     {
17	file = "$dir/$i"$;
18	if (NULL == open (file, O_WRONLY|O_CREAT))
19	  failed ("fopen %S failed", file);
20	files = listdir (dir);
21	if (length (files) != i)
22	  failed ("listdir %S failed to produce %d items", dir, i);
23	files = atoi (files);
24	files = files[array_sort (files)];
25	ifnot (_eqs (files, [1:i]))
26	  failed ("listdir %S failed to produce the proper list of files", dir);
27     }
28
29   foreach file (files)
30     {
31	file = "$dir/$file"$;
32	if (-1 == remove (file))
33	  failed ("remove %S failed: %S", file, errno_string());
34     }
35}
36
37private define test_posdir ()
38{
39   variable dir = util_make_tmp_dir ("tmpdir");
40
41   test_listdir (dir);
42
43   if (-1 == rmdir (dir))
44     failed ("rmdir %s: %s", dir, errno_string());
45}
46test_posdir ();
47
48private define test_non_exist_file_ops ()
49{
50   variable badfile = "/122345 user";
51   if (NULL == stat_file (badfile))
52     () = remove (badfile);	       %  should fail
53   variable tmpfile = util_make_tmp_file ("tmpfileX", NULL);
54   variable tmpfile1 = tmpfile + "-tmp";
55   variable tmpdir = util_make_tmp_dir ("tmpdir");
56   variable dir = getcwd ();
57#ifexists lstat_file
58   if (NULL != lstat_file (badfile))
59     failed ("lstat_file %S succeeded", badfile);
60#endif
61
62#ifexists access
63   if (-1 == access (tmpfile, R_OK))
64     failed ("access %s R_OK", tmpfile);
65   if (0 == access (badfile, R_OK))
66     failed ("access %s R_OK", badfile);
67#endif
68#ifexists chdir
69   if (-1 == chdir (tmpdir))
70     failed ("chdir %S", tmpdir);
71   if (-1 == chdir (dir))
72     failed ("chdir %S", dir);
73   if (0 == chdir (badfile))
74     failed ("chdir %S succeeded", badfile);
75#endif
76#ifexists symlink
77   if (-1 == symlink (tmpfile, tmpfile1))
78     failed ("symlink %s -> %s: %S", tmpfile1, tmpfile, errno_string());
79   variable st = lstat_file (tmpfile1);
80   if ((st == NULL) || (0 == stat_is  ("lnk", st.st_mode)))
81     failed ("stat_is lnk for %s", tmpfile1);
82   if (-1 != symlink ("", ""))
83     failed ("expected symlink to fail for empty strings");
84# ifexists readlink
85   if (tmpfile != readlink (tmpfile1))
86     failed ("readlink %s: %s", tmpfile1, errno_string());
87   if (NULL != readlink (tmpfile))
88     failed ("Expected readlink to fail when reading a regular file");
89# endif
90   if (-1 == remove (tmpfile1))
91     failed ("remove %s: %s", tmpfile1, errno_string());
92#endif
93
94#ifexists hardlink
95   if (0 == hardlink (badfile, tmpfile1))
96     failed ("hardlink to bad file succeeded");
97   if (-1 == hardlink (tmpfile, tmpfile1))
98     failed ("hardlink %s -> %s failed: %s", tmpfile1, tmpfile, errno_string());
99   () = remove (tmpfile1);
100#endif
101
102   if (-1 != rename (tmpfile1, tmpfile1))
103     failed ("Expected rename to fail to rename a non-existent file to itself");
104   if (0 != rename (tmpfile, tmpfile1))
105     failed ("rename %s->%s; %s", tmpfile, tmpfile1, errno_string());
106   if (0 != rename (tmpfile1, tmpfile))
107     failed ("rename %s->%s; %s", tmpfile, tmpfile1, errno_string());
108   if (0 == rmdir (tmpfile))
109     failed ("Expected rmdir to fail to remove %S", tmpfile);
110
111#ifexists utime
112   if (-1 != utime (badfile, 2000.0, 3000.0))
113     failed ("Expected utime to fail on a non-existent file");
114   if (-1 == utime (tmpfile, 2000.0, 3000))
115     failed ("utime %s: %s", tmpfile, errno_string());
116   st = stat_file (tmpfile);
117   if (st == NULL)
118     failed ("stat_file %S failed: %S", tmpfile, errno_string());
119   if ((st.st_atime != 2000) || (st.st_mtime != 3000))
120     failed ("stat_file times differ from utime values");
121#endif
122#ifexists chmod
123   if (-1 == chmod (tmpfile, S_IWUSR))
124     failed ("chmod %s failed: %s", tmpfile, errno_string());
125   st = stat_file (tmpfile);
126   if (st == NULL)
127     failed ("stat_file %s failed: %s", tmpfile, errno_string());
128   if ((st.st_mode&0777) != S_IWUSR)
129     failed ("chmod %s mode=%d does not match stat_file mode=%d",
130	     tmpfile, S_IWUSR, st.st_mode);
131   if (-1 != chmod (badfile, S_IWUSR))
132     failed ("expected chmod to fail on a non-existent file");
133#endif
134#ifexists chown
135   if (-1 != chown ("", 0, 0))
136     failed ("chown of an empty string succeeded");
137   st = stat_file (tmpfile);
138   if (-1 == chown (tmpfile, st.st_uid, st.st_gid))
139     failed ("chown %s failed: %s", errno_string());
140#endif
141#ifexists lchown
142   % FIXME: Add a better test
143   if (-1 != lchown ("", 0, 0))
144     failed ("chown of an empty string succeeded");
145#endif
146   () = rmdir (tmpdir);
147   () = remove (tmpfile);
148}
149test_non_exist_file_ops ();
150
151#ifexists statvfs
152private define test_statvfs ()
153{
154   () = statvfs (".");
155   () = statvfs (stdout);
156   () = statvfs (1);
157   () = statvfs (fileno(stdout));
158   try
159     {
160	() = statvfs (1.2);
161     }
162   catch TypeMismatchError;
163   catch AnyError: failed ("expected statvfs to fail on an invalid object");
164
165   if (NULL != statvfs ("/* non * existent * file *"))
166     failed ("Expected statvfs to fail on a non-existent file");
167}
168test_statvfs ();
169#endif
170
171#ifexists umask
172private define test_umask ()
173{
174   variable m = umask (022);
175   if (022 != umask (m))
176     failed ("umask 022");
177   if (m != umask (m))
178     failed ("umask m");
179}
180test_umask ();
181#endif
182
183print ("Ok\n");
184exit (0);
185