source: rtems/cpukit/libfs/src/nfsclient/src/dirutils.c @ bbe9596

4.104.115
Last change on this file since bbe9596 was bbe9596, checked in by Ralf Corsepius <ralf.corsepius@…>, on 12/08/08 at 13:11:10

2008-12-08 Ralf Corsépius <ralf.corsepius@…>

  • libfs/src/nfsclient/src/dirutils.c: #include <limits.h>.
  • Property mode set to 100644
File size: 7.4 KB
Line 
1/* $Id$ */
2
3/* very crude and basic fs utilities for testing the NFS */
4
5/* Till Straumann, <strauman@slac.stanford.edu>, 10/2002 */
6
7/*
8 * Authorship
9 * ----------
10 * This software (NFS-2 client implementation for RTEMS) was created by
11 *     Till Straumann <strauman@slac.stanford.edu>, 2002-2007,
12 *         Stanford Linear Accelerator Center, Stanford University.
13 *
14 * Acknowledgement of sponsorship
15 * ------------------------------
16 * The NFS-2 client implementation for RTEMS was produced by
17 *     the Stanford Linear Accelerator Center, Stanford University,
18 *         under Contract DE-AC03-76SFO0515 with the Department of Energy.
19 *
20 * Government disclaimer of liability
21 * ----------------------------------
22 * Neither the United States nor the United States Department of Energy,
23 * nor any of their employees, makes any warranty, express or implied, or
24 * assumes any legal liability or responsibility for the accuracy,
25 * completeness, or usefulness of any data, apparatus, product, or process
26 * disclosed, or represents that its use would not infringe privately owned
27 * rights.
28 *
29 * Stanford disclaimer of liability
30 * --------------------------------
31 * Stanford University makes no representations or warranties, express or
32 * implied, nor assumes any liability for the use of this software.
33 *
34 * Stanford disclaimer of copyright
35 * --------------------------------
36 * Stanford University, owner of the copyright, hereby disclaims its
37 * copyright and all other rights in this software.  Hence, anyone may
38 * freely use it for any purpose without restriction. 
39 *
40 * Maintenance of notices
41 * ----------------------
42 * In the interest of clarity regarding the origin and status of this
43 * SLAC software, this and all the preceding Stanford University notices
44 * are to remain affixed to any copy or derivative of this software made
45 * or distributed by the recipient and are to be affixed to any copy of
46 * software made or distributed by the recipient that contains a copy or
47 * derivative of this software.
48 *
49 * ------------------ SLAC Software Notices, Set 4 OTT.002a, 2004 FEB 03
50 */
51
52#ifdef __vxworks
53#include <vxWorks.h>
54#endif
55#include <stdio.h>
56#include <dirent.h>
57#include <unistd.h>
58#include <string.h>
59#include <fcntl.h>
60#include <sys/stat.h>
61#include <errno.h>
62#include <stdlib.h>
63#include <limits.h> /* PATH_MAX */
64
65#ifdef HAVE_CEXP
66#include <cexpHelp.h>
67#endif
68
69#ifndef __vxworks
70int
71pwd(void)
72{
73char buf[PATH_MAX];
74
75        if ( !getcwd(buf,PATH_MAX)) {
76                perror("getcwd");
77                return -1;
78        } else {
79                printf("%s\n",buf);
80        }
81        return 0;
82}
83
84static int
85ls_r(char *path, char *chpt, char *name, struct stat *buf)
86{
87char *t;
88        sprintf(chpt, "/%s", name);
89        if (lstat(path,buf)) {
90                fprintf(stderr,"stat(%s): %s\n", path, strerror(errno));
91                return -1;
92        }
93        switch ( buf->st_mode & S_IFMT ) {
94                case S_IFSOCK:
95                case S_IFIFO:   t = "|"; break;
96
97                default:
98                case S_IFREG:
99                case S_IFBLK:
100                case S_IFCHR:
101                                                t = "";  break;
102                case S_IFDIR:
103                                                t = "/"; break;
104                case S_IFLNK:
105                                                t = "@"; break;
106        }
107
108        printf("%10li, %10lib, %5i.%-5i 0%04o %s%s\n",
109                                buf->st_ino,
110                                buf->st_size,
111                                buf->st_uid,
112                                buf->st_gid,
113                                buf->st_mode & ~S_IFMT,
114                                name,
115                                t);
116        *chpt = 0;
117        return 0;
118}
119
120int
121ls(char *dir, char *opts)
122{
123struct dirent   *de;
124char                    path[PATH_MAX+1];
125char                    *chpt;
126DIR                             *dp  = 0;
127int                             rval = -1;
128struct stat             buf;
129
130        if ( !dir )
131                dir = ".";
132
133        strncpy(path, dir, PATH_MAX);
134        path[PATH_MAX] = 0;
135        chpt = path+strlen(path);
136
137        if ( !(dp=opendir(dir)) ) {
138                perror("opendir");
139                goto cleanup;
140        }
141
142        while ( (de = readdir(dp)) ) {
143                ls_r(path, chpt, de->d_name, &buf);
144        }
145
146        rval = 0;
147
148cleanup:
149        if (dp)
150                closedir(dp);
151        return rval;
152}
153#endif
154
155#if 0
156                fprintf(stderr, "usage: cp(""from"",[""to""[,""-f""]]\n");
157                fprintf(stderr, "          ""to""==NULL -> stdout\n");
158                fprintf(stderr, "          ""-f""       -> overwrite existing file\n");
159#endif
160
161int
162cp(char *from, char *to, char *opts)
163{
164struct stat     st;
165int                     rval  = -1;
166int                     fd    = -1;
167FILE            *fst  = 0;
168FILE            *tst  = 0;
169int                     flags = O_CREAT | O_WRONLY | O_TRUNC | O_EXCL;
170
171        if (from) {
172
173        if ((fd=open(from,O_RDONLY,0)) < 0) {
174                fprintf(stderr,
175                                "Opening %s for reading: %s\n",
176                                from,
177                                strerror(errno));
178                goto cleanup;
179        }
180
181        if (fstat(fd, &st)) {
182                fprintf(stderr,
183                                "rstat(%s): %s\n",
184                                from,
185                                strerror(errno));
186                goto cleanup;
187        }
188
189
190        if (!S_ISREG(st.st_mode)) {
191                fprintf(stderr,"Refuse to copy a non-regular file\n");
192                errno = EINVAL;
193                goto cleanup;
194        }
195        /* Now create a stream -- I experienced occasional weirdness
196         * when circumventing the streams attached to fildno(stdin)
197         * by reading/writing to the underlying fd's directly ->
198         * for now we always go through buffered I/O...
199         */
200        if ( !(fst=fdopen(fd,"r")) ) {
201                fprintf(stderr,
202                                "Opening input stream [fdopen()] failed: %s\n",
203                                strerror(errno));
204                goto cleanup;
205        }
206        /* at this point, we have a stream and don't need 'fd' anymore */
207        fd = -1;
208
209        } else {
210                fst                     = stdin;
211                st.st_mode      = 0644;
212        }
213
214        if (opts && strchr(opts,'f'))
215                flags &= ~ O_EXCL;
216
217        if (to) {
218                if ( (fd=open(to,flags,st.st_mode)) < 0 ) {
219                        fprintf(stderr,
220                                        "Opening %s for writing: %s\n",
221                                        to,
222                                        strerror(errno));
223                        goto cleanup;
224                }
225                if ( !(tst=fdopen(fd, "w")) ) {
226                        fprintf(stderr,
227                                        "Opening output stream [fdopen()] failed: %s\n",
228                                        strerror(errno));
229                        goto cleanup;
230                }
231                /* at this point we have a stream and don't need 'fd' anymore */
232                fd = -1;
233        } else {
234                tst = stdout;
235        }
236
237        /* clear old errors */
238        clearerr(fst);
239        clearerr(tst);
240
241        /* use macro versions on register vars; stdio is already buffered,
242         * there's nothing to be gained by reading/writing blocks into
243         * a secondary buffer...
244         */
245        {
246        register int ch;
247        register FILE *f = fst;
248        register FILE *t = tst;
249                while ( EOF != (ch = getc(f)) && EOF != putc(ch, t) )
250                        /* nothing else */;
251        }
252
253        if ( ferror(fst) ) {
254                fprintf(stderr,"Read error: %s\n",strerror(errno));
255                goto cleanup;
256        }
257        if ( ferror(tst) ) {
258                fprintf(stderr,"Write error: %s\n",strerror(errno));
259                goto cleanup;
260        }
261
262        rval = 0;
263
264cleanup:
265
266        if ( fd >= 0 )
267                close(fd);
268
269        if ( fst ) {
270                if ( from )
271                        fclose(fst);
272                else
273                        clearerr(fst);
274        }
275        if ( tst ) {
276                if ( to )
277                        fclose(tst);
278                else {
279                        /* flush stdout */
280                        fflush(tst);
281                        clearerr(tst);
282                }
283        }
284
285        return rval;
286}
287
288int
289ln(char *to, char *name, char *opts)
290{
291        if (!to) {
292                fprintf(stderr,"ln: need 'to' argument\n");
293                return -1;
294        }
295        if (!name) {
296                if ( !(name = strrchr(to,'/')) ) {
297                        fprintf(stderr,
298                                        "ln: 'unable to link %s to %s\n",
299                                        to,to);
300                        return -1;
301                }
302                name++;
303        }
304        if (opts || strchr(opts,'s')) {
305                if (symlink(name,to)) {
306                        fprintf(stderr,"symlink: %s\n",strerror(errno));
307                        return -1;
308                }
309        } else {
310                if (link(name,to)) {
311                        fprintf(stderr,"hardlink: %s\n",strerror(errno));
312                        return -1;
313                }
314        }
315        return 0;
316}
317
318int
319rm(char *path)
320{
321        return unlink(path);
322}
323
324int
325cd(char *path)
326{
327        return chdir(path);
328}
329
330#ifdef HAVE_CEXP
331static CexpHelpTabRec _cexpHelpTabDirutils[] __attribute__((unused)) = {
332        HELP(
333"copy a file: cp(""from"",[""to""[,""-f""]])\n\
334                 from = NULL <-- stdin\n\
335                 to   = NULL --> stdout\n\
336                 option -f: overwrite existing file\n",
337                int,
338                cp, (char *from, char *to, char *options)
339                ),
340        HELP(
341"list a directory: ls([""dir""])\n",
342                int,
343                ls, (char *dir)
344                ),
345        HELP(
346"remove a file\n",
347                int,
348                rm, (char *path)
349                ),
350        HELP(
351"change the working directory\n",
352                int,
353                cd, (char *path)
354                ),
355        HELP(
356"create a link: ln(""to"",""name"",""[-s]""\n\
357                   -s creates a symlink\n",
358                int,
359                ln, (char *to, char *name, char *options)
360                ),
361        HELP("",,0,)
362};
363#endif
Note: See TracBrowser for help on using the repository browser.