I’m trying to do this in C, I have some code in Python but I can’t figure how to make it work in C, I’m used to high level languages, and C is really new to me, maybe you can help me to translate it, I have nothing so far as I don’t know where to begin …
Python code:
archivo = open('passwd.txt')
for linea in archivo:
campos = linea.strip().split(':')
print 'User',campos[0],'has shell',campos[6]
archivo.close()
passwd.txt is the following:
root:x:0:0:root:/root:/bin/bash
daemon:x:1:1:daemon:/usr/sbin:/bin/sh
bin:x:2:2:bin:/bin:/bin/sh
The output should be:
User root has shell /bin/bash
User daemon has shell /bin/sh
User bin has shell /bin/sh
Can anyone help me?
However, if /etc/passwd has empty fields, indicated by
::, thenstrtokwill not work. You could usestrsepinstead, or juststrchr, as in:On my machine, some of the so-called GECOS fields are empty, and user
newshas no shell.