source: network-demos/nfsClientTest/dirutils.c @ c8d1675

4.11network-demos-4-10-branchnetwork-demos-4-8-branchnetwork-demos-4-9-branch
Last change on this file since c8d1675 was c8d1675, checked in by Joel Sherrill <joel.sherrill@…>, on 07/16/07 at 15:32:18

2007-07-16 Joel Sherrill <joel.sherrill@…>

Initial add of NFS Client test.

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