File: linkedlist.c

package info (click to toggle)
dsh 0.25.10-1
  • links: PTS
  • area: main
  • in suites: lenny, squeeze, wheezy
  • size: 2,120 kB
  • ctags: 110
  • sloc: sh: 9,638; ansic: 1,102; makefile: 144
file content (196 lines) | stat: -rw-r--r-- 3,736 bytes parent folder | download | duplicates (8)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
/*
 *  DSH / dancer's shell or the distributed shell
 *  Copyright (C) 2001, 2002, 2003 Junichi Uekawa
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 * Linked list library routines.
 */


#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include "dsh.h"
#include "linkedlist.h"
#include "parameter.h"

#include "gettext.h"
#define _(A) gettext(A)

/**
   free a linked list.
 */
void 
llfree(linkedlist* a)
{				/* add paranoia checks */
  if (!a)
    return;
  llfree(a->next);
  a->next = NULL;
  free (a->string);  
  a->string = NULL;
  free (a);  
}

/** 
 * add a char to linked list
 *
 * @newly added list, or exit(1) on error
 */
linkedlist* 
lladd(linkedlist*next, const char * b)
{
  linkedlist*tmp= malloc_with_error(sizeof(linkedlist));
  if (!(tmp->string = strdup(b)))
    {
      fprintf (stderr, _("Out of memory in lladd\n"));
      exit (1);
    }
  tmp->next=next;  
  return tmp;
}

/**
 * concatenate linked list.  
 * a comes after b.
 *
 * @return concatenated list, never errors.
 */
linkedlist* 
llcat(linkedlist*a, linkedlist*b)
{
  linkedlist* orig=b;
  
  if (NULL==a) return b;
  if (NULL==b) return a;
  while (b->next)
    b=b->next;
  b->next = a;
  return orig;  
}

/**
 * Reverse the order of the linked list.
 *
 * @return the reversed list, never NULL.
 */
linkedlist* 
llreverse(linkedlist*a)
{
  linkedlist*prev=NULL;
  linkedlist*next;
  
  while (a)
    {
      next = a->next ;
      a->next=prev;
      prev=a;
      a=next;
    }
  return prev;  
}

/**
 * duplicate the list 
 *
 * @return the new list member, or NULL in failure
 */
linkedlist* 
lldup(const linkedlist*a)
{
  if (a)
    return lladd (lldup(a->next), a->string);
  else 
    return NULL;  
}

/**
 * Count the number of members in the list
 *
 * @return number of members.
 */
int 
llcount (const linkedlist*a)
{				/* count members */
  if (a)
    return llcount(a->next)+1 ;
  else
    return 0;  
}

/**
   execute shell according to linked list

   @returns 1 on failure, does not return on success.
 */
int
llexec (const char * command, const linkedlist * a)
{
  char ** av;
  int ac = llcount(a) + 1 ;
  int i=1;

  av = malloc_with_error (sizeof (char * ) * ac + 1);
  while (a)
    {
      av[i++]=a->string;
      a=a->next;
    }
  av[ac] = NULL;
  av[0] = strdup(command);  
  execvp (command, av);  
  free (av[0]);  
  free (av);
  return 1;			/* when this function returns,
				   there was an error executing the program */
}

static void 
lldump_recursion(const linkedlist*a)
{
  if (a)
    {
      printf ("[%s] ", a->string);
      lldump_recursion(a->next);
    }
}

/**  
 * Dump contents of the linked list for debug
 */
void 
lldump(const linkedlist*a)
{
  lldump_recursion(a);
  printf ("\n");
}

/**
 * Find matching member from the linked list.
 *
 * @return pointer to matching member item, or NULL
 */
const linkedlist * 
llmatch(const linkedlist * a, const char * search)
{
  for ( ; a; a=a->next)
    {
      if (!strcasecmp (a->string, search))
	return a;
    }
  return NULL;
}