home *** CD-ROM | disk | FTP | other *** search
/ Amiga Format CD 28 / amigaformatcd28.iso / -seriously_amiga- / archivers / arcppc / src / sysvarcstuf < prev    next >
Text File  |  1998-04-23  |  9KB  |  376 lines

  1. #--------------------------------CUT HERE-------------------------------------
  2. #! /bin/sh
  3. #
  4. # This is a shell archive.  Save this into a file, edit it
  5. # and delete all lines above this comment.  Then give this
  6. # file to sh by executing the command "sh file".  The files
  7. # will be extracted into the current directory owned by
  8. # you with default permissions.
  9. #
  10. # The files contained herein are:
  11. #
  12. # -rw-r--r--  1 hyc           539 Jul 22 16:30 README
  13. # -rw-r--r--  1 hyc           787 Jul 22 15:10 getwd.c
  14. # -rw-r--r--  1 hyc           280 Jul 22 16:14 rename.c
  15. # -rw-r--r--  1 hyc          2353 Jul 22 15:55 scandir.3
  16. # -rw-r--r--  1 hyc          1875 Jul 22 15:55 scandir.c
  17. # -rw-r--r--  1 hyc           397 Jul 22 15:10 utimes.c
  18. #
  19. echo 'x - README'
  20. if test -f README; then echo 'shar: not overwriting README'; else
  21. sed 's/^X//' << '________This_Is_The_END________' > README
  22. XThe enclosed files should be sufficient for bringing up ARC on a Sys V R3
  23. Xsystem. As Jon mentions, Doug Gwyn's directory routines are needed for
  24. XSys V R2. The enclosed copy of scandir is new, as far as I can tell, and
  25. XI've removed the (unneeded) ftw.? files. Also added a rename() routine,
  26. Xcourtesy of Janet Walz. (And an addition from Rich Salz.)
  27. X
  28. X[see comp.sources.unix, volume 9, for gwyn-dir-lib...]
  29. X
  30. XThanks again to Jon Zeeff, Janet Walz, and Rich Salz for their help.
  31. X  -- Howard Chu
  32. X    hyc@umix.cc.umich.edu
  33. X    {uunet,rutgers}!umix!hyc
  34. ________This_Is_The_END________
  35. if test `wc -c < README` -ne      539; then
  36.     echo 'shar: README was damaged during transit (should have been      539 bytes)'
  37. fi
  38. fi        ; : end of overwriting check
  39. echo 'x - getwd.c'
  40. if test -f getwd.c; then echo 'shar: not overwriting getwd.c'; else
  41. sed 's/^X//' << '________This_Is_The_END________' > getwd.c
  42. X/*
  43. X * 4.2bsd getwd simulation for Sys V.3
  44. X */
  45. X
  46. X#include <stdio.h>
  47. X
  48. X#define SYSV3
  49. X
  50. X#define MAXWD 1024         /* limited by 4.2 getwd(2) */
  51. X
  52. X#ifdef SYSV3
  53. X
  54. Xchar *getcwd();
  55. X
  56. Xchar *
  57. Xgetwd(path)
  58. Xchar *path;
  59. X{
  60. X    return(getcwd(path,MAXWD));
  61. X}
  62. X
  63. X#else
  64. X
  65. X/*
  66. X * 4.2bsd getwd simulation for Sys V.2
  67. X */
  68. X
  69. X#include <stdio.h>
  70. X
  71. X#define MAXWD 1024         /* limited by 4.2 getwd(2) */
  72. X
  73. Xchar *
  74. Xgetwd(path)
  75. Xchar *path;
  76. X{
  77. X     char *nlp;
  78. X     FILE *fp;
  79. X     FILE *popen();
  80. X     char *strrchr();
  81. X
  82. X    putenv("IFS= \t\n");
  83. X     fp = popen("PATH=/bin:/usr/bin pwd", "r");
  84. X     if (fp == NULL)
  85. X         return 0;
  86. X     if (fgets(path, MAXWD, fp) == NULL) {
  87. X         (void) pclose(fp);
  88. X         return 0;
  89. X     }
  90. X     if ((nlp = strrchr(path, '\n')) != NULL)
  91. X         *nlp = '\0';
  92. X     (void) pclose(fp);
  93. X     return path;
  94. X}
  95. X#endif
  96. X
  97. ________This_Is_The_END________
  98. if test `wc -c < getwd.c` -ne      787; then
  99.     echo 'shar: getwd.c was damaged during transit (should have been      787 bytes)'
  100. fi
  101. fi        ; : end of overwriting check
  102. echo 'x - rename.c'
  103. if test -f rename.c; then echo 'shar: not overwriting rename.c'; else
  104. sed 's/^X//' << '________This_Is_The_END________' > rename.c
  105. X/*
  106. X * substitute for BSD/SVR3 rename() system call, from
  107. X * Janet Walz, walz@mimsy.umd.edu & Rich Salz, rsalz@pineapple.bbn.com
  108. X */
  109. X
  110. Xint rename(oldname,newname)
  111. Xchar *oldname,*newname;
  112. X{
  113. X    (void)unlink(newname);
  114. X    if(link(oldname,newname))
  115. X        return(-1);
  116. X    return(unlink(oldname));
  117. X}
  118. ________This_Is_The_END________
  119. if test `wc -c < rename.c` -ne      280; then
  120.     echo 'shar: rename.c was damaged during transit (should have been      280 bytes)'
  121. fi
  122. fi        ; : end of overwriting check
  123. echo 'x - scandir.3'
  124. if test -f scandir.3; then echo 'shar: not overwriting scandir.3'; else
  125. sed 's/^X//' << '________This_Is_The_END________' > scandir.3
  126. X.TH SCANDIR 3
  127. X.\" $Header: scandir.3,v 1.1 87/12/29 21:35:54 rsalz Exp $
  128. X.SH NAME
  129. Xscandir, alphasort \- scan a directory
  130. X.SH SYNOPSIS
  131. X.nf
  132. X.ft B
  133. X#include <sys/types.h>
  134. X#include <sys/dirent.h>
  135. X
  136. Xint
  137. Xscandir(name, list, selector, sorter)
  138. X.in +4n
  139. Xchar *name;
  140. Xstruct dirent ***list;
  141. Xint (*selector)();
  142. Xint (*sorter)();
  143. X.in -4n
  144. X
  145. Xint
  146. Xalphasort(d1, d2)
  147. X.in +4n
  148. Xstruct dirent **d1;
  149. Xstruct dirent **d2;
  150. X.in -4n
  151. X.ft R
  152. X.fi
  153. X.SH DESCRIPTION
  154. X.I Scandir
  155. Xreads the directory
  156. X.I name
  157. Xand builds a NULL\-terminated array of pointers to the entries found
  158. Xin that directory.
  159. XThis array is put into the location pointed to by the
  160. X.I list
  161. Xparameter.
  162. X.PP
  163. XIf the
  164. X.I selector
  165. Xparameter is non\-NULL, it is taken to be a pointer to a function called
  166. Xwith each entry, to determine whether or not it should be included in
  167. Xthe returned list.
  168. XIf the parameter is NULL, all entries are included.
  169. X.PP
  170. XAs an added feature, the entries can be sorted (with
  171. X.IR qsort (3))
  172. Xbefore the list is returned.
  173. XIf the
  174. X.I sorter
  175. Xparameter is non\-NULL, it is passed to qsort to use as the comparison
  176. Xfunction.
  177. XThe
  178. X.I alphasort
  179. Xroutine is provided to sort the array alphabetically.
  180. X.PP
  181. XThe array pointed to by
  182. X.I list
  183. Xand the items it points to are all space obtained through
  184. X.IR malloc (3),
  185. Xand their storage can be reclaimed as shown in the example below.
  186. X.SH "EXAMPLE"
  187. XHere is a small
  188. X.IR ls (1)\-like
  189. Xprogram:
  190. X.ne 50
  191. X.RS
  192. X.nf
  193. X#include <stdio.h>
  194. X#include <sys/types.h>
  195. X#include <sys/stat.h>
  196. X#include <sys/dir.h>
  197. X
  198. Xextern int alphasort();
  199. X
  200. Xstatic int
  201. Xfilesonly(e)
  202. X    struct dirent *e;
  203. X{
  204. X    struct stat sb;
  205. X
  206. X    return(stat(e->d_name, &sb) >= 0 && (sb.st_mode & S_IFMT) == S_IFREG);
  207. X}
  208. X
  209. Xmain(ac, av)
  210. X    int ac;
  211. X    char *av[];
  212. X{
  213. X    register int i;
  214. X    register int j;
  215. X    struct dirent **list;
  216. X
  217. X    if (ac != 2) {
  218. X        fprintf(stderr, "usage: %s dirname\n", av[0]);
  219. X        exit(1);
  220. X    }
  221. X    if (chdir(av[1]) < 0) {
  222. X        perror(av[1]);
  223. X        exit(1);
  224. X    }
  225. X    if ((i = scandir(".", &list, filesonly, alphasort)) < 0) {
  226. X        perror("Error reading directory");
  227. X        exit(1);
  228. X    }
  229. X    for (j = 0; j < i; j++)
  230. X        printf("%s\n", list[j]->d_name);
  231. X    for (j = 0; j < i; j++)
  232. X        free((char *)list[j]);
  233. X    free((char *)list);
  234. X    exit(0);
  235. X}
  236. X.fi
  237. X.RE
  238. X.SH "SEE ALSO"
  239. Xdirectory(3), qsort(3)
  240. X.SH DIAGNOSTICS
  241. XReturns the number of entries in the ``list,'' or \-1 if the directory
  242. Xcould not be opened or a memory allocation failed.
  243. X.SH BUGS
  244. XThe routine can be slightly wasteful of space.
  245. ________This_Is_The_END________
  246. if test `wc -c < scandir.3` -ne     2353; then
  247.     echo 'shar: scandir.3 was damaged during transit (should have been     2353 bytes)'
  248. fi
  249. fi        ; : end of overwriting check
  250. echo 'x - scandir.c'
  251. if test -f scandir.c; then echo 'shar: not overwriting scandir.c'; else
  252. sed 's/^X//' << '________This_Is_The_END________' > scandir.c
  253. X/*
  254. X**  SCANDIR
  255. X**  Scan a directory, collecting all (selected) items into a an array.
  256. X*/
  257. X#include <stdio.h>
  258. X#include <sys/types.h>
  259. X#include <dirent.h>
  260. X
  261. X#ifdef    RCSID
  262. Xstatic char RCS[] = "$Header: scandir.c,v 1.1 87/12/29 21:35:56 rsalz Exp $";
  263. X#endif    /* RCSID */
  264. X
  265. X/* Initial guess at directory size. */
  266. X#define INITIAL_SIZE    20
  267. X
  268. X/* A convenient shorthand. */
  269. Xtypedef struct dirent     ENTRY;
  270. X        
  271. X#define DIRSIZ(d) (sizeof(struct dirent) + strlen(d->d_name) + 1) 
  272. X
  273. X/* Linked in later. */
  274. Xextern char        *malloc();
  275. Xextern char        *realloc();
  276. Xextern char        *strcpy();
  277. X
  278. X
  279. Xint
  280. Xscandir(Name, List, Selector, Sorter)
  281. X    char          *Name;
  282. X    ENTRY        ***List;
  283. X    int             (*Selector)();
  284. X    int             (*Sorter)();
  285. X{
  286. X    register ENTRY     **names;
  287. X    register ENTRY      *E;
  288. X    register DIR      *Dp;
  289. X    register int       i;
  290. X    register int       size;
  291. X
  292. X    /* Get initial list space and open directory. */
  293. X    size = INITIAL_SIZE;
  294. X    if ((names = (ENTRY **)malloc(size * sizeof names[0])) == NULL
  295. X     || (Dp = opendir(Name)) == NULL)
  296. X    return(-1);
  297. X
  298. X    /* Read entries in the directory. */
  299. X    for (i = 0; E = readdir(Dp); )
  300. X    if (Selector == NULL || (*Selector)(E)) {
  301. X        /* User wants them all, or he wants this one. */
  302. X        if (++i >= size) {
  303. X        size <<= 1;
  304. X        names = (ENTRY **)realloc((char *)names, size * sizeof names[0]);
  305. X        if (names == NULL) {
  306. X            closedir(Dp);
  307. X            return(-1);
  308. X        }
  309. X        }
  310. X
  311. X        /* Copy the entry. */
  312. X        if ((names[i - 1] = (ENTRY *)malloc(DIRSIZ(E))) == NULL) { 
  313. X        closedir(Dp);
  314. X        return(-1);
  315. X        }
  316. X        names[i - 1]->d_ino = E->d_ino;
  317. X        names[i - 1]->d_reclen = E->d_reclen;
  318. X     /*   names[i - 1]->d_namlen = E->d_namlen; */
  319. X        (void)strcpy(names[i - 1]->d_name, E->d_name);
  320. X    }
  321. X
  322. X    /* Close things off. */
  323. X    names[i] = NULL;
  324. X    *List = names;
  325. X    closedir(Dp);
  326. X
  327. X    /* Sort? */
  328. X    if (i && Sorter)
  329. X    qsort((char *)names, i, sizeof names[0], Sorter);
  330. X
  331. X    return(i);
  332. X}
  333. ________This_Is_The_END________
  334. if test `wc -c < scandir.c` -ne     1875; then
  335.     echo 'shar: scandir.c was damaged during transit (should have been     1875 bytes)'
  336. fi
  337. fi        ; : end of overwriting check
  338. echo 'x - utimes.c'
  339. if test -f utimes.c; then echo 'shar: not overwriting utimes.c'; else
  340. sed 's/^X//' << '________This_Is_The_END________' > utimes.c
  341. X
  342. X/* bsd utimes emulation for Sys V */
  343. X/* by Jon Zeeff */
  344. X
  345. X#include <sys/types.h>
  346. X
  347. Xstruct utimbuf {
  348. X     time_t  actime;
  349. X     time_t  modtime;
  350. X};
  351. X
  352. Xstruct timeval {
  353. X     long    tv_sec;
  354. X     long    tv_usec;
  355. X};
  356. X
  357. Xutimes(path,tvp)
  358. Xchar *path;
  359. Xstruct timeval tvp[2];
  360. X{
  361. X
  362. Xstruct utimbuf times;
  363. X
  364. Xtimes.actime = (time_t) tvp[0].tv_sec;
  365. Xtimes.modtime = (time_t) tvp[1].tv_sec;
  366. X
  367. Xreturn utime(path,times);
  368. X
  369. X}
  370. ________This_Is_The_END________
  371. if test `wc -c < utimes.c` -ne      397; then
  372.     echo 'shar: utimes.c was damaged during transit (should have been      397 bytes)'
  373. fi
  374. fi        ; : end of overwriting check
  375. exit 0
  376.