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

4.115
Last change on this file since 4e59276 was 4e59276, checked in by Mathew Kallada <matkallada@…>, on 12/28/12 at 14:05:20

libfs: Doxygen Enhancement Task #5

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